Skip to content

Commit 1b24eb0

Browse files
Feature: Git Integration - Phase 4 (#12439)
1 parent 4785b53 commit 1b24eb0

File tree

14 files changed

+298
-29
lines changed

14 files changed

+298
-29
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Files.App.Contexts;
2+
3+
namespace Files.App.Actions
4+
{
5+
internal class GitFetchAction : ObservableObject, IAction
6+
{
7+
private readonly IContentPageContext _context;
8+
9+
public string Label { get; } = "GitFetch".GetLocalizedResource();
10+
11+
public string Description { get; } = "GitFetchDescription".GetLocalizedResource();
12+
13+
public bool IsExecutable
14+
=> _context.CanExecuteGitAction;
15+
16+
public GitFetchAction()
17+
{
18+
_context = Ioc.Default.GetRequiredService<IContentPageContext>();
19+
20+
_context.PropertyChanged += Context_PropertyChanged;
21+
}
22+
23+
public Task ExecuteAsync()
24+
{
25+
GitHelpers.FetchOrigin(_context.ShellPage!.InstanceViewModel.GitRepositoryPath);
26+
27+
return Task.CompletedTask;
28+
}
29+
30+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
31+
{
32+
if (e.PropertyName is nameof(IContentPageContext.CanExecuteGitAction))
33+
OnPropertyChanged(nameof(IsExecutable));
34+
}
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Files.App.Commands;
2+
using Files.App.Contexts;
3+
4+
namespace Files.App.Actions
5+
{
6+
internal class GitPullAction : ObservableObject, IAction
7+
{
8+
private readonly IContentPageContext _context;
9+
10+
public string Label { get; } = "GitPull".GetLocalizedResource();
11+
12+
public string Description { get; } = "GitPullDescription".GetLocalizedResource();
13+
14+
public RichGlyph Glyph { get; } = new("\uE74B");
15+
16+
public bool IsExecutable
17+
=> _context.CanExecuteGitAction;
18+
19+
public GitPullAction()
20+
{
21+
_context = Ioc.Default.GetRequiredService<IContentPageContext>();
22+
23+
_context.PropertyChanged += Context_PropertyChanged;
24+
}
25+
26+
public Task ExecuteAsync()
27+
{
28+
GitHelpers.PullOrigin(_context.ShellPage!.InstanceViewModel.GitRepositoryPath);
29+
30+
return Task.CompletedTask;
31+
}
32+
33+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
34+
{
35+
if (e.PropertyName is nameof(IContentPageContext.CanExecuteGitAction))
36+
OnPropertyChanged(nameof(IsExecutable));
37+
}
38+
}
39+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,9 @@ public enum CommandCodes
176176

177177
// Play
178178
PlayAll,
179+
180+
// Git
181+
GitFetch,
182+
GitPull,
179183
}
180184
}

src/Files.App/Commands/Manager/CommandManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public IRichCommand this[HotKey hotKey]
154154
public IRichCommand ClosePane => commands[CommandCodes.ClosePane];
155155
public IRichCommand OpenFileLocation => commands[CommandCodes.OpenFileLocation];
156156
public IRichCommand PlayAll => commands[CommandCodes.PlayAll];
157+
public IRichCommand GitFetch => commands[CommandCodes.GitFetch];
158+
public IRichCommand GitPull => commands[CommandCodes.GitPull];
157159

158160
public CommandManager()
159161
{
@@ -302,6 +304,8 @@ public CommandManager()
302304
[CommandCodes.ClosePane] = new ClosePaneAction(),
303305
[CommandCodes.OpenFileLocation] = new OpenFileLocationAction(),
304306
[CommandCodes.PlayAll] = new PlayAllAction(),
307+
[CommandCodes.GitFetch] = new GitFetchAction(),
308+
[CommandCodes.GitPull] = new GitPullAction(),
305309
};
306310

307311
private void UpdateHotKeys()

src/Files.App/Commands/Manager/ICommandManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
156156
IRichCommand ClosePane { get; }
157157

158158
IRichCommand PlayAll { get; }
159+
160+
IRichCommand GitFetch { get; }
161+
IRichCommand GitPull { get; }
159162
}
160163
}

