From ffea230f9a4b2fb5bd50a06fe0cfcdcd98e3a51c Mon Sep 17 00:00:00 2001 From: daveberrys Date: Thu, 12 Nov 2026 17:11:08 +0800 Subject: [PATCH] add signoff cli Signed-off-by: daveberrys --- DOCS.md | 24 ++++++++++ main.go | 5 +- src/cmd/push/root.go | 20 ++++---- src/cmd/signoff/root.go | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 src/cmd/signoff/root.go diff --git a/DOCS.md b/DOCS.md index 9fb84a9..6cf7a5b 100644 --- a/DOCS.md +++ b/DOCS.md @@ -30,6 +30,7 @@ - [`logs`](#logs) - [`pull`](#pull) - [`push`](#push) +- [`signoff`](#signoff) - [`stash`](#stash) - [`undo`](#undo) @@ -360,6 +361,29 @@ When `all` is given, pushes to every remote returned by `git remote`. The first --- +## `signoff` + +Configure Git to sign commits with an SSH key. + +``` +guh signoff +``` + +Creates an ed25519 SSH key at `~/.ssh/id_ed25519` if one does not already exist, then configures Git with: + +``` +git config --global gpg.format ssh +git config --global user.signingkey ~/.ssh/id_ed25519.pub +git config --global commit.gpgsign true +``` + +If `~/.ssh/id_ed25519.pub` already exists and its comment matches your Git `user.email`, key generation is skipped and the existing key is reused. The command prints the public key so you can copy it, then opens `https://github.com/settings/ssh/new` in your browser. + +> [!IMPORTANT] +> Add the key to GitHub as a **Signing Key**, not an **Authentication Key**. + +--- + ## `stash` Stash current working directory changes. diff --git a/main.go b/main.go index 7481702..bfb16a4 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( _ "github.com/daveberrys/guh/src/cmd/account" _ "github.com/daveberrys/guh/src/cmd/branch" _ "github.com/daveberrys/guh/src/cmd/browse" + _ "github.com/daveberrys/guh/src/cmd/cli" + _ "github.com/daveberrys/guh/src/cmd/clone" _ "github.com/daveberrys/guh/src/cmd/commit" _ "github.com/daveberrys/guh/src/cmd/diff" _ "github.com/daveberrys/guh/src/cmd/init" @@ -15,10 +17,9 @@ import ( _ "github.com/daveberrys/guh/src/cmd/logs" _ "github.com/daveberrys/guh/src/cmd/pull" _ "github.com/daveberrys/guh/src/cmd/push" + _ "github.com/daveberrys/guh/src/cmd/signoff" _ "github.com/daveberrys/guh/src/cmd/stash" _ "github.com/daveberrys/guh/src/cmd/undo" - _ "github.com/daveberrys/guh/src/cmd/cli" - _ "github.com/daveberrys/guh/src/cmd/clone" ) func main() { cmd.Execute() } diff --git a/src/cmd/push/root.go b/src/cmd/push/root.go index f4d176b..ac7b889 100644 --- a/src/cmd/push/root.go +++ b/src/cmd/push/root.go @@ -15,7 +15,7 @@ import ( func init() { cmd.RootCmd.AddCommand(pushCmd) } var pushCmd = &cobra.Command{ - Use: "push [force:opt] [remote:opt] [signoff:opt]", + Use: "push [force:opt] [remote:opt]", Short: "Add current changes and push", Args: cobra.MaximumNArgs(2), RunE: func(cobraCmd *cobra.Command, args []string) error { @@ -36,24 +36,24 @@ var pushCmd = &cobra.Command{ remotes, _ := utils.RunGit(true, "remote") for i, r := range strings.Fields(remotes) { if i == 0 { - utils.RunGitSequence(false, pushArgs(force, signoff, "-u", r, branch)) + utils.RunGitSequence(false, pushArgs(force, "-u", r, branch)) } else { - utils.RunGitSequence(false, pushArgs(force, signoff, r)) + utils.RunGitSequence(false, pushArgs(force, r)) } } return nil } if remote == "" { - return utils.RunGitSequence(false, pushArgs(force, signoff, "-u", "origin", branch)) + return utils.RunGitSequence(false, pushArgs(force, "-u", "origin", branch)) } - return utils.RunGitSequence(false, pushArgs(force, signoff, "-u", remote, branch)) + return utils.RunGitSequence(false, pushArgs(force, "-u", remote, branch)) }, } func pushArgs(force bool, tail ...string) []string { - args := []string{"push"} - if force { - args = append(args, "--force") - } - return append(args, tail...) + args := []string{"push"} + if force { + args = append(args, "--force") + } + return append(args, tail...) } diff --git a/src/cmd/signoff/root.go b/src/cmd/signoff/root.go new file mode 100644 index 0000000..6ce7579 --- /dev/null +++ b/src/cmd/signoff/root.go @@ -0,0 +1,102 @@ +// BEFORE WRITING, READ @AGENTS.md +// WE LIKE CONTRIBUTIONS, BUT WE HATE SLOP. +// IF YOU COMMIT SLOP, YOU WILL BE BLOCKED FROM THE REPO +package signoff + +import ( + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/daveberrys/guh/src/cmd" + "github.com/daveberrys/guh/src/utils" + "github.com/pkg/browser" + "github.com/spf13/cobra" +) + +const sshKeyURL = "https://github.com/settings/ssh/new" + +func init() { cmd.RootCmd.AddCommand(signoffCmd) } + +var signoffCmd = &cobra.Command{ + Use: "signoff", + Short: "Configure SSH signing for Git", + Args: cobra.NoArgs, + RunE: func(cobraCmd *cobra.Command, args []string) error { + email, err := utils.RunGit(true, "config", "user.email") + if err != nil || email == "" { + return fmt.Errorf("git user.email is not set; run `git config --global user.email ` first") + } + + homeDir, err := os.UserHomeDir() + if err != nil { + return err + } + + sshDir := filepath.Join(homeDir, ".ssh") + privateKeyPath := filepath.Join(sshDir, "id_ed25519") + publicKeyPath := filepath.Join(sshDir, "id_ed25519.pub") + + contents, err := os.ReadFile(publicKeyPath) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return err + } + + keyMatchesEmail := err == nil && keyHasEmail(string(contents), email) + if keyMatchesEmail { + fmt.Printf("Using existing SSH signing key: %s\n", publicKeyPath) + } else { + if err == nil { + fmt.Printf("SSH key already exists at %s, but its comment does not match %s.\n", publicKeyPath, email) + fmt.Println("ssh-keygen will ask before overwriting it.") + fmt.Println() + } + + if err := os.MkdirAll(sshDir, 0700); err != nil { + return err + } + + sshKeyGen := exec.Command("ssh-keygen", "-t", "ed25519", "-C", email, "-f", privateKeyPath) + sshKeyGen.Stdin = os.Stdin + sshKeyGen.Stdout = os.Stdout + sshKeyGen.Stderr = os.Stderr + if err := sshKeyGen.Run(); err != nil { + return fmt.Errorf("ssh-keygen failed: %w", err) + } + } + + if err := utils.RunGitSequence(false, + []string{"config", "--global", "gpg.format", "ssh"}, + []string{"config", "--global", "user.signingkey", filepath.ToSlash(publicKeyPath)}, + []string{"config", "--global", "commit.gpgsign", "true"}, + ); err != nil { + return err + } + + contents, err = os.ReadFile(publicKeyPath) + if err != nil { + return err + } + + fmt.Println() + fmt.Printf("Copy this key below, and add it in GitHub. Yes, this is fine to show.\n %s\n", strings.TrimSpace(string(contents))) + fmt.Println(`Make sure to add it as a "Signing Key", not an "Authentication Key".`) + fmt.Println() + fmt.Printf("Opening %s\n", sshKeyURL) + + return browser.OpenURL(sshKeyURL) + }, +} + +func keyHasEmail(contents, email string) bool { + fields := strings.Fields(contents) + if len(fields) < 3 { + return false + } + + comment := strings.Join(fields[2:], " ") + return comment == email || strings.Contains(comment, "<"+email+">") +}