Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#应用程序检索git提交Id?_C#_Git_Libgit2sharp - Fatal编程技术网

如何从C#应用程序检索git提交Id?

如何从C#应用程序检索git提交Id?,c#,git,libgit2sharp,C#,Git,Libgit2sharp,我正在研究一个CI构建自动化任务,我想使用Git提交Id命名我的构建。 我计划写一个C#程序来实现这一点。我可以使用哪些库从C#调用Git存储库?我能够调用本地存储库克隆,并使用git.exe(Windows)或libgit2sharp检索此信息,但我不知道如何在远程源上执行此操作,我已经使用了相当长的一段时间,这很好 下面是一个示例,它将在url中的commits中进行迭代 注意:我必须进行克隆,不确定是否有更好的方法: string url = "http://github.com/libg

我正在研究一个CI构建自动化任务,我想使用Git提交Id命名我的构建。 我计划写一个C#程序来实现这一点。我可以使用哪些库从C#调用Git存储库?我能够调用本地存储库克隆,并使用git.exe(Windows)或libgit2sharp检索此信息,但我不知道如何在远程源上执行此操作,我已经使用了相当长的一段时间,这很好

下面是一个示例,它将在
url
中的
commits
中进行迭代

注意:我必须进行
克隆
,不确定是否有更好的方法:

string url = "http://github.com/libgit2/TestGitRepository";

using (Repository repo = Repository.Clone(url, @"C:\Users\Documents\test"))
{
    foreach (var commit in repo.Commits)
    {
        var commitId = commit.Id;
        var commitRawId = commitId.RawId;
        var commitSha = commitId.Sha; //0ab936416fa3bec6f1bf3d25001d18a00ee694b8
        var commitAuthorName = commit.Author.Name;

        commits.Add(commit);
    }
}

从CI的角度来看,您可能愿意建立一个特定的分支

下面的代码演示了这一点

using (Repository repo = Repository.Clone(url, localPath))
{
    // Retrieve the branch to build
    var branchToBuild = repo.Branches["vNext"];

    // Updates the content of the working directory with the content of the branch
    branchToBuild.Checkout();

    // Perform your build magic here ;-)
    Build();

    // Retrieve the commit sha of the branch that has just been built
    string sha = branchToBuild.Tip.Sha;

    // Package your build artifacts using the sha to name the package
    Package(sha);
}
注意:
url
可以指向:

  • 远程http url(
    http://www.example.com/repo.git
  • CI服务器上的位置(
    file:///C:/My%20Documents/repo.git
  • 网络上的位置(
    file://server/repos/repo.git