c# - 如何从 C# 应用程序检索 git Commit Id?

标签 c# git libgit2sharp

我正在研究 CI 构建自动化任务,并且想使用 Git 提交 ID 来命名我的构建。 我打算编写一个 C# 程序来执行此操作。我可以使用哪些库从 C# 调用 Git 存储库?我可以调用本地存储库克隆并使用 git.exe(Windows) 或 libgit2sharp 检索此信息,但我不知道如何在远程源上执行此操作

最佳答案

从 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)

关于c# - 如何从 C# 应用程序检索 git Commit Id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16972964/

相关文章:

c# - 混淆器的 "Prevent Microsoft IL Disassembler from opening my assembly"选项

git 格式补丁 : Repository name in prefix or generated prefix

git - 升级 Symfony2 标准版时解决 Composer merge 冲突

c# - 如何使用 libgit2 连接到 GitHub 存储库?

c# - 在 LibGit2Sharp 中找出提交所属的分支?

javascript - 我如何开始使用 C# 和 MVC 创建这个 javascript 结构?

c# - 如何获取对象的类型名称值作为字符串?

Git blame : View all Lines Authored by Specific User

libgit2sharp - libgit2sharp 可以依赖已安装的 git 全局配置提供程序吗?

c# - 我可以获得指向 Span 的指针吗?