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