|
|
Re: Hooks for EGit [message #783468 is a reply to message #783383] |
Wed, 25 January 2012 19:18 |
|
I would need the most the commit-msg Hook to check that the user added a tracker ID to the commit. Is there any other way to check the commit message for matching regex?
|
|
|
|
|
|
|
|
Re: Hooks for EGit [message #784984 is a reply to message #784711] |
Sat, 28 January 2012 08:43 |
|
Ahh ok. Since Gerrit is down and I am not finished the contributor guide I post my idea here in thr forum.
public RevCommit call() throws NoHeadException, NoMessageException,
UnmergedPathException, ConcurrentRefUpdateException,
JGitInternalException, WrongRepositoryStateException, HookException {
checkCallable();
RepositoryState state = repo.getRepositoryState();
if (!state.canCommit())
throw new WrongRepositoryStateException(MessageFormat.format(
JGitText.get().cannotCommitOnARepoWithState, state.name()));
processOptions(state);
processCommitMessageHook();
...
}
private void processCommitMessageHook() throws HookException {
File repoDir = repo.getDirectory();
File commit_msg_file = new File(repoDir, "hooks/commit-msg");
File messageFile = null;
try {
String executor = getHookExecutor(commit_msg_file);
if (executor == null) {
return;
}
messageFile = exportMessageToFile();
ProcessBuilder processBuilder = new ProcessBuilder(executor,
commit_msg_file.getAbsolutePath(), messageFile
.getAbsolutePath());
Process process = processBuilder.start();
int exitValue = process.waitFor();
if (exitValue > 0) {
String processOutput = getProcessOutput(process);
throw new HookException(processOutput);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (messageFile != null) {
messageFile.delete();
}
}
}
private File exportMessageToFile() throws IOException {
FileWriter writer = null;
File messageFile;
try {
messageFile = File.createTempFile("commit_msg_", ".git");
writer = new FileWriter(messageFile);
writer.append(message);
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
return messageFile;
}
// Only direct definition of bash executor is supported
private String getHookExecutor(File commit_msg_file) throws IOException {
if (!commit_msg_file.exists()) {
return null;
}
BufferedReader reader = new BufferedReader(new FileReader(
commit_msg_file));
// Read first line for bash executor
String firstLine = reader.readLine();
Pattern executorPattern = Pattern.compile("#!(.*)");
Matcher matcher = executorPattern.matcher(firstLine);
matcher.find();
if (!matcher.matches()) {
return null;
}
return matcher.group(1);
}
private String getProcessOutput(Process process) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader tempReader = new InputStreamReader(
new BufferedInputStream(process.getInputStream()));
BufferedReader br = new BufferedReader(tempReader);
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
stringBuilder.append(line);
}
return stringBuilder.toString();
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.04006 seconds