* chain, "return false" at the appropriate failure points.\r
*\r
* Bound Variables:\r
- * gitblit Gitblit Server com.gitblit.GitBlit\r
- * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
- * user Gitblit User com.gitblit.models.UserModel\r
- * clientLogger Logs messages to client com.gitblit.utils.ClientLogger\r
- * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- * url Base url for Gitblit String\r
- * logger Logger instance org.slf4j.Logger\r
+ * gitblit Gitblit Server com.gitblit.GitBlit\r
+ * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
+ * user Gitblit User com.gitblit.models.UserModel\r
+ * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ * url Base url for Gitblit String\r
+ * logger Logs messages to Gitblit org.slf4j.Logger\r
+ * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger\r
* \r
*/\r
\r
* exception handler so it will not crash another script nor crash Gitblit.\r
* \r
* Bound Variables:\r
- * gitblit Gitblit Server com.gitblit.GitBlit\r
- * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
- * user Gitblit User com.gitblit.models.UserModel\r
- * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- * url Base url for Gitblit String\r
- * logger Logger instance org.slf4j.Logger\r
+ * gitblit Gitblit Server com.gitblit.GitBlit\r
+ * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
+ * user Gitblit User com.gitblit.models.UserModel\r
+ * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ * url Base url for Gitblit String\r
+ * logger Logs messages to Gitblit org.slf4j.Logger\r
+ * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger\r
* \r
*/\r
// Indicate we have started the script\r
* Subsequent scripts, if any, will always be invoked.\r
*\r
* Bound Variables:\r
- * gitblit Gitblit Server com.gitblit.GitBlit\r
- * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
- * user Gitblit User com.gitblit.models.UserModel\r
- * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- * url Base url for Gitblit String\r
- * logger Logger instance org.slf4j.Logger\r
+ * gitblit Gitblit Server com.gitblit.GitBlit\r
+ * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
+ * user Gitblit User com.gitblit.models.UserModel\r
+ * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ * url Base url for Gitblit String\r
+ * logger Logs messages to Gitblit org.slf4j.Logger\r
+ * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger\r
* \r
*/\r
\r
* chain, "return false" at the appropriate failure points.\r
* \r
* Bound Variables:\r
- * gitblit Gitblit Server com.gitblit.GitBlit\r
- * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
- * user Gitblit User com.gitblit.models.UserModel\r
- * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- * url Base url for Gitblit String\r
- * logger Logger instance org.slf4j.Logger\r
+ * gitblit Gitblit Server com.gitblit.GitBlit\r
+ * repository Gitblit Repository com.gitblit.models.RepositoryModel\r
+ * user Gitblit User com.gitblit.models.UserModel\r
+ * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ * url Base url for Gitblit String\r
+ * logger Logs messages to Gitblit org.slf4j.Logger\r
+ * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger\r
* \r
*/\r
\r
+/*
+ * Copyright 2012 John Crygier
+ * Copyright 2012 gitblit.com
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.gitblit.utils;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
import org.eclipse.jgit.transport.ReceivePack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Class to log messages to the pushing client. Intended to be used by
- * the Groovy Hooks.
+ * Class to log messages to the pushing Git client. Intended to be used by the
+ * Groovy Hooks.
+ *
+ * @author John Crygier
*
- * @author jcrygier
- *
*/
public class ClientLogger {
-
- static final Logger logger = LoggerFactory.getLogger(ClientLogger.class);
+
+ static final Logger logger = LoggerFactory.getLogger(ClientLogger.class);
private ReceivePack rp;
-
+
public ClientLogger(ReceivePack rp) {
this.rp = rp;
}
-
+
/**
- * Sends a message to the git client. Useful for sending INFO / WARNING messages.
+ * Sends an info/warning message to the git client.
*
* @param message
*/
- public void sendMessage(String message) {
+ public void info(String message) {
rp.sendMessage(message);
}
-
+
+ /**
+ * Sends an error message to the git client.
+ *
+ * @param message
+ */
+ public void error(String message) {
+ rp.sendError(message);
+ }
+
+ /**
+ * Sends an error message to the git client with an exception.
+ *
+ * @param message
+ * @param t
+ * an exception
+ */
+ public void error(String message, Throwable t) {
+ PrintWriter writer = new PrintWriter(new StringWriter());
+ if (!StringUtils.isEmpty(message)) {
+ writer.append(message);
+ writer.append('\n');
+ }
+ t.printStackTrace(writer);
+ rp.sendError(writer.toString());
+ }
+
}
import java.io.BufferedWriter;\r
import java.io.File;\r
import java.io.FileWriter;\r
+import java.io.PrintWriter;\r
+import java.io.StringWriter;\r
import java.text.MessageFormat;\r
import java.util.ArrayList;\r
import java.util.Arrays;\r
\r
BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript));\r
\r
- writer.write("import com.gitblit.utils.ClientLogger\n");\r
- writer.write("clientLogger.sendMessage('this is a test message')\n");\r
+ writer.write("clientLogger.info('this is a test message')\n");\r
writer.flush();\r
writer.close();\r
\r
class MockClientLogger {\r
List<String> messages = new ArrayList<String>();\r
\r
- public void sendMessage(String message) {\r
+ public void info(String message) {\r
+ messages.add(message);\r
+ }\r
+ \r
+ public void error(String message) {\r
messages.add(message);\r
}\r
+ \r
+ public void error(String message, Throwable t) {\r
+ PrintWriter writer = new PrintWriter(new StringWriter());\r
+ if (!StringUtils.isEmpty(message)) {\r
+ writer.append(message);\r
+ writer.append('\n');\r
+ }\r
+ t.printStackTrace(writer);\r
+ messages.add(writer.toString());\r
+ }\r
}\r
\r
class MockMail {\r