MCPcopy Index your code
hub / github.com/bluekeyes/patch2pr / execute

Function execute

cmd/patch2pr/main.go:201–321  ·  view source on GitHub ↗
(ctx context.Context, client *github.Client, patchFiles []string, opts *Options)

Source from the content-addressed store, hash-verified

199}
200
201func execute(ctx context.Context, client *github.Client, patchFiles []string, opts *Options) (*Result, error) {
202 targetRepo := *opts.Repository
203 patchBase, baseBranch, headBranch := opts.PatchBase, opts.BaseBranch, opts.HeadBranch
204
205 if patchBase == "" || (baseBranch == "" && !opts.NoPullRequest) {
206 r, _, err := client.Repositories.Get(ctx, targetRepo.Owner, targetRepo.Name)
207 if err != nil {
208 return nil, fmt.Errorf("get repository failed: %w", err)
209 }
210 if patchBase == "" {
211 patchBase = fmt.Sprintf("refs/heads/%s", r.GetDefaultBranch())
212 }
213 if baseBranch == "" {
214 baseBranch = r.GetDefaultBranch()
215 }
216 }
217
218 if baseRef, ok := strings.CutPrefix(patchBase, "refs/"); ok {
219 ref, _, err := client.Git.GetRef(ctx, targetRepo.Owner, targetRepo.Name, baseRef)
220 if err != nil {
221 return nil, fmt.Errorf("get ref for patch base %q failed: %w", patchBase, err)
222 }
223 patchBase = ref.GetObject().GetSHA()
224 }
225
226 commit, _, err := client.Git.GetCommit(ctx, targetRepo.Owner, targetRepo.Name, patchBase)
227 if err != nil {
228 return nil, fmt.Errorf("get commit for %s failed: %w", patchBase, err)
229 }
230
231 var allPatches []Patch
232 for _, patchFile := range patchFiles {
233 patches, err := parse(patchFile)
234 if err != nil {
235 return nil, err
236 }
237 allPatches = append(allPatches, patches...)
238 }
239
240 sourceRepo, err := prepareSourceRepo(ctx, client, opts)
241 if err != nil {
242 return nil, err
243 }
244
245 applier := patch2pr.NewApplier(client, sourceRepo, commit)
246
247 var newCommit *github.Commit
248 for _, patch := range allPatches {
249 for _, file := range patch.files {
250 if _, err := applier.Apply(ctx, file); err != nil {
251 var namePart string
252 if !errors.Is(err, &patch2pr.Conflict{}) {
253 name := file.NewName
254 if name == "" {
255 name = file.OldName
256 }
257 namePart = name + ": "
258 }

Callers 1

mainFunction · 0.85

Calls 11

ApplyMethod · 0.95
CommitMethod · 0.95
SetMethod · 0.95
NewApplierFunction · 0.92
NewReferenceFunction · 0.92
parseFunction · 0.85
prepareSourceRepoFunction · 0.85
fillHeaderFunction · 0.85
splitMessageFunction · 0.85
IsMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected