mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:26:46 +08:00
add signoff for commit and push
Signed-off-by: daveberrys <84567826+daveberrys@users.noreply.github.com>
This commit is contained in:
1 parent
a6abd9bd2a
commit
e89cc16ace
3 files changed
+59
-51
No files matched your search
@@ -218,17 +218,21 @@ guh clone <url>
|
||||
|
||||
## `commit`
|
||||
|
||||
Add files and commit changes. Shows `git status --short` before committing. Optionally pushes after commit.
|
||||
Add files and commit changes. Shows `git status --short` before committing. Optionally pushes after commit and/or adds a `Signed-off-by` trailer.
|
||||
|
||||
```
|
||||
guh commit # show edited files only
|
||||
guh commit "." "message" # git add . && commit
|
||||
guh commit '["file1", "file2"]' "message" # add specific files && commit
|
||||
guh commit '["file1"]' "message" "description" # commit with message + description
|
||||
guh commit '["file1"]' "message" signoff # commit with signoff
|
||||
guh commit '["file1"]' "message" "description" signoff # commit with description + signoff
|
||||
guh commit '["file1"]' "message" push # commit + push to origin
|
||||
guh commit '["file1"]' "message" "description" push # commit + push to origin
|
||||
guh commit '["file1"]' "message" push signoff # commit + push to origin + signoff
|
||||
guh commit '["file1"]' "message" push "upstream" # commit + push to named remote
|
||||
guh commit '["file1"]' "message" "description" push "all" # commit + push to all remotes
|
||||
guh commit '["file1"]' "message" push "origin" signoff # commit + push to origin + signoff
|
||||
```
|
||||
|
||||
**Arguments**
|
||||
@@ -237,9 +241,10 @@ guh commit '["file1"]' "message" "description" push "all" # commit + push
|
||||
|---|------|----------|-------------|
|
||||
| 1 | files | yes* | JSON array of files, or `"."` to add everything |
|
||||
| 2 | message | yes* | Commit message |
|
||||
| 3 | description | no | Optional commit description (uses `-m` twice) |
|
||||
| 4 | push | no | Literal `"push"` to trigger a push after commit |
|
||||
| 5 | remote | no | Remote name (default `"origin"`; use `"all"` to push to all remotes) |
|
||||
| 3 | description | no | Optional commit description (uses `-m` twice), or `"push"`/`"signoff"` |
|
||||
| 4 | push | no | Literal `"push"` to trigger a push after commit, or `"signoff"` |
|
||||
| 5 | remote | no | Remote name (default `"origin"`; use `"all"` to push to all remotes), or `"signoff"` |
|
||||
| 6 | signoff | no | Literal `"signoff"` to add a `Signed-off-by` trailer |
|
||||
|
||||
\*When no arguments are given, shows `git status --short` instead.
|
||||
|
||||
|
||||
+32
-35
@@ -16,9 +16,9 @@ import (
|
||||
func init() { cmd.RootCmd.AddCommand(commitCmd) }
|
||||
|
||||
var commitCmd = &cobra.Command{
|
||||
Use: "commit [files] [message] [description:opt]/push:opt [push:opt] [remote_name:opt]",
|
||||
Use: "commit [files] [message] [description:opt]/push:opt/signoff:opt [push:opt]/signoff:opt [remote_name:opt] [signoff:opt]",
|
||||
Short: "Add specified files and commit",
|
||||
Args: cobra.RangeArgs(0, 5),
|
||||
Args: cobra.RangeArgs(0, 6),
|
||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return utils.RunGitSequence(false, []string{"status", "--short"})
|
||||
@@ -48,42 +48,39 @@ var commitCmd = &cobra.Command{
|
||||
utils.RunGitSequence(false, addArgs)
|
||||
|
||||
commitArgs := []string{"commit", "-m", args[1]}
|
||||
afterPush, remoteName := false, "origin"
|
||||
afterPush, signoff, remoteName := false, false, "origin"
|
||||
remoteSet := false
|
||||
|
||||
// case 1 -> guh commit [files] [message] [desc] push[:opt] [remote_name:opt(origin)]
|
||||
// case 2 -> guh commit [files] [message] [desc] push[:opt]
|
||||
// case 3 -> guh commit [files] [message] push[:opt] [remote_name:opt(origin)]
|
||||
// case 4 -> guh commit [files] [message] push[:opt]
|
||||
// case 5 -> guh commit [files] [message] [desc]
|
||||
// case 6 -> guh commit [files] [message]
|
||||
switch len(args) {
|
||||
case 5:
|
||||
// [files] [message] [desc] push [remote] (case 1)
|
||||
commitArgs = append(commitArgs, "-m", args[2])
|
||||
afterPush = true
|
||||
if args[4] != "" {
|
||||
remoteName = args[4]
|
||||
}
|
||||
case 4:
|
||||
if args[2] == "push" {
|
||||
// [files] [message] push [remote] (case 3)
|
||||
afterPush = true
|
||||
if args[3] != "" {
|
||||
remoteName = args[3]
|
||||
}
|
||||
} else {
|
||||
// [files] [message] [desc] push (case 2)
|
||||
commitArgs = append(commitArgs, "-m", args[2])
|
||||
// The third argument is either a description or an action keyword.
|
||||
rest := args[2:]
|
||||
if len(rest) > 0 && rest[0] != "push" && rest[0] != "signoff" {
|
||||
commitArgs = append(commitArgs, "-m", rest[0])
|
||||
rest = rest[1:]
|
||||
}
|
||||
|
||||
// case 1 -> guh commit [files] [message] [desc] push[:opt] [remote_name:opt(origin)] [signoff:opt]
|
||||
// case 2 -> guh commit [files] [message] [desc] push[:opt] [signoff:opt]
|
||||
// case 3 -> guh commit [files] [message] push[:opt] [remote_name:opt(origin)] [signoff:opt]
|
||||
// case 4 -> guh commit [files] [message] push[:opt] [signoff:opt]
|
||||
// case 5 -> guh commit [files] [message] [desc] [signoff:opt]
|
||||
// case 6 -> guh commit [files] [message] [signoff:opt]
|
||||
for _, arg := range rest {
|
||||
switch {
|
||||
case arg == "push":
|
||||
afterPush = true
|
||||
case arg == "signoff":
|
||||
signoff = true
|
||||
case arg == "":
|
||||
// ignore empty optional arguments
|
||||
case !remoteSet:
|
||||
remoteName, remoteSet = arg, true
|
||||
default:
|
||||
return fmt.Errorf("unexpected argument %q", arg)
|
||||
}
|
||||
case 3:
|
||||
if args[2] == "push" {
|
||||
// [files] [message] push (case 4)
|
||||
afterPush = true
|
||||
} else {
|
||||
// [files] [message] [desc] (case 5)
|
||||
commitArgs = append(commitArgs, "-m", args[2])
|
||||
}
|
||||
}
|
||||
|
||||
if signoff {
|
||||
commitArgs = append(commitArgs, "--signoff")
|
||||
}
|
||||
|
||||
utils.RunGitSequence(false, commitArgs)
|
||||
|
||||
+18
-12
@@ -15,16 +15,18 @@ import (
|
||||
func init() { cmd.RootCmd.AddCommand(pushCmd) }
|
||||
|
||||
var pushCmd = &cobra.Command{
|
||||
Use: "push [force:opt] [remote:opt]",
|
||||
Use: "push [force:opt] [remote:opt] [signoff:opt]",
|
||||
Short: "Add current changes and push",
|
||||
Args: cobra.MaximumNArgs(2),
|
||||
Args: cobra.MaximumNArgs(3),
|
||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||
branch, _ := utils.RunGit(true, "rev-parse", "--abbrev-ref", "HEAD")
|
||||
|
||||
force, remote := false, ""
|
||||
force, remote, signoff := false, "", false
|
||||
for _, arg := range args {
|
||||
if arg == "force" {
|
||||
force = true
|
||||
} else if arg == "" {
|
||||
signoff = true
|
||||
} else if remote == "" {
|
||||
remote = arg
|
||||
} else {
|
||||
@@ -36,23 +38,27 @@ var pushCmd = &cobra.Command{
|
||||
remotes, _ := utils.RunGit(true, "remote")
|
||||
for i, r := range strings.Fields(remotes) {
|
||||
if i == 0 {
|
||||
utils.RunGitSequence(false, pushArgs(force, "-u", r, branch))
|
||||
utils.RunGitSequence(false, pushArgs(force, signoff, "-u", r, branch))
|
||||
} else {
|
||||
utils.RunGitSequence(false, pushArgs(force, r))
|
||||
utils.RunGitSequence(false, pushArgs(force, signoff, r))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if remote == "" {
|
||||
return utils.RunGitSequence(false, pushArgs(force, "-u", "origin", branch))
|
||||
return utils.RunGitSequence(false, pushArgs(force, signoff, "-u", "origin", branch))
|
||||
}
|
||||
return utils.RunGitSequence(false, pushArgs(force, "-u", remote, branch))
|
||||
return utils.RunGitSequence(false, pushArgs(force, signoff, "-u", remote, branch))
|
||||
},
|
||||
}
|
||||
|
||||
func pushArgs(force bool, tail ...string) []string {
|
||||
if force {
|
||||
return append([]string{"push", "--force"}, tail...)
|
||||
}
|
||||
return append([]string{"push"}, tail...)
|
||||
func pushArgs(force, signoff bool, tail ...string) []string {
|
||||
args := []string{"push"}
|
||||
if force {
|
||||
args = append(args, "--force")
|
||||
}
|
||||
if signoff {
|
||||
args = append(args, "--signoff")
|
||||
}
|
||||
return append(args, tail...)
|
||||
}
|
||||
Reference in new issue
Block a user