-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Developer CLI command to easily amend commits with co-authors (#683)
### Summary & Motivation Introduce a new `amend-as-coauthor` CLI command to easily annotate a Git commit with a co-author. This simplifies the process of making small adjustments to commits while properly crediting maintainers. The command appends a `Co-authored-by:` line to the commit message, ensuring clean and traceable contributions in PlatformPlatform. ### Checklist - [x] I have added tests, or done manual regression tests - [x] I have updated the documentation, if necessary
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.NamingConventionBinder; | ||
using PlatformPlatform.DeveloperCli.Installation; | ||
using PlatformPlatform.DeveloperCli.Utilities; | ||
using Spectre.Console; | ||
|
||
namespace PlatformPlatform.DeveloperCli.Commands; | ||
|
||
public sealed class AmendAsCoAuthorCommand : Command | ||
{ | ||
private const string CoAuthorTrailer = "Co-authored-by"; | ||
|
||
public AmendAsCoAuthorCommand() : base("amend-as-coauthor", "Amends the current commit and adds you as a co-author") | ||
{ | ||
Handler = CommandHandler.Create(Execute); | ||
} | ||
|
||
private static int Execute() | ||
{ | ||
var gitUserName = ProcessHelper.StartProcess("git config user.name", Configuration.SourceCodeFolder, true).Trim(); | ||
var gitUserEmail = ProcessHelper.StartProcess("git config user.email", Configuration.SourceCodeFolder, true).Trim(); | ||
|
||
if (string.IsNullOrEmpty(gitUserName) || string.IsNullOrEmpty(gitUserEmail)) | ||
{ | ||
AnsiConsole.MarkupLine("[red]Git user name or email not configured.[/]"); | ||
Environment.Exit(1); | ||
} | ||
|
||
var commitAuthor = ProcessHelper.StartProcess( | ||
"git log -1 --format=\"%an <%ae>\"", Configuration.SourceCodeFolder, true, throwOnError: true | ||
).Trim(); | ||
var currentUser = $"{gitUserName} <{gitUserEmail}>"; | ||
if (commitAuthor == currentUser) | ||
{ | ||
AnsiConsole.MarkupLine("[yellow]You are already the author of this commit.[/]"); | ||
Environment.Exit(0); | ||
} | ||
|
||
var stagedChanges = ProcessHelper.StartProcess("git diff --cached --name-only", Configuration.SourceCodeFolder, true); | ||
var hasNoChanges = string.IsNullOrWhiteSpace(stagedChanges); | ||
|
||
var commitMessage = ProcessHelper.StartProcess("git log -1 --format=%B", Configuration.SourceCodeFolder, true).Trim(); | ||
var coAuthorLine = $"{CoAuthorTrailer}: {currentUser}"; | ||
var isAlreadyCoAuthor = commitMessage.Contains(coAuthorLine); | ||
|
||
if (hasNoChanges && isAlreadyCoAuthor) | ||
{ | ||
AnsiConsole.MarkupLine("[yellow]No staged changes, and you are already a co-author of this commit.[/]"); | ||
Environment.Exit(0); | ||
} | ||
|
||
if (hasNoChanges && !AnsiConsole.Confirm("No staged changes. Do you want to add co-author information only?")) | ||
{ | ||
Environment.Exit(0); | ||
} | ||
|
||
var amendMessage = isAlreadyCoAuthor ? "--no-edit" : $"-m \"{commitMessage.TrimEnd()}\n\n{coAuthorLine}\""; | ||
ProcessHelper.StartProcess($"git commit --amend {amendMessage}", Configuration.SourceCodeFolder); | ||
AnsiConsole.MarkupLine("[green]Successfully amended commit with co-author information.[/]"); | ||
|
||
return 0; | ||
} | ||
} |