MCPcopy Create free account
hub / github.com/antonmedv/gitmal / Validate

Method Validate

pkg/gitdiff/gitdiff.go:88–150  ·  view source on GitHub ↗

Validate checks that the fragment is self-consistent and appliable. Validate returns an error if and only if the fragment is invalid.

()

Source from the content-addressed store, hash-verified

86// Validate checks that the fragment is self-consistent and appliable. Validate
87// returns an error if and only if the fragment is invalid.
88func (f *TextFragment) Validate() error {
89 if f == nil {
90 return errors.New("nil fragment")
91 }
92
93 var (
94 oldLines, newLines int64
95 leadingContext, trailingContext int64
96 contextLines, addedLines, deletedLines int64
97 )
98
99 // count the types of lines in the fragment content
100 for i, line := range f.Lines {
101 switch line.Op {
102 case OpContext:
103 oldLines++
104 newLines++
105 contextLines++
106 if addedLines == 0 && deletedLines == 0 {
107 leadingContext++
108 } else {
109 trailingContext++
110 }
111 case OpAdd:
112 newLines++
113 addedLines++
114 trailingContext = 0
115 case OpDelete:
116 oldLines++
117 deletedLines++
118 trailingContext = 0
119 default:
120 return fmt.Errorf("unknown operator %q on line %d", line.Op, i+1)
121 }
122 }
123
124 // check the actual counts against the reported counts
125 if oldLines != f.OldLines {
126 return lineCountErr("old", oldLines, f.OldLines)
127 }
128 if newLines != f.NewLines {
129 return lineCountErr("new", newLines, f.NewLines)
130 }
131 if leadingContext != f.LeadingContext {
132 return lineCountErr("leading context", leadingContext, f.LeadingContext)
133 }
134 if trailingContext != f.TrailingContext {
135 return lineCountErr("trailing context", trailingContext, f.TrailingContext)
136 }
137 if addedLines != f.LinesAdded {
138 return lineCountErr("added", addedLines, f.LinesAdded)
139 }
140 if deletedLines != f.LinesDeleted {
141 return lineCountErr("deleted", deletedLines, f.LinesDeleted)
142 }
143
144 // if a file is being created, it can only contain additions
145 if f.OldPosition == 0 && f.OldLines != 0 {

Callers 2

ApplyFragmentMethod · 0.80
TestTextFragmentValidateFunction · 0.80

Calls 3

lineCountErrFunction · 0.85
NewMethod · 0.80
ErrorfMethod · 0.80

Tested by 1

TestTextFragmentValidateFunction · 0.64