Skip to content

Feature: Allow deleting of local Git branches #14059

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 4 commits into from
Nov 27, 2023
Merged
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
3 changes: 3 additions & 0 deletions src/Files.App/Converters/MultiBooleanConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static Boolean OrAndConvert(bool a, bool b, bool c)
public static Visibility OrConvertToVisibility(bool a, bool b)
=> (a || b) ? Visibility.Visible : Visibility.Collapsed;

public static Visibility NorConvertToVisibility(bool a, bool b)
=> !(a || b) ? Visibility.Visible : Visibility.Collapsed;

public static Visibility OrNotConvertToVisibility(bool a, bool b)
=> OrConvertToVisibility(a, !b);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Data/Items/BranchItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace Files.App.Data.Items
{
public record BranchItem(string Name, bool IsRemote, int? AheadBy, int? BehindBy);
public record BranchItem(string Name, bool IsHead, bool IsRemote, int? AheadBy, int? BehindBy);
}
23 changes: 14 additions & 9 deletions src/Files.App/Data/Models/DirectoryPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class DirectoryPropertiesViewModel : ObservableObject

private string? _gitRepositoryPath;

private readonly ObservableCollection<string> _localBranches = new();
private readonly ObservableCollection<BranchItem> _localBranches = new();

private readonly ObservableCollection<string> _remoteBranches = new();
private readonly ObservableCollection<BranchItem> _remoteBranches = new();

public bool IsBranchesFlyoutExpaned { get; set; } = false;

Expand All @@ -43,9 +43,9 @@ public int SelectedBranchIndex
if (SetProperty(ref _SelectedBranchIndex, value) &&
value != -1 &&
(value != ACTIVE_BRANCH_INDEX || !_ShowLocals) &&
value < BranchesNames.Count)
value < Branches.Count)
{
CheckoutRequested?.Invoke(this, BranchesNames[value]);
CheckoutRequested?.Invoke(this, Branches[value].Name);
}
}
}
Expand All @@ -58,7 +58,7 @@ public bool ShowLocals
{
if (SetProperty(ref _ShowLocals, value))
{
OnPropertyChanged(nameof(BranchesNames));
OnPropertyChanged(nameof(Branches));

if (value)
SelectedBranchIndex = ACTIVE_BRANCH_INDEX;
Expand All @@ -80,7 +80,7 @@ public string ExtendedStatusInfo
set => SetProperty(ref _ExtendedStatusInfo, value);
}

public ObservableCollection<string> BranchesNames => _ShowLocals
public ObservableCollection<BranchItem> Branches => _ShowLocals
? _localBranches
: _remoteBranches;

Expand All @@ -91,7 +91,7 @@ public string ExtendedStatusInfo
public DirectoryPropertiesViewModel()
{
NewBranchCommand = new AsyncRelayCommand(()
=> GitHelpers.CreateNewBranchAsync(_gitRepositoryPath!, _localBranches[ACTIVE_BRANCH_INDEX]));
=> GitHelpers.CreateNewBranchAsync(_gitRepositoryPath!, _localBranches[ACTIVE_BRANCH_INDEX].Name));
}

public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchItem? head)
Expand Down Expand Up @@ -128,12 +128,17 @@ public async Task LoadBranches()
foreach (var branch in branches)
{
if (branch.IsRemote)
_remoteBranches.Add(branch.Name);
_remoteBranches.Add(branch);
else
_localBranches.Add(branch.Name);
_localBranches.Add(branch);
}

SelectedBranchIndex = ShowLocals ? ACTIVE_BRANCH_INDEX : -1;
}

public Task ExecuteDeleteBranch(string? branchName)
{
return GitHelpers.DeleteBranchAsync(_gitRepositoryPath, GitBranchDisplayName, branchName);
}
}
}
20 changes: 20 additions & 0 deletions src/Files.App/Helpers/Dialog/DynamicDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,25 @@ public static DynamicDialog GetFor_GitCannotInitializeqRepositoryHere()
DynamicButtons = DynamicDialogButtons.Primary
});
}

