From 25b015579be1247537787dbf0cd2bbbfc3cbc64e Mon Sep 17 00:00:00 2001 From: Filippo Ferrario <102259289+ferrariofilippo@users.noreply.github.com> Date: Fri, 30 Jun 2023 23:36:20 +0200 Subject: [PATCH] Feature: Added support for initializing folders as Git repos (#12803) --- src/Files.App/Actions/Git/GitFetchAction.cs | 5 +- src/Files.App/Actions/Git/GitInitAction.cs | 46 +++++++++++++++++++ src/Files.App/Actions/Git/GitPullAction.cs | 5 +- src/Files.App/Actions/Git/GitPushAction.cs | 5 +- src/Files.App/Actions/Git/GitSyncAction.cs | 5 +- src/Files.App/Commands/CommandCodes.cs | 1 + .../Commands/Manager/CommandManager.cs | 2 + .../Commands/Manager/ICommandManager.cs | 1 + src/Files.App/Helpers/GitHelpers.cs | 8 ++++ src/Files.App/Strings/en-US/Resources.resw | 6 +++ 10 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/Files.App/Actions/Git/GitInitAction.cs diff --git a/src/Files.App/Actions/Git/GitFetchAction.cs b/src/Files.App/Actions/Git/GitFetchAction.cs index f15813c41e2c..c6d096570a3b 100644 --- a/src/Files.App/Actions/Git/GitFetchAction.cs +++ b/src/Files.App/Actions/Git/GitFetchAction.cs @@ -1,4 +1,7 @@ -using Files.App.Contexts; +// Copyright (c) 2023 Files Community +// Licensed under the MIT License. See the LICENSE. + +using Files.App.Contexts; namespace Files.App.Actions { diff --git a/src/Files.App/Actions/Git/GitInitAction.cs b/src/Files.App/Actions/Git/GitInitAction.cs new file mode 100644 index 000000000000..d4d2c84c9b51 --- /dev/null +++ b/src/Files.App/Actions/Git/GitInitAction.cs @@ -0,0 +1,46 @@ +// Copyright (c) 2023 Files Community +// Licensed under the MIT License. See the LICENSE. + +using Files.App.Contexts; + +namespace Files.App.Actions +{ + sealed class GitInitAction : ObservableObject, IAction + { + private readonly IContentPageContext _context; + + public string Label + => "InitRepo".GetLocalizedResource(); + + public string Description + => "InitRepoDescription".GetLocalizedResource(); + + public bool IsExecutable => + _context.Folder is not null && + !_context.IsGitRepository; + + public GitInitAction() + { + _context = Ioc.Default.GetRequiredService(); + + _context.PropertyChanged += Context_PropertyChanged; + } + + public Task ExecuteAsync() + { + GitHelpers.InitializeRepository(_context.Folder?.ItemPath); + return Task.CompletedTask; + } + + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(IContentPageContext.Folder): + case nameof(IContentPageContext.IsGitRepository): + OnPropertyChanged(nameof(IsExecutable)); + break; + } + } + } +} diff --git a/src/Files.App/Actions/Git/GitPullAction.cs b/src/Files.App/Actions/Git/GitPullAction.cs index 36329d1a6253..6dc38d656e21 100644 --- a/src/Files.App/Actions/Git/GitPullAction.cs +++ b/src/Files.App/Actions/Git/GitPullAction.cs @@ -1,4 +1,7 @@ -using Files.App.Commands; +// Copyright (c) 2023 Files Community +// Licensed under the MIT License. See the LICENSE. + +using Files.App.Commands; using Files.App.Contexts; namespace Files.App.Actions diff --git a/src/Files.App/Actions/Git/GitPushAction.cs b/src/Files.App/Actions/Git/GitPushAction.cs index 622d1b3002ed..5c681d5031b2 100644 --- a/src/Files.App/Actions/Git/GitPushAction.cs +++ b/src/Files.App/Actions/Git/GitPushAction.cs @@ -1,4 +1,7 @@ -using Files.App.Commands; +// Copyright (c) 2023 Files Community +// Licensed under the MIT License. See the LICENSE. + +using Files.App.Commands; using Files.App.Contexts; namespace Files.App.Actions diff --git a/src/Files.App/Actions/Git/GitSyncAction.cs b/src/Files.App/Actions/Git/GitSyncAction.cs index 1da853fd31e7..7321fc156923 100644 --- a/src/Files.App/Actions/Git/GitSyncAction.cs +++ b/src/Files.App/Actions/Git/GitSyncAction.cs @@ -1,4 +1,7 @@ -using Files.App.Commands; +// Copyright (c) 2023 Files Community +// Licensed under the MIT License. See the LICENSE. + +using Files.App.Commands; using Files.App.Contexts; namespace Files.App.Actions diff --git a/src/Files.App/Commands/CommandCodes.cs b/src/Files.App/Commands/CommandCodes.cs index a0f5b65256a2..4683a7a0ac0c 100644 --- a/src/Files.App/Commands/CommandCodes.cs +++ b/src/Files.App/Commands/CommandCodes.cs @@ -182,6 +182,7 @@ public enum CommandCodes // Git GitFetch, + GitInit, GitPull, GitPush, GitSync, diff --git a/src/Files.App/Commands/Manager/CommandManager.cs b/src/Files.App/Commands/Manager/CommandManager.cs index 09a1fc3a44d9..f30c7ca186be 100644 --- a/src/Files.App/Commands/Manager/CommandManager.cs +++ b/src/Files.App/Commands/Manager/CommandManager.cs @@ -158,6 +158,7 @@ public IRichCommand this[HotKey hotKey] public IRichCommand OpenFileLocation => commands[CommandCodes.OpenFileLocation]; public IRichCommand PlayAll => commands[CommandCodes.PlayAll]; public IRichCommand GitFetch => commands[CommandCodes.GitFetch]; + public IRichCommand GitInit => commands[CommandCodes.GitInit]; public IRichCommand GitPull => commands[CommandCodes.GitPull]; public IRichCommand GitPush => commands[CommandCodes.GitPush]; public IRichCommand GitSync => commands[CommandCodes.GitSync]; @@ -313,6 +314,7 @@ public CommandManager() [CommandCodes.OpenFileLocation] = new OpenFileLocationAction(), [CommandCodes.PlayAll] = new PlayAllAction(), [CommandCodes.GitFetch] = new GitFetchAction(), + [CommandCodes.GitInit] = new GitInitAction(), [CommandCodes.GitPull] = new GitPullAction(), [CommandCodes.GitPush] = new GitPushAction(), [CommandCodes.GitSync] = new GitSyncAction(), diff --git a/src/Files.App/Commands/Manager/ICommandManager.cs b/src/Files.App/Commands/Manager/ICommandManager.cs index d603edde90ce..8e1246588577 100644 --- a/src/Files.App/Commands/Manager/ICommandManager.cs +++ b/src/Files.App/Commands/Manager/ICommandManager.cs @@ -161,6 +161,7 @@ public interface ICommandManager : IEnumerable IRichCommand PlayAll { get; } IRichCommand GitFetch { get; } + IRichCommand GitInit { get; } IRichCommand GitPull { get; } IRichCommand GitPush { get; } IRichCommand GitSync { get; } diff --git a/src/Files.App/Helpers/GitHelpers.cs b/src/Files.App/Helpers/GitHelpers.cs index 7a969bbc8032..58817a1788a0 100644 --- a/src/Files.App/Helpers/GitHelpers.cs +++ b/src/Files.App/Helpers/GitHelpers.cs @@ -532,6 +532,14 @@ public static GitItemModel GetGitInformationForItem(Repository repository, strin return gitItemModel; } + public static void InitializeRepository(string? path) + { + if (string.IsNullOrWhiteSpace(path)) + return; + + Repository.Init(path); + } + private static Commit? GetLastCommitForFile(Repository repository, string currentPath) { foreach (var currentCommit in repository.Commits) diff --git a/src/Files.App/Strings/en-US/Resources.resw b/src/Files.App/Strings/en-US/Resources.resw index 5f0be4c613c7..2ed5513b4ee8 100644 --- a/src/Files.App/Strings/en-US/Resources.resw +++ b/src/Files.App/Strings/en-US/Resources.resw @@ -3371,4 +3371,10 @@ Great! You are now authorized. + + Initialize repo + + + Initialize a Git repository + \ No newline at end of file