]> source.dussan.org Git - jgit.git/commitdiff
Support [<ref>]@{upstream} revision syntax 78/6878/2
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Thu, 19 Jul 2012 14:05:28 +0000 (16:05 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Fri, 20 Jul 2012 06:29:10 +0000 (08:29 +0200)
Resolves into a ref name corresponding to the named (or current)
branch's upstream ref.

Change-Id: I98df46cedb498724cf14343fbb168f24ff667929

org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

index 1c6a547729cea652cb79ab3e199c8af070f3fbe6..deb830ff5bc4a6d5a57a6f4003adffaf0c0b9b80 100644 (file)
@@ -55,6 +55,7 @@ import java.io.IOException;
 
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.junit.Test;
 
 public class RepositoryResolveTest extends SampleDataRepositoryTestCase {
@@ -243,6 +244,28 @@ public class RepositoryResolveTest extends SampleDataRepositoryTestCase {
                assertEquals("HEAD", db.simplify("HEAD"));
        }
 
+       @Test
+       public void resolveUpstream() throws Exception {
+               Git git = new Git(db);
+               writeTrashFile("file.txt", "content");
+               git.add().addFilepattern("file.txt").call();
+               RevCommit c1 = git.commit().setMessage("create file").call();
+               writeTrashFile("file2.txt", "content");
+               RefUpdate updateRemoteRef = db.updateRef("refs/remotes/origin/main");
+               updateRemoteRef.setNewObjectId(c1);
+               updateRemoteRef.update();
+               db.getConfig().setString("branch", "master", "remote", "origin");
+               db.getConfig()
+                               .setString("branch", "master", "merge", "refs/heads/main");
+               db.getConfig().setString("remote", "origin", "url",
+                               "git://example.com/here");
+               db.getConfig().setString("remote", "origin", "fetch",
+                               "+refs/heads/*:refs/remotes/origin/*");
+               git.add().addFilepattern("file2.txt").call();
+               git.commit().setMessage("create file").call();
+               assertEquals("refs/remotes/origin/main", db.simplify("@{upstream}"));
+       }
+
        private static ObjectId id(String name) {
                return ObjectId.fromString(name);
        }
index db030952a008a7c7a683a294b2de3e6a45d5b868..82394dd75709e64ac7dce9f67510e7f98ef60dec 100644 (file)
@@ -51,6 +51,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.text.MessageFormat;
 import java.util.Collection;
 import java.util.Collections;
@@ -82,6 +83,8 @@ import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.storage.file.CheckoutEntry;
 import org.eclipse.jgit.storage.file.ReflogEntry;
 import org.eclipse.jgit.storage.file.ReflogReader;
+import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.RemoteConfig;
 import org.eclipse.jgit.treewalk.TreeWalk;
 import org.eclipse.jgit.util.FS;
 import org.eclipse.jgit.util.FileUtils;
@@ -577,7 +580,45 @@ public abstract class Repository {
                                        }
                                }
                                if (time != null) {
-                                       if (time.matches("^-\\d+$")) {
+                                       if (time.equals("upstream")) {
+                                               if (name == null)
+                                                       name = new String(revChars, done, i);
+                                               if (name.equals(""))
+                                                       // Currently checked out branch, HEAD if
+                                                       // detached
+                                                       name = Constants.HEAD;
+                                               Ref ref = getRef(name);
+                                               if (ref == null)
+                                                       return null;
+                                               if (ref.isSymbolic())
+                                                       ref = ref.getLeaf();
+                                               name = ref.getName();
+
+                                               RemoteConfig remoteConfig;
+                                               try {
+                                                       remoteConfig = new RemoteConfig(getConfig(),
+                                                                       "origin");
+                                               } catch (URISyntaxException e) {
+                                                       throw new RevisionSyntaxException(revstr);
+                                               }
+                                               String remoteBranchName = getConfig()
+                                                               .getString(
+                                                                               ConfigConstants.CONFIG_BRANCH_SECTION,
+                                                               Repository.shortenRefName(ref.getName()),
+                                                                               ConfigConstants.CONFIG_KEY_MERGE);
+                                               List<RefSpec> fetchRefSpecs = remoteConfig
+                                                               .getFetchRefSpecs();
+                                               for (RefSpec refSpec : fetchRefSpecs) {
+                                                       if (refSpec.matchSource(remoteBranchName)) {
+                                                               RefSpec expandFromSource = refSpec
+                                                                               .expandFromSource(remoteBranchName);
+                                                               name = expandFromSource.getDestination();
+                                                               break;
+                                                       }
+                                               }
+                                               if (name == null)
+                                                       throw new RevisionSyntaxException(revstr);
+                                       } else if (time.matches("^-\\d+$")) {
                                                if (name != null)
                                                        throw new RevisionSyntaxException(revstr);
                                                else {