]> source.dussan.org Git - jgit.git/commitdiff
TestRepository: Allow setting explicit Change-Id 49/43749/1
authorDave Borowitz <dborowitz@google.com>
Thu, 12 Mar 2015 19:14:47 +0000 (12:14 -0700)
committerDave Borowitz <dborowitz@google.com>
Thu, 12 Mar 2015 19:47:18 +0000 (12:47 -0700)
This includes both leaving existing Change-Ids alone (as, for example
Gerrit's commit-msg hook does) and programmatically setting a value.

Change-Id: Iaaffb0107ae27de24df1f0e95a8d628fb8ea5364

org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java

index 2afc7163b0493bf082766bb5a2f97aef0150818d..be17bcdc79f889ad22bd603ddb8af33ef7dc9f2f 100644 (file)
@@ -875,7 +875,7 @@ public class TestRepository<R extends Repository> {
                private PersonIdent author;
                private PersonIdent committer;
 
-               private boolean insertChangeId;
+               private String changeId;
 
                private boolean updateCommitterTime;
 
@@ -1002,7 +1002,14 @@ public class TestRepository<R extends Repository> {
                }
 
                public CommitBuilder insertChangeId() {
-                       insertChangeId = true;
+                       changeId = "";
+                       return this;
+               }
+
+               public CommitBuilder insertChangeId(String c) {
+                       // Validate, but store as a string so we can use "" as a sentinel.
+                       ObjectId.fromString(c);
+                       changeId = c;
                        return this;
                }
 
@@ -1029,8 +1036,7 @@ public class TestRepository<R extends Repository> {
                                                c.setTreeId(topLevelTree);
                                        else
                                                c.setTreeId(tree.writeTree(ins));
-                                       if (insertChangeId)
-                                               insertChangeId(c);
+                                       insertChangeId(c);
                                        c.setMessage(message);
                                        commitId = ins.insert(c);
                                        ins.flush();
@@ -1045,6 +1051,8 @@ public class TestRepository<R extends Repository> {
 
                private void insertChangeId(org.eclipse.jgit.lib.CommitBuilder c)
                                throws IOException {
+                       if (changeId == null)
+                               return;
                        int idx = ChangeIdUtil.indexOfChangeId(message, "\n");
                        if (idx >= 0)
                                return;
@@ -1052,13 +1060,18 @@ public class TestRepository<R extends Repository> {
                        ObjectId firstParentId = null;
                        if (!parents.isEmpty())
                                firstParentId = parents.get(0);
-                       ObjectId changeId = ChangeIdUtil.computeChangeId(c.getTreeId(),
-                                       firstParentId, c.getAuthor(), c.getCommitter(), message);
-                       message = ChangeIdUtil.insertId(message, changeId);
-                       if (changeId != null)
+
+                       ObjectId cid;
+                       if (changeId.equals(""))
+                               cid = ChangeIdUtil.computeChangeId(c.getTreeId(), firstParentId,
+                                               c.getAuthor(), c.getCommitter(), message);
+                       else
+                               cid = ObjectId.fromString(changeId);
+                       message = ChangeIdUtil.insertId(message, cid);
+                       if (cid != null)
                                message = message.replaceAll("\nChange-Id: I" //$NON-NLS-1$
                                                + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I" //$NON-NLS-1$ //$NON-NLS-2$
-                                               + changeId.getName() + "\n"); //$NON-NLS-1$
+                                               + cid.getName() + "\n"); //$NON-NLS-1$
                }
 
                public CommitBuilder child() throws Exception {
index 09259de7db74b479b8df6b96fbafd94936e377e2..cefc779a27516387aa548f4cf963873992871589 100644 (file)
@@ -112,6 +112,17 @@ public class TestRepositoryTest {
                assertEquals(msg, c.getFullMessage());
        }
 
+       @Test
+       public void insertExplicitChangeId() throws Exception {
+               RevCommit c = tr.commit().message("message")
+                               .insertChangeId("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
+                               .create();
+               rw.parseBody(c);
+               assertEquals("message\n\n"
+                               + "Change-Id: Ideadbeefdeadbeefdeadbeefdeadbeefdeadbeef\n"
+                               , c.getFullMessage());
+       }
+
        @Test
        public void resetFromSymref() throws Exception {
                repo.updateRef("HEAD").link("refs/heads/master");