How to use libgit2sharp to commit to Git repositories in C# ?
💻 coding

How to use libgit2sharp to commit to Git repositories in C# ?

2 min read 369 words
2 min read
ShareWhatsAppPost on X
  • 1LibGit2Sharp is a NuGet package that simplifies committing changes to Git repositories in C# applications.
  • 2The GitRepositoryManager class manages Git operations, including committing changes and pushing to remote repositories.
  • 3To use the GitRepositoryManager, provide Git credentials, repository URL, and local folder path during initialization.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"LibGit2Sharp is a NuGet package that simplifies committing changes to Git repositories in C# applications."

How to use libgit2sharp to commit to Git repositories in C# ?

I helped design an interesting deployment strategy for an Azure application. For one of our projects, we have an existing upgrade solution that will distribute new versions of our ASP.NET application to our clients. One of our clients runs in the app in Azure. To support this automated distribution, we use Azure’s Git Repository Deploy feature to push up the new code to a staging environment. We can then manually swap the staging environment with our production environment after a smoke test, completing the deployment.

One step to this process is committing the web app to the Azure Git repository. To do this, I took advantage of the Nuget package LibGit2Sharp.

Install-Package LibGit2Sharp -Version 0.21.0.176

I abstracted some of the code used to do this Git repository push into the following GitRepositoryManager class.

using System;
using System.IO;
using System.Linq;
using LibGit2Sharp;

namespace GitDeploy
{
 public class GitRepositoryManager
 {
 private readonly string _repoSource;
 private readonly UsernamePasswordCredentials _credentials;
 private readonly DirectoryInfo _localFolder;

 /// <summary>
 /// Initializes a new instance of the <see cref="GitRepositoryManager" /> class.
 /// </summary>
 /// <param name="username">The Git credentials username.</param>
 /// <param name="password">The Git credentials password.</param>
 /// <param name="gitRepoUrl">The Git repo URL.</param>
 /// <param name="localFolder">The full path to local folder.</param>
 public GitRepositoryManager(string username, string password, string gitRepoUrl, string localFolder)
 {
 var folder = new DirectoryInfo(localFolder);

 if (!folder.Exists)
 {
 throw new Exception(string.Format("Source folder '{0}' does not exist.", _localFolder));
 }

 _localFolder = folder;

 _credentials = new UsernamePasswordCredentials
 {
 Username = username,
 Password = password
 };

 _repoSource = gitRepoUrl;
 }

 /// <summary>
 /// Commits all changes.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <exception cref="System.Exception"></exception>
 public void CommitAllChanges(string message)
 {
 using (var repo = new Repository(_localFolder.FullName))
 {
 var files = _localFolder.GetFiles("*", SearchOption.AllDirectories).Select(f => f.FullName);
 repo.Stage(files);

 repo.Commit(message);
 }
 }

 /// <summary>
 /// Pushes all commits.
 /// </summary>
 /// <param name="remoteName">Name of the remote server.</param>
 /// <param name="branchName">Name of the remote branch.</param>
 /// <exception cref="System.Exception"></exception>
 public void PushCommits(string remoteName, string branchName)
 {
 using (var repo = new Repository(_localFolder.FullName))
 {
 var remote = repo.Network.Remotes.FirstOrDefault(r => r.Name == remoteName);
 if (remote == null)
 {
 repo.Network.Remotes.Add(remoteName, _repoSource);
 remote = repo.Network.Remotes.FirstOrDefault(r => r.Name == remoteName);
 }

 var options = new PushOptions
 {
 CredentialsProvider = (url, usernameFromUrl, types) => _credentials
 };

 repo.Network.Push(remote, branchName, options);
 }
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 23 March 2019 · 2 min read · 369 words

Part of AskGif Blog · coding

You might also like