-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconflict.go
More file actions
93 lines (78 loc) · 2.28 KB
/
Copy pathconflict.go
File metadata and controls
93 lines (78 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package patch2pr
import (
"fmt"
"strings"
"github.com/bluekeyes/go-gitdiff/gitdiff"
)
// Conflict is the error returned when applying a patch fails because of a
// conflict between the patch and the target.
type Conflict struct {
// The type of the conflict.
Type ConflictType
// The path to the file in which the conflict occurs.
File string
// The line number of the conflict, if known.
Line int64
cause *gitdiff.Conflict
}
// ConflictType identifies the type of conflict.
type ConflictType int
const (
// ConflictUnspecified indicates the cause of the conflict was not specified.
ConflictUnspecified ConflictType = iota
// ConflictNewFileExists indicates the patch creates a new file that already exists.
ConflictNewFileExists
// ConflictDeletedFileMissing indicates the patch deletes a file that does not exist.
ConflictDeletedFileMissing
// ConflictModifiedFileMissing indicates the patch modifies a file that does not exist.
ConflictModifiedFileMissing
// ConflictContent indicates the patch content does not apply cleanly against the file's content.
ConflictContent
)
func (c *Conflict) Error() string {
var msg strings.Builder
if c.File != "" {
msg.WriteString(c.File)
if c.Line > 0 {
fmt.Fprintf(&msg, ":%d", c.Line)
}
msg.WriteString(": ")
}
switch c.Type {
case ConflictNewFileExists:
msg.WriteString("conflict: new file already exists")
case ConflictDeletedFileMissing:
msg.WriteString("conflict: deleted file does not exist")
case ConflictModifiedFileMissing:
msg.WriteString("conflict: modified file does not exist")
case ConflictContent:
if c.cause != nil {
msg.WriteString(c.cause.Error())
} else {
msg.WriteString("conflict: content")
}
default:
msg.WriteString("conflict")
}
return msg.String()
}
// Is returns true if all of the non-zero fields of target equal the values of
// this Conflict. Passing an empty *Conflict{} always returns true.
func (c *Conflict) Is(target error) bool {
if other, ok := target.(*Conflict); ok {
if other.Type == ConflictUnspecified {
if other.File == "" {
return true
}
return c.File == other.File
}
if other.File == "" {
return c.Type == other.Type
}
return other.Type == c.Type && other.File == c.File
}
return false
}
func (c *Conflict) Unwrap() error {
return c.cause
}