2017-07-19 65 views
0

我有一個控制檯應用程序使用來自某個git存儲庫的內容。它具有以下功能:如何使用C#和Git簽出標籤?

  • 拉從Git的回購
  • 會把它以一定的方式
  • 移動有組織的內容到不同的位置

問題的內容是第二個項目。內容應根據給定的Git標籤進行組織和處理。目前,C#控制檯應用程序可以克隆回購,但現在我需要它逐一檢出回購中的所有標籤。檢查每個標籤後,控制檯應用程序應該處理這些文件,然後轉到下一個標籤。

我的C#控制檯應用程序如何檢出標記?我想只使用本地Git命令,如git checkout tags/<tagname>

+0

我會發現使用Libgit2sharp比較容易:https://github.com/libgit2/libgit2sharp – Philippe

回答

1

要做到這一點其實很簡單。我創建了一個通用的Git命令方法,使用一個新的進程,然後從StandardOutput中讀取。然後我將所有的StandardOutput作爲逗號分隔的字符串返回,以後可以迭代。

public string RunGitCommand(string command, string args, string workingDirectory) 
{ 
    string git = "git"; 
    var results = ""; 
    var proc = new Process 
    { 
     StartInfo = new ProcessStartInfo 
     { 
      FileName = git, 
      Arguments = $"{command} {args}", 
      UseShellExecute = false, 
      RedirectStandardOutput = true, 
      CreateNoWindow = true, 
      WorkingDirectory = workingDirectory, 
     } 
    }; 
     proc.Start(); 
     while (!proc.StandardOutput.EndOfStream) 
     { 
      results += $"{proc.StandardOutput.ReadLine()},"; 
     } 
     proc.WaitForExit(); 
     return results;  
} 

這讓我然後調用標籤,像這樣

var tags = RunGitCommand("tag", "", $"{location}"); // get all tags 

最後,然後我可以遍歷所有的標籤和檢查出來與RunGitCommand方法我上面寫的。對於每個迭代標籤,我都可以做類似的事情,其中​​tag是我標籤列表中的單個元素。

git.RunGitCommand("checkout", $"tags/{tag}", $"{location}");