summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java81
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java49
2 files changed, 86 insertions, 44 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 3d86cfd5bb..66e7256432 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
@@ -241,9 +241,9 @@ public class RepoCommandTest extends RepositoryTestCase {
@Test
public void testBareRepo() throws Exception {
- Repository remoteDb = createBareRepository();
- Repository tempDb = createWorkRepository();
- try {
+ try (
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
StringBuilder xmlContent = new StringBuilder();
xmlContent
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
@@ -280,9 +280,6 @@ public class RepoCommandTest extends RepositoryTestCase {
String remote = defaultDb.resolve(Constants.HEAD).name();
assertEquals("The gitlink should be the same as remote head",
remote, gitlink);
- } finally {
- tempDb.close();
- remoteDb.close();
}
}
@@ -366,9 +363,9 @@ public class RepoCommandTest extends RepositoryTestCase {
@Test
public void testRevisionBare() throws Exception {
- Repository remoteDb = createBareRepository();
- Repository tempDb = createWorkRepository();
- try {
+ try (
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
StringBuilder xmlContent = new StringBuilder();
xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<manifest>")
@@ -393,17 +390,14 @@ public class RepoCommandTest extends RepositoryTestCase {
localDb.close();
assertEquals("The gitlink is same as remote head",
oldCommitId.name(), gitlink);
- } finally {
- tempDb.close();
- remoteDb.close();
}
}
@Test
public void testCopyFileBare() throws Exception {
- Repository remoteDb = createBareRepository();
- Repository tempDb = createWorkRepository();
- try {
+ try (
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
StringBuilder xmlContent = new StringBuilder();
xmlContent
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
@@ -435,17 +429,14 @@ public class RepoCommandTest extends RepositoryTestCase {
reader.close();
assertEquals("The Hello file should have expected content",
"branch world", content);
- } finally {
- tempDb.close();
- remoteDb.close();
}
}
@Test
public void testReplaceManifestBare() throws Exception {
- Repository remoteDb = createBareRepository();
- Repository tempDb = createWorkRepository();
- try {
+ try (
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
StringBuilder xmlContent = new StringBuilder();
xmlContent
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
@@ -512,17 +503,14 @@ public class RepoCommandTest extends RepositoryTestCase {
reader.close();
assertTrue("The bar submodule should exist", bar);
assertFalse("The foo submodule shouldn't exist", foo);
- } finally {
- tempDb.close();
- remoteDb.close();
}
}
@Test
public void testRemoveOverlappingBare() throws Exception {
- Repository remoteDb = createBareRepository();
- Repository tempDb = createWorkRepository();
- try {
+ try (
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
StringBuilder xmlContent = new StringBuilder();
xmlContent
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
@@ -571,9 +559,6 @@ public class RepoCommandTest extends RepositoryTestCase {
assertTrue("The foo submodule should exist", foo);
assertFalse("The foo/bar submodule shouldn't exist", foobar);
assertTrue("The a submodule should exist", a);
- } finally {
- tempDb.close();
- remoteDb.close();
}
}
@@ -671,6 +656,42 @@ public class RepoCommandTest extends RepositoryTestCase {
assertTrue("We should have foo", file.exists());
}
+ @Test
+ public void testTargetBranch() throws Exception {
+ try (
+ Repository remoteDb1 = createBareRepository();
+ Repository remoteDb2 = createBareRepository();
+ Repository tempDb = createWorkRepository()) {
+ 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=\"foo\" name=\"").append(defaultUri)
+ .append("\" />").append("</manifest>");
+ JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
+ xmlContent.toString());
+ RepoCommand command = new RepoCommand(remoteDb1);
+ command
+ .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
+ .setURI(rootUri)
+ .setTargetBranch("test")
+ .call();
+ ObjectId branchId = remoteDb1.resolve(
+ Constants.R_HEADS + "test^{tree}");
+ command = new RepoCommand(remoteDb2);
+ command
+ .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
+ .setURI(rootUri)
+ .call();
+ ObjectId defaultId = remoteDb2.resolve(Constants.HEAD + "^{tree}");
+ assertEquals(
+ "The tree id of branch db and default db should be the same",
+ branchId, defaultId);
+ }
+ }
+
private void resolveRelativeUris() {
// Find the longest common prefix ends with "/" as rootUri.
defaultUri = defaultDb.getDirectory().toURI().toString();
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 b39dd8a1f2..790f4db672 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
@@ -105,6 +105,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
private String uri;
private String groups;
private String branch;
+ private String targetBranch = Constants.HEAD;
private PersonIdent author;
private RemoteReader callback;
private InputStream inputStream;
@@ -224,27 +225,27 @@ public class RepoCommand extends GitCommand<RevCommit> {
/**
* @param repo
*/
- public RepoCommand(final Repository repo) {
+ public RepoCommand(Repository repo) {
super(repo);
}
/**
* Set path to the manifest XML file.
- *
+ * <p>
* Calling {@link #setInputStream} will ignore the path set here.
*
* @param path
* (with <code>/</code> as separator)
* @return this command
*/
- public RepoCommand setPath(final String path) {
+ public RepoCommand setPath(String path) {
this.path = path;
return this;
}
/**
* Set the input stream to the manifest XML.
- *
+ * <p>
* Setting inputStream will ignore the path set. It will be closed in
* {@link #call}.
*
@@ -252,7 +253,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
* @return this command
* @since 3.5
*/
- public RepoCommand setInputStream(final InputStream inputStream) {
+ public RepoCommand setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
return this;
}
@@ -263,7 +264,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
* @param uri
* @return this command
*/
- public RepoCommand setURI(final String uri) {
+ public RepoCommand setURI(String uri) {
this.uri = uri;
return this;
}
@@ -274,14 +275,14 @@ public class RepoCommand extends GitCommand<RevCommit> {
* @param groups groups separated by comma, examples: default|all|G1,-G2,-G3
* @return this command
*/
- public RepoCommand setGroups(final String groups) {
+ public RepoCommand setGroups(String groups) {
this.groups = groups;
return this;
}
/**
* Set default branch.
- *
+ * <p>
* This is generally the name of the branch the manifest file was in. If
* there's no default revision (branch) specified in manifest and no
* revision specified in project, this branch will be used.
@@ -289,12 +290,30 @@ public class RepoCommand extends GitCommand<RevCommit> {
* @param branch
* @return this command
*/
- public RepoCommand setBranch(final String branch) {
+ public RepoCommand setBranch(String branch) {
this.branch = branch;
return this;
}
/**
+ * Set target branch.
+ * <p>
+ * This is the target branch of the super project to be updated. If not set,
+ * default is HEAD.
+ * <p>
+ * For non-bare repositories, HEAD will always be used and this will be
+ * ignored.
+ *
+ * @param branch
+ * @return this command
+ * @since 4.1
+ */
+ public RepoCommand setTargetBranch(String branch) {
+ this.targetBranch = Constants.R_HEADS + branch;
+ return this;
+ }
+
+ /**
* The progress monitor associated with the clone operation. By default,
* this is set to <code>NullProgressMonitor</code>
*
@@ -309,7 +328,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
/**
* Set the author/committer for the bare repository commit.
- *
+ * <p>
* For non-bare repositories, the current user will be used and this will be
* ignored.
*
@@ -445,7 +464,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
ObjectId treeId = index.writeTree(inserter);
// Create a Commit object, populate it and write it
- ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
+ ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$
CommitBuilder commit = new CommitBuilder();
commit.setTreeId(treeId);
if (headId != null)
@@ -457,7 +476,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
ObjectId commitId = inserter.insert(commit);
inserter.flush();
- RefUpdate ru = repo.updateRef(Constants.HEAD);
+ RefUpdate ru = repo.updateRef(targetBranch);
ru.setNewObjectId(commitId);
ru.setExpectedOldObjectId(headId != null ? headId : ObjectId.zeroId());
Result rc = ru.update(rw);
@@ -471,12 +490,14 @@ public class RepoCommand extends GitCommand<RevCommit> {
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(
- JGitText.get().couldNotLockHEAD, ru.getRef(),
+ MessageFormat.format(
+ JGitText.get().cannotLock, targetBranch),
+ ru.getRef(),
rc);
default:
throw new JGitInternalException(MessageFormat.format(
JGitText.get().updatingRefFailed,
- Constants.HEAD, commitId.name(), rc));
+ targetBranch, commitId.name(), rc));
}
return rw.parseCommit(commitId);