Skip to main content



      Home
Home » Eclipse Projects » EGit / JGit » Hooks for EGit
Hooks for EGit [message #783373] Wed, 25 January 2012 10:51 Go to next message
Eclipse UserFriend
Hi,

Is there a plan when EGit will support/execute git hooks?
Re: Hooks for EGit [message #783383 is a reply to message #783373] Wed, 25 January 2012 11:12 Go to previous messageGo to next message
Eclipse UserFriend
I don't think anyone is working on adding them to JGit/EGit at the moment.

There is a JGit issue with some discussion about them here.

What specific hooks are you most interested in?
Re: Hooks for EGit [message #783468 is a reply to message #783383] Wed, 25 January 2012 14:18 Go to previous messageGo to next message
Eclipse UserFriend
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 #783520 is a reply to message #783468] Wed, 25 January 2012 16:56 Go to previous messageGo to next message
Eclipse UserFriend
Just want to second the request -- support for commit-msg hook would be a very nice feature.
Re: Hooks for EGit [message #783910 is a reply to message #783520] Thu, 26 January 2012 13:02 Go to previous messageGo to next message
Eclipse UserFriend
I just checked out the source of JGIT and implemented a prototyp.

I attached the class I changed.
Re: Hooks for EGit [message #784010 is a reply to message #783910] Thu, 26 January 2012 18:27 Go to previous messageGo to next message
Eclipse UserFriend
Stefan Nöbauer skrev 2012-01-26 19.02:
> I just checked out the source of JGIT and implemented a prototyp.
>
> I attached the class I changed.


We prefer inline patches here.

-- robin
Re: Hooks for EGit [message #784224 is a reply to message #784010] Fri, 27 January 2012 03:11 Go to previous messageGo to next message
Eclipse UserFriend
Sorry, but what do you mean with inline patches?
Re: Hooks for EGit [message #784711 is a reply to message #784224] Fri, 27 January 2012 17:40 Go to previous messageGo to next message
Eclipse UserFriend
Inline means post the code as part of the forum post's text and not as an attachment.
Even better would be to contribute it through Gerrit by following the contributor
guide. Though at the moment Gerrit is offline to migrate to the new server.
Re: Hooks for EGit [message #784984 is a reply to message #784711] Sat, 28 January 2012 03:43 Go to previous messageGo to next message
Eclipse UserFriend
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();
	}

Re: Hooks for EGit [message #788965 is a reply to message #784984] Thu, 02 February 2012 07:50 Go to previous message
Eclipse UserFriend
Gerrit is back online [1].

[1] http://dev.eclipse.org/mhonarc/lists/egit-dev/msg02573.html
Previous Topic:push new local branch to remote repo
Next Topic:expand all in compare and synchronize
Goto Forum:
  


Current Time: Wed Jun 18 08:42:29 EDT 2025

Powered by FUDForum. Page generated in 0.04712 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top