Skip to content

Commit 4d5be9f

Browse files
committed
refactor: rewrite git-flow integration
Signed-off-by: leo <[email protected]>
1 parent 6fa454a commit 4d5be9f

File tree

9 files changed

+195
-164
lines changed

9 files changed

+195
-164
lines changed

src/Commands/GitFlow.cs

Lines changed: 35 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Text;
43

54
using Avalonia.Threading;
@@ -8,34 +7,6 @@ namespace SourceGit.Commands
87
{
98
public static class GitFlow
109
{
11-
public class BranchDetectResult
12-
{
13-
public bool IsGitFlowBranch { get; set; } = false;
14-
public string Type { get; set; } = string.Empty;
15-
public string Prefix { get; set; } = string.Empty;
16-
}
17-
18-
public static bool IsEnabled(string repo, List<Models.Branch> branches)
19-
{
20-
var localBrancheNames = new HashSet<string>();
21-
foreach (var branch in branches)
22-
{
23-
if (branch.IsLocal)
24-
localBrancheNames.Add(branch.Name);
25-
}
26-
27-
var config = new Config(repo).ListAll();
28-
if (!config.TryGetValue("gitflow.branch.master", out string master) || !localBrancheNames.Contains(master))
29-
return false;
30-
31-
if (!config.TryGetValue("gitflow.branch.develop", out string develop) || !localBrancheNames.Contains(develop))
32-
return false;
33-
34-
return config.ContainsKey("gitflow.prefix.feature") &&
35-
config.ContainsKey("gitflow.prefix.release") &&
36-
config.ContainsKey("gitflow.prefix.hotfix");
37-
}
38-
3910
public static bool Init(string repo, List<Models.Branch> branches, string master, string develop, string feature, string release, string hotfix, string version, Models.ICommandLog log)
4011
{
4112
var current = branches.Find(x => x.IsCurrent);
@@ -66,90 +37,53 @@ public static bool Init(string repo, List<Models.Branch> branches, string master
6637
return init.Exec();
6738
}
6839

69-
public static string GetPrefix(string repo, string type)
40+
public static bool Start(string repo, Models.GitFlowBranchType type, string name, Models.ICommandLog log)
7041
{
71-
return new Config(repo).Get($"gitflow.prefix.{type}");
72-
}
73-
74-
public static BranchDetectResult DetectType(string repo, List<Models.Branch> branches, string branch)
75-
{
76-
var rs = new BranchDetectResult();
77-
var localBrancheNames = new HashSet<string>();
78-
foreach (var b in branches)
79-
{
80-
if (b.IsLocal)
81-
localBrancheNames.Add(b.Name);
82-
}
83-
84-
var config = new Config(repo).ListAll();
85-
if (!config.TryGetValue("gitflow.branch.master", out string master) || !localBrancheNames.Contains(master))
86-
return rs;
87-
88-
if (!config.TryGetValue("gitflow.branch.develop", out string develop) || !localBrancheNames.Contains(develop))
89-
return rs;
90-
91-
if (!config.TryGetValue("gitflow.prefix.feature", out var feature) ||
92-
!config.TryGetValue("gitflow.prefix.release", out var release) ||
93-
!config.TryGetValue("gitflow.prefix.hotfix", out var hotfix))
94-
return rs;
95-
96-
if (branch.StartsWith(feature, StringComparison.Ordinal))
97-
{
98-
rs.IsGitFlowBranch = true;
99-
rs.Type = "feature";
100-
rs.Prefix = feature;
101-
}
102-
else if (branch.StartsWith(release, StringComparison.Ordinal))
103-
{
104-
rs.IsGitFlowBranch = true;
105-
rs.Type = "release";
106-
rs.Prefix = release;
107-
}
108-
else if (branch.StartsWith(hotfix, StringComparison.Ordinal))
109-
{
110-
rs.IsGitFlowBranch = true;
111-
rs.Type = "hotfix";
112-
rs.Prefix = hotfix;
113-
}
114-
115-
return rs;
116-
}
42+
var start = new Command();
43+
start.WorkingDirectory = repo;
44+
start.Context = repo;
11745

118-
public static bool Start(string repo, string type, string name, Models.ICommandLog log)
119-
{
120-
if (!SUPPORTED_BRANCH_TYPES.Contains(type))
46+
switch (type)
12147
{
122-
Dispatcher.UIThread.Post(() =>
123-
{
124-
App.RaiseException(repo, "Bad branch type!!!");
125-
});
126-
127-
return false;
48+
case Models.GitFlowBranchType.Feature:
49+
start.Args = $"flow feature start {name}";
50+
break;
51+
case Models.GitFlowBranchType.Release:
52+
start.Args = $"flow release start {name}";
53+
break;
54+
case Models.GitFlowBranchType.Hotfix:
55+
start.Args = $"flow hotfix start {name}";
56+
break;
57+
default:
58+
Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, "Bad git-flow branch type!!!"));
59+
return false;
12860
}
12961

130-
var start = new Command();
131-
start.WorkingDirectory = repo;
132-
start.Context = repo;
133-
start.Args = $"flow {type} start {name}";
13462
start.Log = log;
13563
return start.Exec();
13664
}
13765

138-
public static bool Finish(string repo, string type, string name, bool squash, bool push, bool keepBranch, Models.ICommandLog log)
66+
public static bool Finish(string repo, Models.GitFlowBranchType type, string name, bool squash, bool push, bool keepBranch, Models.ICommandLog log)
13967
{
140-
if (!SUPPORTED_BRANCH_TYPES.Contains(type))
141-
{
142-
Dispatcher.UIThread.Post(() =>
143-
{
144-
App.RaiseException(repo, "Bad branch type!!!");
145-
});
68+
var builder = new StringBuilder();
69+
builder.Append("flow ");
14670

147-
return false;
71+
switch (type)
72+
{
73+
case Models.GitFlowBranchType.Feature:
74+
builder.Append("feature");
75+
break;
76+
case Models.GitFlowBranchType.Release:
77+
builder.Append("release");
78+
break;
79+
case Models.GitFlowBranchType.Hotfix:
80+
builder.Append("hotfix");
81+
break;
82+
default:
83+
Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, "Bad git-flow branch type!!!"));
84+
return false;
14885
}
14986

150-
var builder = new StringBuilder();
151-
builder.Append("flow ");
152-
builder.Append(type);
15387
builder.Append(" finish ");
15488
if (squash)
15589
builder.Append("--squash ");
@@ -166,14 +100,5 @@ public static bool Finish(string repo, string type, string name, bool squash, bo
166100
finish.Log = log;
167101
return finish.Exec();
168102
}
169-
170-
private static readonly List<string> SUPPORTED_BRANCH_TYPES = new List<string>()
171-
{
172-
"feature",
173-
"release",
174-
"bugfix",
175-
"hotfix",
176-
"support",
177-
};
178103
}
179104
}

