Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Git hooks in eclipse
Git hooks in eclipse [message #1698215] Fri, 12 June 2015 08:41 Go to next message
Daniel van Dijk is currently offline Daniel van DijkFriend
Messages: 3
Registered: June 2015
Junior Member
In commits:
https://git.eclipse.org/r/#/c/40504/
https://git.eclipse.org/r/#/c/40506/
git hook integration was introduced for JGit.

Is it possible to make use of git hooks in eclipse? Especially the pre-commit hook.
Re: Git hooks in eclipse [message #1698499 is a reply to message #1698215] Tue, 16 June 2015 06:42 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
simply use it. E.g. rename in your repo <root>/.git/hooks/pre-commit.sample to <root>/.git/hooks/pre-commit . Then from EGit to to commit a file with non-ascii path and you'll see the error message coming from this hook.

  • Attachment: lub14.png
    (Size: 248.02KB, Downloaded 1565 times)


Ciao
Chris
Re: Git hooks in eclipse [message #1698506 is a reply to message #1698499] Tue, 16 June 2015 07:14 Go to previous messageGo to next message
Daniel van Dijk is currently offline Daniel van DijkFriend
Messages: 3
Registered: June 2015
Junior Member
This doesn't work for me. When I try to commit using Git via bash (edit: either the windows client 'Git bash', the command line or tortoise git) the hook works as expected, but Eclipse completely ignores it.

I use Eclipse Version: Luna Service Release 2 (4.4.2) on Windows and Eclipse Git Team Provider 3.7.1.201504261725-r.

Maybe the (Linux?) version you are using is newer?

[Updated on: Mon, 22 June 2015 08:00]

Report message to a moderator

Re: Git hooks in eclipse [message #1698542 is a reply to message #1698506] Tue, 16 June 2015 10:13 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 274
Registered: July 2009
Senior Member
Now I get it. You are running on windows. Typically hooks are implemented as scripts which need to be executed by "/bin/sh". But on a plain windows machine you don't have a "sh.exe". So, you need to install cygwin and add <cygwin_root>/bin to your PATH. Make sure that during cygwin installation you also install git (because the hook scripts want to execute git commands). I saw the same problems as you and after installing cygwin the hooks


Ciao
Chris
Re: Git hooks in eclipse [message #1699154 is a reply to message #1698542] Mon, 22 June 2015 08:02 Go to previous messageGo to next message
Daniel van Dijk is currently offline Daniel van DijkFriend
Messages: 3
Registered: June 2015
Junior Member
I have no trouble running git hooks, the Git bash, git from command line, tortoiseGit and other programms respond to it as expected. (Note that in my previous message I used unfortunate wording when referring to the clients I'm using.)

If I understand correctly, Egit implemented all git commands natively. If this is correct, than changing git client wouldn't change anything in how Eclipse performs git commands.

Did you manage to get git hooks working in Eclipse in Windows using cygwin?

I use this script to test:
#!/bin/sh
cat <<\EOF
Not allowed to commit.
EOF
exit 1


Re: Git hooks in eclipse [message #1714882 is a reply to message #1698215] Tue, 17 November 2015 13:04 Go to previous messageGo to next message
Christoph Zauner is currently offline Christoph ZaunerFriend
Messages: 7
Registered: October 2015
Junior Member
As of JGit v4.1.0 support for hooks is only enabled when the JGit file system layer detects a POSIX or a Win32 + Cygwin file system. The JGit file system class hierachy is as follows:

->abstract class FS: Does not support hooks.
-->class FS_POSIX: Supports hooks.
-->class FS_Win32: Does not support hooks.
--->class FS_Win32_Cygwin: Supports hooks. 


The default implementation of runHookIfPresent(...) in FS does not support running hooks:

public ProcessResult runHookIfPresent(Repository repository,
		final String hookName,
		String[] args, PrintStream outRedirect, PrintStream errRedirect,
		String stdinArgs) throws JGitInternalException {
	
	return new ProcessResult(Status.NOT_SUPPORTED);
}


And how does the FS layer decide if it's running on a Win32 or FS_Win32 + Cygwin file system? The presece of a Cygwin installation is determined by the isCygwin() function in the FS_Win32_Cygwin class:

It simply checks if the Windows PATH variable contains the file cygpath.exe. See here for the source code of the FS_Win32_Cygwin class. (Reminder: The java.library.path is pre-populated with the PATH entries when running under Windows.)

So in order for the git hooks to be called by JGit when running under Windows you have to do two things:

1. Install Cygwin (do not forget to install the git package)
2. Add the Cygwin bin folder (e.g. cygwin64\system\bin) to the Windows PATH environmental variable.

- Chris
Re: Git hooks in eclipse [message #1715068 is a reply to message #1714882] Wed, 18 November 2015 22:54 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
third option would be to implement hook support in FS_Win32, maybe you want to contribute that ?
Re: Git hooks in eclipse [message #1715070 is a reply to message #1715068] Wed, 18 November 2015 22:57 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
in case you are considering this: here is the contributor guide
https://wiki.eclipse.org/EGit/Contributor_Guide
Re: Git hooks in eclipse [message #1716527 is a reply to message #1698215] Fri, 04 December 2015 15:12 Go to previous messageGo to next message
JP Math is currently offline JP MathFriend
Messages: 3
Registered: April 2015
Junior Member
As far as I understood, there is also support for the post-commit hook (https://git.eclipse.org/r/#/c/35791/). Do you know if this will be included in the next version of JGit?

[Updated on: Thu, 10 December 2015 08:53]

Report message to a moderator

Re: Git hooks in eclipse [message #1716689 is a reply to message #1716527] Mon, 07 December 2015 14:39 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
this change needs to be reworked since the way how hooks are implemented in JGit changed since this change stalled
Re: Git hooks in eclipse [message #1721554 is a reply to message #1698499] Thu, 28 January 2016 09:52 Go to previous messageGo to next message
Stefano Callegari is currently offline Stefano CallegariFriend
Messages: 1
Registered: January 2016
Junior Member
Christian Halstrick wrote on Tue, 16 June 2015 06:42
simply use it. E.g. rename in your repo <root>/.git/hooks/pre-commit.sample to <root>/.git/hooks/pre-commit . Then from EGit to to commit a file with non-ascii path and you'll see the error message coming from this hook.



This not works for me.

I have also tried a my pre-commit script that works if I execute the commit by hand.

My machine is Linux (Debian SID) with the last version of eclipse and EGit (downloaded from their sites).

What I miss?

Thanks

Stefano
Re: Git hooks in eclipse [message #1722060 is a reply to message #1698215] Tue, 02 February 2016 14:40 Go to previous messageGo to next message
ran greenberg is currently offline ran greenbergFriend
Messages: 4
Registered: January 2016
Junior Member
i am also having problems with hooks on my window machine
i have Mars eclipse and i added the hooks to the <root>.git
i have installed cygwin and added it to my Windows path
but still get
An internal error occurred
Exception caught during execution of "commit-msg" hook.

is there any way to get the hooks to work ??
Re: Git hooks in eclipse [message #1722472 is a reply to message #1722060] Fri, 05 February 2016 07:59 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
Hooks should be installed in the .git/hooks directory of the respective repository.

Are there any stack traces written to the Eclipse error log when this fails ?

Which version of EGit/Eclipse are you using ?
Re: Git hooks in eclipse [message #1722507 is a reply to message #1715068] Fri, 05 February 2016 10:42 Go to previous messageGo to next message
Christoph Zauner is currently offline Christoph ZaunerFriend
Messages: 7
Registered: October 2015
Junior Member
Matthias Sohn wrote on Wed, 18 November 2015 22:54
third option would be to implement hook support in FS_Win32, maybe you want to contribute that ?


The first thing I do when I am forced to use the Windows OS is to install Cygwin Smile Therefore I have absolutely no problem if one has to have Cygwin installed to enable git hooks in EGit. Besides, writing git hooks when you only have the tools available a naked Windows offers seems fairly cumbersome to me. I am sure hardly anybody would want to write his hooks using the cmd.exe shell.
Re: Git hooks in eclipse [message #1722660 is a reply to message #1722060] Mon, 08 February 2016 06:56 Go to previous messageGo to next message
Christoph Zauner is currently offline Christoph ZaunerFriend
Messages: 7
Registered: October 2015
Junior Member
ran greenberg wrote on Tue, 02 February 2016 14:40
i am also having problems with hooks on my window machine
i have Mars eclipse and i added the hooks to the <root>.git
i have installed cygwin and added it to my Windows path
but still get
An internal error occurred
Exception caught during execution of "commit-msg" hook.

is there any way to get the hooks to work ??


Make sure that the git hook script does use Unix style line endings (just line feed characters). Windows uses a carriage return together with line feed character as a newline separator. This can sometimes lead to problems when using Cygwin but writting the bash scripts using a Windows editor like Notepad++.
Re: Git hooks in eclipse [message #1732313 is a reply to message #1698215] Sun, 15 May 2016 15:53 Go to previous messageGo to next message
Elyahu Eaton is currently offline Elyahu EatonFriend
Messages: 6
Registered: May 2016
Junior Member
Hi,

The PATH solution didn't work for me. I am using

MARS 2
EGI 4.5.1
Jgit 4.5.2

I added the C:\...\Git\bin to my PATH as sh.exe is found there

My hook is the "update" one, which exist on the server side.

When do you accept that EGIT will fully support running the GIT hooks ?without being dependent on any environment variables that can be changed by the uses so it is actually a very big security vulnerability.

Thanks,
Eli.
Re: Git hooks in eclipse [message #1732462 is a reply to message #1732313] Tue, 17 May 2016 13:06 Go to previous messageGo to next message
Elyahu Eaton is currently offline Elyahu EatonFriend
Messages: 6
Registered: May 2016
Junior Member
Hi,

I opened a bug on that -
https://bugs.eclipse.org/bugs/show_bug.cgi?id=493721

It happens on any hook, even on a "pre-commit" which is a client side hook. EGIT is not reading the hook file at all, no output is returned when executing the relevant GIT command that suppose to trigger the hook.

Cygwin installation didn't help

using either the full path in the hook, like "/c/cygwin/bin/sh.exe", Or, putting the path to sh.exe in the PATH environment variable also didn't help.

I used a 3 line hook:
#/bin/sh.exe
echo "Here"
exit 1

IMHO it's a blocker bug, am I wrong ?

Thanks,
Eli
Re: Git hooks in eclipse [message #1732763 is a reply to message #1732462] Fri, 20 May 2016 05:30 Go to previous messageGo to next message
Lokesh Singal is currently offline Lokesh SingalFriend
Messages: 1
Registered: May 2016
Junior Member
Hello,

The same issue occurred to me when running "pre-commit" hook using eclipse Egit.

Whats worked for me was :
1. Installed cygwin64 as i have 64 bit windows, make sure you have correct "Cygwin" installed along with the git packages.
2. Set the path in your environment variable like C:\cygwin64\bin
3. Restart your system
4. My pre-commit file contained this snippet
#!/bin/sh
echo >&2 Test pre-commit hook.
	exit 1


Thanks,
Hope it might help.
Re: Git hooks in eclipse [message #1733093 is a reply to message #1732763] Tue, 24 May 2016 03:32 Go to previous messageGo to next message
Elyahu Eaton is currently offline Elyahu EatonFriend
Messages: 6
Registered: May 2016
Junior Member
Hi,

IMHO, its not a solution.

Running hooks shouldn't be dependent on PATH and installation of stuff which is totally not relevant to GIT

Thanks,
Eli
Re: Git hooks in eclipse [message #1761194 is a reply to message #1732763] Mon, 08 May 2017 18:24 Go to previous messageGo to next message
Kishor Patil is currently offline Kishor PatilFriend
Messages: 1
Registered: May 2017
Junior Member
Hello Lokesh,

I'm on Windows 7 Enterprise.
I'm using Spring Tool Suite 3.8.4.RELEASE (Eclipse Neaon.3 4.6.3)

I followed all your instructions :

Installed cygwin in C:\cygwin64\bin
I ensured cygpath.exe is in the bin directory
I updated my PATH to add C:\cygwin64\bin
I installed windows git bash 2.12.2.windows.2
I restarted my machine

My precommit hook works from git bash command but doesn't work from within STS.

Is there something I'm missing? Please help



Re: Git hooks in eclipse [message #1766807 is a reply to message #1761194] Tue, 27 June 2017 13:03 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 392
Registered: December 2015
Senior Member
I don't have and dont' want cygwin for this to work. I do have the mysysgit git bash however. Should't that work for running hooks?
Re: Git hooks in eclipse [message #1766831 is a reply to message #1698215] Tue, 27 June 2017 19:06 Go to previous messageGo to next message
Elyahu Eaton is currently offline Elyahu EatonFriend
Messages: 6
Registered: May 2016
Junior Member
The only solution I found is using the URL as the reference for the Git repository and not UNC path as "\\server\git\Repository_name"
Re: Git hooks in eclipse [message #1766942 is a reply to message #1766831] Wed, 28 June 2017 21:27 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
JGit hook support on Windows is currently only implemented for cygwin.

runHookIfPresent() and findHook() are implemented for posix:
https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java#239
https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java#289
and cygwin:
https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32_Cygwin.java#171
but not for Windows:
https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/heads/master/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java
also the code for discovering git bash executable which is required to start external process is missing.

Someone using Windows needs to do the work and contribute the missing Windows
implementation using git bash. Maybe you want to contribute that ?
Re: Git hooks in eclipse [message #1783169 is a reply to message #1698215] Thu, 08 March 2018 17:09 Go to previous messageGo to next message
IKO BH is currently offline IKO BHFriend
Messages: 1
Registered: March 2018
Junior Member
Im using Oxygen 2 on Ubuntu 16.04.

After setting post-commit hook & committing from shell I get the hook working.

BUT, committing from eclipse, I see no hook response.

Does someone figured how to make hooks work via eclipse?

My post-commit hook (some-path/.git/hooks/post-commit):
"
#!/bin/sh
#
# An example hook script that is called after a successful
# commit is made.
#
# To enable this hook, rename this file to "post-commit".

echo hi
"
Re: Git hooks in eclipse [message #1787268 is a reply to message #1783169] Wed, 23 May 2018 02:37 Go to previous messageGo to next message
lea ilagan is currently offline lea ilaganFriend
Messages: 59
Registered: October 2012
Member
Hello,
I was able to make it work in eclipse by making sure that eclipse uses the PATH environment variables.

I Added environment variables in eclipse by:
Right click on any projects in Package Explorer, then click
Run As -> Run Configurations. And here, in tab Environment -> Select

Thanks.
Re: Git hooks in eclipse [message #1787269 is a reply to message #1783169] Wed, 23 May 2018 02:37 Go to previous messageGo to next message
lea ilagan is currently offline lea ilaganFriend
Messages: 59
Registered: October 2012
Member
Hello,
I was able to make it work in eclipse by making sure that eclipse uses the PATH environment variables.

I Added environment variables in eclipse by:
Right click on any projects in Package Explorer, then click
Run As -> Run Configurations. And here, in tab Environment -> Select

Thanks.
Re: Git hooks in eclipse [message #1798744 is a reply to message #1698215] Fri, 23 November 2018 13:06 Go to previous messageGo to next message
Uladzimir Simankou is currently offline Uladzimir SimankouFriend
Messages: 4
Registered: November 2018
Junior Member
I'm having a similar issue under Linux. I tried to commit, but got an error:

Rejected by "pre-commit" hook.
it looks like you invoked this script directly, but it's supposed to be used as a pre-commit git hook.
To install the hook try:
.git/hooks/pre-commit install
For more details on this script try:
.git/hooks/pre-commit --help

I tried .git/hooks/pre-commit install and also git install from console, but it works for the system git only. How to install that hook for EGit?

Is there a way to fix it?
Version: Oxygen.3a Release (4.7.3a)
Eclipse EGit: 4.9.2.2017121509
Re: Git hooks in eclipse [message #1798901 is a reply to message #1798744] Tue, 27 November 2018 08:28 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
This is not EGit having trouble calling the hook; it's the hook having trouble being called.

Look in the hook script why it prints those messages.
Re: Git hooks in eclipse [message #1798942 is a reply to message #1798901] Tue, 27 November 2018 17:36 Go to previous messageGo to next message
Uladzimir Simankou is currently offline Uladzimir SimankouFriend
Messages: 4
Registered: November 2018
Junior Member
Thank you for the clue, Thomas. I found a line where this message is printed:

# This is a real run of the hook, not a install/uninstall run.

if [ -z "${GIT_DIR:-}" ] && [ -z "${GIT_INDEX_FILE:-}" ]; then
    error_exit \
        $'It looks like you invoked this script directly, but it\'s supposed to be used\n' \
        $'as a pre-commit git hook.\n' \
        $'\n' \
        $'To install the hook try:\n' \
        $'    ' "$bash_source" $' install\n' \
        $'\n' \
        $'For more details on this script try:\n' \
        $'    ' "$bash_source" $' --help\n'
fi


This hook works for console system git, but having troubles with EGit. Now I'm investigating how to modify the script to avoid the error. If you have any guess, share please.
Re: Git hooks in eclipse [message #1798975 is a reply to message #1798942] Wed, 28 November 2018 08:55 Go to previous messageGo to next message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 576
Registered: August 2016
Senior Member
So it's a JGit problem after all: environment variables are not set for hooks. See bug 541622. Interesting to find that only after so many years.

As a work-around, set $GIT_DIR inside the script if it's not set. The current working directory should be either the .git directory if the repo is bare, or the working tree root if it isn't.
Re: Git hooks in eclipse [message #1799309 is a reply to message #1798975] Tue, 04 December 2018 14:34 Go to previous messageGo to next message
Uladzimir Simankou is currently offline Uladzimir SimankouFriend
Messages: 4
Registered: November 2018
Junior Member
Thank you Thomas for great support! Looking forward to see this fix in upcoming Eclipse releases. For now I just commented out this check in my working copy of the script, as these variables weren't used further in the script's body.
Re: Git hooks in eclipse [message #1799852 is a reply to message #1799309] Fri, 14 December 2018 20:05 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
This fix will be shipped with EGit/JGit 5.2 next week
Re: Git hooks in eclipse [message #1859388 is a reply to message #1714882] Sun, 04 June 2023 17:00 Go to previous messageGo to next message
sandeep goud is currently offline sandeep goudFriend
Messages: 1
Registered: June 2023
Junior Member
Team,
I am using eclipse ide version 4.23.0 on windows 10.
I Can ran the pre-commit hooks(bash script) from git bash and eclipse terminal. But pre-commit hooks are not executing while by using git staging.
let me know eclipse Ide (git staging GUI) is executable for pre-commit(supported) or not ?
Re: Git hooks in eclipse [message #1859820 is a reply to message #1859388] Wed, 28 June 2023 19:03 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
Looks like you didn't read the complete discussion thread.
See
https://www.eclipse.org/forums/index.php?t=msg&th=1067404&goto=1714882&#msg_1714882
https://www.eclipse.org/forums/index.php?t=msg&th=1067404&goto=1766942&#msg_1766942
Re: Git hooks in eclipse [message #1862318 is a reply to message #1859820] Fri, 24 November 2023 13:57 Go to previous messageGo to next message
Wenzel Jonas is currently offline Wenzel JonasFriend
Messages: 1
Registered: November 2023
Junior Member
It's meanwhile 2023 and i still don't see support for the post-checkout hook, which could be used to automatically run "composer install" or "npm install" after checking out a branch.
Are there any plans to support the other git hooks in JGit/EGit using FS_POSIX?
Re: Git hooks in eclipse [message #1863010 is a reply to message #1862318] Wed, 10 January 2024 21:17 Go to previous message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 1268
Registered: July 2009
Senior Member
I am happy to review contributions adding this but I have no time to implement it myself.
Previous Topic:Recreate Disaster
Next Topic:JGit blobless clone: unable to fetch blobs later
Goto Forum:
  


Current Time: Fri Apr 19 06:46:14 GMT 2024

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

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

Back to the top