/
commit_create.go
103 lines (86 loc) · 2.21 KB
/
commit_create.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"fmt"
"os"
"emperror.dev/errors"
"github.com/aviator-co/av/internal/actions"
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/spf13/cobra"
)
var commitCreateFlags struct {
// The commit message.
Message string
// Same as `git commit --all`.
All bool
}
var commitCreateCmd = &cobra.Command{
Use: "create",
Short: "create a commit",
RunE: func(cmd *cobra.Command, args []string) error {
repo, err := getRepo()
if err != nil {
return err
}
currentBranchName, err := repo.CurrentBranchName()
if err != nil {
return errors.WrapIf(err, "failed to determine current branch")
}
if err := commitCreate(repo, currentBranchName, commitCreateFlags); err != nil {
return err
}
return nil
},
}
func init() {
commitCreateCmd.Flags().
StringVarP(&commitCreateFlags.Message, "message", "m", "", "the commit message")
commitCreateCmd.Flags().
BoolVarP(&commitCreateFlags.All, "all", "a", false, "automatically stage modified files (same as git commit --all)")
}
func commitCreate(repo *git.Repo, currentBranchName string, flags struct {
Message string
All bool
}) error {
commitArgs := []string{"commit"}
if commitCreateFlags.All {
commitArgs = append(commitArgs, "--all")
}
if commitCreateFlags.Message != "" {
commitArgs = append(commitArgs, "--message", commitCreateFlags.Message)
}
if _, err := repo.Run(&git.RunOpts{
Args: commitArgs,
ExitError: true,
Interactive: true,
}); err != nil {
_, _ = fmt.Fprint(os.Stderr,
"\n", colors.Failure("Failed to create commit."), "\n",
)
return actions.ErrExitSilently{ExitCode: 1}
}
state, err := actions.ReadStackSyncState(repo)
state.OriginalBranch = currentBranchName
if err != nil && !os.IsNotExist(err) {
return err
}
ctx := context.Background()
db, err := getDB(repo)
if err != nil {
return err
}
tx := db.WriteTx()
defer tx.Abort()
client, err := getGitHubClient()
if err != nil {
return err
}
branchesToSync := meta.SubsequentBranches(tx, currentBranchName)
err = actions.SyncStack(ctx, repo, client, tx, branchesToSync, state)
if err != nil {
return err
}
return nil
}