Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Hooks for EGit
Hooks for EGit [message #783373] Wed, 25 January 2012 15:51 Go to next message
Stefan Nöbauer is currently offline Stefan NöbauerFriend
Messages: 20
Registered: January 2012
Junior Member
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 16:12 Go to previous messageGo to next message
Kevin Sawicki is currently offline Kevin SawickiFriend
Messages: 47
Registered: September 2011
Member
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 19:18 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan NöbauerFriend
Messages: 20
Registered: January 2012
Junior Member
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 21:56 Go to previous messageGo to next message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
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 18:02 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan NöbauerFriend
Messages: 20
Registered: January 2012
Junior Member
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 23:27 Go to previous messageGo to next message
Robin Rosenberg is currently offline Robin RosenbergFriend
Messages: 332
Registered: July 2009
Senior Member
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 08:11 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan NöbauerFriend
Messages: 20
Registered: January 2012
Junior Member
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 22:40 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
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 08:43 Go to previous messageGo to next message
Stefan Nöbauer is currently offline Stefan NöbauerFriend
Messages: 20
Registered: January 2012
Junior Member
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 12:50 Go to previous message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
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: Tue Apr 23 14:56:22 GMT 2024

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

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

Back to the top