Repository navigation
Expand file tree
/
Copy pathInitGitFlow.cs
More file actions
169 lines (144 loc) · 5.81 KB
/
Copy pathInitGitFlow.cs
File metadata and controls
169 lines (144 loc) · 5.81 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public partial class InitGitFlow : Popup
{
[GeneratedRegex(@"^[\w\-/\.]+$")]
private static partial Regex REG_TAG_PREFIX();
[Required(ErrorMessage = "Production branch name is required!!!")]
[CustomValidation(typeof(InitGitFlow), nameof(ValidateBaseBranch))]
public string Production
{
get => _production;
set => SetProperty(ref _production, value, true);
}
[Required(ErrorMessage = "Development branch name is required!!!")]
[CustomValidation(typeof(InitGitFlow), nameof(ValidateBaseBranch))]
public string Develop
{
get => _develop;
set => SetProperty(ref _develop, value, true);
}
[Required(ErrorMessage = "Feature prefix is required!!!")]
[RegularExpression(@"^[\w\-\.]+/$", ErrorMessage = "Bad feature prefix format!")]
public string FeaturePrefix
{
get => _featurePrefix;
set => SetProperty(ref _featurePrefix, value, true);
}
[Required(ErrorMessage = "Release prefix is required!!!")]
[RegularExpression(@"^[\w\-\.]+/$", ErrorMessage = "Bad release prefix format!")]
public string ReleasePrefix
{
get => _releasePrefix;
set => SetProperty(ref _releasePrefix, value, true);
}
[Required(ErrorMessage = "Hotfix prefix is required!!!")]
[RegularExpression(@"^[\w\-\.]+/$", ErrorMessage = "Bad hotfix prefix format!")]
public string HotfixPrefix
{
get => _hotfixPrefix;
set => SetProperty(ref _hotfixPrefix, value, true);
}
[CustomValidation(typeof(InitGitFlow), nameof(ValidateTagPrefix))]
public string TagPrefix
{
get => _tagPrefix;
set => SetProperty(ref _tagPrefix, value, true);
}
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"))
_production = "master";
else if (localBranches.Contains("main"))
_production = "main";
else if (localBranches.Count > 0)
_production = localBranches[0];
else
_production = "main";
}
public static ValidationResult ValidateBaseBranch(string name, ValidationContext ctx)
{
if (ctx.ObjectInstance is InitGitFlow initializer)
{
if (!Models.RefName.IsValidBranchName(name))
return new ValidationResult("Bad branch name format!");
if (initializer._production == initializer._develop)
return new ValidationResult("Develop branch has the same name with production branch!");
}
return ValidationResult.Success;
}
public static ValidationResult ValidateTagPrefix(string tagPrefix, ValidationContext ctx)
{
if (!string.IsNullOrWhiteSpace(tagPrefix) && !REG_TAG_PREFIX().IsMatch(tagPrefix))
return new ValidationResult("Bad tag prefix format!");
return ValidationResult.Success;
}
public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();
ProgressDescription = "Init git-flow ...";
var log = _repo.CreateLog("Gitflow - Init");
Use(log);
bool succ;
var current = _repo.CurrentBranch;
var productionBranch = _repo.Branches.Find(x => x.IsLocal && x.Name.Equals(_production, StringComparison.Ordinal));
if (productionBranch == null)
{
succ = await new Commands.Branch(_repo.FullPath, _production)
.Use(log)
.CreateAsync(current.Head, true);
if (!succ)
{
log.Complete();
return false;
}
}
var developBranch = _repo.Branches.Find(x => x.IsLocal && x.Name.Equals(_develop, StringComparison.Ordinal));
if (developBranch == null)
{
succ = await new Commands.Branch(_repo.FullPath, _develop)
.Use(log)
.CreateAsync(current.Head, true);
if (!succ)
{
log.Complete();
return false;
}
}
succ = await new Commands.GitFlow(_repo.FullPath)
.Use(log)
.InitAsync(_production, _develop, _featurePrefix, _releasePrefix, _hotfixPrefix, _tagPrefix);
log.Complete();
if (succ)
{
var gitflow = new Models.GitFlow();
gitflow.ProductionBranch = _production;
gitflow.DevelopmentBranch = _develop;
gitflow.FeaturePrefix = _featurePrefix;
gitflow.ReleasePrefix = _releasePrefix;
gitflow.HotfixPrefix = _hotfixPrefix;
_repo.GitFlow = gitflow;
}
return succ;
}
private readonly Repository _repo;
private string _production;
private string _develop = "develop";
private string _featurePrefix = "feature/";
private string _releasePrefix = "release/";
private string _hotfixPrefix = "hotfix/";
private string _tagPrefix = string.Empty;
}
}