要做到這一點其實很簡單。我創建了一個通用的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}");
我會發現使用Libgit2sharp比較容易:https://github.com/libgit2/libgit2sharp – Philippe