public static DynamicDialog GetFor_DeleteGitBranchConfirmation(string branchName)
{
DynamicDialog dialog = null!;
dialog = new DynamicDialog(new DynamicDialogViewModel()
{
TitleText = "GitDeleteBranch".GetLocalizedResource(),
SubtitleText = string.Format("GitDeleteBranchSubtitle".GetLocalizedResource(), branchName),
PrimaryButtonText = "OK".GetLocalizedResource(),
CloseButtonText = "Cancel".GetLocalizedResource(),
AdditionalData = true,
CloseButtonAction = (vm, e) =>
{
dialog.ViewModel.AdditionalData = false;
vm.HideDialog();
}
});

return dialog;
}
}
}
6 changes: 6 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,12 @@
<data name="FailedToSetBackground" xml:space="preserve">
<value>Failed to set the background wallpaper</value>
</data>
<data name="GitDeleteBranch" xml:space="preserve">
<value>Delete Git branch</value>
</data>
<data name="GitDeleteBranchSubtitle" xml:space="preserve">
<value>Are you sure you want to permanently delete "{0}" branch?</value>
</data>
<data name="ConnectedToGitHub" xml:space="preserve">
<value>Connected to GitHub</value>
</data>
Expand Down
33 changes: 31 additions & 2 deletions src/Files.App/UserControls/StatusBarControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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:data="using:Files.App.Data.Items"
xmlns:helpers="using:Files.App.Helpers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:usercontrols="using:Files.App.UserControls"
Expand Down Expand Up @@ -350,9 +351,37 @@
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
IsItemClickEnabled="True"
ItemClick="BranchesList_ItemClick"
ItemsSource="{x:Bind DirectoryPropertiesViewModel.BranchesNames, Mode=OneWay}"
ItemsSource="{x:Bind DirectoryPropertiesViewModel.Branches, Mode=OneWay}"
SelectedIndex="{x:Bind DirectoryPropertiesViewModel.SelectedBranchIndex, Mode=TwoWay}"
SelectionMode="Single" />
SelectionMode="Single">

<ListView.ItemTemplate>
<DataTemplate x:DataType="data:BranchItem">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock
VerticalAlignment="Center"
Text="{x:Bind Name}"
TextTrimming="CharacterEllipsis" />
<Button
Grid.Column="1"
AutomationProperties.Name="{helpers:ResourceString Name=Delete}"
Background="Transparent"
BorderBrush="Transparent"
Click="DeleteBranch_Click"
ToolTipService.ToolTip="{helpers:ResourceString Name=Delete}"
Visibility="{x:Bind converters:MultiBooleanConverter.NorConvertToVisibility(IsHead, IsRemote)}">
<FontIcon FontSize="12" Glyph="&#xE74D;" />
</Button>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>

</ListView>
</Grid>
</Flyout>
</Button.Flyout>
Expand Down
9 changes: 9 additions & 0 deletions src/Files.App/UserControls/StatusBarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,14 @@ private void BranchesFlyout_Closing(object _, object e)

DirectoryPropertiesViewModel.IsBranchesFlyoutExpaned = false;
}

private async void DeleteBranch_Click(object sender, RoutedEventArgs e)
{
if (DirectoryPropertiesViewModel is null)
return;

BranchesFlyout.Hide();
await DirectoryPropertiesViewModel.ExecuteDeleteBranch(((BranchItem)((Button)sender).DataContext).Name);
}
}
}
53 changes: 49 additions & 4 deletions src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
.OrderByDescending(b => b.Tip?.Committer.When)
.Take(MAX_NUMBER_OF_BRANCHES)
.OrderByDescending(b => b.IsCurrentRepositoryHead)
.Select(b => new BranchItem(b.FriendlyName, b.IsRemote, TryGetTrackingDetails(b)?.AheadBy ?? 0, TryGetTrackingDetails(b)?.BehindBy ?? 0))
.Select(b => new BranchItem(b.FriendlyName, b.IsCurrentRepositoryHead, b.IsRemote, TryGetTrackingDetails(b)?.AheadBy ?? 0, TryGetTrackingDetails(b)?.BehindBy ?? 0))
.ToArray();
}
}
catch (Exception)
{
result = GitOperationResult.GenericError;
}

return (result, branches);
});

