Skip to content

Commit 65ca95b

Browse files
committed
Fix allowing local branches to be created to track their remote
1 parent e9dcb3f commit 65ca95b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/main/java/org/commonwl/view/git/GitService.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import org.apache.commons.codec.digest.DigestUtils;
2323
import org.commonwl.view.researchobject.HashableAgent;
2424
import org.eclipse.jgit.api.Git;
25+
import org.eclipse.jgit.api.ListBranchCommand;
2526
import org.eclipse.jgit.api.errors.GitAPIException;
27+
import org.eclipse.jgit.lib.ObjectId;
2628
import org.eclipse.jgit.lib.PersonIdent;
29+
import org.eclipse.jgit.lib.Ref;
2730
import org.eclipse.jgit.revwalk.RevCommit;
2831
import org.slf4j.Logger;
2932
import org.slf4j.LoggerFactory;
@@ -37,6 +40,7 @@
3740
import java.net.URISyntaxException;
3841
import java.nio.file.Path;
3942
import java.util.HashSet;
43+
import java.util.List;
4044
import java.util.Set;
4145

4246
import static org.apache.jena.ext.com.google.common.io.Files.createTempDir;
@@ -106,7 +110,28 @@ public Git getRepository(GitDetails gitDetails, boolean reuseDir)
106110

107111
// Checkout the specific branch or commit ID
108112
if (repo != null) {
113+
System.out.println("Listing local branches:");
114+
List<Ref> call = repo.branchList().call();
115+
for (Ref ref : call) {
116+
System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
117+
}
118+
119+
System.out.println("Now including remote branches:");
120+
call = repo.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
121+
for (Ref ref : call) {
122+
System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
123+
}
124+
125+
// Create a new local branch if it does not exist and not a commit ID
126+
boolean createBranch = !ObjectId.isId(gitDetails.getBranch());
127+
if (createBranch) {
128+
Ref ref = repo.getRepository().exactRef("refs/heads/" + gitDetails.getBranch());
129+
if (ref != null) {
130+
createBranch = false;
131+
}
132+
}
109133
repo.checkout()
134+
.setCreateBranch(createBranch)
110135
.setName(gitDetails.getBranch())
111136
.call();
112137
String branch = repo.getRepository().getFullBranch();

0 commit comments

Comments
 (0)