]> source.dussan.org Git - jgit.git/commitdiff
Add lookup to remote config if the refSpec is not explicitly set 08/3808/3
authorStefan Lay <stefan.lay@sap.com>
Mon, 4 Jul 2011 11:20:51 +0000 (13:20 +0200)
committerStefan Lay <stefan.lay@sap.com>
Mon, 4 Jul 2011 11:20:51 +0000 (13:20 +0200)
If no refSpec is explicitly set, the PushCommand should first check the
remote config and then as a fallback use the current behavior.

Change-Id: I2bc648abc517b1d01b2de15d383423ace2081e72
Signed-off-by: Stefan Lay <stefan.lay@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/PushCommand.java

index 760d31a349ccc1d5a5b7599258bc1764a422a65d..d5ae0c99300618bce0cf6607d799811494a39a1e 100644 (file)
@@ -195,4 +195,76 @@ public class PushCommandTest extends RepositoryTestCase {
 
                }
        }
+
+       /**
+        * Check that the push refspec is read from config.
+        *
+        * @throws Exception
+        */
+       @Test
+       public void testPushWithRefSpecFromConfig() throws Exception {
+               Git git = new Git(db);
+               Git git2 = new Git(createBareRepository());
+
+               final StoredConfig config = git.getRepository().getConfig();
+               RemoteConfig remoteConfig = new RemoteConfig(config, "test");
+               URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
+                               .toURL());
+               remoteConfig.addURI(uri);
+               remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch"));
+               remoteConfig.update(config);
+               config.save();
+
+               writeTrashFile("f", "content of f");
+               git.add().addFilepattern("f").call();
+               RevCommit commit = git.commit().setMessage("adding f").call();
+
+               assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
+               git.push().setRemote("test").call();
+               assertEquals(commit.getId(),
+                               git2.getRepository().resolve("refs/heads/newbranch"));
+
+
+       }
+
+       /**
+        * Check that only HEAD is pushed if no refspec is given.
+        *
+        * @throws Exception
+        */
+       @Test
+       public void testPushWithoutPushRefSpec() throws Exception {
+               Git git = new Git(db);
+               Git git2 = new Git(createBareRepository());
+
+               final StoredConfig config = git.getRepository().getConfig();
+               RemoteConfig remoteConfig = new RemoteConfig(config, "test");
+               URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
+                               .toURL());
+               remoteConfig.addURI(uri);
+               remoteConfig.addFetchRefSpec(new RefSpec(
+                               "+refs/heads/*:refs/remotes/origin/*"));
+               remoteConfig.update(config);
+               config.save();
+
+               writeTrashFile("f", "content of f");
+               git.add().addFilepattern("f").call();
+               RevCommit commit = git.commit().setMessage("adding f").call();
+
+               git.checkout().setName("not-pushed").setCreateBranch(true).call();
+               git.checkout().setName("branchtopush").setCreateBranch(true).call();
+
+               assertEquals(null,
+                               git2.getRepository().resolve("refs/heads/branchtopush"));
+               assertEquals(null, git2.getRepository()
+                               .resolve("refs/heads/not-pushed"));
+               assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
+               git.push().setRemote("test").call();
+               assertEquals(commit.getId(),
+                               git2.getRepository().resolve("refs/heads/branchtopush"));
+               assertEquals(null, git2.getRepository()
+                               .resolve("refs/heads/not-pushed"));
+               assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
+
+       }
 }
index 40bade8840b8272fd40ef8f3dc697ff6cfcdd4be..f843a02441b9277279ce4ce2a1ed012635711e85 100644 (file)
@@ -124,6 +124,11 @@ public class PushCommand extends GitCommand<Iterable<PushResult>> {
                ArrayList<PushResult> pushResults = new ArrayList<PushResult>(3);
 
                try {
+                       if (refSpecs.isEmpty()) {
+                               RemoteConfig config = new RemoteConfig(repo.getConfig(),
+                                               getRemote());
+                               refSpecs.addAll(config.getPushRefSpecs());
+                       }
                        if (refSpecs.isEmpty()) {
                                Ref head = repo.getRef(Constants.HEAD);
                                if (head != null && head.isSymbolic())