Skip to content
Closed
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
46 changes: 39 additions & 7 deletions src/ViewModels/AddWorktree.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace SourceGit.ViewModels
Expand Down Expand Up @@ -51,13 +52,43 @@ public string SelectedBranch
public bool SetTrackingBranch
{
get => _setTrackingBranch;
set => SetProperty(ref _setTrackingBranch, value);
set
{
if (SetProperty(ref _setTrackingBranch, value))
{
if (value)
{
var remoteAndBranchName = RemoteBranches.Where(b => b.EndsWith(SelectedBranch));
SetSelectedTrackingBranch = remoteAndBranchName.FirstOrDefault();
}

}
}
}

public string SelectedTrackingBranch
[Required(ErrorMessage = "Tracking branch is required!")]
[CustomValidation(typeof(AddWorktree), nameof(ValidateTrackingBranch))]
public string SetSelectedTrackingBranch
{
get;
set;
get => _setTrackingBranch ? _selectedTrackingBranch : string.Empty;
set => SetProperty(ref _selectedTrackingBranch, value, validate: true);
}

public static ValidationResult ValidateTrackingBranch(string trackingBranch, ValidationContext ctx)
{
if (ctx.ObjectInstance is not AddWorktree creator)
return new ValidationResult("Missing runtime context to create branch!");

if(!creator._setTrackingBranch)
return ValidationResult.Success;

if (string.IsNullOrEmpty(trackingBranch))
return new ValidationResult("Tracking branch is required!");

if(!creator.RemoteBranches.Contains(trackingBranch))
return new ValidationResult("Invalid tracking branch!");

return ValidationResult.Success;
}

public AddWorktree(Repository repo)
Expand All @@ -75,9 +106,9 @@ public AddWorktree(Repository repo)
}

if (RemoteBranches.Count > 0)
SelectedTrackingBranch = RemoteBranches[0];
SetSelectedTrackingBranch = RemoteBranches[0];
else
SelectedTrackingBranch = string.Empty;
SetSelectedTrackingBranch = string.Empty;
}

public static ValidationResult ValidateWorktreePath(string path, ValidationContext ctx)
Expand Down Expand Up @@ -110,7 +141,7 @@ public override async Task<bool> Sure()
ProgressDescription = "Adding worktree ...";

var branchName = _selectedBranch;
var tracking = _setTrackingBranch ? SelectedTrackingBranch : string.Empty;
var tracking = _setTrackingBranch ? _selectedTrackingBranch : string.Empty;
var log = _repo.CreateLog("Add Worktree");

Use(log);
Expand All @@ -128,5 +159,6 @@ public override async Task<bool> Sure()
private bool _createNewBranch = true;
private string _selectedBranch = string.Empty;
private bool _setTrackingBranch = false;
private string _selectedTrackingBranch = string.Empty;
}
}
2 changes: 1 addition & 1 deletion src/Views/AddWorktree.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
VerticalAlignment="Center" HorizontalAlignment="Stretch"
ItemsSource="{Binding RemoteBranches}"
IsTextSearchEnabled="True"
SelectedItem="{Binding SelectedTrackingBranch, Mode=TwoWay}"
SelectedItem="{Binding SetSelectedTrackingBranch, Mode=TwoWay}"
IsVisible="{Binding SetTrackingBranch, Mode=OneWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
Expand Down