Skip to content

Commit

Permalink
Feature: Added support for pushing Git commits (#12633)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrariofilippo authored Jun 28, 2023
1 parent fd002b5 commit 7b463ec
Show file tree
Hide file tree
Showing 20 changed files with 727 additions and 72 deletions.
11 changes: 11 additions & 0 deletions builds/azure-pipelines-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ jobs:
}
failOnStderr: true

# Injects the GitHub token to the project
- task: PowerShell@2
displayName: 'Inject AppCenter token'
inputs:
targetType: 'inline'
script: |
gci $(Build.SourcesDirectory)\src -Include *.cs -recurse | ForEach -Process {
(Get-Content $_ -Raw | ForEach -Process {$_ -replace "githubclientid.secret", "$(githubclientid.secret)"}) | Set-Content $_ -NoNewline
}
failOnStderr: true

- task: UseDotNet@2
inputs:
packageType: sdk
Expand Down
22 changes: 22 additions & 0 deletions builds/azure-pipelines-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ jobs:
}
failOnStderr: true

# Injects the GitHub token to the project
- task: PowerShell@2
displayName: 'Inject AppCenter token'
inputs:
targetType: 'inline'
script: |
gci $(Build.SourcesDirectory)\src -Include *.cs -recurse | ForEach -Process {
(Get-Content $_ -Raw | ForEach -Process {$_ -replace "githubclientid.secret", "$(githubclientid.secret)"}) | Set-Content $_ -NoNewline
}
failOnStderr: true

- task: UseDotNet@2
inputs:
packageType: sdk
Expand Down Expand Up @@ -215,6 +226,17 @@ jobs:
}
failOnStderr: true

# Injects the GitHub token to the project
- task: PowerShell@2
displayName: 'Inject AppCenter token'
inputs:
targetType: 'inline'
script: |
gci $(Build.SourcesDirectory)\src -Include *.cs -recurse | ForEach -Process {
(Get-Content $_ -Raw | ForEach -Process {$_ -replace "githubclientid.secret", "$(githubclientid.secret)"}) | Set-Content $_ -NoNewline
}
failOnStderr: true

- task: UseDotNet@2
inputs:
packageType: sdk
Expand Down
4 changes: 1 addition & 3 deletions src/Files.App/Actions/Git/GitPullAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public GitPullAction()

public Task ExecuteAsync()
{
GitHelpers.PullOrigin(_context.ShellPage!.InstanceViewModel.GitRepositoryPath);

return Task.CompletedTask;
return GitHelpers.PullOrigin(_context.ShellPage!.InstanceViewModel.GitRepositoryPath);
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
39 changes: 39 additions & 0 deletions src/Files.App/Actions/Git/GitPushAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Files.App.Commands;
using Files.App.Contexts;

namespace Files.App.Actions
{
internal class GitPushAction : ObservableObject, IAction
{
private readonly IContentPageContext _context;

public string Label { get; } = "Push".GetLocalizedResource();

public string Description { get; } = "GitPushDescription".GetLocalizedResource();

public RichGlyph Glyph { get; } = new("\uE74A");

public bool IsExecutable =>
_context.CanExecuteGitAction;

public GitPushAction()
{
_context = Ioc.Default.GetRequiredService<IContentPageContext>();

_context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
return GitHelpers.PushToOrigin(
_context.ShellPage?.InstanceViewModel.GitRepositoryPath,
_context.ShellPage?.InstanceViewModel.GitBranchName);
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.CanExecuteGitAction))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
42 changes: 42 additions & 0 deletions src/Files.App/Actions/Git/GitSyncAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Files.App.Commands;
using Files.App.Contexts;

namespace Files.App.Actions
{
internal class GitSyncAction : ObservableObject, IAction
{
private readonly IContentPageContext _context;

public string Label { get; } = "GitSync".GetLocalizedResource();

public string Description { get; } = "GitSyncDescription".GetLocalizedResource();

public RichGlyph Glyph { get; } = new("\uEDAB");

public bool IsExecutable =>
_context.CanExecuteGitAction;

public GitSyncAction()
{
_context = Ioc.Default.GetRequiredService<IContentPageContext>();

_context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
var instance = _context.ShellPage?.InstanceViewModel;

return GitHelpers.PullOrigin(instance?.GitRepositoryPath)
.ContinueWith(t => GitHelpers.PushToOrigin(
instance?.GitRepositoryPath,
instance?.GitBranchName));
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.CanExecuteGitAction))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
2 changes: 2 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,7 @@ public enum CommandCodes
// Git
GitFetch,
GitPull,
GitPush,
GitSync,
}
}
4 changes: 4 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public IRichCommand this[HotKey hotKey]
public IRichCommand PlayAll => commands[CommandCodes.PlayAll];
public IRichCommand GitFetch => commands[CommandCodes.GitFetch];
public IRichCommand GitPull => commands[CommandCodes.GitPull];
public IRichCommand GitPush => commands[CommandCodes.GitPush];
public IRichCommand GitSync => commands[CommandCodes.GitSync];

public CommandManager()
{
Expand Down Expand Up @@ -312,6 +314,8 @@ public CommandManager()
[CommandCodes.PlayAll] = new PlayAllAction(),
[CommandCodes.GitFetch] = new GitFetchAction(),
[CommandCodes.GitPull] = new GitPullAction(),
[CommandCodes.GitPush] = new GitPushAction(),
[CommandCodes.GitSync] = new GitSyncAction(),
};

private void UpdateHotKeys()
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>

IRichCommand GitFetch { get; }
IRichCommand GitPull { get; }
IRichCommand GitPush { get; }
IRichCommand GitSync { get; }
}
}
23 changes: 16 additions & 7 deletions src/Files.App/Data/Models/DirectoryPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,18 @@ public bool ShowLocals
}
}

