-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcommit_create.go
More file actions
83 lines (72 loc) · 2.51 KB
/
commit_create.go
File metadata and controls
83 lines (72 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"errors"
"fmt"
"go.abhg.dev/gs/internal/cli"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/handler/restack"
"go.abhg.dev/gs/internal/silog"
"go.abhg.dev/gs/internal/text"
)
type commitCreateCmd struct {
All bool `short:"a" help:"Stage all changes before committing."`
AllowEmpty bool `help:"Create a new commit even if it contains no changes."`
Fixup string `help:"Create a fixup commit. See also 'git-spice commit fixup'." placeholder:"COMMIT"`
Message string `short:"m" placeholder:"MSG" help:"Use the given message as the commit message."`
NoVerify bool `help:"Bypass pre-commit and commit-msg hooks."`
Signoff bool `config:"commit.signoff" help:"Add Signed-off-by trailer to the commit message"`
}
func (*commitCreateCmd) Help() string {
name := cli.Name()
return text.Dedent(fmt.Sprintf(`
Staged changes are committed to the current branch.
Branches upstack are restacked if necessary.
Use this as a shortcut for 'git commit'
followed by '%[1]s upstack restack'.
An editor is opened to edit the commit message.
Use the -m/--message option to specify the message
without opening an editor.
Git hooks are run unless the --no-verify flag is given.
Use the -a/--all flag to stage all changes before committing.
Use the --fixup flag to create a new commit that will be merged
into another commit when run with 'git rebase --autosquash'.
See also, the '%[1]s commit fixup' command, which is preferable
when you want to apply changes to an older commit.
`, name))
}
func (cmd *commitCreateCmd) Run(
ctx context.Context,
log *silog.Logger,
wt *git.Worktree,
restackHandler RestackHandler,
) error {
if err := wt.Commit(ctx, git.CommitRequest{
Message: cmd.Message,
All: cmd.All,
AllowEmpty: cmd.AllowEmpty,
Fixup: cmd.Fixup,
NoVerify: cmd.NoVerify,
Signoff: cmd.Signoff,
}); err != nil {
return fmt.Errorf("commit: %w", err)
}
if _, err := wt.RebaseState(ctx); err == nil {
// In the middle of a rebase.
// Don't restack upstack branches.
log.Debug("A rebase is in progress, skipping restack")
return nil
}
currentBranch, err := wt.CurrentBranch(ctx)
if err != nil {
// No restack needed if we're in a detached head state.
if errors.Is(err, git.ErrDetachedHead) {
log.Debug("HEAD is detached, skipping restack")
return nil
}
return fmt.Errorf("get current branch: %w", err)
}
return restackHandler.RestackUpstack(ctx, currentBranch, &restack.UpstackOptions{
SkipStart: true,
})
}