-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpull.go
More file actions
238 lines (208 loc) · 6.28 KB
/
pull.go
File metadata and controls
238 lines (208 loc) · 6.28 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2019 Preferred Networks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"os"
"github.com/pfnet-research/git-ghost/pkg/ghost"
"github.com/pfnet-research/git-ghost/pkg/ghost/types"
"github.com/pfnet-research/git-ghost/pkg/util/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type pullFlags struct {
// forceApply bool
}
func NewPullCommand() *cobra.Command {
var (
flags pullFlags
)
command := &cobra.Command{
Use: "pull [from-hash(default=HEAD)] [diff-hash]",
Short: "pull commits(hash1...hash2), diff(hash...current state) from ghost repo and apply them to working dir",
Long: "pull commits or diff or all from ghost repo and apply them to working dir. If you didn't specify any subcommand, this commands works as an alias for 'pull diff' command.",
Args: cobra.RangeArgs(1, 2),
Run: runPullDiffCommand(&flags),
}
// command.PersistentFlags().BoolVarP(&flags.forceApply, "force", "f", true, "force apply pulled ghost branches to working dir")
command.AddCommand(&cobra.Command{
Use: "diff [diff-from-hash(default=HEAD)] [diff-hash]",
Short: "pull diff from ghost repo and apply it to working dir",
Long: "pull diff from [diff-from-hash] to [diff-hash] from your ghost repo and apply it to working dir",
Args: cobra.RangeArgs(1, 2),
Run: runPullDiffCommand(&flags),
})
command.AddCommand(&cobra.Command{
Use: "commits [from-hash(default=HEAD)] [to-hash]",
Short: "pull commits from ghost repo and apply it to working dir",
Long: "pull commits from [from-hash] to [to-hash] from your ghost repo and apply it to working dir",
Args: cobra.RangeArgs(1, 2),
Run: runPullCommitsCommand(&flags),
})
command.AddCommand(&cobra.Command{
Use: "all [from-hash(default=HEAD)] [to-hash] [diff-hash]",
Short: "pull both commits and diff from ghost repo and apply them to working dir sequentially",
Long: "pull commits([from-hash]...[to-hash]) and diff([to-hash]...[diff-hash]) and apply them to working dir sequentially",
Args: cobra.RangeArgs(2, 3),
Run: runPullAllCommand(&flags),
})
return command
}
type pullCommitsArg struct {
commitsFrom string
commitsTo string
}
func newPullCommitsArg(args []string) pullCommitsArg {
arg := pullCommitsArg{
commitsFrom: "HEAD",
commitsTo: "",
}
if len(args) >= 2 {
arg.commitsFrom = args[0]
arg.commitsTo = args[1]
return arg
}
if len(args) >= 1 {
arg.commitsTo = args[0]
return arg
}
return arg
}
func (arg pullCommitsArg) validate() errors.GitGhostError {
if err := nonEmpty("commit-from", arg.commitsFrom); err != nil {
return err
}
if err := nonEmpty("commit-to", arg.commitsTo); err != nil {
return err
}
return nil
}
func runPullCommitsCommand(flags *pullFlags) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
arg := newPullCommitsArg(args)
if err := arg.validate(); err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
options := ghost.PullOptions{
WorkingEnvSpec: globalOpts.WorkingEnvSpec(),
CommitsBranchSpec: &types.CommitsBranchSpec{
Prefix: globalOpts.ghostPrefix,
CommittishFrom: arg.commitsFrom,
CommittishTo: arg.commitsTo,
},
// ForceApply: flags.forceApply,
}
err := ghost.Pull(options)
if err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
}
}
type pullDiffArg struct {
diffFrom string
diffHash string
}
func newPullDiffArg(args []string) pullDiffArg {
arg := pullDiffArg{
diffFrom: "HEAD",
diffHash: "",
}
if len(args) >= 2 {
arg.diffFrom = args[0]
arg.diffHash = args[1]
return arg
}
if len(args) >= 1 {
arg.diffHash = args[0]
return arg
}
return arg
}
func (arg pullDiffArg) validate() errors.GitGhostError {
if err := nonEmpty("diff-from-hash", arg.diffFrom); err != nil {
return err
}
if err := nonEmpty("diff-hash", arg.diffHash); err != nil {
return err
}
return nil
}
func runPullDiffCommand(flags *pullFlags) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
arg := newPullDiffArg(args)
if err := arg.validate(); err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
options := ghost.PullOptions{
WorkingEnvSpec: globalOpts.WorkingEnvSpec(),
PullableDiffBranchSpec: &types.PullableDiffBranchSpec{
Prefix: globalOpts.ghostPrefix,
CommittishFrom: arg.diffFrom,
DiffHash: arg.diffHash,
},
// ForceApply: flags.forceApply,
}
err := ghost.Pull(options)
if err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
}
}
func runPullAllCommand(flags *pullFlags) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
var pullCommitsArg pullCommitsArg
var pullDiffArg pullDiffArg
switch len(args) {
case 3:
pullCommitsArg = newPullCommitsArg(args[0:2])
pullDiffArg = newPullDiffArg(args[1:])
case 2:
pullCommitsArg = newPullCommitsArg(args[0:1])
pullDiffArg = newPullDiffArg(args)
default:
log.Error(cmd.Args(cmd, args))
os.Exit(1)
}
if err := pullCommitsArg.validate(); err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
if err := pullDiffArg.validate(); err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
options := ghost.PullOptions{
WorkingEnvSpec: globalOpts.WorkingEnvSpec(),
CommitsBranchSpec: &types.CommitsBranchSpec{
Prefix: globalOpts.ghostPrefix,
CommittishFrom: pullCommitsArg.commitsFrom,
CommittishTo: pullCommitsArg.commitsTo,
},
PullableDiffBranchSpec: &types.PullableDiffBranchSpec{
Prefix: globalOpts.ghostPrefix,
CommittishFrom: pullDiffArg.diffFrom,
DiffHash: pullDiffArg.diffHash,
},
// ForceApply: flags.forceApply,
}
err := ghost.Pull(options)
if err != nil {
errors.LogErrorWithStack(err)
os.Exit(1)
}
}
}