github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/error.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 // Copyright 2015 The Gogs Authors. All rights reserved. 7 // Use of this source code is governed by a MIT-style 8 // license that can be found in the LICENSE file. 9 10 package git 11 12 import ( 13 "fmt" 14 "strings" 15 "time" 16 ) 17 18 // ErrExecTimeout error when exec timed out 19 type ErrExecTimeout struct { 20 Duration time.Duration 21 } 22 23 // IsErrExecTimeout if some error is ErrExecTimeout 24 func IsErrExecTimeout(err error) bool { 25 _, ok := err.(ErrExecTimeout) 26 return ok 27 } 28 29 func (err ErrExecTimeout) Error() string { 30 return fmt.Sprintf("execution is timeout [duration: %v]", err.Duration) 31 } 32 33 // ErrNotExist commit not exist error 34 type ErrNotExist struct { 35 ID string 36 RelPath string 37 } 38 39 // IsErrNotExist if some error is ErrNotExist 40 func IsErrNotExist(err error) bool { 41 _, ok := err.(ErrNotExist) 42 return ok 43 } 44 45 func (err ErrNotExist) Error() string { 46 return fmt.Sprintf("object does not exist [id: %s, rel_path: %s]", err.ID, err.RelPath) 47 } 48 49 // ErrBadLink entry.FollowLink error 50 type ErrBadLink struct { 51 Name string 52 Message string 53 } 54 55 func (err ErrBadLink) Error() string { 56 return fmt.Sprintf("%s: %s", err.Name, err.Message) 57 } 58 59 // IsErrBadLink if some error is ErrBadLink 60 func IsErrBadLink(err error) bool { 61 _, ok := err.(ErrBadLink) 62 return ok 63 } 64 65 // ErrUnsupportedVersion error when required git version not matched 66 type ErrUnsupportedVersion struct { 67 Required string 68 } 69 70 // IsErrUnsupportedVersion if some error is ErrUnsupportedVersion 71 func IsErrUnsupportedVersion(err error) bool { 72 _, ok := err.(ErrUnsupportedVersion) 73 return ok 74 } 75 76 func (err ErrUnsupportedVersion) Error() string { 77 return fmt.Sprintf("Operation requires higher version [required: %s]", err.Required) 78 } 79 80 // ErrBranchNotExist represents a "BranchNotExist" kind of error. 81 type ErrBranchNotExist struct { 82 Name string 83 } 84 85 // IsErrBranchNotExist checks if an error is a ErrBranchNotExist. 86 func IsErrBranchNotExist(err error) bool { 87 _, ok := err.(ErrBranchNotExist) 88 return ok 89 } 90 91 func (err ErrBranchNotExist) Error() string { 92 return fmt.Sprintf("branch does not exist [name: %s]", err.Name) 93 } 94 95 // ErrPushOutOfDate represents an error if merging fails due to unrelated histories 96 type ErrPushOutOfDate struct { 97 StdOut string 98 StdErr string 99 Err error 100 } 101 102 // IsErrPushOutOfDate checks if an error is a ErrPushOutOfDate. 103 func IsErrPushOutOfDate(err error) bool { 104 _, ok := err.(*ErrPushOutOfDate) 105 return ok 106 } 107 108 func (err *ErrPushOutOfDate) Error() string { 109 return fmt.Sprintf("PushOutOfDate Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) 110 } 111 112 // Unwrap unwraps the underlying error 113 func (err *ErrPushOutOfDate) Unwrap() error { 114 return fmt.Errorf("%v - %s", err.Err, err.StdErr) 115 } 116 117 // ErrPushRejected represents an error if merging fails due to rejection from a hook 118 type ErrPushRejected struct { 119 Message string 120 StdOut string 121 StdErr string 122 Err error 123 } 124 125 // IsErrPushRejected checks if an error is a ErrPushRejected. 126 func IsErrPushRejected(err error) bool { 127 _, ok := err.(*ErrPushRejected) 128 return ok 129 } 130 131 func (err *ErrPushRejected) Error() string { 132 return fmt.Sprintf("PushRejected Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) 133 } 134 135 // Unwrap unwraps the underlying error 136 func (err *ErrPushRejected) Unwrap() error { 137 return fmt.Errorf("%v - %s", err.Err, err.StdErr) 138 } 139 140 // GenerateMessage generates the remote message from the stderr 141 func (err *ErrPushRejected) GenerateMessage() { 142 messageBuilder := &strings.Builder{} 143 i := strings.Index(err.StdErr, "remote: ") 144 if i < 0 { 145 err.Message = "" 146 return 147 } 148 for { 149 if len(err.StdErr) <= i+8 { 150 break 151 } 152 if err.StdErr[i:i+8] != "remote: " { 153 break 154 } 155 i += 8 156 nl := strings.IndexByte(err.StdErr[i:], '\n') 157 if nl >= 0 { 158 messageBuilder.WriteString(err.StdErr[i : i+nl+1]) 159 i = i + nl + 1 160 } else { 161 messageBuilder.WriteString(err.StdErr[i:]) 162 i = len(err.StdErr) 163 } 164 } 165 err.Message = strings.TrimSpace(messageBuilder.String()) 166 } 167 168 // ErrMoreThanOne represents an error if pull request fails when there are more than one sources (branch, tag) with the same name 169 type ErrMoreThanOne struct { 170 StdOut string 171 StdErr string 172 Err error 173 } 174 175 // IsErrMoreThanOne checks if an error is a ErrMoreThanOne 176 func IsErrMoreThanOne(err error) bool { 177 _, ok := err.(*ErrMoreThanOne) 178 return ok 179 } 180 181 func (err *ErrMoreThanOne) Error() string { 182 return fmt.Sprintf("ErrMoreThanOne Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut) 183 }