Reputation: 1681
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
Upvotes: 2
Views: 9623
Reputation: 2597
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);
}
}
Upvotes: 2
Reputation: 67589
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:
http://www.example.com/repo.git
)file:///C:/My%20Documents/repo.git
)file://server/repos/repo.git
)Upvotes: 2