受欢迎的博客标签

How to Git and GitHub Integration in C# with LibGit2Sharp

Published

let's explore how we can leverage C# to interact with Git and GitHub repositories programmatically. We'll achieve this integration using the LibGit2Sharp NuGet package, a powerful tool that provides a C# API for executing Git commands seamlessly.

 

https://github.com/libgit2/libgit2sharp

 

Commit files in Git

public void CommitFiles(string localRepoPath, string userName, string userEmail, string userComments)
{
    try
    {
        using (var repository = new Repository(localRepoPath))
        {
            Signature author = new Signature(userName, userEmail, DateTime.Now);
            Signature committer = author;

            // Commit to the repository
            Commit commit = repository.Commit(userComments, author, committer);
        }
    }
    catch (LibGit2SharpException)
    {
        throw;
    }
}

 

Push files in git

public void CommitFiles(string localRepoPath, string repoUrl, string branchName, string userName, string password)
{
    try
    {
        using (var repo = new Repository(localRepoPath))
        {
            var remote = repo.Network.Remotes["origin"];
            if (remote != null)
            {
                repo.Network.Remotes.Remove("origin");
            }
            repo.Network.Remotes.Add("origin", repoUrl);
            remote = repo.Network.Remotes["origin"];
            if (remote == null)
            {
                return;
            }
            FetchOptions fetop = new FetchOptions
            {
                CredentialsProvider = (url, usernameFromUrl, types) =>
                    new UsernamePasswordCredentials
                    {
                        Username = userName,
                        Password = password
                    }
            };
            var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
            Commands.Fetch(repo, remote.Name, refSpecs, fetop, string.Empty);
            var localBranchName = string.IsNullOrEmpty(branchName) ? "master" : branchName;
            // Get the branch you want to push
            var localBranch = repo.Branches[localBranchName];
            if (localBranch == null)
            {
                return;
            }
            repo.Branches.Update(localBranch,
                b => b.Remote = remote.Name,
                b => b.UpstreamBranch = localBranch.CanonicalName);
            // Create a new push options object
            var pushOptions = new PushOptions
            {
                CredentialsProvider = (url, usernameFromUrl, types) =>
                    new UsernamePasswordCredentials
                    {
                        Username = userName,
                        Password = password
                    }
            };
            // Push the branch to the remote repository
            repo.Network.Push(localBranch, pushOptions);
        }
    }
    catch (LibGit2SharpException)
    {
        throw;
    }
}

 

public void StageFiles(string localRepoPath)
{
    try
    {
        var files = from file in Directory.EnumerateFiles(localRepoPath) select file;

        using (var repository = new Repository(localRepoPath))
        {
            foreach (var file in files)
            {
                var fileName = Path.GetFileName(file);

                // Stage the file
                repository.Index.Add(fileName);
                repository.Index.Write();
            }
        }
    }
    catch (LibGit2SharpException)
    {
        throw;
    }
}

https://www.c-sharpcorner.com/article/git-and-github-integration-in-c-sharp-apps-with-libgit2sharp/