Hello Mike,
Your struggle sounds similar to the one I had that lead to this SO question. I don’t know if any of the JGit command classes have been updated to handle your situation, but lower-level API will work.There are four low-level steps to create and checkout a detached orphan branch with JGit from the files in the working dir: - Create a tree object.
- I used the index (JGit DirCache) for that, but I think there are other methods.
- Create a parentless (orphan) commit from the tree object.
- Update a Ref to point to the commit.
- This creates an orphan branch.
- Update the link for the HEAD symbolic ref.
- If you want a detached checkout, this is when you specify that.
I’m including some code below for that. This is untested and extracted from a production application using JGit 2.0, but it at least compiles without warnings with JGit 5.12. It should be an ok starting point.
Cheers, Will
import java.io.IOException;
import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository;
public class OrphanBranchExample { private final Repository repo; private final PersonIdent personIdent;
private OrphanBranchExample(Repository repo, PersonIdent personIdent) { this.repo = repo; this.personIdent = personIdent; }
public void createAndCheckoutOrphanBranch( String refName, boolean detach, boolean force ) throws IOException { ObjectInserter inserter = repo.newObjectInserter(); ObjectId commitId = null;
try { ObjectId treeId = createTree(inserter); if (treeId != null) { commitId = insertCommit(inserter, treeId); } } finally { inserter.flush(); }
if (commitId != null) { String fullRef = Constants.R_HEADS + refName; setRef(fullRef, commitId, force); updateHead(fullRef, detach, force); } }
/** Returns null to basically abort. */ private ObjectId createTree(ObjectInserter inserter) { // TODO Auto-generated method stub return null; }
private ObjectId insertCommit( ObjectInserter inserter, ObjectId treeId ) throws IOException { CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setAuthor(personIdent); commitBuilder.setCommitter(personIdent); commitBuilder.setTreeId(treeId);
return inserter.insert(commitBuilder); }
private void setRef( String fullRef, ObjectId commitId, boolean force ) throws IOException { RefUpdate refUpdate = repo.getRefDatabase().newUpdate(fullRef, false); refUpdate.setNewObjectId(commitId); refUpdate.setForceUpdate(force); refUpdate.update(); }
private void updateHead( String newHead, boolean detach, boolean force ) throws IOException { RefUpdate refUpdate = repo.getRefDatabase() .newUpdate(Constants.HEAD, detach); refUpdate.setForceUpdate(force); refUpdate.link(newHead); }
}
On Jun 27, 2021, at 03:35:05, Mike Limansky <mike.limansky@xxxxxxxxx> wrote:
Hello,
I've tried to use CheckoutCommand to create an orphan branch, and found that it still has a parent. As I see from the Javadocs and sources, the startCommit has to be defined (or current HEAD is used). This is really different from `git checkout --orphan` which creates branches detached from current history. I tried to set empty start point (.setStartPoint(walk.parseCommit(ObjectId.zeroId()))) but It fails, because zero commit is not resolved.
Could anybody please clarify if it is possible to achieve the same behavior with git checkout --detach using JGit API?
-- Regards, Mike _______________________________________________ jgit-dev mailing list jgit-dev@xxxxxxxxxxx To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jgit-dev
|