forked from salsaflow/salsaflow
/
command.go
152 lines (128 loc) · 3.65 KB
/
command.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package changesCmd
import (
// Stdlib
"errors"
"fmt"
"os"
// Internal
"github.com/salsaflow/salsaflow/app"
"github.com/salsaflow/salsaflow/changes"
"github.com/salsaflow/salsaflow/commands/release/changes/cherrypick"
"github.com/salsaflow/salsaflow/errs"
"github.com/salsaflow/salsaflow/git"
"github.com/salsaflow/salsaflow/log"
"github.com/salsaflow/salsaflow/modules"
"github.com/salsaflow/salsaflow/releases"
"github.com/salsaflow/salsaflow/version"
// Other
"gopkg.in/tchap/gocli.v2"
)
var Command = &gocli.Command{
UsageLine: "changes [-porcelain] [-to_cherrypick]",
Short: "list the changes associated with the current release",
Long: `
List the change sets (the commits with the same change ID)
associated with the current release together with some details,
e.g. the commit SHA, the source ref and the commit title.
The 'porcelain' flag will make the output more script-friendly,
e.g. it will fill the change ID in every column.
The 'to_cherrypick' flag can be used to list the changes that are assigned
to the release but haven't been cherry-picked onto the release branch yet.
`,
Action: run,
}
var (
flagPorcelain bool
flagToCherryPick bool
)
func init() {
// Register the command line flags.
Command.Flags.BoolVar(&flagPorcelain, "porcelain", flagPorcelain,
"enable script-friendly output")
Command.Flags.BoolVar(&flagToCherryPick, "to_cherrypick", flagToCherryPick,
"list the changes to cherry-pick")
// Register the subcommands.
Command.MustRegisterSubcommand(cherrypickCmd.Command)
}
func run(cmd *gocli.Command, args []string) {
if len(args) != 0 {
cmd.Usage()
os.Exit(2)
}
app.InitOrDie()
if flagPorcelain {
log.Disable()
}
err := runMain()
if err != nil {
errs.Fatal(err)
}
}
func runMain() (err error) {
// Load repo config.
gitConfig, err := git.LoadConfig()
if err != nil {
return err
}
var (
remoteName = gitConfig.RemoteName()
releaseBranch = gitConfig.ReleaseBranchName()
)
// Make sure that the local release branch exists.
task := "Make sure that the local release branch exists"
if err := git.CheckOrCreateTrackingBranch(releaseBranch, remoteName); err != nil {
return errs.NewError(task, err)
}
// Get the current release version string.
task = "Get the release branch version string"
releaseVersion, err := version.GetByBranch(releaseBranch)
if err != nil {
return errs.NewError(task, err)
}
// Get the stories associated with the current release.
task = "Fetch the stories associated with the current release"
log.Run(task)
tracker, err := modules.GetIssueTracker()
if err != nil {
return errs.NewError(task, err)
}
release, err := tracker.RunningRelease(releaseVersion)
if err != nil {
return errs.NewError(task, err)
}
stories, err := release.Stories()
if err != nil {
return errs.NewError(task, err)
}
if len(stories) == 0 {
return errs.NewError(task, errors.New("no relevant stories found"))
}
// Get the story changes.
task = "Collect the story changes"
log.Run(task)
groups, err := changes.StoryChanges(stories)
if err != nil {
return errs.NewError(task, err)
}
// Just return in case there are no relevant commits found.
if len(groups) == 0 {
return errs.NewError(task, errors.New("no relevant commits found"))
}
// Sort the change groups.
groups = changes.SortStoryChanges(groups, stories)
if flagToCherryPick {
groups, err = releases.StoryChangesToCherryPick(groups)
if err != nil {
return errs.NewError(task, err)
}
}
// Dump the change details into the console.
if !flagPorcelain {
fmt.Println()
}
changes.DumpStoryChanges(os.Stdout, groups, tracker, flagPorcelain)
if !flagPorcelain {
fmt.Println()
}
return nil
}