簡體   English   中英

如何使用LibGit2Sharp庫獲取特定選定分支的存儲庫名稱

[英]How to get the repository names for a particular selected branch using LibGit2Sharp library

我們正在使用LibGit2Sharp庫來處理Github中的提交。

問題 :我們需要通過LibGit2Sharp庫獲取Github中所選分支的所有存儲庫名稱。

哪個類將具有特定分支的存儲庫名稱集合。

我們搜索了下面的LibGit2Sharp文檔但我們沒有任何想法。

http://www.nudoq.org/#!/Projects/LibGit2Sharp

有人可以提出任何解決方案。

免責聲明:

在下面的答案中,我假設你的意思是:

我們需要通過LibGit2Sharp庫獲取Github中所選存儲庫的所有分支名稱

使用Repository類的Branches屬性

以下程序使用Repository.Branches屬性,然后迭代它:

class Program
{
    // Tested with LibGit2Sharp version 0.21.0.176
    static void Main(string[] args)
    {
        // Load the repository with the path (Replace E:\mono2 with a valid git path)
        Repository repository = new Repository(@"E:\mono2");
        foreach (var branch in repository.Branches)
        {
            // Display the branch name
            System.Console.WriteLine(branch.Name);
        }
        System.Console.ReadLine();
    }
}

程序的輸出將顯示如下內容:

origin/mono-4.0.0-branch 
origin/mono-4.0.0-branch-ServiceModelFix
upstream/mono-4.0.0-branch-c5sr2

如果您需要其他類似分支的RemoteUpstreamBranchCanonicalName ,則您具有相關屬性。

我將猜測並假設你的意思是:

嘗試列出給定存儲庫的所有文件和目錄。

這基本上意味着您要運行命令:git ls-files

我已經提到了以下鏈接: https//github.com/libgit2/libgit2sharp/wiki/Git-ls-files

以下代碼片段應該可以解決問題:

using System;
using LibGit2Sharp;

class Program
{
    public static void Main(string[] args)
    {
        using (var repo = new Repository(@"C:\Repo"))
        {
            foreach (IndexEntry e in repo.Index)
            {
                Console.WriteLine("{0} {1}", e.Path, e.StageLevel);
            }
        }

        Console.ReadLine();
    }
}

我還想補充一點,如果您知道git命令,那么您可以在github項目wiki上找到libgit2sharp的相應api:

希望這可以幫助。

最好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM