|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 |
|
3 | 4 | using Avalonia.Threading;
|
4 | 5 |
|
5 | 6 | namespace SourceGit.Commands
|
6 | 7 | {
|
7 |
| - public class GitFlow : Command |
| 8 | + public static class GitFlow |
8 | 9 | {
|
9 |
| - public GitFlow(string repo) |
| 10 | + public class BranchDetectResult |
10 | 11 | {
|
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; |
13 | 15 | }
|
14 | 16 |
|
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) |
16 | 39 | {
|
17 | 40 | var current = branches.Find(x => x.IsCurrent);
|
18 | 41 |
|
19 | 42 | var masterBranch = branches.Find(x => x.Name == master);
|
20 | 43 | if (masterBranch == null && current != null)
|
21 |
| - Branch.Create(WorkingDirectory, master, current.Head); |
| 44 | + Branch.Create(repo, master, current.Head); |
22 | 45 |
|
23 | 46 | var devBranch = branches.Find(x => x.Name == develop);
|
24 | 47 | 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(); |
39 | 65 | }
|
40 | 66 |
|
41 |
| - public bool Start(Models.GitFlowBranchType type, string name) |
| 67 | + public static string Prefix(string repo, string type) |
42 | 68 | {
|
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) |
44 | 77 | {
|
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); |
60 | 80 | }
|
61 | 81 |
|
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; |
63 | 114 | }
|
64 | 115 |
|
65 |
| - public bool Finish(Models.GitFlowBranchType type, string name, bool keepBranch) |
| 116 | + public static bool Start(string repo, string type, string name) |
66 | 117 | {
|
67 |
| - var option = keepBranch ? "-k" : string.Empty; |
68 |
| - switch (type) |
| 118 | + if (!SUPPORTED_BRANCH_TYPES.Contains(type)) |
69 | 119 | {
|
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; |
85 | 126 | }
|
86 | 127 |
|
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(); |
88 | 133 | }
|
| 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 | + }; |
89 | 163 | }
|
90 | 164 | }
|
0 commit comments