Skip to content

Commit 229e390

Browse files
authored
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
2 parents 02c1c81 + d20effd commit 229e390

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.CommandLine;
2+
using System.CommandLine.NamingConventionBinder;
3+
using PlatformPlatform.DeveloperCli.Installation;
4+
using PlatformPlatform.DeveloperCli.Utilities;
5+
using Spectre.Console;
6+
7+
namespace PlatformPlatform.DeveloperCli.Commands;
8+
9+
public sealed class AmendAsCoAuthorCommand : Command
10+
{
11+
private const string CoAuthorTrailer = "Co-authored-by";
12+
13+
public AmendAsCoAuthorCommand() : base("amend-as-coauthor", "Amends the current commit and adds you as a co-author")
14+
{
15+
Handler = CommandHandler.Create(Execute);
16+
}
17+
18+
private static int Execute()
19+
{
20+
var gitUserName = ProcessHelper.StartProcess("git config user.name", Configuration.SourceCodeFolder, true).Trim();
21+
var gitUserEmail = ProcessHelper.StartProcess("git config user.email", Configuration.SourceCodeFolder, true).Trim();
22+
23+
if (string.IsNullOrEmpty(gitUserName) || string.IsNullOrEmpty(gitUserEmail))
24+
{
25+
AnsiConsole.MarkupLine("[red]Git user name or email not configured.[/]");
26+
Environment.Exit(1);
27+
}
28+
29+
var commitAuthor = ProcessHelper.StartProcess(
30+
"git log -1 --format=\"%an <%ae>\"", Configuration.SourceCodeFolder, true, throwOnError: true
31+
).Trim();
32+
var currentUser = $"{gitUserName} <{gitUserEmail}>";
33+
if (commitAuthor == currentUser)
34+
{
35+
AnsiConsole.MarkupLine("[yellow]You are already the author of this commit.[/]");
36+
Environment.Exit(0);
37+
}
38+
39+
var stagedChanges = ProcessHelper.StartProcess("git diff --cached --name-only", Configuration.SourceCodeFolder, true);
40+
var hasNoChanges = string.IsNullOrWhiteSpace(stagedChanges);
41+
42+
var commitMessage = ProcessHelper.StartProcess("git log -1 --format=%B", Configuration.SourceCodeFolder, true).Trim();
43+
var coAuthorLine = $"{CoAuthorTrailer}: {currentUser}";
44+
var isAlreadyCoAuthor = commitMessage.Contains(coAuthorLine);
45+
46+
if (hasNoChanges && isAlreadyCoAuthor)
47+
{
48+
AnsiConsole.MarkupLine("[yellow]No staged changes, and you are already a co-author of this commit.[/]");
49+
Environment.Exit(0);
50+
}
51+
52+
if (hasNoChanges && !AnsiConsole.Confirm("No staged changes. Do you want to add co-author information only?"))
53+
{
54+
Environment.Exit(0);
55+
}
56+
57+
var amendMessage = isAlreadyCoAuthor ? "--no-edit" : $"-m \"{commitMessage.TrimEnd()}\n\n{coAuthorLine}\"";
58+
ProcessHelper.StartProcess($"git commit --amend {amendMessage}", Configuration.SourceCodeFolder);
59+
AnsiConsole.MarkupLine("[green]Successfully amended commit with co-author information.[/]");
60+
61+
return 0;
62+
}
63+
}

0 commit comments

Comments
 (0)