Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (a *Applier) applyCreate(ctx context.Context, f *gitdiff.File) (*github.Tre
return nil, err
}
if exists {
return nil, errors.New("existing entry for new file")
return nil, ErrNewFileAlreadyExists
}

c, err := base64Apply(nil, f)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (a *Applier) applyDelete(ctx context.Context, f *gitdiff.File) (*github.Tre
if !exists {
// because the rest of application is strict, return an error if the
// file was already deleted, since it indicates a conflict of some kind
return nil, errors.New("missing entry for deleted file")
return nil, ErrNoSuchFileToDelete
}

data, _, err := a.client.Git.GetBlobRaw(ctx, a.owner, a.repo, entry.GetSHA())
Expand All @@ -128,7 +128,7 @@ func (a *Applier) applyDelete(ctx context.Context, f *gitdiff.File) (*github.Tre
}

if err := gitdiff.Apply(ioutil.Discard, bytes.NewReader(data), f); err != nil {
return nil, err
return nil, wrapGitdiffApplyError(err)
}

path := f.OldName
Expand All @@ -147,7 +147,7 @@ func (a *Applier) applyModify(ctx context.Context, f *gitdiff.File) (*github.Tre
return nil, err
}
if !exists {
return nil, errors.New("no entry for modified file")
return nil, ErrNoSuchFileToModify
}

path := f.NewName
Expand Down Expand Up @@ -346,7 +346,7 @@ func base64Apply(data []byte, f *gitdiff.File) (string, error) {

enc := base64.NewEncoder(base64.StdEncoding, &b)
if err := gitdiff.Apply(enc, bytes.NewReader(data), f); err != nil {
return "", err
return "", wrapGitdiffApplyError(err)
}
if err := enc.Close(); err != nil {
return "", fmt.Errorf("base64 encoding failed: %w", err)
Expand Down
17 changes: 17 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ package patch2pr
import (
"errors"
"fmt"

"github.com/bluekeyes/go-gitdiff/gitdiff"
)

var (
ErrConflict = errors.New("conflict prevented applying patch")

ErrNewFileAlreadyExists = fmt.Errorf("%w: existing entry for new file", ErrConflict)
ErrNoSuchFileToDelete = fmt.Errorf("%w: missing entry for deleted file", ErrConflict)
ErrNoSuchFileToModify = fmt.Errorf("%w: no entry for modified file", ErrConflict)
)

func wrapGitdiffApplyError(err error) error {
if errors.Is(err, &gitdiff.Conflict{}) {
return fmt.Errorf("%w: gitdiff apply failed: %w", ErrConflict, err)
}
return fmt.Errorf("gitdiff apply failed: %w", err)
}

func unsupported(msg string, args ...any) error {
return unsupportedErr{reason: fmt.Sprintf(msg, args...)}
}
Expand Down
13 changes: 6 additions & 7 deletions graphql_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -122,12 +121,12 @@ func (a *GraphQLApplier) applyCreate(ctx context.Context, f *gitdiff.File) error
return err
}
if exists {
return errors.New("existing entry for new file")
return ErrNewFileAlreadyExists
}

var b bytes.Buffer
if err := gitdiff.Apply(&b, bytes.NewReader(nil), f); err != nil {
return err
return wrapGitdiffApplyError(err)
}

a.changes[f.NewName] = pendingChange{Content: b.Bytes()}
Expand All @@ -143,11 +142,11 @@ func (a *GraphQLApplier) applyDelete(ctx context.Context, f *gitdiff.File) error
if !exists {
// because the rest of application is strict, return an error if the
// file was already deleted, since it indicates a conflict of some kind
return errors.New("missing entry for deleted file")
return ErrNoSuchFileToDelete
}

if err := gitdiff.Apply(ioutil.Discard, bytes.NewReader(data), f); err != nil {
return err
return wrapGitdiffApplyError(err)
}

a.changes[f.OldName] = pendingChange{IsDelete: true}
Expand All @@ -160,13 +159,13 @@ func (a *GraphQLApplier) applyModify(ctx context.Context, f *gitdiff.File) error
return err
}
if !exists {
return errors.New("no entry for modified file")
return ErrNoSuchFileToModify
}

if len(f.TextFragments) > 0 || f.BinaryFragment != nil {
var b bytes.Buffer
if err := gitdiff.Apply(&b, bytes.NewReader(data), f); err != nil {
return err
return wrapGitdiffApplyError(err)
}
data = b.Bytes()
}
Expand Down