]> source.dussan.org Git - jgit.git/commitdiff
Allow a @ without branch in revision syntax 74/6774/2
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Mon, 16 Jul 2012 21:47:46 +0000 (23:47 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Mon, 16 Jul 2012 21:56:51 +0000 (23:56 +0200)
No branch before @ is interpreted as the currently checked out branch.
For detached heads it would be HEAD, but normally it is the branch
that HEAD refers to.

Change-Id: I051a1724fa390b8212e8986ba832b1347a20371e

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

index c3de79b6c47a5e60854e0586657168ac4c35b7c8..15cbbcbdf0613c6196fc543211389d05cfb0f37d 100644 (file)
@@ -71,6 +71,38 @@ public class ReflogResolveTest extends RepositoryTestCase {
                assertEquals(c1, db.resolve("master@{1}"));
        }
 
+       @Test
+       public void resolveUnnamedCurrentBranchCommits() 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("file.txt", "content2");
+               git.add().addFilepattern("file.txt").call();
+               RevCommit c2 = git.commit().setMessage("edit file").call();
+
+               assertEquals(c2, db.resolve("master@{0}"));
+               assertEquals(c1, db.resolve("master@{1}"));
+
+               git.checkout().setCreateBranch(true).setName("newbranch")
+                               .setStartPoint(c1).call();
+
+               // same as current branch, e.g. master
+               assertEquals(c1, db.resolve("@{0}"));
+               try {
+                       assertEquals(c1, db.resolve("@{1}"));
+                       fail(); // Looking at wrong ref, e.g HEAD
+               } catch (RevisionSyntaxException e) {
+                       assertNotNull(e);
+               }
+
+               // detached head, read HEAD reflog
+               git.checkout().setName(c2.getName()).call();
+               assertEquals(c2, db.resolve("@{0}"));
+               assertEquals(c1, db.resolve("@{1}"));
+               assertEquals(c2, db.resolve("@{2}"));
+       }
+
        @Test
        public void resolveReflogParent() throws Exception {
                Git git = new Git(db);
index 1f9286871fa9680ea8cd70bad39e27f0e373bae9..2916c29b644024f12cd960bd8804c77ea4e37704 100644 (file)
@@ -533,10 +533,22 @@ public abstract class Repository {
                                }
                                if (time != null) {
                                        String refName = new String(revChars, 0, i);
-                                       Ref resolved = getRefDatabase().getRef(refName);
-                                       if (resolved == null)
+                                       Ref ref;
+                                       if (refName.equals("")) {
+                                               // Currently checked out branch, HEAD if
+                                               // detached
+                                               ref = getRef(Constants.HEAD);
+                                               if (ref == null)
+                                                       return null;
+                                               if (ref.isSymbolic())
+                                                       ref = ref.getLeaf();
+                                               if (ref.getObjectId() == null)
+                                                       return null;
+                                       } else
+                                               ref = getRef(refName);
+                                       if (ref == null)
                                                return null;
-                                       rev = resolveReflog(rw, resolved, time);
+                                       rev = resolveReflog(rw, ref, time);
                                        i = m;
                                } else
                                        i = m - 1;