Skip to content

Commit

Permalink
Merge branch 'main' into create-github-prs
Browse files Browse the repository at this point in the history
  • Loading branch information
geofflamrock committed Nov 12, 2024
2 parents 860f875 + 985a394 commit 715d924
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 43 deletions.
18 changes: 16 additions & 2 deletions src/Stack/Commands/Branch/BranchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,25 @@ public override async Task<int> 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
});
}
}
}
8 changes: 6 additions & 2 deletions src/Stack/Commands/Helpers/CommandSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/Stack/Commands/Helpers/DryRunCommandSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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);

public override GitHubOperationSettings GetGitHubOperationSettings() => new(DryRun, Verbose);
public override GitHubOperationSettings GetGitHubOperationSettings() => new(DryRun, Verbose, WorkingDirectory);
}
2 changes: 1 addition & 1 deletion src/Stack/Commands/Stack/NewStackCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override async Task<int> 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;
Expand Down
6 changes: 3 additions & 3 deletions src/Stack/Git/GitHubOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,7 +78,7 @@ private string ExecuteGitHubCommandAndReturnOutput(string command, GitHubOperati
var result = ShellExecutor.ExecuteCommand(
"gh",
command,
".",
settings.WorkingDirectory ?? ".",
(_) => { },
(info) => infoBuilder.AppendLine(info),
(error) => errorBuilder.AppendLine(error));
Expand Down
44 changes: 14 additions & 30 deletions src/Stack/Git/GitOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Stack/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

// Stack commands
configure.AddCommand<NewStackCommand>("new").WithDescription("Creates a new stack.");
configure.AddCommand<ListStacksCommand>("list").WithDescription("Lists all stacks.");
configure.AddCommand<ListStacksCommand>("list").WithDescription("List stacks.");
configure.AddCommand<StackStatusCommand>("status").WithDescription("Shows the status of a stack.");
configure.AddCommand<StackSwitchCommand>("switch").WithDescription("Switches to a branch in a stack.");
configure.AddCommand<DeleteStackCommand>("delete").WithDescription("Deletes a stack.");
configure.AddCommand<UpdateStackCommand>("update").WithDescription("Updates the branches in a stack.");

// Branch commands
configure.AddCommand<StackSwitchCommand>("switch").WithDescription("Switches to a branch in a stack.");
configure.AddCommand<UpdateStackCommand>("update").WithDescription("Updates the branches in a stack.");
configure.AddBranch("branch", branch =>
{
branch.SetDescription("Manages branches in a stack.");
Expand Down

0 comments on commit 715d924

Please sign in to comment.