Skip to content

Feature: Git Integration #12020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
30 changes: 30 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

## libgit2sharp

**Source**: [https://github.com/libgit2/libgit2sharp](https://github.com/libgit2/libgit2sharp)

### License

```
MIT License

Copyright (c) LibGit2Sharp contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```
1 change: 1 addition & 0 deletions src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="FluentFTP" Version="43.0.1" />
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.1" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.1" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.5" />
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/DynamicDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,4 @@ public static DynamicDialog GetFor_CredentialEntryDialog(string path)
return dialog;
}
}
}
}
27 changes: 27 additions & 0 deletions src/Files.App/Helpers/GitHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using LibGit2Sharp;
using System;
using Files.App.Filesystem.StorageItems;

namespace Files.App.Helpers
{
public static class GitHelpers
{
public static string? GetGitRepositoryPath(string? path, string root)
{
if (root.EndsWith('\\'))
root = root.Substring(0, root.Length - 1);

if (
string.IsNullOrWhiteSpace(path) ||
path.Equals(root, StringComparison.OrdinalIgnoreCase) ||
path.Equals("Home", StringComparison.OrdinalIgnoreCase) ||
ShellStorageFolder.IsShellPath(path)
)
return null;

return Repository.IsValid(path)
? path
: GetGitRepositoryPath(PathNormalization.GetParentDir(path), root);
}
}
}
5 changes: 4 additions & 1 deletion src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,9 @@
<data name="CloseTab" xml:space="preserve">
<value>Closes current tab</value>
</data>
<data name="Branch" xml:space="preserve">
<value>Branch: {0}</value>
</data>
<data name="Redo" xml:space="preserve">
<value>Redo</value>
</data>
Expand Down Expand Up @@ -3196,4 +3199,4 @@
<data name="RedoDescription" xml:space="preserve">
<value>Redo the last file operation</value>
</data>
</root>
</root>
72 changes: 47 additions & 25 deletions src/Files.App/UserControls/StatusBarControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,60 @@
x:Class="Files.App.UserControls.StatusBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:Files.App.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="32"
d:DesignWidth="400"
mc:Ignorable="d">

<StackPanel
Height="32"
Padding="8,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Orientation="Horizontal"
Spacing="8">
<UserControl.Resources>
<converters:NullToTrueConverter x:Key="NullToFalseConverter" Inverse="True" />
</UserControl.Resources>

<TextBlock
x:Name="DirectoryItemCount"
VerticalAlignment="Center"
x:Load="{x:Bind ShowInfoText, Mode=OneWay}"
Text="{x:Bind DirectoryPropertiesViewModel.DirectoryItemCount, Mode=OneWay}" />
<Grid Padding="8,0" ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel
Grid.Column="0"
Height="32"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Orientation="Horizontal"
Spacing="8">

<TextBlock
x:Name="SelectedItemsCountString"
VerticalAlignment="Center"
x:Load="{x:Bind ShowInfoText, Mode=OneWay}"
Text="{x:Bind SelectedItemsPropertiesViewModel.SelectedItemsCountString, Mode=OneWay}"
Visibility="{x:Bind SelectedItemsPropertiesViewModel.IsItemSelected, Mode=OneWay}" />
<TextBlock
x:Name="DirectoryItemCount"
VerticalAlignment="Center"
x:Load="{x:Bind ShowInfoText, Mode=OneWay}"
Text="{x:Bind DirectoryPropertiesViewModel.DirectoryItemCount, Mode=OneWay}" />

<TextBlock
x:Name="SelectedItemsCountString"
VerticalAlignment="Center"
x:Load="{x:Bind ShowInfoText, Mode=OneWay}"
Text="{x:Bind SelectedItemsPropertiesViewModel.SelectedItemsCountString, Mode=OneWay}"
Visibility="{x:Bind SelectedItemsPropertiesViewModel.IsItemSelected, Mode=OneWay}" />

