2009-11-04 18:55:06 -07:00
|
|
|
package patch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes";
|
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
|
|
|
type TextDiff []TextChunk
|
|
|
|
|
|
|
|
// A TextChunk specifies an edit to a section of a file:
|
|
|
|
// the text beginning at Line, which should be exactly Old,
|
|
|
|
// is to be replaced with New.
|
|
|
|
type TextChunk struct {
|
|
|
|
Line int;
|
|
|
|
Old []byte;
|
|
|
|
New []byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseTextDiff(raw []byte) (TextDiff, os.Error) {
|
|
|
|
// Copy raw so it is safe to keep references to slices.
|
|
|
|
_, chunks := sections(raw, "@@ -");
|
|
|
|
delta := 0;
|
|
|
|
diff := make(TextDiff, len(chunks));
|
|
|
|
for i, raw := range chunks {
|
|
|
|
c := &diff[i];
|
|
|
|
|
|
|
|
// Parse start line: @@ -oldLine,oldCount +newLine,newCount @@ junk
|
|
|
|
chunk := splitLines(raw);
|
|
|
|
chunkHeader := chunk[0];
|
|
|
|
var ok bool;
|
|
|
|
var oldLine, oldCount, newLine, newCount int;
|
|
|
|
s := chunkHeader;
|
|
|
|
if oldLine, s, ok = atoi(s, "@@ -", 10); !ok {
|
|
|
|
ErrChunkHdr:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("unexpected chunk header line: " + string(chunkHeader))
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if len(s) == 0 || s[0] != ',' {
|
2009-11-09 13:07:39 -07:00
|
|
|
oldCount = 1
|
2009-11-04 18:55:06 -07:00
|
|
|
} else if oldCount, s, ok = atoi(s, ",", 10); !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto ErrChunkHdr
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if newLine, s, ok = atoi(s, " +", 10); !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto ErrChunkHdr
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if len(s) == 0 || s[0] != ',' {
|
2009-11-09 13:07:39 -07:00
|
|
|
newCount = 1
|
2009-11-04 18:55:06 -07:00
|
|
|
} else if newCount, s, ok = atoi(s, ",", 10); !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto ErrChunkHdr
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if !hasPrefix(s, " @@") {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto ErrChunkHdr
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case: for created or deleted files, the empty half
|
|
|
|
// is given as starting at line 0. Translate to line 1.
|
|
|
|
if oldCount == 0 && oldLine == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
oldLine = 1
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if newCount == 0 && newLine == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
newLine = 1
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count lines in text
|
|
|
|
var dropOldNL, dropNewNL bool;
|
|
|
|
var nold, nnew int;
|
|
|
|
var lastch byte;
|
|
|
|
chunk = chunk[1:len(chunk)];
|
|
|
|
for _, l := range chunk {
|
|
|
|
if nold == oldCount && nnew == newCount && (len(l) == 0 || l[0] != '\\') {
|
|
|
|
if len(bytes.TrimSpace(l)) != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("too many chunk lines")
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if len(l) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("empty chunk line")
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
switch l[0] {
|
|
|
|
case '+':
|
2009-11-09 13:07:39 -07:00
|
|
|
nnew++
|
2009-11-04 18:55:06 -07:00
|
|
|
case '-':
|
2009-11-09 13:07:39 -07:00
|
|
|
nold++
|
2009-11-04 18:55:06 -07:00
|
|
|
case ' ':
|
|
|
|
nnew++;
|
|
|
|
nold++;
|
|
|
|
case '\\':
|
|
|
|
if _, ok := skip(l, "\\ No newline at end of file"); ok {
|
|
|
|
switch lastch {
|
|
|
|
case '-':
|
2009-11-09 13:07:39 -07:00
|
|
|
dropOldNL = true
|
2009-11-04 18:55:06 -07:00
|
|
|
case '+':
|
2009-11-09 13:07:39 -07:00
|
|
|
dropNewNL = true
|
2009-11-04 18:55:06 -07:00
|
|
|
case ' ':
|
|
|
|
dropOldNL = true;
|
|
|
|
dropNewNL = true;
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("message `\\ No newline at end of file' out of context")
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fallthrough;
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("unexpected chunk line: " + string(l))
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
lastch = l[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does it match the header?
|
|
|
|
if nold != oldCount || nnew != newCount {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("chunk header does not match line count: " + string(chunkHeader))
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if oldLine+delta != newLine {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, SyntaxError("chunk delta is out of sync with previous chunks")
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
delta += nnew-nold;
|
|
|
|
c.Line = oldLine;
|
|
|
|
|
|
|
|
var old, new bytes.Buffer;
|
|
|
|
nold = 0;
|
|
|
|
nnew = 0;
|
|
|
|
for _, l := range chunk {
|
|
|
|
if nold == oldCount && nnew == newCount {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
ch, l := l[0], l[1:len(l)];
|
|
|
|
if ch == '\\' {
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if ch != '+' {
|
|
|
|
old.Write(l);
|
|
|
|
nold++;
|
|
|
|
}
|
|
|
|
if ch != '-' {
|
|
|
|
new.Write(l);
|
|
|
|
nnew++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Old = old.Bytes();
|
|
|
|
c.New = new.Bytes();
|
|
|
|
if dropOldNL {
|
2009-11-09 13:07:39 -07:00
|
|
|
c.Old = c.Old[0 : len(c.Old)-1]
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if dropNewNL {
|
2009-11-09 13:07:39 -07:00
|
|
|
c.New = c.New[0 : len(c.New)-1]
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrPatchFailure = os.NewError("patch did not apply cleanly")
|
|
|
|
|
|
|
|
// Apply applies the changes listed in the diff
|
|
|
|
// to the data, returning the new version.
|
|
|
|
func (d TextDiff) Apply(data []byte) ([]byte, os.Error) {
|
|
|
|
var buf bytes.Buffer;
|
|
|
|
line := 1;
|
|
|
|
for _, c := range d {
|
|
|
|
var ok bool;
|
|
|
|
var prefix []byte;
|
|
|
|
prefix, data, ok = getLine(data, c.Line - line);
|
|
|
|
if !ok || !bytes.HasPrefix(data, c.Old) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, ErrPatchFailure
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
buf.Write(prefix);
|
|
|
|
data = data[len(c.Old):len(data)];
|
|
|
|
buf.Write(c.New);
|
|
|
|
line = c.Line + bytes.Count(c.Old, newline);
|
|
|
|
}
|
|
|
|
buf.Write(data);
|
|
|
|
return buf.Bytes(), nil;
|
|
|
|
}
|