mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:26:46 +08:00
4 files changed
+134
-7
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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() }
|
||||
@@ -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,17 +36,17 @@ 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))
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <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+">")
|
||||
}
|
||||
Reference in new issue
Block a user