<TextBlock
x:Name="ItemSize"
VerticalAlignment="Center"
x:Load="{x:Bind ShowInfoText, Mode=OneWay}"
Text="{x:Bind SelectedItemsPropertiesViewModel.ItemSize, Mode=OneWay}"
Visibility="{x:Bind SelectedItemsPropertiesViewModel.ItemSizeVisibility, Mode=OneWay}" />
</StackPanel>

<TextBlock
x:Name="ItemSize"
<StackPanel
Grid.Column="1"
VerticalAlignment="Center"
x:Load="{x:Bind ShowInfoText, Mode=OneWay}"
Text="{x:Bind SelectedItemsPropertiesViewModel.ItemSize, Mode=OneWay}"
Visibility="{x:Bind SelectedItemsPropertiesViewModel.ItemSizeVisibility, Mode=OneWay}" />
</StackPanel>
</UserControl>
Orientation="Horizontal"
Spacing="8">
<TextBlock
x:Name="GitBranch"
x:Load="{x:Bind DirectoryPropertiesViewModel.GitBranchDisplayName, Mode=OneWay, Converter={StaticResource NullToFalseConverter}}"
Text="{x:Bind DirectoryPropertiesViewModel.GitBranchDisplayName, Mode=OneWay}" />
</StackPanel>
</Grid>
</UserControl>
13 changes: 2 additions & 11 deletions src/Files.App/UserControls/StatusBarControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Files.App.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Files.App.UserControls
{
public sealed partial class StatusBarControl : UserControl, INotifyPropertyChanged
public sealed partial class StatusBarControl : UserControl
{
public DirectoryPropertiesViewModel DirectoryPropertiesViewModel
{
Expand Down Expand Up @@ -41,12 +39,5 @@ public StatusBarControl()
{
InitializeComponent();
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
47 changes: 43 additions & 4 deletions src/Files.App/ViewModels/CurrentInstanceViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Files.App.Extensions;
using Files.Shared.Enums;
using LibGit2Sharp;
using System.Linq;

namespace Files.App.ViewModels
{
public class CurrentInstanceViewModel : ObservableObject
{
// TODO:
// In the future, we should consolidate these public variables into
// a single enum property providing simplified customization of the
// values being manipulated inside the setter blocks
// TODO:
// In the future, we should consolidate these public variables into
// a single enum property providing simplified customization of the
// values being manipulated inside the setter blocks

public FolderSettingsViewModel FolderSettings { get; }

Expand Down Expand Up @@ -186,5 +189,41 @@ public bool CanTagFilesInPage
{
get => !isPageTypeRecycleBin && !isPageTypeFtp && !isPageTypeZipFolder;
}

public bool IsGitRepository => !string.IsNullOrWhiteSpace(gitRepositoryPath);

private string? gitRepositoryPath;
public string? GitRepositoryPath
{
get => gitRepositoryPath;
set
{
if (SetProperty(ref gitRepositoryPath, value))
{
OnPropertyChanged(nameof(IsGitRepository));
OnPropertyChanged(nameof(GitBranchName));
}
}
}

public string GitBranchName
{
get
{
if (IsGitRepository)
{
using var repository = new Repository(gitRepositoryPath);
return repository.Branches.First(branch =>
branch.IsCurrentRepositoryHead).FriendlyName;
}

return string.Empty;
}
}

public void UpdateCurrentBranchName()
{
OnPropertyChanged(nameof(GitBranchName));
}
}
}
7 changes: 7 additions & 0 deletions src/Files.App/ViewModels/DirectoryPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ public string DirectoryItemCount
get => directoryItemCount;
set => SetProperty(ref directoryItemCount, value);
}

private string? gitBranchDisplayName;
public string? GitBranchDisplayName
{
get => gitBranchDisplayName;
set => SetProperty(ref gitBranchDisplayName, value);
}
}
}
Loading