diff options
author | Thomas Wolf <twolf@apache.org> | 2022-10-20 23:25:38 +0200 |
---|---|---|
committer | Thomas Wolf <twolf@apache.org> | 2022-10-20 23:34:56 +0200 |
commit | 71af0d6a5c4417a9c9c6523d4aa811579d8c867f (patch) | |
tree | 0dfdb780d9738ffea2442465614b7b12a012f5c9 /org.eclipse.jgit.test | |
parent | 96236fdcb5502f3071317b9cf2bf88019d7fb309 (diff) | |
download | jgit-71af0d6a5c4417a9c9c6523d4aa811579d8c867f.tar.gz jgit-71af0d6a5c4417a9c9c6523d4aa811579d8c867f.zip |
I/O redirection for the pre-push hook
Fix and complete the implementation of calling the pre-push hook.
Add the missing error stream redirect, and add the missing setters
in Transport and in PushCommand. In Transport, delay setting up a
PrePushHook such that it happens only on a push. Previously, the
hook was set up also for fetches.
Bug: 549246
Change-Id: I64a576dfc6b139426f05d9ea6654027ab805734e
Signed-off-by: Thomas Wolf <twolf@apache.org>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java index 6f7aa63edc..ff5f8b76cc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java @@ -16,9 +16,12 @@ import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.PrintStream; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.Properties; import org.eclipse.jgit.api.errors.DetachedHeadException; @@ -115,7 +118,7 @@ public class PushCommandTest extends RepositoryTestCase { + "\"\nexit 0"); try (Git git1 = new Git(db)) { - // create some refs via commits and tag + // create a commit RevCommit commit = git1.commit().setMessage("initial commit").call(); RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); @@ -126,6 +129,54 @@ public class PushCommandTest extends RepositoryTestCase { } } + @Test + public void testPrePushHookRedirects() throws JGitInternalException, + IOException, GitAPIException, URISyntaxException { + + // create other repository + Repository db2 = createWorkRepository(); + + // setup the first repository + final StoredConfig config = db.getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + URIish uri = new URIish(db2.getDirectory().toURI().toURL()); + remoteConfig.addURI(uri); + remoteConfig.update(config); + config.save(); + + writeHookFile(PrePushHook.NAME, "#!/bin/sh\n" + + "echo \"1:$1, 2:$2, 3:$3\"\n" // to stdout + + "cat - 1>&2\n" // to stderr + + "exit 0\n"); + + try (Git git1 = new Git(db)) { + // create a commit + RevCommit commit = git1.commit().setMessage("initial commit") + .call(); + + RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); + try (ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); + ByteArrayOutputStream errBytes = new ByteArrayOutputStream(); + PrintStream stdout = new PrintStream(outBytes, true, + StandardCharsets.UTF_8); + PrintStream stderr = new PrintStream(errBytes, true, + StandardCharsets.UTF_8)) { + git1.push() + .setRemote("test") + .setRefSpecs(spec) + .setHookOutputStream(stdout) + .setHookErrorStream(stderr) + .call(); + String out = outBytes.toString(StandardCharsets.UTF_8); + String err = errBytes.toString(StandardCharsets.UTF_8); + assertEquals("1:test, 2:" + uri + ", 3:\n", out); + assertEquals("refs/heads/master " + commit.getName() + + " refs/heads/x " + ObjectId.zeroId().name() + '\n', + err); + } + } + } + private File writeHookFile(String name, String data) throws IOException { File path = new File(db.getWorkTree() + "/.git/hooks/", name); |