Skip to content

Commit 3386cb1

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

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

src/Commands/GitFlow.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
using System.Collections.Generic;
2-
using System.Text;
3-
1+
using System.Text;
42
using Avalonia.Threading;
53

64
namespace SourceGit.Commands
75
{
86
public static class GitFlow
97
{
10-
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)
8+
public static bool Init(string repo, string master, string develop, string feature, string release, string hotfix, string version, Models.ICommandLog log)
119
{
12-
var current = branches.Find(x => x.IsCurrent);
13-
14-
var masterBranch = branches.Find(x => x.Name == master);
15-
if (masterBranch == null && current != null)
16-
Branch.Create(repo, master, current.Head, log);
17-
18-
var devBranch = branches.Find(x => x.Name == develop);
19-
if (devBranch == null && current != null)
20-
Branch.Create(repo, develop, current.Head, log);
21-
2210
var config = new Config(repo);
2311
config.Set("gitflow.branch.master", master);
2412
config.Set("gitflow.branch.develop", develop);

src/ViewModels/InitGitFlow.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Text.RegularExpressions;
45
using System.Threading.Tasks;
@@ -109,9 +110,35 @@ public override Task<bool> Sure()
109110

110111
return Task.Run(() =>
111112
{
112-
var succ = Commands.GitFlow.Init(
113+
var succ = false;
114+
var current = _repo.CurrentBranch;
115+
116+
var masterBranch = _repo.Branches.Find(x => x.IsLocal && x.Name.Equals(_master, StringComparison.Ordinal));
117+
if (masterBranch == null)
118+
{
119+
succ = Commands.Branch.Create(_repo.FullPath, _master, current.Head, log);
120+
if (!succ)
121+
{
122+
log.Complete();
123+
CallUIThread(() => _repo.SetWatcherEnabled(true));
124+
return false;
125+
}
126+
}
127+
128+
var developBranch = _repo.Branches.Find(x => x.IsLocal && x.Name.Equals(_develop, StringComparison.Ordinal));
129+
if (developBranch == null)
130+
{
131+
succ = Commands.Branch.Create(_repo.FullPath, _develop, current.Head, log);
132+
if (!succ)
133+
{
134+
log.Complete();
135+
CallUIThread(() => _repo.SetWatcherEnabled(true));
136+
return false;
137+
}
138+
}
139+
140+
succ = Commands.GitFlow.Init(
113141
_repo.FullPath,
114-
_repo.Branches,
115142
_master,
116143
_develop,
117144
_featurePrefix,

src/ViewModels/Repository.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,8 +1459,15 @@ public ContextMenu CreateContextMenuForGitFlow()
14591459
init.Icon = App.CreateMenuIcon("Icons.Init");
14601460
init.Click += (_, e) =>
14611461
{
1462-
if (CanCreatePopup())
1462+
if (_currentBranch == null)
1463+
{
1464+
App.RaiseException(_fullpath, "Git flow init failed: No branch found!!!");
1465+
}
1466+
else if (CanCreatePopup())
1467+
{
14631468
ShowPopup(new InitGitFlow(this));
1469+
}
1470+
14641471
e.Handled = true;
14651472
};
14661473
menu.Items.Add(init);

0 commit comments

Comments
 (0)