Try to checkout branch after cloning
When no branch was specified in the clone command, HEAD pointed to a commit after clone. Now the clone command tries to find a branch which points to the same commit and checks out this branch. Bug: 339354 Change-Id: Ie3844465329f213dee4a8868dbf434ac3ce23a08 Signed-off-by: Stefan Lay <stefan.lay@sap.com>
This commit is contained in:
parent
e24005de2d
commit
792b93bc62
|
@ -50,6 +50,7 @@
|
|||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jgit.junit.TestRepository;
|
||||
import org.eclipse.jgit.lib.ConfigConstants;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.RefUpdate;
|
||||
|
@ -99,6 +100,20 @@ public void testCloneRepository() {
|
|||
assertNotNull(git2);
|
||||
ObjectId id = git2.getRepository().resolve("tag-for-blob");
|
||||
assertNotNull(id);
|
||||
assertEquals(git2.getRepository().getFullBranch(),
|
||||
"refs/heads/test");
|
||||
assertEquals(
|
||||
"origin",
|
||||
git2.getRepository()
|
||||
.getConfig()
|
||||
.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
|
||||
"test", ConfigConstants.CONFIG_KEY_REMOTE));
|
||||
assertEquals(
|
||||
"refs/heads/test",
|
||||
git2.getRepository()
|
||||
.getConfig()
|
||||
.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
|
||||
"test", ConfigConstants.CONFIG_KEY_MERGE));
|
||||
} catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
@ -109,14 +124,14 @@ public void testCloneRepositoryWithBranch() {
|
|||
try {
|
||||
File directory = createTempDirectory("testCloneRepositoryWithBranch");
|
||||
CloneCommand command = Git.cloneRepository();
|
||||
command.setBranch("refs/heads/test");
|
||||
command.setBranch("refs/heads/master");
|
||||
command.setDirectory(directory);
|
||||
command.setURI("file://"
|
||||
+ git.getRepository().getWorkTree().getPath());
|
||||
Git git2 = command.call();
|
||||
assertNotNull(git2);
|
||||
assertEquals(git2.getRepository().getFullBranch(),
|
||||
"refs/heads/test");
|
||||
"refs/heads/master");
|
||||
} catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
|
|
@ -78,7 +78,7 @@ public void testWrap() {
|
|||
assertEquals(1, git.branchList().call().size());
|
||||
|
||||
git = Git.wrap(bareRepo);
|
||||
assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
|
||||
assertEquals(2, git.branchList().setListMode(ListMode.ALL).call()
|
||||
.size());
|
||||
|
||||
try {
|
||||
|
@ -95,7 +95,7 @@ public void testOpen() throws IOException {
|
|||
assertEquals(1, git.branchList().call().size());
|
||||
|
||||
git = Git.open(bareRepo.getDirectory());
|
||||
assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
|
||||
assertEquals(2, git.branchList().setListMode(ListMode.ALL).call()
|
||||
.size());
|
||||
|
||||
git = Git.open(db.getWorkTree());
|
||||
|
|
|
@ -139,11 +139,6 @@ private FetchResult fetch(Repository repo, URIish u)
|
|||
config.addFetchRefSpec(refSpec);
|
||||
config.update(repo.getConfig());
|
||||
|
||||
repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
|
||||
branch, ConfigConstants.CONFIG_KEY_REMOTE, remote);
|
||||
repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
|
||||
branch, ConfigConstants.CONFIG_KEY_MERGE, branch);
|
||||
|
||||
repo.getConfig().save();
|
||||
|
||||
// run the fetch command
|
||||
|
@ -160,16 +155,23 @@ private void checkout(Repository repo, FetchResult result)
|
|||
throws JGitInternalException,
|
||||
MissingObjectException, IncorrectObjectTypeException, IOException {
|
||||
|
||||
if (branch.startsWith(Constants.R_HEADS)) {
|
||||
final RefUpdate head = repo.updateRef(Constants.HEAD);
|
||||
head.disableRefLog();
|
||||
head.link(branch);
|
||||
Ref head = result.getAdvertisedRef(branch);
|
||||
if (branch.equals(Constants.HEAD)) {
|
||||
Ref foundBranch = findBranchToCheckout(result);
|
||||
if (foundBranch != null)
|
||||
head = foundBranch;
|
||||
}
|
||||
|
||||
final Ref head = result.getAdvertisedRef(branch);
|
||||
if (head == null || head.getObjectId() == null)
|
||||
return; // throw exception?
|
||||
|
||||
if (head.getName().startsWith(Constants.R_HEADS)) {
|
||||
final RefUpdate newHead = repo.updateRef(Constants.HEAD);
|
||||
newHead.disableRefLog();
|
||||
newHead.link(head.getName());
|
||||
addMergeConfig(repo, head);
|
||||
}
|
||||
|
||||
final RevCommit commit = parseCommit(repo, head);
|
||||
|
||||
boolean detached = !head.getName().startsWith(Constants.R_HEADS);
|
||||
|
@ -185,6 +187,32 @@ private void checkout(Repository repo, FetchResult result)
|
|||
}
|
||||
}
|
||||
|
||||
private Ref findBranchToCheckout(FetchResult result) {
|
||||
Ref foundBranch = null;
|
||||
final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
|
||||
for (final Ref r : result.getAdvertisedRefs()) {
|
||||
final String n = r.getName();
|
||||
if (!n.startsWith(Constants.R_HEADS))
|
||||
continue;
|
||||
if (idHEAD == null)
|
||||
continue;
|
||||
if (r.getObjectId().equals(idHEAD.getObjectId())) {
|
||||
foundBranch = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return foundBranch;
|
||||
}
|
||||
|
||||
private void addMergeConfig(Repository repo, Ref head) throws IOException {
|
||||
String branchName = Repository.shortenRefName(head.getName());
|
||||
repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
|
||||
branchName, ConfigConstants.CONFIG_KEY_REMOTE, remote);
|
||||
repo.getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
|
||||
branchName, ConfigConstants.CONFIG_KEY_MERGE, head.getName());
|
||||
repo.getConfig().save();
|
||||
}
|
||||
|
||||
private RevCommit parseCommit(final Repository repo, final Ref ref)
|
||||
throws MissingObjectException, IncorrectObjectTypeException,
|
||||
IOException {
|
||||
|
|
Loading…
Reference in New Issue