JGit orphan branch checkout [message #1842652] |
Sat, 26 June 2021 19:23  |
Eclipse User |
|
|
|
Hello,
I've tried to use CheckoutCommand to create orphan branch, and found that the still has a parent. As I see from the Javadocs and sources, the startCommit have to be defined (or current HEAD is used). This is really different from `git checkout --orphan` which creates branch 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 possible to achieve behavior same with git checkout --detach using JGit API?
|
|
|
Re: JGit orphan branch checkout [message #1871502 is a reply to message #1842652] |
Thu, 26 September 2024 12:04  |
Eclipse User |
|
|
|
I know it is very very old, but I was not able to find a solution for this, so I am just pasting what I found:
The checkout command is correct, but do NOT use setCreateBranch(true). Just use: git.checkout().setOrphan(true).setName("your_branch_name").call();
Here a small example creating an orphan branch with jgit:
package com.sap.ais.security.nwdicodeql.service;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
public class GitHubOrphanBranchCreator {
private static final String REMOTE_URL = "https://your_git_uri.git";
private static final String USERNAME = "your_login_user_id";
private static final String ACCESS_TOKEN = "access_token";
private static final String NEW_BRANCH_NAME = "my_orphan_branch";
public static void main(String[] args) {
Path tempDirectory = null;
try {
tempDirectory = Files.createTempDirectory("testing_orphan_branch");
// Clone the remote repository
Git git = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(tempDirectory.toFile())
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(USERNAME, ACCESS_TOKEN)).call();
// Create an orphan branch, but do NOT set the flag to create a new branch!!!!
git.checkout().setOrphan(true).setName(NEW_BRANCH_NAME).call();
// delete all files in the working directory
git.rm().addFilepattern(".").call();
// Create a new file in the orphan branch
Files.write(tempDirectory.resolve("config.yml"), "just some sample content".getBytes());
// Add the new file to the index
git.add().addFilepattern("config.yml").call();
// Make an initial commit
git.commit().setMessage("Initial commit with dummy file")
.setCommitter(new PersonIdent(USERNAME, "john@example.com")).call();
// Push the orphan branch to GitHub
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(USERNAME, ACCESS_TOKEN)).call();
// Clean up local repository
git.getRepository().close();
System.out.println("Orphan branch created and pushed successfully.");
} catch (GitAPIException | IOException e) {
e.printStackTrace();
} finally {
if (tempDirectory != null) {
deleteDirectory(tempDirectory.toFile());
}
}
}
// Helper method to delete a directory recursively
private static void deleteDirectory(File directory) {
if (directory.exists()) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
directory.delete();
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.02796 seconds