Skip to content

Commit 6de92bb

Browse files
committed
refactor: git-flow supports.
1 parent 5bb41ed commit 6de92bb

File tree

7 files changed

+192
-192
lines changed

7 files changed

+192
-192
lines changed

src/Commands/GitFlow.cs

Lines changed: 132 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,164 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
using Avalonia.Threading;
45

56
namespace SourceGit.Commands
67
{
7-
public class GitFlow : Command
8+
public static class GitFlow
89
{
9-
public GitFlow(string repo)
10+
public class BranchDetectResult
1011
{
11-
WorkingDirectory = repo;
12-
Context = repo;
12+
public bool IsGitFlowBranch { get; set; } = false;
13+
public string Type { get; set; } = string.Empty;
14+
public string Prefix { get; set; } = string.Empty;
1315
}
1416

15-
public bool Init(List<Models.Branch> branches, string master, string develop, string feature, string release, string hotfix, string version)
17+
public static bool IsEnabled(string repo, List<Models.Branch> branches)
18+
{
19+
var localBrancheNames = new HashSet<string>();
20+
foreach (var branch in branches)
21+
{
22+
if (branch.IsLocal)
23+
localBrancheNames.Add(branch.Name);
24+
}
25+
26+
var config = new Config(repo).ListAll();
27+
if (!config.TryGetValue("gitflow.branch.master", out string master) || !localBrancheNames.Contains(master))
28+
return false;
29+
30+
if (!config.TryGetValue("gitflow.branch.develop", out string develop) || !localBrancheNames.Contains(develop))
31+
return false;
32+
33+
return config.ContainsKey("gitflow.prefix.feature") &&
34+
config.ContainsKey("gitflow.prefix.release") &&
35+
config.ContainsKey("gitflow.prefix.hotfix");
36+
}
37+
38+
public static bool Init(string repo, List<Models.Branch> branches, string master, string develop, string feature, string release, string hotfix, string version)
1639
{
1740
var current = branches.Find(x => x.IsCurrent);
1841

1942
var masterBranch = branches.Find(x => x.Name == master);
2043
if (masterBranch == null && current != null)
21-
Branch.Create(WorkingDirectory, master, current.Head);
44+
Branch.Create(repo, master, current.Head);
2245

2346
var devBranch = branches.Find(x => x.Name == develop);
2447
if (devBranch == null && current != null)
25-
Branch.Create(WorkingDirectory, develop, current.Head);
26-
27-
var cmd = new Config(WorkingDirectory);
28-
cmd.Set("gitflow.branch.master", master);
29-
cmd.Set("gitflow.branch.develop", develop);
30-
cmd.Set("gitflow.prefix.feature", feature);
31-
cmd.Set("gitflow.prefix.bugfix", "bugfix/");
32-
cmd.Set("gitflow.prefix.release", release);
33-
cmd.Set("gitflow.prefix.hotfix", hotfix);
34-
cmd.Set("gitflow.prefix.support", "support/");
35-
cmd.Set("gitflow.prefix.versiontag", version, true);
36-
37-
Args = "flow init -d";
38-
return Exec();
48+
Branch.Create(repo, develop, current.Head);
49+
50+
var config = new Config(repo);
51+
config.Set("gitflow.branch.master", master);
52+
config.Set("gitflow.branch.develop", develop);
53+
config.Set("gitflow.prefix.feature", feature);
54+
config.Set("gitflow.prefix.bugfix", "bugfix/");
55+
config.Set("gitflow.prefix.release", release);
56+
config.Set("gitflow.prefix.hotfix", hotfix);
57+
config.Set("gitflow.prefix.support", "support/");
58+
config.Set("gitflow.prefix.versiontag", version, true);
59+
60+
var init = new Command();
61+
init.WorkingDirectory = repo;
62+
init.Context = repo;
63+
init.Args = "flow init -d";
64+
return init.Exec();
3965
}
4066

41-
public bool Start(Models.GitFlowBranchType type, string name)
67+
public static string Prefix(string repo, string type)
4268
{
43-
switch (type)
69+
return new Config(repo).Get($"gitflow.prefix.{type}");
70+
}
71+
72+
public static BranchDetectResult DetectType(string repo, List<Models.Branch> branches, string branch)
73+
{
74+
var rs = new BranchDetectResult();
75+
var localBrancheNames = new HashSet<string>();
76+
foreach (var b in branches)
4477
{
45-
case Models.GitFlowBranchType.Feature:
46-
Args = $"flow feature start {name}";
47-
break;
48-
case Models.GitFlowBranchType.Release:
49-
Args = $"flow release start {name}";
50-
break;
51-
case Models.GitFlowBranchType.Hotfix:
52-
Args = $"flow hotfix start {name}";
53-
break;
54-
default:
55-
Dispatcher.UIThread.Invoke(() =>
56-
{
57-
App.RaiseException(Context, "Bad branch type!!!");
58-
});
59-
return false;
78+
if (b.IsLocal)
79+
localBrancheNames.Add(b.Name);
6080
}
6181

62-
return Exec();
82+
var config = new Config(repo).ListAll();
83+
if (!config.TryGetValue("gitflow.branch.master", out string master) || !localBrancheNames.Contains(master))
84+
return rs;
85+
86+
if (!config.TryGetValue("gitflow.branch.develop", out string develop) || !localBrancheNames.Contains(develop))
87+
return rs;
88+
89+
if (!config.TryGetValue("gitflow.prefix.feature", out var feature) ||
90+
!config.TryGetValue("gitflow.prefix.release", out var release) ||
91+
!config.TryGetValue("gitflow.prefix.hotfix", out var hotfix))
92+
return rs;
93+
94+
if (branch.StartsWith(feature, StringComparison.Ordinal))
95+
{
96+
rs.IsGitFlowBranch = true;
97+
rs.Type = "feature";
98+
rs.Prefix = feature;
99+
}
100+
else if (branch.StartsWith(release, StringComparison.Ordinal))
101+
{
102+
rs.IsGitFlowBranch = true;
103+
rs.Type = "release";
104+
rs.Prefix = release;
105+
}
106+
else if (branch.StartsWith(hotfix, StringComparison.Ordinal))
107+
{
108+
rs.IsGitFlowBranch = true;
109+
rs.Type = "hotfix";
110+
rs.Prefix = hotfix;
111+
}
112+
113+
return rs;
63114
}
64115

65-
public bool Finish(Models.GitFlowBranchType type, string name, bool keepBranch)
116+
public static bool Start(string repo, string type, string name)
66117
{
67-
var option = keepBranch ? "-k" : string.Empty;
68-
switch (type)
118+
if (!SUPPORTED_BRANCH_TYPES.Contains(type))
69119
{
70-
case Models.GitFlowBranchType.Feature:
71-
Args = $"flow feature finish {option} {name}";
72-
break;
73-
case Models.GitFlowBranchType.Release:
74-
Args = $"flow release finish {option} {name} -m \"RELEASE_DONE\"";
75-
break;
76-
case Models.GitFlowBranchType.Hotfix:
77-
Args = $"flow hotfix finish {option} {name} -m \"HOTFIX_DONE\"";
78-
break;
79-
default:
80-
Dispatcher.UIThread.Invoke(() =>
81-
{
82-
App.RaiseException(Context, "Bad branch type!!!");
83-
});
84-
return false;
120+
Dispatcher.UIThread.Post(() =>
121+
{
122+
App.RaiseException(repo, "Bad branch type!!!");
123+
});
124+
125+
return false;
85126
}
86127

87-
return Exec();
128+
var start = new Command();
129+
start.WorkingDirectory = repo;
130+
start.Context = repo;
131+
start.Args = $"flow {type} start {name}";
132+
return start.Exec();
88133
}
134+
135+
public static bool Finish(string repo, string type, string name, bool keepBranch)
136+
{
137+
if (!SUPPORTED_BRANCH_TYPES.Contains(type))
138+
{
139+
Dispatcher.UIThread.Post(() =>
140+
{
141+
App.RaiseException(repo, "Bad branch type!!!");
142+
});
143+
144+
return false;
145+
}
146+
147+
var option = keepBranch ? "-k" : string.Empty;
148+
var finish = new Command();
149+
finish.WorkingDirectory = repo;
150+
finish.Context = repo;
151+
finish.Args = $"flow {type} finish {option} {name}";
152+
return finish.Exec();
153+
}
154+
155+
private static readonly List<string> SUPPORTED_BRANCH_TYPES = new List<string>()
156+
{
157+
"feature",
158+
"release",
159+
"bugfix",
160+
"hotfix",
161+
"support",
162+
};
89163
}
90164
}

