diff options
author | zeripath <art27@cantab.net> | 2020-02-22 13:08:48 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-22 15:08:48 +0200 |
commit | 089ccb0c80050efc8c3afef2402ea4f4a5ed250e (patch) | |
tree | 4ff172035ffd0a9a5263dedc051a91ab24c33fa6 /models/error.go | |
parent | 2ed9ead6dea29f9e006fa1831892c9c239f4bd70 (diff) | |
download | gitea-089ccb0c80050efc8c3afef2402ea4f4a5ed250e.tar.gz gitea-089ccb0c80050efc8c3afef2402ea4f4a5ed250e.zip |
Handle push rejection message in Merge & Web Editor (#10373)
* Handle push rejection message in Merge
* placate golangci-lint
* Fix sanitize, adjust message handling
* oops
* Oops
* Handle push-rejection in webeditor CRUD too
* Apply suggestions from code review
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'models/error.go')
-rw-r--r-- | models/error.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/models/error.go b/models/error.go index fe9af70f3a..a679372737 100644 --- a/models/error.go +++ b/models/error.go @@ -7,6 +7,7 @@ package models import ( "fmt" + "strings" "code.gitea.io/gitea/modules/git" ) @@ -1371,6 +1372,53 @@ func (err ErrMergePushOutOfDate) Error() string { return fmt.Sprintf("Merge PushOutOfDate Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) } +// ErrPushRejected represents an error if merging fails due to rejection from a hook +type ErrPushRejected struct { + Style MergeStyle + 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("Merge PushRejected Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) +} + +// 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()) +} + // ErrRebaseConflicts represents an error if rebase fails with a conflict type ErrRebaseConflicts struct { Style MergeStyle |