2016-11-10 202 views
4

如何确定Git存储库中最新的提交分支? 我想克隆只是最近更新的分支,而不是克隆所有分支,尽管它是否合并到主(默认分支)或不。在JGit中获取最新提交的分支(名称)详细信息

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(); 

任何人都可以帮助我吗?

回答

2

恐怕你将不得不克隆整个存储库及其所有分支,以找出最新的分支。

LsRemoteCommand列出了分支名称和它们指向的提交的id,但没有提交的时间戳。

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()); 
    } 
} 

现在你知道最新的分支,你可以检出这个分支。

+0

感谢您的回复!!!!!如果我得到任何路障,会发布...... – AshokDev