]> source.dussan.org Git - jgit.git/commitdiff
Set git environment variables for hooks 74/133174/2
authorThomas Wolf <thomas.wolf@paranor.ch>
Wed, 28 Nov 2018 10:06:08 +0000 (11:06 +0100)
committerThomas Wolf <thomas.wolf@paranor.ch>
Thu, 29 Nov 2018 23:37:03 +0000 (00:37 +0100)
Set GIT_DIR and GIT_WORK_TREE when calling hooks.

Bug: 541622
Change-Id: I6153d8a6a934ec37a3a5e7319c2d0e516f539ab7
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java
org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java

index e34c3cebd69e110671ab3b274d670c218dc8657d..e5fcbf9d7c6461cd3daaf6bd885784ebed9d2342 100644 (file)
@@ -199,7 +199,8 @@ public class HookTest extends RepositoryTestCase {
                assumeSupportedPlatform();
 
                writeHookFile(PreCommitHook.NAME,
-                               "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\necho 1>&2 \"stderr\"");
+                               "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\n"
+                                               + "echo $GIT_DIR\necho $GIT_WORK_TREE\necho 1>&2 \"stderr\"");
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ByteArrayOutputStream err = new ByteArrayOutputStream();
                ProcessResult res = FS.DETECTED.runHookIfPresent(db,
@@ -208,7 +209,9 @@ public class HookTest extends RepositoryTestCase {
                                "arg1", "arg2" },
                                new PrintStream(out), new PrintStream(err), "stdin");
 
-               assertEquals("unexpected hook output", "test arg1 arg2\nstdin\n",
+               assertEquals("unexpected hook output",
+                               "test arg1 arg2\nstdin\n" + db.getDirectory().getAbsolutePath()
+                                               + '\n' + db.getWorkTree().getAbsolutePath() + '\n',
                                out.toString("UTF-8"));
                assertEquals("unexpected output on stderr stream", "stderr\n",
                                err.toString("UTF-8"));
index 7f0308f0710d16714f4ef382bb5ba4ba4b27edb9..e864ba76c1f8e3f8e5fae605754118df7d344915 100644 (file)
@@ -1110,6 +1110,13 @@ public abstract class FS {
                                hookPath);
                ProcessBuilder hookProcess = runInShell(cmd, args);
                hookProcess.directory(runDirectory);
+               Map<String, String> environment = hookProcess.environment();
+               environment.put(Constants.GIT_DIR_KEY,
+                               repository.getDirectory().getAbsolutePath());
+               if (!repository.isBare()) {
+                       environment.put(Constants.GIT_WORK_TREE_KEY,
+                                       repository.getWorkTree().getAbsolutePath());
+               }
                try {
                        return new ProcessResult(runProcess(hookProcess, outRedirect,
                                        errRedirect, stdinArgs), Status.OK);