-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapply_options.go
More file actions
82 lines (71 loc) · 2.03 KB
/
apply_options.go
File metadata and controls
82 lines (71 loc) · 2.03 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
package git_diff_parser
import "math"
// applyMode controls how the apply engine treats hunks that cannot be placed
// directly into the target content.
type applyMode int
const (
// applyModeApply keeps the output neutral when a hunk cannot be applied.
applyModeApply applyMode = iota
// applyModeMerge renders conflict markers into the output for misses.
applyModeMerge
)
// conflictLabels controls the labels rendered into conflict markers.
// The zero value renders neutral markers without any labels.
type conflictLabels struct {
Current string
Incoming string
}
// applyOptions configures the apply engine.
type applyOptions struct {
Mode applyMode
ConflictLabels conflictLabels
IgnoreWhitespace bool
AllowOverlap bool
MinContext int
MinContextSet bool
}
func defaultApplyOptions() applyOptions {
return applyOptions{
Mode: applyModeApply,
MinContext: math.MaxInt,
}
}
func defaultMergeApplyOptions() applyOptions {
options := defaultApplyOptions()
options.Mode = applyModeMerge
options.ConflictLabels = conflictLabels{
Current: "Current",
Incoming: "Incoming patch",
}
return options
}
// patchApply holds apply-time configuration and mirrors Git's stateful apply design.
type patchApply struct {
options applyOptions
}
func newPatchApply(options applyOptions) *patchApply {
return &patchApply{options: normalizeApplyOptions(options)}
}
func (o applyOptions) normalize() applyOptions {
if o.Mode != applyModeMerge {
o.Mode = applyModeApply
}
if o.Mode == applyModeMerge {
defaults := defaultMergeApplyOptions()
if !o.MinContextSet {
o.MinContext = defaultApplyOptions().MinContext
}
if o.ConflictLabels.Current == "" {
o.ConflictLabels.Current = defaults.ConflictLabels.Current
}
if o.ConflictLabels.Incoming == "" {
o.ConflictLabels.Incoming = defaults.ConflictLabels.Incoming
}
} else if !o.MinContextSet {
o.MinContext = defaultApplyOptions().MinContext
}
return o
}
func normalizeApplyOptions(options applyOptions) applyOptions {
return options.normalize()
}