private string _PullInfo = "0";
public string PullInfo
private string _StatusInfo = "0 / 0";
public string StatusInfo
{
get => _PullInfo;
set => SetProperty(ref _PullInfo, value);
get => _StatusInfo;
set => SetProperty(ref _StatusInfo, value);
}

private string _ExtendedStatusInfo = string.Format("CommitsNumber".GetLocalizedResource(), 0);
public string ExtendedStatusInfo
{
get => _ExtendedStatusInfo;
set => SetProperty(ref _ExtendedStatusInfo, value);
}

public ObservableCollection<string> BranchesNames => _ShowLocals
Expand All @@ -92,9 +99,11 @@ public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchIt
_gitRepositoryPath = repositoryPath;
ShowLocals = true;

PullInfo = branches.Any()
? branches[0].BehindBy.ToString() ?? "0"
: "0";
var behind = branches.Any() ? branches[0].BehindBy ?? 0 : 0;
var ahead = branches.Any() ? branches[0].AheadBy ?? 0 : 0;

ExtendedStatusInfo = string.Format("GitSyncStatusExtendedInfo".GetLocalizedResource(), ahead, behind);
StatusInfo = $"{ahead} / {behind}";

if (isGitRepository)
{
Expand Down
95 changes: 95 additions & 0 deletions src/Files.App/Dialogs/GitHubLoginDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!-- Copyright (c) 2023 Files Community. Licensed under the MIT License. See the LICENSE. -->
<ContentDialog
x:Class="Files.App.Dialogs.GitHubLoginDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:Files.App.Helpers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="{helpers:ResourceString Name=ConnectGitHub}"
CloseButtonCommand="{x:Bind ViewModel.CloseButtonCommand}"
CloseButtonStyle="{StaticResource AccentButtonStyle}"
CloseButtonText="{helpers:ResourceString Name=Close}"
CornerRadius="{StaticResource OverlayCornerRadius}"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
PrimaryButtonText="{helpers:ResourceString Name=OK}"
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
Style="{StaticResource DefaultContentDialogStyle}"
mc:Ignorable="d">

<ContentDialog.PrimaryButtonStyle>
<Style BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel
HorizontalAlignment="Center"
Orientation="Horizontal"
Spacing="8">
<FontIcon FontSize="14" Glyph="&#xE8C8;" />
<TextBlock Text="{helpers:ResourceString Name=CopyCode}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ContentDialog.PrimaryButtonStyle>

<ContentDialog.Resources>
<ResourceDictionary>
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
</ResourceDictionary>
</ContentDialog.Resources>

<Grid RowSpacing="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>

<StackPanel Width="360" Height="40">
<!-- Subtitle -->
<TextBlock
x:Name="Subtitle"
Grid.Row="0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="{x:Bind ViewModel.Subtitle, Mode=OneWay}"
TextWrapping="WrapWholeWords" />
</StackPanel>

<StackPanel
x:Name="UserCodeContainer"
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
x:Load="{x:Bind ViewModel.LoginConfirmed, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}">
<HyperlinkButton
x:Name="LoginUrl"
Content="{x:Bind ViewModel.LoginUrl}"
NavigateUri="{x:Bind ViewModel.NavigateUri}" />
<TextBlock
Margin="12,8"
CharacterSpacing="150"
FontFamily="Cascadia Mono,Consolas,Segoe UI Variable"
FontSize="32"
FontWeight="Normal"
Text="{x:Bind ViewModel.UserCode}" />
</StackPanel>
<Border
x:Name="LoginSuccessBadge"
Grid.Row="1"
Width="70"
Height="70"
Margin="8"
x:Load="{x:Bind ViewModel.LoginConfirmed, Mode=OneWay}"
Background="#6ccb5f"
CornerRadius="35">
<SymbolIcon
Width="52"
Height="52"
Symbol="Accept" />
</Border>
</Grid>
</ContentDialog>
49 changes: 49 additions & 0 deletions src/Files.App/Dialogs/GitHubLoginDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Controls;
using Windows.ApplicationModel.DataTransfer;

namespace Files.App.Dialogs
{
public sealed partial class GitHubLoginDialog : ContentDialog, IDialog<GitHubLoginDialogViewModel>
{
public GitHubLoginDialogViewModel ViewModel
{
get => (GitHubLoginDialogViewModel)DataContext;
set
{
if (ViewModel is not null)
ViewModel.PropertyChanged -= ViewModel_PropertyChanged;

DataContext = value;
if (value is not null)
value.PropertyChanged += ViewModel_PropertyChanged;
}
}

public GitHubLoginDialog()
{
InitializeComponent();
}

public new async Task<DialogResult> ShowAsync() => (DialogResult)await base.ShowAsync();

private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
args.Cancel = true;

var data = new DataPackage();
data.SetText(ViewModel.UserCode);

Clipboard.SetContent(data);
Clipboard.Flush();
}

private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(GitHubLoginDialogViewModel.LoginConfirmed) && ViewModel.LoginConfirmed)
PrimaryButtonText = null;
}
}
}
Loading

0 comments on commit 7b463ec

Please sign in to comment.