Question

I am looking into a CI Build Automation Task and I want to name my builds using the Git Commit Id. I am planning to write a C# program to do this. What Libraries I can use to call Git repositories from C#? I am able to call into local repository clone and retrieve this information using git.exe(Windows) or libgit2sharp but I don't know how to do it on a remote source

Was it helpful?

Solution 2

From a CI perspective, you may be willing to build a specific branch.

The following piece of code demonstrates this.

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);
}

Note: url can either point to:

  • a remote http url (http://www.example.com/repo.git)
  • a location on the CI server (file:///C:/My%20Documents/repo.git)
  • a location on the network (file://server/repos/repo.git)

OTHER TIPS

I have been using LibGit2Sharp for quite some time, and it is good.

Below is a sample, which will iterate through the commits in your url.

Note: I had to do a clone, not sure if there is a better way:

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top