Get the latest branch information (name) in JGit

How to determine the last linked branch in a Git repository? I want to clone only the recently updated branch instead of cloning all the branches, regardless of whether it is merged with the master (default branch) or not.

LsRemoteCommand remoteCommand = Git.lsRemoteRepository();
Collection <Ref> refs = remoteCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, password))
                    .setHeads(true)
                    .setRemote(uri)
                    .call();

for (Ref ref : refs) {
    System.out.println("Ref: " + ref.getName());
}


//cloning the repo
CloneCommand cloneCommand = Git.cloneRepository();
result = cloneCommand.setURI(uri.trim())
 .setDirectory(localPath).setBranchesToClone(branchList)
.setBranch("refs/heads/branchName")
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName,password)).call();

Can someone help me with this?

+4
source share
1 answer

I'm afraid you will have to clone the entire repository with all its branches in order to find out the newest branch.

The list LsRemoteCommandcontains the names of the branches and the identifier of the commits that they point to, but not the commit timestamp.

Git " " , , . : Git/JGit /API, , .

, ( ), , , .

, , , :

Git git = Git.cloneRepository().setURI( ... ).setNoCheckout( true ).setCloneAllBranches( true ).call();
List<Ref> branches = git.branchList().setListMode( ListMode.REMOTE ).call();
try( RevWalk walk = new RevWalk( git.getRepository() ) ) {
  for( Ref branch : branches ) {
    RevCommit commit = walk.parseCommit( branch.getObjectId() );
    System.out.println( "Time committed: " + commit.getCommitterIdent().getWhen() );
    System.out.println( "Time authored: " + commit.getAuthorIdent().getWhen() );
  }
}

, , .

+2

Source: https://habr.com/ru/post/1660386/


All Articles