2016-11-10 203 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