Browse Source

Make PrePushHook properly terminate ref lines

All of the input lines passed to pre-push hook scripts must be properly
terminated by '\n', so that normal shell scripts like the git-supplied
pre-push.sample work properly, even when pushing just a single branch.

With the old code, hook scripts that use the following pattern didn't
process the last line, because 'read' has a non-zero exit status when
EOF is encountered:
  while read local_ref local_sha remote_ref remote_sha; do ... done

Change-Id: Id899662ed3fedef6c314fc4b2ddf91a6dcb98cbb
Signed-off-by: Markus Keller <markus.kell.r@gmail.com>
stable-5.2
Markus Keller 6 years ago committed by Matthias Sohn
parent
commit
8420c729a0
  1. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java
  2. 6
      org.eclipse.jgit/src/org/eclipse/jgit/hooks/PrePushHook.java

2
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java

@ -148,7 +148,7 @@ public class PushCommandTest extends RepositoryTestCase {
git1.push().setRemote("test").setRefSpecs(spec).call(); git1.push().setRemote("test").setRefSpecs(spec).call();
assertEquals("1:test, 2:" + uri + ", 3:\n" + "refs/heads/master " assertEquals("1:test, 2:" + uri + ", 3:\n" + "refs/heads/master "
+ commit.getName() + " refs/heads/x " + commit.getName() + " refs/heads/x "
+ ObjectId.zeroId().name(), read(hookOutput)); + ObjectId.zeroId().name() + "\n", read(hookOutput));
} }
} }

6
org.eclipse.jgit/src/org/eclipse/jgit/hooks/PrePushHook.java

@ -164,12 +164,7 @@ public class PrePushHook extends GitHook<String> {
*/ */
public void setRefs(Collection<RemoteRefUpdate> toRefs) { public void setRefs(Collection<RemoteRefUpdate> toRefs) {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
boolean first = true;
for (RemoteRefUpdate u : toRefs) { for (RemoteRefUpdate u : toRefs) {
if (!first)
b.append("\n"); //$NON-NLS-1$
else
first = false;
b.append(u.getSrcRef()); b.append(u.getSrcRef());
b.append(" "); //$NON-NLS-1$ b.append(" "); //$NON-NLS-1$
b.append(u.getNewObjectId().getName()); b.append(u.getNewObjectId().getName());
@ -179,6 +174,7 @@ public class PrePushHook extends GitHook<String> {
ObjectId ooid = u.getExpectedOldObjectId(); ObjectId ooid = u.getExpectedOldObjectId();
b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
.getName()); .getName());
b.append("\n"); //$NON-NLS-1$
} }
refs = b.toString(); refs = b.toString();
} }

Loading…
Cancel
Save