Skip to content

Commit 78056f7

Browse files
committed
Improve parent branch selection
1 parent 231920d commit 78056f7

3 files changed

Lines changed: 50 additions & 31 deletions

File tree

src/Stack/Commands/Branch/NewBranchCommand.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,28 @@ public override async Task Handle(NewBranchCommandInputs inputs)
6868
return;
6969
}
7070

71+
if (stackData.SchemaVersion == SchemaVersion.V1 && inputs.ParentBranchName is not null)
72+
{
73+
throw new InvalidOperationException("Parent branches are not supported in stacks with schema version v1. Please migrate the stack to v2 format.");
74+
}
75+
7176
var stack = inputProvider.SelectStack(logger, inputs.StackName, stacksForRemote, currentBranch);
7277

7378
if (stack is null)
7479
{
7580
throw new InvalidOperationException($"Stack '{inputs.StackName}' not found.");
7681
}
7782

78-
if (stackData.SchemaVersion == SchemaVersion.V1 && inputs.ParentBranchName is not null)
83+
var branchName = inputProvider.Text(logger, Questions.BranchName, inputs.BranchName, stack.GetDefaultBranchName());
84+
85+
if (stack.AllBranchNames.Contains(branchName))
7986
{
80-
throw new InvalidOperationException("Parent branches are not supported in stacks with schema version v1. Please migrate the stack to v2 format.");
87+
throw new InvalidOperationException($"Branch '{branchName}' already exists in stack '{stack.Name}'.");
88+
}
89+
90+
if (gitClient.DoesLocalBranchExist(branchName))
91+
{
92+
throw new InvalidOperationException($"Branch '{branchName}' already exists locally.");
8193
}
8294

8395
Branch? sourceBranch = null;
@@ -89,28 +101,18 @@ public override async Task Handle(NewBranchCommandInputs inputs)
89101
}
90102
if (stackData.SchemaVersion == SchemaVersion.V2)
91103
{
92-
var parentBranchName = inputs.ParentBranchName ?? inputProvider.SelectBranch(logger, null, [stack.SourceBranch, .. stack.AllBranchNames], Questions.SelectParentBranch);
104+
var parentBranchName = inputProvider.SelectParentBranch(logger, inputs.ParentBranchName, stack);
93105

94-
var flattenedBranches = stack.Branches.SelectMany(branch => MoreEnumerable.TraverseDepthFirst(branch, b => b.Children)).ToList();
95-
sourceBranch = flattenedBranches.FirstOrDefault(b => b.Name.Equals(parentBranchName, StringComparison.OrdinalIgnoreCase));
96-
if (sourceBranch is null)
106+
if (parentBranchName != stack.SourceBranch)
97107
{
98-
throw new InvalidOperationException($"Branch '{parentBranchName}' not found in stack '{stack.Name}'.");
108+
sourceBranch = stack.GetAllBranches().FirstOrDefault(b => b.Name.Equals(parentBranchName, StringComparison.OrdinalIgnoreCase));
109+
if (sourceBranch is null)
110+
{
111+
throw new InvalidOperationException($"Branch '{parentBranchName}' not found in stack '{stack.Name}'.");
112+
}
99113
}
100114
}
101115

102-
var branchName = inputProvider.Text(logger, Questions.BranchName, inputs.BranchName, stack.GetDefaultBranchName());
103-
104-
if (stack.AllBranchNames.Contains(branchName))
105-
{
106-
throw new InvalidOperationException($"Branch '{branchName}' already exists in stack '{stack.Name}'.");
107-
}
108-
109-
if (gitClient.DoesLocalBranchExist(branchName))
110-
{
111-
throw new InvalidOperationException($"Branch '{branchName}' already exists locally.");
112-
}
113-
114116
var sourceBranchName = sourceBranch?.Name ?? stack.SourceBranch;
115117

116118
logger.Information($"Creating branch {branchName.Branch()} from {sourceBranchName.Branch()} in stack {stack.Name.Stack()}");

src/Stack/Commands/Helpers/InputProviderExtensionMethods.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,33 @@ public static string SelectBranch(
8484
{
8585
return inputProvider.Select(logger, question, name, branches);
8686
}
87+
88+
public static string SelectParentBranch(
89+
this IInputProvider inputProvider,
90+
ILogger logger,
91+
string? name,
92+
Config.Stack stack)
93+
{
94+
void GetBranchNamesWithIndentation(Branch branch, List<string> names, int level = 0)
95+
{
96+
names.Add($"{new string(' ', level * 2)}{branch.Name}");
97+
foreach (var child in branch.Children)
98+
{
99+
GetBranchNamesWithIndentation(child, names, level + 1);
100+
}
101+
}
102+
103+
var allBranchNamesWithLevel = new List<string>();
104+
foreach (var branch in stack.Branches)
105+
{
106+
GetBranchNamesWithIndentation(branch, allBranchNamesWithLevel, 1);
107+
}
108+
109+
var branchSelection = name ?? inputProvider.Select(Questions.SelectParentBranch, [stack.SourceBranch, .. allBranchNamesWithLevel]).Trim();
110+
111+
logger.Information($"{Questions.SelectParentBranch} {branchSelection}");
112+
113+
return branchSelection;
114+
}
87115
}
88116

src/Stack/Config/Stack.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,7 @@ static List<Branch> GetAllBranches(Branch branch)
3636
return branchesToReturn;
3737
}
3838

39-
public List<string> AllBranchNames
40-
{
41-
get
42-
{
43-
var branches = new List<string>();
44-
foreach (var branch in Branches)
45-
{
46-
branches.AddRange(branch.AllBranchNames);
47-
}
48-
return [.. branches.Distinct()];
49-
}
50-
}
39+
public List<string> AllBranchNames => [.. GetAllBranches().Select(b => b.Name).Distinct()];
5140

5241
public bool HasSingleTree
5342
{

0 commit comments

Comments
 (0)