From 3214171decf596f003c154685638b01faba31ff3 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Wed, 14 Feb 2018 20:34:03 +0100 Subject: [PATCH] RepoCommand: don't record new commit if tree did not change Signed-off-by: Han-Wen Nienhuys Change-Id: Ib6509e816684256d723558d5e8f3c37de58a2ff8 --- .../eclipse/jgit/gitrepo/RepoCommandTest.java | 56 ++++++++++++++++++- .../org/eclipse/jgit/gitrepo/RepoCommand.java | 5 ++ 2 files changed, 59 insertions(+), 2 deletions(-) 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) -- 2.39.5