src/Models/GitFlow.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/ViewModels/GitFlowFinish.cs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ namespace SourceGit.ViewModels
44
{
55
public class GitFlowFinish : Popup
66
{
7-
public Models.Branch Branch => _branch;
8-
public bool IsFeature => _type == Models.GitFlowBranchType.Feature;
9-
public bool IsRelease => _type == Models.GitFlowBranchType.Release;
10-
public bool IsHotfix => _type == Models.GitFlowBranchType.Hotfix;
7+
public Models.Branch Branch
8+
{
9+
get;
10+
set;
11+
} = null;
12+
13+
public bool IsFeature => _type == "feature";
14+
public bool IsRelease => _type == "release";
15+
public bool IsHotfix => _type == "hotfix";
1116

1217
public bool KeepBranch
1318
{
1419
get;
1520
set;
1621
} = false;
1722

18-
public GitFlowFinish(Repository repo, Models.Branch branch, Models.GitFlowBranchType type)
23+
public GitFlowFinish(Repository repo, Models.Branch branch, string type, string prefix)
1924
{
2025
_repo = repo;
21-
_branch = branch;
2226
_type = type;
27+
_prefix = prefix;
28+
29+
Branch = branch;
2330
View = new Views.GitFlowFinish() { DataContext = this };
2431
}
2532

@@ -28,29 +35,16 @@ public override Task<bool> Sure()
2835
_repo.SetWatcherEnabled(false);
2936
return Task.Run(() =>
3037
{
31-
var branch = _branch.Name;
32-
switch (_type)
33-
{
34-
case Models.GitFlowBranchType.Feature:
35-
branch = branch.Substring(_repo.GitFlow.Feature.Length);
36-
break;
37-
case Models.GitFlowBranchType.Release:
38-
branch = branch.Substring(_repo.GitFlow.Release.Length);
39-
break;
40-
default:
41-
branch = branch.Substring(_repo.GitFlow.Hotfix.Length);
42-
break;
43-
}
44-
45-
SetProgressDescription($"Git Flow - finishing {_branch.Name} ...");
46-
var succ = new Commands.GitFlow(_repo.FullPath).Finish(_type, branch, KeepBranch);
38+
var name = Branch.Name.StartsWith(_prefix) ? Branch.Name.Substring(_prefix.Length) : Branch.Name;
39+
SetProgressDescription($"Git Flow - finishing {_type} {name} ...");
40+
var succ = Commands.GitFlow.Finish(_repo.FullPath, _type, name, KeepBranch);
4741
CallUIThread(() => _repo.SetWatcherEnabled(true));
4842
return succ;
4943
});
5044
}
5145

5246
private readonly Repository _repo = null;
53-
private readonly Models.Branch _branch = null;
54-
private readonly Models.GitFlowBranchType _type = Models.GitFlowBranchType.None;
47+
private readonly string _type = "feature";
48+
private readonly string _prefix = string.Empty;
5549
}
5650
}

