From ce363a6caeea5cc6242fa8449c9c18ccd53322e7 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Wed, 13 Nov 2024 08:29:24 +1100 Subject: [PATCH 1/2] Move branch related commands together (#23) --- src/Stack/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Stack/Program.cs b/src/Stack/Program.cs index 79321ac..bd5f175 100644 --- a/src/Stack/Program.cs +++ b/src/Stack/Program.cs @@ -23,13 +23,13 @@ // Stack commands configure.AddCommand("new").WithDescription("Creates a new stack."); - configure.AddCommand("list").WithDescription("Lists all stacks."); + configure.AddCommand("list").WithDescription("List stacks."); configure.AddCommand("status").WithDescription("Shows the status of a stack."); - configure.AddCommand("switch").WithDescription("Switches to a branch in a stack."); configure.AddCommand("delete").WithDescription("Deletes a stack."); - configure.AddCommand("update").WithDescription("Updates the branches in a stack."); // Branch commands + configure.AddCommand("switch").WithDescription("Switches to a branch in a stack."); + configure.AddCommand("update").WithDescription("Updates the branches in a stack."); configure.AddBranch("branch", branch => { branch.SetDescription("Manages branches in a stack."); From 985a394bace5eb2b6144b983805a424f0c813acf Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Wed, 13 Nov 2024 09:05:22 +1100 Subject: [PATCH 2/2] Adds `--working-dir` option (#24) --- src/Stack/Commands/Branch/BranchCommand.cs | 18 +++++++- .../Commands/Helpers/CommandSettingsBase.cs | 8 +++- .../Helpers/DryRunCommandSettingsBase.cs | 2 +- src/Stack/Commands/Stack/NewStackCommand.cs | 2 +- src/Stack/Git/GitHubOperations.cs | 6 +-- src/Stack/Git/GitOperations.cs | 44 ++++++------------- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/Stack/Commands/Branch/BranchCommand.cs b/src/Stack/Commands/Branch/BranchCommand.cs index 2c48286..966d03b 100644 --- a/src/Stack/Commands/Branch/BranchCommand.cs +++ b/src/Stack/Commands/Branch/BranchCommand.cs @@ -68,11 +68,25 @@ public override async Task ExecuteAsync(CommandContext context, BranchComma if (action == BranchAction.Add) { - return await new AddBranchCommand(console, gitOperations, stackConfig).ExecuteAsync(context, new AddBranchCommandSettings { Stack = stack.Name, Name = settings.Name, DryRun = settings.DryRun, Verbose = settings.Verbose }); + return await new AddBranchCommand(console, gitOperations, stackConfig).ExecuteAsync(context, new AddBranchCommandSettings + { + Stack = stack.Name, + Name = settings.Name, + DryRun = settings.DryRun, + Verbose = settings.Verbose, + WorkingDirectory = settings.WorkingDirectory + }); } else { - return await new NewBranchCommand(console, gitOperations, stackConfig).ExecuteAsync(context, new NewBranchCommandSettings { Stack = stack.Name, Name = settings.Name, DryRun = settings.DryRun, Verbose = settings.Verbose }); + return await new NewBranchCommand(console, gitOperations, stackConfig).ExecuteAsync(context, new NewBranchCommandSettings + { + Stack = stack.Name, + Name = settings.Name, + DryRun = settings.DryRun, + Verbose = settings.Verbose, + WorkingDirectory = settings.WorkingDirectory + }); } } } diff --git a/src/Stack/Commands/Helpers/CommandSettingsBase.cs b/src/Stack/Commands/Helpers/CommandSettingsBase.cs index a39c76f..a0cc9c5 100644 --- a/src/Stack/Commands/Helpers/CommandSettingsBase.cs +++ b/src/Stack/Commands/Helpers/CommandSettingsBase.cs @@ -11,6 +11,10 @@ internal class CommandSettingsBase : CommandSettings [DefaultValue(false)] public bool Verbose { get; init; } - public virtual GitOperationSettings GetGitOperationSettings() => new(false, Verbose); - public virtual GitHubOperationSettings GetGitHubOperationSettings() => new(false, Verbose); + [Description("The path to the directory containing the git repository. Defaults to the current directory.")] + [CommandOption("--working-dir")] + public string? WorkingDirectory { get; init; } + + public virtual GitOperationSettings GetGitOperationSettings() => new(false, Verbose, WorkingDirectory); + public virtual GitHubOperationSettings GetGitHubOperationSettings() => new(false, Verbose, WorkingDirectory); } diff --git a/src/Stack/Commands/Helpers/DryRunCommandSettingsBase.cs b/src/Stack/Commands/Helpers/DryRunCommandSettingsBase.cs index 9610c26..dc14d1b 100644 --- a/src/Stack/Commands/Helpers/DryRunCommandSettingsBase.cs +++ b/src/Stack/Commands/Helpers/DryRunCommandSettingsBase.cs @@ -12,5 +12,5 @@ internal class DryRunCommandSettingsBase : CommandSettingsBase [DefaultValue(false)] public bool DryRun { get; init; } - public override GitOperationSettings GetGitOperationSettings() => new(DryRun, Verbose); + public override GitOperationSettings GetGitOperationSettings() => new(DryRun, Verbose, WorkingDirectory); } diff --git a/src/Stack/Commands/Stack/NewStackCommand.cs b/src/Stack/Commands/Stack/NewStackCommand.cs index 3d50405..eb0930f 100644 --- a/src/Stack/Commands/Stack/NewStackCommand.cs +++ b/src/Stack/Commands/Stack/NewStackCommand.cs @@ -53,7 +53,7 @@ public override async Task ExecuteAsync(CommandContext context, NewStackCom if (console.Prompt(new ConfirmationPrompt("Do you want to add an existing branch or create a new branch and add it to the stack?"))) { - return await new BranchCommand(console, gitOperations, stackConfig).ExecuteAsync(context, new BranchCommandSettings { Stack = name, Verbose = settings.Verbose }); + return await new BranchCommand(console, gitOperations, stackConfig).ExecuteAsync(context, new BranchCommandSettings { Stack = name, Verbose = settings.Verbose, WorkingDirectory = settings.WorkingDirectory }); } return 0; diff --git a/src/Stack/Git/GitHubOperations.cs b/src/Stack/Git/GitHubOperations.cs index fd963d3..4cf8047 100644 --- a/src/Stack/Git/GitHubOperations.cs +++ b/src/Stack/Git/GitHubOperations.cs @@ -4,9 +4,9 @@ namespace Stack.Git; -internal record GitHubOperationSettings(bool DryRun, bool Verbose) +internal record GitHubOperationSettings(bool DryRun, bool Verbose, string? WorkingDirectory) { - public static GitHubOperationSettings Default => new GitHubOperationSettings(false, false); + public static GitHubOperationSettings Default => new(false, false, null); } internal static class GitHubPullRequestStates @@ -45,7 +45,7 @@ private string ExecuteGitHubCommandAndReturnOutput(string command, GitHubOperati var result = ShellExecutor.ExecuteCommand( "gh", command, - ".", + settings.WorkingDirectory ?? ".", (_) => { }, (info) => infoBuilder.AppendLine(info), (error) => errorBuilder.AppendLine(error)); diff --git a/src/Stack/Git/GitOperations.cs b/src/Stack/Git/GitOperations.cs index e2d23c6..a81dfb2 100644 --- a/src/Stack/Git/GitOperations.cs +++ b/src/Stack/Git/GitOperations.cs @@ -4,9 +4,9 @@ namespace Stack.Git; -internal record GitOperationSettings(bool DryRun, bool Verbose) +internal record GitOperationSettings(bool DryRun, bool Verbose, string? WorkingDirectory) { - public static GitOperationSettings Default => new GitOperationSettings(false, false); + public static GitOperationSettings Default => new(false, false, null); } @@ -151,7 +151,7 @@ private string ExecuteGitCommandAndReturnOutput(string command, GitOperationSett var result = ShellExecutor.ExecuteCommand( "git", command, - ".", + settings.WorkingDirectory ?? ".", (_) => { }, (info) => infoBuilder.AppendLine(info), (error) => errorBuilder.AppendLine(error)); @@ -172,38 +172,22 @@ private string ExecuteGitCommandAndReturnOutput(string command, GitOperationSett private void ExecuteGitCommand(string command, GitOperationSettings settings) { - if (settings.Verbose) - console.MarkupLine($"[grey]git {command}[/]"); - - if (!settings.DryRun) - { - ExecuteGitCommandInternal(command); - } - } - - private void ExecuteGitCommandInternal(string command) - { - var infoBuilder = new StringBuilder(); - var errorBuilder = new StringBuilder(); - - var result = ShellExecutor.ExecuteCommand( - "git", - command, - ".", - (_) => { }, - (info) => infoBuilder.AppendLine(info), - (error) => errorBuilder.AppendLine(error)); - - if (result != 0) + if (settings.DryRun) { - console.MarkupLine($"[red]{errorBuilder}[/]"); - throw new Exception("Failed to execute git command."); + if (settings.Verbose) + console.MarkupLine($"[grey]git {command}[/]"); } else { - if (infoBuilder.Length > 0) + var output = ExecuteGitCommandAndReturnOutput(command, settings); + + if (!settings.Verbose && output.Length > 0) { - console.WriteLine(infoBuilder.ToString()); + // We want to write the output of commands that perform + // changes to the Git repository as the output might be important. + // In verbose mode we would have already written the output + // of the command so don't write it again. + console.WriteLine(output); } } }