You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
recyclarr/src/VersionControl/GitRepository.cs

51 lines
1.5 KiB

using Common.Extensions;
using LibGit2Sharp;
namespace VersionControl;
public sealed class GitRepository : IGitRepository
{
private readonly Lazy<Repository> _repo;
public GitRepository(string repoPath)
{
// Lazily construct the Repository object because it does too much work in its constructor
// We want to keep our own constructor here as thin as possible for DI and testability.
_repo = new Lazy<Repository>(() => new Repository(repoPath));
}
public void Dispose()
{
if (_repo.IsValueCreated)
{
_repo.Value.Dispose();
}
}
public void ForceCheckout(string branch)
{
Commands.Checkout(_repo.Value, branch, new CheckoutOptions
{
CheckoutModifiers = CheckoutModifiers.Force
});
}
public void Fetch(string remote = "origin")
{
var origin = _repo.Value.Network.Remotes[remote];
Commands.Fetch(_repo.Value, origin.Name, origin.FetchRefSpecs.Select(s => s.Specification), null, "");
}
public void ResetHard(string toBranchOrSha1)
{
var branch = _repo.Value.Branches.FirstOrDefault(b => b.FriendlyName.ContainsIgnoreCase(toBranchOrSha1));
var commit = branch is not null ? branch.Tip : _repo.Value.Lookup<Commit>(toBranchOrSha1);
_repo.Value.Reset(ResetMode.Hard, commit);
}
public void SetRemote(string name, string newUrl)
{
_repo.Value.Network.Remotes.Update(name, updater => updater.Url = newUrl);
}
}