src/Files.App/Contexts/ContentPage/ContentPageContext.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using CommunityToolkit.Mvvm.ComponentModel;
5-
using CommunityToolkit.Mvvm.DependencyInjection;
6-
using Files.App.Filesystem;
74
using Files.App.UserControls.MultitaskingControl;
8-
using Files.App.ViewModels;
9-
using Files.App.Views.LayoutModes;
10-
using System;
11-
using System.Collections.Generic;
125
using System.Collections.Immutable;
13-
using System.ComponentModel;
14-
using System.Linq;
156

167
namespace Files.App.Contexts
178
{
@@ -58,13 +49,22 @@ internal class ContentPageContext : ObservableObject, IContentPageContext
5849

5950
public bool ShowSearchUnindexedItemsMessage => ShellPage is not null && ShellPage.InstanceViewModel.ShowSearchUnindexedItemsMessage;
6051

52+
public bool CanExecuteGitAction => ShellPage is not null && ShellPage.InstanceViewModel.IsGitRepository && !GitHelpers.IsExecutingGitAction;
53+
6154
public ContentPageContext()
6255
{
6356
context.Changing += Context_Changing;
6457
context.Changed += Context_Changed;
58+
GitHelpers.IsExecutingGitActionChanged += GitHelpers_IsExecutingGitActionChanged;
59+
6560
Update();
6661
}
6762

63+
private void GitHelpers_IsExecutingGitActionChanged(object? sender, PropertyChangedEventArgs e)
64+
{
65+
OnPropertyChanged(nameof(CanExecuteGitAction));
66+
}
67+
6868
private void Context_Changing(object? sender, EventArgs e)
6969
{
7070
if (ShellPage is IShellPage page)
@@ -149,6 +149,9 @@ private void InstanceViewModel_PropertyChanged(object? sender, PropertyChangedEv
149149
case nameof(CurrentInstanceViewModel.ShowSearchUnindexedItemsMessage):
150150
OnPropertyChanged(nameof(ShowSearchUnindexedItemsMessage));
151151
break;
152+
case nameof(CurrentInstanceViewModel.IsGitRepository):
153+
OnPropertyChanged(nameof(CanExecuteGitAction));
154+
break;
152155
}
153156
}
154157

@@ -191,6 +194,7 @@ private void Update()
191194
OnPropertyChanged(nameof(IsMultiPaneEnabled));
192195
OnPropertyChanged(nameof(IsMultiPaneActive));
193196
OnPropertyChanged(nameof(ShowSearchUnindexedItemsMessage));
197+
OnPropertyChanged(nameof(CanExecuteGitAction));
194198
}
195199

196200
private void UpdatePageType()

src/Files.App/Contexts/ContentPage/IContentPageContext.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using Files.App.Filesystem;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.ComponentModel;
8-
94
namespace Files.App.Contexts
105
{
116
public interface IContentPageContext : INotifyPropertyChanged
@@ -36,5 +31,7 @@ public interface IContentPageContext : INotifyPropertyChanged
3631
bool IsMultiPaneActive { get; }
3732

3833
bool ShowSearchUnindexedItemsMessage { get; }
34+
35+
bool CanExecuteGitAction { get; }
3936
}
4037
}

src/Files.App/Data/Items/BranchItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
namespace Files.App.Data.Items
55
{
6-
public record BranchItem(string Name, bool IsRemote);
6+
public record BranchItem(string Name, bool IsRemote, int? AheadBy, int? BehindBy);
77
}

src/Files.App/Data/Models/DirectoryPropertiesViewModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public bool ShowLocals
6161
}
6262
}
6363

64+
private string _PullInfo = "0";
65+
public string PullInfo
66+
{
67+
get => _PullInfo;
68+
set => SetProperty(ref _PullInfo, value);
69+
}
70+
6471
public ObservableCollection<string> BranchesNames => _ShowLocals
6572
? _localBranches
6673
: _remoteBranches;
@@ -84,6 +91,10 @@ public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchIt
8491
_gitRepositoryPath = repositoryPath;
8592
ShowLocals = true;
8693

94+
PullInfo = branches.Any()
95+
? branches[0].BehindBy.ToString() ?? "0"
96+
: "0";
97+
8798
if (isGitRepository)
8899
{
89100
_localBranches.Clear();

0 commit comments

Comments
 (0)