From: Kevin Sawicki Date: Wed, 23 Nov 2011 00:29:28 +0000 (-0800) Subject: Guard against null branch in PullCommand X-Git-Tag: v1.2.0.201112221803-r~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c3fe50bb18ed4e4e5e563172be4076fa5901a18a;p=jgit.git Guard against null branch in PullCommand Throw a NoHeadException when Repository.getFullBranch returns null Bug: 351543 Change-Id: I666cd5b67781508a293ae553c6fe5c080c8f4d99 Signed-off-by: Kevin Sawicki --- diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandTest.java index 12b2f210e8..eaed8d8b8b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandTest.java @@ -55,8 +55,11 @@ import java.io.IOException; import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode; import org.eclipse.jgit.api.MergeResult.MergeStatus; +import org.eclipse.jgit.api.errors.NoHeadException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.lib.StoredConfig; @@ -220,6 +223,15 @@ public class PullCommandTest extends RepositoryTestCase { .getRepositoryState()); } + @Test(expected = NoHeadException.class) + public void testPullEmptyRepository() throws Exception { + Repository empty = createWorkRepository(); + RefUpdate delete = empty.updateRef(Constants.HEAD, true); + delete.setForceUpdate(true); + delete.delete(); + Git.wrap(empty).pull().call(); + } + @Override @Before public void setUp() throws Exception { diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties index a5fea35ab7..fc32069999 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties @@ -352,6 +352,7 @@ prefixRemote=remote: problemWithResolvingPushRefSpecsLocally=Problem with resolving push ref specs locally: {0} progressMonUploading=Uploading {0} propertyIsAlreadyNonNull=Property is already non null +pullOnRepoWithoutHEADCurrentlyNotSupported=Pull on repository without HEAD currently not supported pullTaskName=Pull pushCancelled=push cancelled pushIsNotSupportedForBundleTransport=Push is not supported for bundle transport diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java index fcc6cafd5e..4dabe07412 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java @@ -412,6 +412,7 @@ public class JGitText extends TranslationBundle { /***/ public String problemWithResolvingPushRefSpecsLocally; /***/ public String progressMonUploading; /***/ public String propertyIsAlreadyNonNull; + /***/ public String pullOnRepoWithoutHEADCurrentlyNotSupported; /***/ public String pullTaskName; /***/ public String pushCancelled; /***/ public String pushIsNotSupportedForBundleTransport; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java index 67f9832fb6..9aa33ddf68 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/PullCommand.java @@ -112,7 +112,8 @@ public class PullCommand extends TransportCommand { */ public PullResult call() throws WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, - InvalidRemoteException, CanceledException, RefNotFoundException { + InvalidRemoteException, CanceledException, RefNotFoundException, + NoHeadException { checkCallable(); monitor.beginTask(JGitText.get().pullTaskName, 2); @@ -120,6 +121,9 @@ public class PullCommand extends TransportCommand { String branchName; try { String fullBranch = repo.getFullBranch(); + if (fullBranch == null) + throw new NoHeadException( + JGitText.get().pullOnRepoWithoutHEADCurrentlyNotSupported); if (!fullBranch.startsWith(Constants.R_HEADS)) { // we can not pull if HEAD is detached and branch is not // specified explicitly