refactor: git-flow supports.

This commit is contained in:
leo 2024-06-15 12:44:35 +08:00
parent 5bb41ed65f
commit 6de92bb4d8
No known key found for this signature in database
GPG key ID: B528468E49CD0E58
7 changed files with 192 additions and 192 deletions

View file

@ -4,10 +4,15 @@ namespace SourceGit.ViewModels
{
public class GitFlowFinish : Popup
{
public Models.Branch Branch => _branch;
public bool IsFeature => _type == Models.GitFlowBranchType.Feature;
public bool IsRelease => _type == Models.GitFlowBranchType.Release;
public bool IsHotfix => _type == Models.GitFlowBranchType.Hotfix;
public Models.Branch Branch
{
get;
set;
} = null;
public bool IsFeature => _type == "feature";
public bool IsRelease => _type == "release";
public bool IsHotfix => _type == "hotfix";
public bool KeepBranch
{
@ -15,11 +20,13 @@ namespace SourceGit.ViewModels
set;
} = false;
public GitFlowFinish(Repository repo, Models.Branch branch, Models.GitFlowBranchType type)
public GitFlowFinish(Repository repo, Models.Branch branch, string type, string prefix)
{
_repo = repo;
_branch = branch;
_type = type;
_prefix = prefix;
Branch = branch;
View = new Views.GitFlowFinish() { DataContext = this };
}
@ -28,29 +35,16 @@ namespace SourceGit.ViewModels
_repo.SetWatcherEnabled(false);
return Task.Run(() =>
{
var branch = _branch.Name;
switch (_type)
{
case Models.GitFlowBranchType.Feature:
branch = branch.Substring(_repo.GitFlow.Feature.Length);
break;
case Models.GitFlowBranchType.Release:
branch = branch.Substring(_repo.GitFlow.Release.Length);
break;
default:
branch = branch.Substring(_repo.GitFlow.Hotfix.Length);
break;
}
SetProgressDescription($"Git Flow - finishing {_branch.Name} ...");
var succ = new Commands.GitFlow(_repo.FullPath).Finish(_type, branch, KeepBranch);
var name = Branch.Name.StartsWith(_prefix) ? Branch.Name.Substring(_prefix.Length) : Branch.Name;
SetProgressDescription($"Git Flow - finishing {_type} {name} ...");
var succ = Commands.GitFlow.Finish(_repo.FullPath, _type, name, KeepBranch);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});
}
private readonly Repository _repo = null;
private readonly Models.Branch _branch = null;
private readonly Models.GitFlowBranchType _type = Models.GitFlowBranchType.None;
private readonly string _type = "feature";
private readonly string _prefix = string.Empty;
}
}

View file

@ -19,27 +19,15 @@ namespace SourceGit.ViewModels
get => _prefix;
}
public bool IsFeature => _type == Models.GitFlowBranchType.Feature;
public bool IsRelease => _type == Models.GitFlowBranchType.Release;
public bool IsHotfix => _type == Models.GitFlowBranchType.Hotfix;
public bool IsFeature => _type == "feature";
public bool IsRelease => _type == "release";
public bool IsHotfix => _type == "hotfix";
public GitFlowStart(Repository repo, Models.GitFlowBranchType type)
public GitFlowStart(Repository repo, string type)
{
_repo = repo;
_type = type;
switch (type)
{
case Models.GitFlowBranchType.Feature:
_prefix = repo.GitFlow.Feature;
break;
case Models.GitFlowBranchType.Release:
_prefix = repo.GitFlow.Release;
break;
default:
_prefix = repo.GitFlow.Hotfix;
break;
}
_prefix = Commands.GitFlow.Prefix(repo.FullPath, type);
View = new Views.GitFlowStart() { DataContext = this };
}
@ -65,15 +53,15 @@ namespace SourceGit.ViewModels
_repo.SetWatcherEnabled(false);
return Task.Run(() =>
{
SetProgressDescription($"Git Flow - starting {_prefix}{_name} ...");
var succ = new Commands.GitFlow(_repo.FullPath).Start(_type, _name);
SetProgressDescription($"Git Flow - starting {_type} {_name} ...");
var succ = Commands.GitFlow.Start(_repo.FullPath, _type, _name);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});
}
private readonly Repository _repo = null;
private readonly Models.GitFlowBranchType _type = Models.GitFlowBranchType.Feature;
private readonly string _type = "feature";
private readonly string _prefix = string.Empty;
private string _name = null;
}

