C#C
C#2y ago
TED

Best C# Git library ?

I'm trying to make a generic git c# class lib with the base methods checkout and pull with the help of LibGit2Sharp. I've looked at alternatives, but they seem not to have been updated for a VERY long time.

For some reason I can't seem find out how the credentials section works. I basically want the Setcredentials to return the credentials for each method im making.

using System;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
using CL.GitHelper.Config.Configuration;
using CodeLogic.Libraries;

namespace CL.GitHelper
{
    public class GitManager
    {
        public enum GitResult
        {
            RESULT_SUCCESS,
            RESULT_FAIL_GENERAL,
            RESULT_FAIL_BRANCH_NOT_FOUND
        }

        public GitResult CheckoutBranch(string branchName, string id = "Default")
        {
            try
            {
                var repositoryConfig = LibraryConfiguration.GetConfigClass<Repositories.DefaultRepository>(id);

                string repositoryPath = repositoryConfig.Url;
                using (var repo = new Repository(repositoryPath))
                {
                    SetCredentials(repo, repositoryConfig);

                    var branch = repo.Branches[branchName];

                    if (branch == null)
                    {
                        return GitResult.RESULT_FAIL_BRANCH_NOT_FOUND;
                    }

                    // Checkout the specified branch
                    Commands.Checkout(repo, branch);
                    Console.WriteLine($"Checked out branch {branchName} successfully.");
                    return GitResult.RESULT_SUCCESS;
                }
            }
            catch (Exception)
            {
                return GitResult.RESULT_FAIL_GENERAL;
            }
        }

        public GitResult PullChanges(string id = "Default")
        {
            try
            {
                var repositoryConfig = LibraryConfiguration.GetConfigClass<Repositories.DefaultRepository>(id);

                string repositoryPath = repositoryConfig.Url;
                using (var repo = new Repository(repositoryPath))
                {
                    SetCredentials(repo, repositoryConfig);

                    var options = new PullOptions
                    {
                        FetchOptions = new FetchOptions(),
                        MergeOptions = new MergeOptions()
                    };

                    // Pull latest changes from remote
                    var signature = new Signature("Your Name", "your.email@example.com", DateTimeOffset.Now);
                    Commands.Pull(repo, signature, options);

                    Console.WriteLine("Pull operation completed successfully.");
                    return GitResult.RESULT_SUCCESS;
                }
            }
            catch (Exception)
            {
                return GitResult.RESULT_FAIL_GENERAL;
            }
        }

        private void SetCredentials(Repository repo, Repositories.DefaultRepository repositoryConfig)
        {
            if (!string.IsNullOrEmpty(repositoryConfig.Username) && !string.IsNullOrEmpty(repositoryConfig.Password))
            {
                var remoteUrl = repositoryConfig.Url;

                // Create a CredentialsProvider callback
                var credentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
                {
                    Username = repositoryConfig.Username,
                    Password = repositoryConfig.Password
                };

                var remote = repo.Network.Remotes["origin"];

                // Assign the CredentialsProvider callback
                remote.PushCredentialsProvider = credentialsProvider;
                remote.FetchCredentialsProvider = credentialsProvider;
            }
        }

    }
}
Was this page helpful?