src/Models/GitFlow.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace SourceGit.Models
2+
{
3+
public enum GitFlowBranchType
4+
{
5+
None = 0,
6+
Feature,
7+
Release,
8+
Hotfix,
9+
}
10+
11+
public class GitFlow
12+
{
13+
public string Master { get; set; } = string.Empty;
14+
public string Develop { get; set; } = string.Empty;
15+
public string FeaturePrefix { get; set; } = string.Empty;
16+
public string ReleasePrefix { get; set; } = string.Empty;
17+
public string HotfixPrefix { get; set; } = string.Empty;
18+
19+
public bool IsValid
20+
{
21+
get
22+
{
23+
return !string.IsNullOrEmpty(Master) &&
24+
!string.IsNullOrEmpty(Develop) &&
25+
!string.IsNullOrEmpty(FeaturePrefix) &&
26+
!string.IsNullOrEmpty(ReleasePrefix) &&
27+
!string.IsNullOrEmpty(HotfixPrefix);
28+
}
29+
}
30+
31+
public string GetPrefix(GitFlowBranchType type)
32+
{
33+
switch (type)
34+
{
35+
case GitFlowBranchType.Feature:
36+
return FeaturePrefix;
37+
case GitFlowBranchType.Release:
38+
return ReleasePrefix;
39+
case GitFlowBranchType.Hotfix:
40+
return HotfixPrefix;
41+
default:
42+
return string.Empty;
43+
}
44+
}
45+
}
46+
}

src/ViewModels/GitFlowFinish.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ public Models.Branch Branch
99
get;
1010
}
1111

12-
public bool IsFeature => _type == "feature";
13-
public bool IsRelease => _type == "release";
14-
public bool IsHotfix => _type == "hotfix";
12+
public Models.GitFlowBranchType Type
13+
{
14+
get;
15+
private set;
16+
}
1517

1618
public bool Squash
1719
{
@@ -31,35 +33,33 @@ public bool KeepBranch
3133
set;
3234
} = false;
3335

