From c24eee4fa4db682b138a7a212cf711ab682d02f6 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Sun, 20 Oct 2019 18:08:59 +0200 Subject: [PATCH] [pgm] Add --mirror option to clone command Bug: 552173 Change-Id: Ic8a98b2e0f8f29afd599723f93e51b06b9f13314 Signed-off-by: Matthias Sohn --- .../tst/org/eclipse/jgit/pgm/CloneTest.java | 44 ++++++++++++++++++- .../jgit/pgm/internal/CLIText.properties | 3 ++ .../src/org/eclipse/jgit/pgm/Clone.java | 6 ++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java index c31f28c25..0ecbd656e 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java @@ -44,6 +44,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.File; @@ -54,7 +55,13 @@ import org.eclipse.jgit.junit.MockSystemReader; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.StoredConfig; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.util.SystemReader; import org.junit.Before; @@ -90,10 +97,10 @@ public void testClone() throws Exception { assertEquals("expected 1 branch", 1, branches.size()); } - private void createInitialCommit() throws Exception { + private RevCommit createInitialCommit() throws Exception { JGitTestUtil.writeTrashFile(db, "hello.txt", "world"); git.add().addFilepattern("hello.txt").call(); - git.commit().setMessage("Initial commit").call(); + return git.commit().setMessage("Initial commit").call(); } @Test @@ -154,4 +161,37 @@ public void testCloneBare() throws Exception { assertEquals("expected 1 branch", 1, branches.size()); assertTrue("expected bare repository", git2.getRepository().isBare()); } + + @Test + public void testCloneMirror() throws Exception { + ObjectId head = createInitialCommit(); + // create a non-standard ref + RefUpdate ru = db.updateRef("refs/meta/foo/bar"); + ru.setNewObjectId(head); + ru.update(); + + File gitDir = db.getDirectory(); + String sourcePath = gitDir.getAbsolutePath(); + String targetPath = (new File(sourcePath)).getParentFile() + .getParentFile().getAbsolutePath() + File.separator + + "target.git"; + String cmd = "git clone --mirror " + shellQuote(sourcePath) + " " + + shellQuote(targetPath); + String[] result = execute(cmd); + assertArrayEquals( + new String[] { "Cloning into '" + targetPath + "'...", "", "" }, + result); + Git git2 = Git.open(new File(targetPath)); + List branches = git2.branchList().call(); + assertEquals("expected 1 branch", 1, branches.size()); + assertTrue("expected bare repository", git2.getRepository().isBare()); + StoredConfig config = git2.getRepository().getConfig(); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertTrue("expected mirror configuration", rc.isMirror()); + RefSpec fetchRefSpec = rc.getFetchRefSpecs().get(0); + assertTrue("exected force udpate", fetchRefSpec.isForceUpdate()); + assertEquals("refs/*", fetchRefSpec.getSource()); + assertEquals("refs/*", fetchRefSpec.getDestination()); + assertNotNull(git2.getRepository().exactRef("refs/meta/foo/bar")); + } } diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties index f02944265..7cca02376 100644 --- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties +++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties @@ -223,6 +223,9 @@ updating=Updating {0}..{1} usage_Aggressive=This option will cause gc to more aggressively optimize the repository at the expense of taking much more time usage_AlwaysFallback=Show uniquely abbreviated commit object as fallback usage_bareClone=Make a bare Git repository. That is, instead of creating [DIRECTORY] and placing the administrative files in [DIRECTORY]/.git, make the [DIRECTORY] itself the $GIT_DIR. +usage_mirrorClone=Set up a mirror of the source repository. This implies --bare. Compared to --bare, --mirror not only maps \ +local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) \ +and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository. usage_Blame=Show what revision and author last modified each line usage_Clean=Remove untracked files from the working tree usage_CommandLineClientForamazonsS3Service=Command line client for Amazon's S3 service diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java index fe2462012..a6a031e2c 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Clone.java @@ -74,6 +74,9 @@ class Clone extends AbstractFetchCommand implements CloneCommand.Callback { @Option(name = "--bare", usage = "usage_bareClone") private boolean isBare; + @Option(name = "--mirror", usage = "usage_mirrorClone") + private boolean isMirror; + @Option(name = "--quiet", usage = "usage_quiet") private Boolean quiet; @@ -103,7 +106,7 @@ protected void run() throws Exception { if (localName == null) { try { localName = uri.getHumanishName(); - if (isBare) { + if (isBare || isMirror) { localName = localName + Constants.DOT_GIT_EXT; } localNameF = new File(SystemReader.getInstance().getProperty( @@ -120,6 +123,7 @@ protected void run() throws Exception { CloneCommand command = Git.cloneRepository(); command.setURI(sourceUri).setRemote(remoteName).setBare(isBare) + .setMirror(isMirror) .setNoCheckout(noCheckout).setBranch(branch) .setCloneSubmodules(cloneSubmodules);