Add setTargetBranch in RepoCommand.

This will allow us to write the super project in a branch other than
master.

Change-Id: I578ed9ecbc6423416239e31ad644531dae9fb5c3
Signed-off-by: Yuxuan 'fishy' Wang <fishywang@google.com>
This commit is contained in:
Yuxuan 'fishy' Wang 2015-07-09 22:12:28 -07:00
parent 37a1e4beaa
commit 217b2a7cc5
2 changed files with 86 additions and 44 deletions

View File

@ -241,9 +241,9 @@ public void testRepoManifestCopyFile() throws Exception {
@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 void testBareRepo() throws Exception {
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 void testRevisionTag() throws Exception {
@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 void testRevisionBare() throws Exception {
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 void testCopyFileBare() throws Exception {
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 void testReplaceManifestBare() throws Exception {
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 void testRemoveOverlappingBare() throws Exception {
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 void testRemoteAlias() throws Exception {
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();

View File

@ -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 @@ private static class RemoteUnavailableException extends GitAPIException {
/**
* @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 RepoCommand setPath(final String path) {
* @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 RepoCommand setInputStream(final InputStream inputStream) {
* @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 RepoCommand setURI(final String uri) {
* @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,11 +290,29 @@ public RepoCommand setGroups(final String groups) {
* @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 RepoCommand setProgressMonitor(final ProgressMonitor monitor) {
/**
* 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 RevCommit call() throws GitAPIException {
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 RevCommit call() throws GitAPIException {
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 RevCommit call() throws GitAPIException {
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);