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`
|
## `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 # show edited files only
|
||||||
guh commit "." "message" # git add . && commit
|
guh commit "." "message" # git add . && commit
|
||||||
guh commit '["file1", "file2"]' "message" # add specific files && commit
|
guh commit '["file1", "file2"]' "message" # add specific files && commit
|
||||||
guh commit '["file1"]' "message" "description" # commit with message + description
|
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" push # commit + push to origin
|
||||||
guh commit '["file1"]' "message" "description" 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" push "upstream" # commit + push to named remote
|
||||||
guh commit '["file1"]' "message" "description" push "all" # commit + push to all remotes
|
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**
|
**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 |
|
| 1 | files | yes* | JSON array of files, or `"."` to add everything |
|
||||||
| 2 | message | yes* | Commit message |
|
| 2 | message | yes* | Commit message |
|
||||||
| 3 | description | no | Optional commit description (uses `-m` twice) |
|
| 3 | description | no | Optional commit description (uses `-m` twice), or `"push"`/`"signoff"` |
|
||||||
| 4 | push | no | Literal `"push"` to trigger a push after commit |
|
| 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) |
|
| 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.
|
\*When no arguments are given, shows `git status --short` instead.
|
||||||
|
|
||||||
|
|||||||
+32
-35
@@ -16,9 +16,9 @@ import (
|
|||||||
func init() { cmd.RootCmd.AddCommand(commitCmd) }
|
func init() { cmd.RootCmd.AddCommand(commitCmd) }
|
||||||
|
|
||||||
var commitCmd = &cobra.Command{
|
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",
|
Short: "Add specified files and commit",
|
||||||
Args: cobra.RangeArgs(0, 5),
|
Args: cobra.RangeArgs(0, 6),
|
||||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return utils.RunGitSequence(false, []string{"status", "--short"})
|
return utils.RunGitSequence(false, []string{"status", "--short"})
|
||||||
@@ -48,42 +48,39 @@ var commitCmd = &cobra.Command{
|
|||||||
utils.RunGitSequence(false, addArgs)
|
utils.RunGitSequence(false, addArgs)
|
||||||
|
|
||||||
commitArgs := []string{"commit", "-m", args[1]}
|
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)]
|
// The third argument is either a description or an action keyword.
|
||||||
// case 2 -> guh commit [files] [message] [desc] push[:opt]
|
rest := args[2:]
|
||||||
// case 3 -> guh commit [files] [message] push[:opt] [remote_name:opt(origin)]
|
if len(rest) > 0 && rest[0] != "push" && rest[0] != "signoff" {
|
||||||
// case 4 -> guh commit [files] [message] push[:opt]
|
commitArgs = append(commitArgs, "-m", rest[0])
|
||||||
// case 5 -> guh commit [files] [message] [desc]
|
rest = rest[1:]
|
||||||
// case 6 -> guh commit [files] [message]
|
}
|
||||||
switch len(args) {
|
|
||||||
case 5:
|
// case 1 -> guh commit [files] [message] [desc] push[:opt] [remote_name:opt(origin)] [signoff:opt]
|
||||||
// [files] [message] [desc] push [remote] (case 1)
|
// case 2 -> guh commit [files] [message] [desc] push[:opt] [signoff:opt]
|
||||||
commitArgs = append(commitArgs, "-m", args[2])
|
// case 3 -> guh commit [files] [message] push[:opt] [remote_name:opt(origin)] [signoff:opt]
|
||||||
afterPush = true
|
// case 4 -> guh commit [files] [message] push[:opt] [signoff:opt]
|
||||||
if args[4] != "" {
|
// case 5 -> guh commit [files] [message] [desc] [signoff:opt]
|
||||||
remoteName = args[4]
|
// case 6 -> guh commit [files] [message] [signoff:opt]
|
||||||
}
|
for _, arg := range rest {
|
||||||
case 4:
|
switch {
|
||||||
if args[2] == "push" {
|
case arg == "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])
|
|
||||||
afterPush = true
|
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)
|
if signoff {
|
||||||
afterPush = true
|
commitArgs = append(commitArgs, "--signoff")
|
||||||
} else {
|
|
||||||
// [files] [message] [desc] (case 5)
|
|
||||||
commitArgs = append(commitArgs, "-m", args[2])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.RunGitSequence(false, commitArgs)
|
utils.RunGitSequence(false, commitArgs)
|
||||||
|
|||||||
+18
-12
@@ -15,16 +15,18 @@ import (
|
|||||||
func init() { cmd.RootCmd.AddCommand(pushCmd) }
|
func init() { cmd.RootCmd.AddCommand(pushCmd) }
|
||||||
|
|
||||||
var pushCmd = &cobra.Command{
|
var pushCmd = &cobra.Command{
|
||||||
Use: "push [force:opt] [remote:opt]",
|
Use: "push [force:opt] [remote:opt] [signoff:opt]",
|
||||||
Short: "Add current changes and push",
|
Short: "Add current changes and push",
|
||||||
Args: cobra.MaximumNArgs(2),
|
Args: cobra.MaximumNArgs(3),
|
||||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||||
branch, _ := utils.RunGit(true, "rev-parse", "--abbrev-ref", "HEAD")
|
branch, _ := utils.RunGit(true, "rev-parse", "--abbrev-ref", "HEAD")
|
||||||
|
|
||||||
force, remote := false, ""
|
force, remote, signoff := false, "", false
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if arg == "force" {
|
if arg == "force" {
|
||||||
force = true
|
force = true
|
||||||
|
} else if arg == "" {
|
||||||
|
signoff = true
|
||||||
} else if remote == "" {
|
} else if remote == "" {
|
||||||
remote = arg
|
remote = arg
|
||||||
} else {
|
} else {
|
||||||
@@ -36,23 +38,27 @@ var pushCmd = &cobra.Command{
|
|||||||
remotes, _ := utils.RunGit(true, "remote")
|
remotes, _ := utils.RunGit(true, "remote")
|
||||||
for i, r := range strings.Fields(remotes) {
|
for i, r := range strings.Fields(remotes) {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
utils.RunGitSequence(false, pushArgs(force, "-u", r, branch))
|
utils.RunGitSequence(false, pushArgs(force, signoff, "-u", r, branch))
|
||||||
} else {
|
} else {
|
||||||
utils.RunGitSequence(false, pushArgs(force, r))
|
utils.RunGitSequence(false, pushArgs(force, signoff, r))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if remote == "" {
|
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 {
|
func pushArgs(force, signoff bool, tail ...string) []string {
|
||||||
if force {
|
args := []string{"push"}
|
||||||
return append([]string{"push", "--force"}, tail...)
|
if force {
|
||||||
}
|
args = append(args, "--force")
|
||||||
return append([]string{"push"}, tail...)
|
}
|
||||||
|
if signoff {
|
||||||
|
args = append(args, "--signoff")
|
||||||
|
}
|
||||||
|
return append(args, tail...)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user