34-
public GitFlowFinish(Repository repo, Models.Branch branch, string type, string prefix)
36+
public GitFlowFinish(Repository repo, Models.Branch branch, Models.GitFlowBranchType type)
3537
{
3638
_repo = repo;
37-
_type = type;
38-
_prefix = prefix;
3939
Branch = branch;
40+
Type = type;
4041
}
4142

4243
public override Task<bool> Sure()
4344
{
4445
_repo.SetWatcherEnabled(false);
46+
ProgressDescription = $"Git Flow - Finish {Branch.Name} ...";
4547

46-
var name = Branch.Name.StartsWith(_prefix) ? Branch.Name.Substring(_prefix.Length) : Branch.Name;
47-
ProgressDescription = $"Git Flow - finishing {_type} {name} ...";
48-
49-
var log = _repo.CreateLog("Gitflow - Finish");
48+
var log = _repo.CreateLog("GitFlow - Finish");
5049
Use(log);
5150

51+
var prefix = _repo.GitFlow.GetPrefix(Type);
52+
var name = Branch.Name.StartsWith(prefix) ? Branch.Name.Substring(prefix.Length) : Branch.Name;
53+
5254
return Task.Run(() =>
5355
{
54-
var succ = Commands.GitFlow.Finish(_repo.FullPath, _type, name, Squash, AutoPush, KeepBranch, log);
56+
var succ = Commands.GitFlow.Finish(_repo.FullPath, Type, name, Squash, AutoPush, KeepBranch, log);
5557
log.Complete();
5658
CallUIThread(() => _repo.SetWatcherEnabled(true));
5759
return succ;
5860
});
5961
}
6062

6163
private readonly Repository _repo;
62-
private readonly string _type;
63-
private readonly string _prefix;
6464
}
6565
}

src/ViewModels/GitFlowStart.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ namespace SourceGit.ViewModels
55
{
66
public class GitFlowStart : Popup
77
{
8+
public Models.GitFlowBranchType Type
9+
{
10+
get;
11+
private set;
12+
}
13+
14+
public string Prefix
15+
{
16+
get;
17+
private set;
18+
}
19+
820
[Required(ErrorMessage = "Name is required!!!")]
921
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
1022
[CustomValidation(typeof(GitFlowStart), nameof(ValidateBranchName))]
@@ -14,27 +26,19 @@ public string Name
1426
set => SetProperty(ref _name, value, true);
1527
}
1628

17-
public string Prefix
18-
{
19-
get => _prefix;
20-
}
21-
22-
public bool IsFeature => _type == "feature";
23-
public bool IsRelease => _type == "release";
24-
public bool IsHotfix => _type == "hotfix";
25-
26-
public GitFlowStart(Repository repo, string type)
29+
public GitFlowStart(Repository repo, Models.GitFlowBranchType type)
2730
{
2831
_repo = repo;
29-
_type = type;
30-
_prefix = Commands.GitFlow.GetPrefix(repo.FullPath, type);
32+
33+
Type = type;
34+
Prefix = _repo.GitFlow.GetPrefix(type);
3135
}
3236

3337
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
3438
{
3539
if (ctx.ObjectInstance is GitFlowStart starter)
3640
{
37-
var check = $"{starter._prefix}{name}";
41+
var check = $"{starter.Prefix}{name}";
3842
foreach (var b in starter._repo.Branches)
3943
{
4044
if (b.FriendlyName == check)
@@ -48,23 +52,21 @@ public static ValidationResult ValidateBranchName(string name, ValidationContext
4852
public override Task<bool> Sure()
4953
{
5054
_repo.SetWatcherEnabled(false);
51-
ProgressDescription = $"Git Flow - starting {_type} {_name} ...";
55+
ProgressDescription = $"Git Flow - Start {Prefix}{_name} ...";
5256

53-
var log = _repo.CreateLog("Gitflow - Start");
57+
var log = _repo.CreateLog("GitFlow - Start");
5458
Use(log);
5559

5660
return Task.Run(() =>
5761
{
58-
var succ = Commands.GitFlow.Start(_repo.FullPath, _type, _name, log);
62+
var succ = Commands.GitFlow.Start(_repo.FullPath, Type, _name, log);
5963
log.Complete();
6064
CallUIThread(() => _repo.SetWatcherEnabled(true));
6165
return succ;
6266
});
6367
}
6468

6569
private readonly Repository _repo;
66-
private readonly string _type;
67-
private readonly string _prefix;
6870
private string _name = null;
6971
}
7072
}

0 commit comments

Comments
 (0)