Repository navigation
Expand file tree
/
Copy pathGitFlow.cs
More file actions
108 lines (99 loc) · 4.24 KB
/
Copy pathGitFlow.cs
File metadata and controls
108 lines (99 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
namespace SourceGit.Models
{
public enum GitFlowVersion
{
None = 0,
Legacy,
Next,
}
public enum GitFlowBranchType
{
None = 0,
Feature,
Release,
Hotfix,
}
public class GitFlow
{
public string ProductionBranch { get; set; } = string.Empty;
public string DevelopmentBranch { get; set; } = string.Empty;
public string FeaturePrefix { get; set; } = string.Empty;
public string ReleasePrefix { get; set; } = string.Empty;
public string HotfixPrefix { get; set; } = string.Empty;
public void Parse(Dictionary<string, string> config)
{
// Reset to default values
ProductionBranch = string.Empty;
DevelopmentBranch = string.Empty;
FeaturePrefix = string.Empty;
ReleasePrefix = string.Empty;
HotfixPrefix = string.Empty;
// Try to parse `git-flow-next` style configuration first if the `git-flow-next` is installed.
if (Native.OS.GitFlowVersion == GitFlowVersion.Next &&
config.TryGetValue("gitflow.initialized", out var initialized) &&
initialized.Equals("true", StringComparison.OrdinalIgnoreCase))
{
foreach (var kv in config)
{
if (!kv.Key.StartsWith("gitflow.branch.", StringComparison.Ordinal))
continue;
if (kv.Key.EndsWith(".type", StringComparison.Ordinal) && kv.Value.Equals("base", StringComparison.Ordinal))
{
var b = kv.Key.Substring("gitflow.branch.".Length, kv.Key.Length - "gitflow.branch.".Length - ".type".Length);
if (config.ContainsKey($"gitflow.branch.{b}.parent"))
DevelopmentBranch = b;
else
ProductionBranch = b;
}
else if (kv.Key.EndsWith(".prefix", StringComparison.Ordinal))
{
var t = kv.Key.Substring("gitflow.branch.".Length, kv.Key.Length - "gitflow.branch.".Length - ".prefix".Length);
if (t.Equals("feature", StringComparison.Ordinal))
FeaturePrefix = kv.Value;
else if (t.Equals("release", StringComparison.Ordinal))
ReleasePrefix = kv.Value;
else if (t.Equals("hotfix", StringComparison.Ordinal))
HotfixPrefix = kv.Value;
}
}
}
// Fall back to `git-flow` style configuration if `git-flow-next` style is not valid.
if (!IsValid)
{
if (config.TryGetValue("gitflow.branch.master", out var masterName))
ProductionBranch = masterName;
if (config.TryGetValue("gitflow.branch.develop", out var developName))
DevelopmentBranch = developName;
if (config.TryGetValue("gitflow.prefix.feature", out var featurePrefix))
FeaturePrefix = featurePrefix;
if (config.TryGetValue("gitflow.prefix.release", out var releasePrefix))
ReleasePrefix = releasePrefix;
if (config.TryGetValue("gitflow.prefix.hotfix", out var hotfixPrefix))
HotfixPrefix = hotfixPrefix;
}
}
public bool IsValid
{
get
{
return !string.IsNullOrEmpty(ProductionBranch) &&
!string.IsNullOrEmpty(DevelopmentBranch) &&
!string.IsNullOrEmpty(FeaturePrefix) &&
!string.IsNullOrEmpty(ReleasePrefix) &&
!string.IsNullOrEmpty(HotfixPrefix);
}
}
public string GetPrefix(GitFlowBranchType type)
{
return type switch
{
GitFlowBranchType.Feature => FeaturePrefix,
GitFlowBranchType.Release => ReleasePrefix,
GitFlowBranchType.Hotfix => HotfixPrefix,
_ => string.Empty,
};
}
}
}