src/ViewModels/GitFlowStart.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,15 @@ public string Prefix
1919
get => _prefix;
2020
}
2121

22-
public bool IsFeature => _type == Models.GitFlowBranchType.Feature;
23-
public bool IsRelease => _type == Models.GitFlowBranchType.Release;
24-
public bool IsHotfix => _type == Models.GitFlowBranchType.Hotfix;
22+
public bool IsFeature => _type == "feature";
23+
public bool IsRelease => _type == "release";
24+
public bool IsHotfix => _type == "hotfix";
2525

26-
public GitFlowStart(Repository repo, Models.GitFlowBranchType type)
26+
public GitFlowStart(Repository repo, string type)
2727
{
2828
_repo = repo;
2929
_type = type;
30-
31-
switch (type)
32-
{
33-
case Models.GitFlowBranchType.Feature:
34-
_prefix = repo.GitFlow.Feature;
35-
break;
36-
case Models.GitFlowBranchType.Release:
37-
_prefix = repo.GitFlow.Release;
38-
break;
39-
default:
40-
_prefix = repo.GitFlow.Hotfix;
41-
break;
42-
}
30+
_prefix = Commands.GitFlow.Prefix(repo.FullPath, type);
4331

4432
View = new Views.GitFlowStart() { DataContext = this };
4533
}
@@ -65,15 +53,15 @@ public override Task<bool> Sure()
6553
_repo.SetWatcherEnabled(false);
6654
return Task.Run(() =>
6755
{
68-
SetProgressDescription($"Git Flow - starting {_prefix}{_name} ...");
69-
var succ = new Commands.GitFlow(_repo.FullPath).Start(_type, _name);
56+
SetProgressDescription($"Git Flow - starting {_type} {_name} ...");
57+
var succ = Commands.GitFlow.Start(_repo.FullPath, _type, _name);
7058
CallUIThread(() => _repo.SetWatcherEnabled(true));
7159
return succ;
7260
});
7361
}
7462

7563
private readonly Repository _repo = null;
76-
private readonly Models.GitFlowBranchType _type = Models.GitFlowBranchType.Feature;
64+
private readonly string _type = "feature";
7765
private readonly string _prefix = string.Empty;
7866
private string _name = null;
7967
}

0 commit comments

Comments
 (0)