View file

@ -451,8 +451,8 @@ namespace SourceGit.ViewModels
submenu.Items.Add(push);
submenu.Items.Add(new MenuItem() { Header = "-" });
var type = _repo.GitFlow.GetBranchType(current.Name);
if (type != Models.GitFlowBranchType.None)
var detect = Commands.GitFlow.DetectType(_repo.FullPath, _repo.Branches, current.Name);
if (detect.IsGitFlowBranch)
{
var finish = new MenuItem();
finish.Header = new Views.NameHighlightedTextBlock("BranchCM.Finish", current.Name);
@ -460,7 +460,7 @@ namespace SourceGit.ViewModels
finish.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new GitFlowFinish(_repo, current, type));
PopupHost.ShowPopup(new GitFlowFinish(_repo, current, detect.Type, detect.Prefix));
e.Handled = true;
};
submenu.Items.Add(finish);
@ -510,8 +510,8 @@ namespace SourceGit.ViewModels
submenu.Items.Add(merge);
submenu.Items.Add(new MenuItem() { Header = "-" });
var type = _repo.GitFlow.GetBranchType(branch.Name);
if (type != Models.GitFlowBranchType.None)
var detect = Commands.GitFlow.DetectType(_repo.FullPath, _repo.Branches, branch.Name);
if (detect.IsGitFlowBranch)
{
var finish = new MenuItem();
finish.Header = new Views.NameHighlightedTextBlock("BranchCM.Finish", branch.Name);
@ -519,7 +519,7 @@ namespace SourceGit.ViewModels
finish.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new GitFlowFinish(_repo, branch, type));
PopupHost.ShowPopup(new GitFlowFinish(_repo, branch, detect.Type, detect.Prefix));
e.Handled = true;
};
submenu.Items.Add(finish);

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@ -6,7 +7,6 @@ namespace SourceGit.ViewModels
{
public partial class InitGitFlow : Popup
{
[GeneratedRegex(@"^[\w\-/\.]+$")]
private static partial Regex TAG_PREFIX();
@ -62,6 +62,23 @@ namespace SourceGit.ViewModels
public InitGitFlow(Repository repo)
{
_repo = repo;
var localBranches = new List<string>();
foreach (var branch in repo.Branches)
{
if (branch.IsLocal)
localBranches.Add(branch.Name);
}
if (localBranches.Contains("master"))
_master = "master";
else if (localBranches.Contains("main"))
_master = "main";
else if (localBranches.Count > 0)
_master = localBranches[0];
else
_master = "master";
View = new Views.InitGitFlow() { DataContext = this };
}
@ -79,9 +96,7 @@ namespace SourceGit.ViewModels
public static ValidationResult ValidateTagPrefix(string tagPrefix, ValidationContext ctx)
{
if (!string.IsNullOrWhiteSpace(tagPrefix) && !TAG_PREFIX().IsMatch(tagPrefix))
{
return new ValidationResult("Bad tag prefix format!");
}
return ValidationResult.Success;
}
@ -93,14 +108,7 @@ namespace SourceGit.ViewModels
return Task.Run(() =>
{
var succ = new Commands.GitFlow(_repo.FullPath).Init(_repo.Branches, _master, _develop, _featurePrefix, _releasePrefix, _hotfixPrefix, _tagPrefix);
if (succ)
{
_repo.GitFlow.Feature = _featurePrefix;
_repo.GitFlow.Release = _releasePrefix;
_repo.GitFlow.Hotfix = _hotfixPrefix;
}
var succ = Commands.GitFlow.Init(_repo.FullPath, _repo.Branches, _master, _develop, _featurePrefix, _releasePrefix, _hotfixPrefix, _tagPrefix);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});

View file

@ -51,13 +51,6 @@ namespace SourceGit.ViewModels
set;
} = new AvaloniaList<string>();
[JsonIgnore]
public Models.GitFlow GitFlow
{
get => _gitflow;
set => SetProperty(ref _gitflow, value);
}
[JsonIgnore]
public int SelectedViewIndex
{
@ -298,7 +291,6 @@ namespace SourceGit.ViewModels
Task.Run(RefreshSubmodules);
Task.Run(RefreshWorkingCopyChanges);
Task.Run(RefreshStashes);
Task.Run(RefreshGitFlow);
}
public void OpenInFileManager()
@ -697,22 +689,6 @@ namespace SourceGit.ViewModels
});
}
public void RefreshGitFlow()
{
var config = new Commands.Config(_fullpath).ListAll();
var gitFlow = new Models.GitFlow();
if (config.TryGetValue("gitflow.prefix.feature", out var feature))
gitFlow.Feature = feature;
if (config.TryGetValue("gitflow.prefix.release", out var release))
gitFlow.Release = release;
if (config.TryGetValue("gitflow.prefix.hotfix", out var hotfix))
gitFlow.Hotfix = hotfix;
Dispatcher.UIThread.Invoke(() =>
{
GitFlow = gitFlow;
});
}
public void CreateNewBranch()
{
var current = Branches.Find(x => x.IsCurrent);
@ -797,7 +773,8 @@ namespace SourceGit.ViewModels
var menu = new ContextMenu();
menu.Placement = PlacementMode.BottomEdgeAlignedLeft;
if (GitFlow.IsEnabled)
var isGitFlowEnabled = Commands.GitFlow.IsEnabled(_fullpath, _branches);
if (isGitFlowEnabled)
{
var startFeature = new MenuItem();
startFeature.Header = App.Text("GitFlow.StartFeature");
@ -805,7 +782,7 @@ namespace SourceGit.ViewModels
startFeature.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new GitFlowStart(this, Models.GitFlowBranchType.Feature));
PopupHost.ShowPopup(new GitFlowStart(this, "feature"));
e.Handled = true;
};
@ -815,7 +792,7 @@ namespace SourceGit.ViewModels
startRelease.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new GitFlowStart(this, Models.GitFlowBranchType.Release));
PopupHost.ShowPopup(new GitFlowStart(this, "release"));
e.Handled = true;
};
@ -825,7 +802,7 @@ namespace SourceGit.ViewModels
startHotfix.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new GitFlowStart(this, Models.GitFlowBranchType.Hotfix));
PopupHost.ShowPopup(new GitFlowStart(this, "hotfix"));
e.Handled = true;
};
@ -1005,8 +982,8 @@ namespace SourceGit.ViewModels
}
}
var type = GitFlow.GetBranchType(branch.Name);
if (type != Models.GitFlowBranchType.None)
var detect = Commands.GitFlow.DetectType(_fullpath, _branches, branch.Name);
if (detect.IsGitFlowBranch)
{
var finish = new MenuItem();
finish.Header = new Views.NameHighlightedTextBlock("BranchCM.Finish", branch.Name);
@ -1014,7 +991,7 @@ namespace SourceGit.ViewModels
finish.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new GitFlowFinish(this, branch, type));
PopupHost.ShowPopup(new GitFlowFinish(this, branch, detect.Type, detect.Prefix));
e.Handled = true;
};
menu.Items.Add(new MenuItem() { Header = "-" });
@ -1532,7 +1509,6 @@ namespace SourceGit.ViewModels
private string _fullpath = string.Empty;
private string _gitDir = string.Empty;
private Models.GitFlow _gitflow = new Models.GitFlow();
private Models.Watcher _watcher = null;
private Histories _histories = null;