Expand All @@ -165,7 +165,13 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
using var repository = new Repository(path);
var branch = GetValidBranches(repository.Branches).FirstOrDefault(b => b.IsCurrentRepositoryHead);
if (branch is not null)
head = new BranchItem(branch.FriendlyName, branch.IsRemote, TryGetTrackingDetails(branch)?.AheadBy ?? 0, TryGetTrackingDetails(branch)?.BehindBy ?? 0);
head = new BranchItem(
branch.FriendlyName,
branch.IsCurrentRepositoryHead,
branch.IsRemote,
TryGetTrackingDetails(branch)?.AheadBy ?? 0,
TryGetTrackingDetails(branch)?.BehindBy ?? 0
);
}
catch
{
Expand Down Expand Up @@ -280,6 +286,45 @@ await Checkout(repositoryPath, viewModel.BasedOn))
IsExecutingGitAction = false;
}

public static async Task DeleteBranchAsync(string? repositoryPath, string? activeBranch, string? branchToDelete)
{
Analytics.TrackEvent("Triggered delete git branch");

if (string.IsNullOrWhiteSpace(repositoryPath) ||
string.IsNullOrWhiteSpace(activeBranch) ||
string.IsNullOrWhiteSpace(branchToDelete) ||
activeBranch.Equals(branchToDelete, StringComparison.OrdinalIgnoreCase) ||
!Repository.IsValid(repositoryPath))
{
return;
}

var dialog = DynamicDialogFactory.GetFor_DeleteGitBranchConfirmation(branchToDelete);
await dialog.TryShowAsync();
if (!(dialog.ViewModel.AdditionalData as bool? ?? false))
return;

IsExecutingGitAction = true;

await PostMethodToThreadWithMessageQueueAsync<GitOperationResult>(() =>
{
try
{
using var repository = new Repository(repositoryPath);
repository.Branches.Remove(branchToDelete);
}
catch (Exception)
{
return GitOperationResult.GenericError;
}

return GitOperationResult.Success;
});

IsExecutingGitAction = false;
}


public static bool ValidateBranchNameForRepository(string branchName, string repositoryPath)
{
if (string.IsNullOrEmpty(branchName) || !Repository.IsValid(repositoryPath))
Expand Down
22 changes: 11 additions & 11 deletions src/Files.App/Views/Shells/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ protected async void FilesystemViewModel_DirectoryInfoUpdated(object sender, Eve
? "ItemCount/Text".GetLocalizedResource()
: "ItemsCount/Text".GetLocalizedResource();

BranchItem? headBranch = headBranch = InstanceViewModel.IsGitRepository
? await GitHelpers.GetRepositoryHead(InstanceViewModel.GitRepositoryPath)
: null;

if (InstanceViewModel.GitRepositoryPath != FilesystemViewModel.GitDirectory)
{
InstanceViewModel.GitRepositoryPath = FilesystemViewModel.GitDirectory;

BranchItem? headBranch = headBranch = InstanceViewModel.IsGitRepository
? await GitHelpers.GetRepositoryHead(InstanceViewModel.GitRepositoryPath)
: null;

InstanceViewModel.GitBranchName = headBranch is not null
? headBranch.Name
: string.Empty;
Expand All @@ -267,14 +267,14 @@ protected async void FilesystemViewModel_DirectoryInfoUpdated(object sender, Eve
() => GitHelpers.FetchOrigin(InstanceViewModel.GitRepositoryPath),
_gitFetchToken.Token);
}
}

if (!GitHelpers.IsExecutingGitAction)
{
ContentPage.DirectoryPropertiesViewModel.UpdateGitInfo(
InstanceViewModel.IsGitRepository,
InstanceViewModel.GitRepositoryPath,
headBranch);
}
if (!GitHelpers.IsExecutingGitAction)
{
ContentPage.DirectoryPropertiesViewModel.UpdateGitInfo(
InstanceViewModel.IsGitRepository,
InstanceViewModel.GitRepositoryPath,
headBranch);
}

ContentPage.DirectoryPropertiesViewModel.DirectoryItemCount = $"{FilesystemViewModel.FilesAndFolders.Count} {directoryItemCountLocalization}";
Expand Down