44 "bytes"
55 "context"
66 "encoding/base64"
7- "errors"
87 "fmt"
98 "io/ioutil"
109 "os"
@@ -81,6 +80,9 @@ func (a *GraphQLApplier) SetV3Client(client *github.Client) {
8180// When given an unsupported patch, Apply returns an error such that
8281// IsUnsupported(err) is true. Setting a V3 client with SetV3Client allows
8382// Apply to process some patches that are otherwise unsupported.
83+ //
84+ // If the apply fails due to a conflict, Apply returns an error of type
85+ // *Conflict.
8486func (a * GraphQLApplier ) Apply (ctx context.Context , f * gitdiff.File ) error {
8587 // As of 2021-09-22, createCommitOnBranch handles file modes
8688 // inconsistently:
@@ -122,11 +124,11 @@ func (a *GraphQLApplier) applyCreate(ctx context.Context, f *gitdiff.File) error
122124 return err
123125 }
124126 if exists {
125- return errors . New ( "existing entry for new file" )
127+ return & Conflict { Type : ConflictNewFileExists , File : f . NewName }
126128 }
127129
128130 var b bytes.Buffer
129- if err := gitdiff . Apply (& b , bytes .NewReader (nil ), f ); err != nil {
131+ if err := apply (& b , bytes .NewReader (nil ), f . NewName , f ); err != nil {
130132 return err
131133 }
132134
@@ -141,12 +143,12 @@ func (a *GraphQLApplier) applyDelete(ctx context.Context, f *gitdiff.File) error
141143 return err
142144 }
143145 if ! exists {
144- // because the rest of application is strict, return an error if the
146+ // Because the rest of application is strict, return an error if the
145147 // file was already deleted, since it indicates a conflict of some kind
146- return errors . New ( "missing entry for deleted file" )
148+ return & Conflict { Type : ConflictDeletedFileMissing , File : f . OldName }
147149 }
148150
149- if err := gitdiff . Apply (ioutil .Discard , bytes .NewReader (data ), f ); err != nil {
151+ if err := apply (ioutil .Discard , bytes .NewReader (data ), f . OldName , f ); err != nil {
150152 return err
151153 }
152154
@@ -160,12 +162,12 @@ func (a *GraphQLApplier) applyModify(ctx context.Context, f *gitdiff.File) error
160162 return err
161163 }
162164 if ! exists {
163- return errors . New ( "no entry for modified file" )
165+ return & Conflict { Type : ConflictModifiedFileMissing , File : f . OldName }
164166 }
165167
166168 if len (f .TextFragments ) > 0 || f .BinaryFragment != nil {
167169 var b bytes.Buffer
168- if err := gitdiff . Apply (& b , bytes .NewReader (data ), f ); err != nil {
170+ if err := apply (& b , bytes .NewReader (data ), f . OldName , f ); err != nil {
169171 return err
170172 }
171173 data = b .Bytes ()
0 commit comments