@@ -121,14 +121,14 @@ func main() {
121
121
client := github .NewClient (tc )
122
122
client .BaseURL = opts .GitHubURL
123
123
124
- var patchFile string
124
+ var patchFiles [] string
125
125
if fs .NArg () == 0 {
126
- patchFile = "-"
126
+ patchFiles = [] string { "-" }
127
127
} else {
128
- patchFile = fs .Arg ( 0 )
128
+ patchFiles = fs .Args ( )
129
129
}
130
130
131
- res , err := execute (ctx , client , patchFile , & opts )
131
+ res , err := execute (ctx , client , patchFiles , & opts )
132
132
if err != nil {
133
133
die (1 , err )
134
134
}
@@ -147,6 +147,13 @@ func main() {
147
147
}
148
148
}
149
149
150
+ type Patch struct {
151
+ path string
152
+ files []* gitdiff.File
153
+ preamble string
154
+ header * gitdiff.PatchHeader
155
+ }
156
+
150
157
type Result struct {
151
158
Commit string `json:"commit"`
152
159
Tree string `json:"tree"`
@@ -158,7 +165,36 @@ type PullRequestResult struct {
158
165
URL string `json:"url"`
159
166
}
160
167
161
- func execute (ctx context.Context , client * github.Client , patchFile string , opts * Options ) (* Result , error ) {
168
+ func parse (patchFile string ) (* Patch , error ) {
169
+ var r io.ReadCloser
170
+ if patchFile == "-" {
171
+ r = os .Stdin
172
+ } else {
173
+ f , err := os .Open (patchFile )
174
+ if err != nil {
175
+ return nil , fmt .Errorf ("open patch file failed: %w" , err )
176
+ }
177
+ r = f
178
+ }
179
+
180
+ files , preamble , err := gitdiff .Parse (r )
181
+ if err != nil {
182
+ return nil , fmt .Errorf ("parsing patch failed: %w" , err )
183
+ }
184
+ _ = r .Close ()
185
+
186
+ var header * gitdiff.PatchHeader
187
+ if len (preamble ) > 0 {
188
+ header , err = gitdiff .ParsePatchHeader (preamble )
189
+ if err != nil {
190
+ fmt .Fprintf (os .Stderr , "warning: invalid patch header: %v" , err )
191
+ }
192
+ }
193
+
194
+ return & Patch {patchFile , files , preamble , header }, nil
195
+ }
196
+
197
+ func execute (ctx context.Context , client * github.Client , patchFiles []string , opts * Options ) (* Result , error ) {
162
198
targetRepo := * opts .Repository
163
199
patchBase , baseBranch , headBranch := opts .PatchBase , opts .BaseBranch , opts .HeadBranch
164
200
@@ -188,50 +224,37 @@ func execute(ctx context.Context, client *github.Client, patchFile string, opts
188
224
return nil , fmt .Errorf ("get commit for %s failed: %w" , patchBase , err )
189
225
}
190
226
191
- var r io.ReadCloser
192
- if patchFile == "-" {
193
- r = os .Stdin
194
- } else {
195
- f , err := os .Open (patchFile )
227
+ var patches []Patch
228
+ for _ , patchFile := range patchFiles {
229
+ patch , err := parse (patchFile )
196
230
if err != nil {
197
- return nil , fmt .Errorf ("open patch file failed: %w" , err )
198
- }
199
- r = f
200
- }
201
-
202
- files , preamble , err := gitdiff .Parse (r )
203
- if err != nil {
204
- return nil , fmt .Errorf ("parsing patch failed: %w" , err )
205
- }
206
- _ = r .Close ()
207
-
208
- var header * gitdiff.PatchHeader
209
- if len (preamble ) > 0 {
210
- header , err = gitdiff .ParsePatchHeader (preamble )
211
- if err != nil {
212
- fmt .Fprintf (os .Stderr , "warning: invalid patch header: %v" , err )
231
+ return nil , err
213
232
}
233
+ patches = append (patches , * patch )
214
234
}
215
235
216
236
sourceRepo , err := prepareSourceRepo (ctx , client , opts )
217
237
if err != nil {
218
238
return nil , err
219
239
}
220
240
221
- applier := patch2pr .NewApplier (client , sourceRepo , commit )
222
- for _ , file := range files {
223
- if _ , err := applier .Apply (ctx , file ); err != nil {
224
- name := file .NewName
225
- if name == "" {
226
- name = file .OldName
241
+ newCommit := commit
242
+ for _ , patch := range patches {
243
+ applier := patch2pr .NewApplier (client , sourceRepo , newCommit )
244
+ for _ , file := range patch .files {
245
+ if _ , err := applier .Apply (ctx , file ); err != nil {
246
+ name := file .NewName
247
+ if name == "" {
248
+ name = file .OldName
249
+ }
250
+ return nil , fmt .Errorf ("apply failed: %s: %w" , name , err )
227
251
}
228
- return nil , fmt .Errorf ("apply failed: %s: %w" , name , err )
229
252
}
230
- }
231
253
232
- newCommit , err := applier .Commit (ctx , nil , fillHeader (header , patchFile , opts .Message ))
233
- if err != nil {
234
- return nil , fmt .Errorf ("commit failed: %w" , err )
254
+ newCommit , err = applier .Commit (ctx , nil , fillHeader (patch .header , patch .path , opts .Message ))
255
+ if err != nil {
256
+ return nil , fmt .Errorf ("commit failed: %w" , err )
257
+ }
235
258
}
236
259
237
260
ref := patch2pr .NewReference (client , sourceRepo , fmt .Sprintf ("refs/heads/%s" , headBranch ))
@@ -458,7 +481,7 @@ func isCode(err error, code int) bool {
458
481
459
482
func helpText () string {
460
483
help := `
461
- Usage: patch2pr [options] [patch]
484
+ Usage: patch2pr [options] [patch... ]
462
485
463
486
Create a GitHub pull request from a patch file
464
487
0 commit comments