From: Han-Wen Nienhuys Date: Wed, 14 Feb 2018 19:34:03 +0000 (+0100) Subject: RepoCommand: don't record new commit if tree did not change X-Git-Tag: v4.11.0.201803080745-r~100^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fchanges%2F87%2F117387%2F4;p=jgit.git RepoCommand: don't record new commit if tree did not change Signed-off-by: Han-Wen Nienhuys Change-Id: Ib6509e816684256d723558d5e8f3c37de58a2ff8 --- diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java index 2df34df30f..f4b86e0ce6 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java @@ -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("\n") + .append("") + .append("") + .append("") + .append("") + .append(""); + 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 = diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java index 24651b9b6b..377fdf0199 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java @@ -643,6 +643,11 @@ public class RepoCommand extends GitCommand { // 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)