diff options
author | zeripath <art27@cantab.net> | 2020-03-28 04:13:18 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-28 01:13:18 -0300 |
commit | 7cd47046ea6e1fde2c88290a42f345795aee0ea4 (patch) | |
tree | 6004084e3c478a68a47b47deb0143a2b142fb821 /modules/git/error.go | |
parent | cac30abefc02e36ed08810b7101b4f6a7c8bb599 (diff) | |
download | gitea-7cd47046ea6e1fde2c88290a42f345795aee0ea4.tar.gz gitea-7cd47046ea6e1fde2c88290a42f345795aee0ea4.zip |
Handle push rejection in branch and upload (#10854)
* Handle push rejections and push out-of-date in branch creation and
file upload.
* Remove the duplicated sanitize from services/pull/merge
* Move the errors Err(Merge)PushOutOfDate and ErrPushRejected to
modules/git
* Handle errors better in the upload file dialogs
Fix #10460
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
Diffstat (limited to 'modules/git/error.go')
-rw-r--r-- | modules/git/error.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/modules/git/error.go b/modules/git/error.go index 9266c41b06..85a938a7da 100644 --- a/modules/git/error.go +++ b/modules/git/error.go @@ -6,6 +6,7 @@ package git import ( "fmt" + "strings" "time" ) @@ -85,3 +86,76 @@ func IsErrBranchNotExist(err error) bool { func (err ErrBranchNotExist) Error() string { return fmt.Sprintf("branch does not exist [name: %s]", err.Name) } + +// ErrPushOutOfDate represents an error if merging fails due to unrelated histories +type ErrPushOutOfDate struct { + StdOut string + StdErr string + Err error +} + +// IsErrPushOutOfDate checks if an error is a ErrPushOutOfDate. +func IsErrPushOutOfDate(err error) bool { + _, ok := err.(*ErrPushOutOfDate) + return ok +} + +func (err *ErrPushOutOfDate) Error() string { + return fmt.Sprintf("PushOutOfDate Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) +} + +// Unwrap unwraps the underlying error +func (err *ErrPushOutOfDate) Unwrap() error { + return fmt.Errorf("%v - %s", err.Err, err.StdErr) +} + +// ErrPushRejected represents an error if merging fails due to rejection from a hook +type ErrPushRejected struct { + Message string + StdOut string + StdErr string + Err error +} + +// IsErrPushRejected checks if an error is a ErrPushRejected. +func IsErrPushRejected(err error) bool { + _, ok := err.(*ErrPushRejected) + return ok +} + +func (err *ErrPushRejected) Error() string { + return fmt.Sprintf("PushRejected Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) +} + +// Unwrap unwraps the underlying error +func (err *ErrPushRejected) Unwrap() error { + return fmt.Errorf("%v - %s", err.Err, err.StdErr) +} + +// GenerateMessage generates the remote message from the stderr +func (err *ErrPushRejected) GenerateMessage() { + messageBuilder := &strings.Builder{} + i := strings.Index(err.StdErr, "remote: ") + if i < 0 { + err.Message = "" + return + } + for { + if len(err.StdErr) <= i+8 { + break + } + if err.StdErr[i:i+8] != "remote: " { + break + } + i += 8 + nl := strings.IndexByte(err.StdErr[i:], '\n') + if nl >= 0 { + messageBuilder.WriteString(err.StdErr[i : i+nl+1]) + i = i + nl + 1 + } else { + messageBuilder.WriteString(err.StdErr[i:]) + i = len(err.StdErr) + } + } + err.Message = strings.TrimSpace(messageBuilder.String()) +} |