Set git environment variables for hooks
Set GIT_DIR and GIT_WORK_TREE when calling hooks. Bug: 541622 Change-Id: I6153d8a6a934ec37a3a5e7319c2d0e516f539ab7 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
parent
12d840dfcb
commit
9c755c9e7c
|
@ -199,7 +199,8 @@ public void testRunHook() throws Exception {
|
||||||
assumeSupportedPlatform();
|
assumeSupportedPlatform();
|
||||||
|
|
||||||
writeHookFile(PreCommitHook.NAME,
|
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 out = new ByteArrayOutputStream();
|
||||||
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||||
ProcessResult res = FS.DETECTED.runHookIfPresent(db,
|
ProcessResult res = FS.DETECTED.runHookIfPresent(db,
|
||||||
|
@ -208,7 +209,9 @@ public void testRunHook() throws Exception {
|
||||||
"arg1", "arg2" },
|
"arg1", "arg2" },
|
||||||
new PrintStream(out), new PrintStream(err), "stdin");
|
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"));
|
out.toString("UTF-8"));
|
||||||
assertEquals("unexpected output on stderr stream", "stderr\n",
|
assertEquals("unexpected output on stderr stream", "stderr\n",
|
||||||
err.toString("UTF-8"));
|
err.toString("UTF-8"));
|
||||||
|
|
|
@ -1110,6 +1110,13 @@ protected ProcessResult internalRunHookIfPresent(Repository repository,
|
||||||
hookPath);
|
hookPath);
|
||||||
ProcessBuilder hookProcess = runInShell(cmd, args);
|
ProcessBuilder hookProcess = runInShell(cmd, args);
|
||||||
hookProcess.directory(runDirectory);
|
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 {
|
try {
|
||||||
return new ProcessResult(runProcess(hookProcess, outRedirect,
|
return new ProcessResult(runProcess(hookProcess, outRedirect,
|
||||||
errRedirect, stdinArgs), Status.OK);
|
errRedirect, stdinArgs), Status.OK);
|
||||||
|
|
Loading…
Reference in New Issue