]> source.dussan.org Git - jgit.git/commitdiff
RepoCommand: don't record new commit if tree did not change 87/117387/4
authorHan-Wen Nienhuys <hanwen@google.com>
Wed, 14 Feb 2018 19:34:03 +0000 (20:34 +0100)
committerDavid Pursehouse <david.pursehouse@gmail.com>
Mon, 19 Feb 2018 11:24:07 +0000 (20:24 +0900)
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Change-Id: Ib6509e816684256d723558d5e8f3c37de58a2ff8

org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

index 2df34df30fa3d84a6b2aa8d207bc66d5aa2ea38a..f4b86e0ce67a985b52c81ce7d4d2bba3ff2e35c0 100644 (file)
@@ -185,6 +185,59 @@ public class RepoCommandTest extends RepositoryTestCase {
                }
        }
 
+       @Test
+       public void runTwiceIsNOP() throws Exception {
+               Repository child = Git.cloneRepository()
+                       .setURI(groupADb.getDirectory().toURI().toString())
+                       .setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
+                       .getRepository();
+
+               Repository dest = Git.cloneRepository()
+                       .setURI(db.getDirectory().toURI().toString())
+                       .setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
+                       .getRepository();
+
+               assertTrue(dest.isBare());
+               assertTrue(child.isBare());
+
+               StringBuilder xmlContent = new StringBuilder();
+               xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+                       .append("<manifest>")
+                       .append("<remote name=\"remote1\" fetch=\"..\" />")
+                       .append("<default revision=\"master\" remote=\"remote1\" />")
+                       .append("<project path=\"base\" name=\"platform/base\" />")
+                       .append("</manifest>");
+               RepoCommand cmd = new RepoCommand(dest);
+
+               IndexedRepos repos = new IndexedRepos();
+               repos.put("platform/base", child);
+
+               RevCommit commit = cmd
+                       .setInputStream(new ByteArrayInputStream(
+                               xmlContent.toString().getBytes(UTF_8)))
+                       .setRemoteReader(repos)
+                       .setURI("platform/")
+                       .setTargetURI("platform/superproject")
+                       .setRecordRemoteBranch(true)
+                       .setRecordSubmoduleLabels(true)
+                       .call();
+
+               String firstIdStr = commit.getId().name() + ":" + ".gitmodules";
+               commit = new RepoCommand(dest)
+                       .setInputStream(new ByteArrayInputStream(
+                               xmlContent.toString().getBytes(UTF_8)))
+                       .setRemoteReader(repos)
+                       .setURI("platform/")
+                       .setTargetURI("platform/superproject")
+                       .setRecordRemoteBranch(true)
+                       .setRecordSubmoduleLabels(true)
+                       .call();
+               String idStr = commit.getId().name() + ":" + ".gitmodules";
+               assertEquals(firstIdStr, idStr);
+               child.close();
+               dest.close();
+       }
+
        @Test
        public void androidSetup() throws Exception {
                Repository child = Git.cloneRepository()
@@ -235,6 +288,7 @@ public class RepoCommandTest extends RepositoryTestCase {
                child.close();
                dest.close();
        }
+
        @Test
        public void recordUnreachableRemotes() throws Exception {
                StringBuilder xmlContent = new StringBuilder();
@@ -277,8 +331,6 @@ public class RepoCommandTest extends RepositoryTestCase {
                dest.close();
        }
 
-
-
        @Test
        public void gerritSetup() throws Exception {
                Repository child =
index 24651b9b6b66cb93a91b1e2268fa4522207e1950..377fdf0199a8fc42c62ad07b3be271a86461804f 100644 (file)
@@ -643,6 +643,11 @@ public class RepoCommand extends GitCommand<RevCommit> {
 
                                // Create a Commit object, populate it and write it
                                ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$
+                               if (headId != null && rw.parseCommit(headId).getTree().getId().equals(treeId)) {
+                                       // No change. Do nothing.
+                                       return rw.parseCommit(headId);
+                               }
+
                                CommitBuilder commit = new CommitBuilder();
                                commit.setTreeId(treeId);
                                if (headId != null)