Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
using LibGit2Sharp;
using System.ComponentModel;

namespace Files.App.Utils.Git
namespace Files.App.Data.Contracts
{
/// <summary>
/// Defines a version control abstraction.
/// </summary>
/// <remarks>
/// This interface is intended to decouple the app from a specific backend implementation (e.g. a library such as LibGit2Sharp, or a command-line implementation backed by the <c>git.exe</c> executable).
/// </remarks>
internal interface IVersionControl
public interface IVersionControlService
{
/// <summary>
/// Attempts to locate the root of a version control repository (by walking up the directory hierarchy).
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Data/Models/GitItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Files.App.Data.Models
/// <summary>
/// Represents a model for Git items
/// </summary>
internal sealed class GitItemModel
public sealed class GitItemModel
{
/// <summary>
/// Gets or initializes file change kind
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Files.App.Helpers.Application;
using Files.App.Services.Git;
using Files.App.Services.SizeProvider;
using Files.App.Utils.Logger;
using Files.App.ViewModels.Settings;
Expand Down Expand Up @@ -265,6 +266,7 @@ public static IHost ConfigureHost()
.AddSingleton<IStorageArchiveService, StorageArchiveService>()
.AddSingleton<IStorageSecurityService, StorageSecurityService>()
.AddSingleton<IWindowsCompatibilityService, WindowsCompatibilityService>()
.AddSingleton</*IVersionControlService,*/ LibGit2Service>()
// ViewModels
.AddSingleton<MainPageViewModel>()
.AddSingleton<InfoPaneViewModel>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Text;
using System.Text.RegularExpressions;

namespace Files.App.Utils.Git;
namespace Files.App.Services.Git;

internal sealed partial class LibGit2 // : IVersionControl
internal sealed partial class LibGit2Service // : IVersionControl
{
private const string GIT_RESOURCE_NAME = "Files:https://github.com";
private const string GIT_RESOURCE_USERNAME = "Personal Access Token";
Expand All @@ -30,7 +30,7 @@ internal sealed partial class LibGit2 // : IVersionControl
public bool IsExecutingGitAction
{
get => _isExecutingGitAction;
private set
internal set // TODO: Make set method private again when move finished
{
if (_isExecutingGitAction != value)
{
Expand Down Expand Up @@ -159,6 +159,72 @@ public async Task<BranchItem[]> GetBranchNames(string? path)
return returnValue;
}

public async void FetchOrigin(string? repositoryPath, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryPath))
return;

using var repository = new Repository(repositoryPath);
var signature = repository.Config.BuildSignature(DateTimeOffset.Now);

var token = CredentialsHelpers.GetPassword(GIT_RESOURCE_NAME, GIT_RESOURCE_USERNAME);
if (signature is not null && !string.IsNullOrWhiteSpace(token))
{
_fetchOptions.CredentialsProvider = (url, user, cred)
=> new UsernamePasswordCredentials
{
Username = signature.Name,
Password = token
};
}

MainWindow.Instance.DispatcherQueue.TryEnqueue(() =>
{
IsExecutingGitAction = true;
});

await DoGitOperationAsync<GitOperationResult>(() =>
{
cancellationToken.ThrowIfCancellationRequested();

var result = GitOperationResult.Success;
try
{
foreach (var remote in repository.Network.Remotes)
{
cancellationToken.ThrowIfCancellationRequested();

LibGit2Sharp.Commands.Fetch(
repository,
remote.Name,
remote.FetchRefSpecs.Select(rs => rs.Specification),
_fetchOptions,
"git fetch updated a ref");
}

cancellationToken.ThrowIfCancellationRequested();
}
catch (Exception ex)
{
result = IsAuthorizationException(ex)
? GitOperationResult.AuthorizationError
: GitOperationResult.GenericError;
}

return result;
});

MainWindow.Instance.DispatcherQueue.TryEnqueue(() =>
{
if (cancellationToken.IsCancellationRequested)
// Do nothing because the operation was cancelled and another fetch may be in progress
return;

IsExecutingGitAction = false;
GitFetchCompleted?.Invoke(null, EventArgs.Empty);
});
}

private static bool IsRepoValid(string path)
{
return SafetyExtensions.IgnoreExceptions(() => Repository.IsValid(path));
Expand Down
Loading
Loading