Skip to content

Commit 34ea173

Browse files
authored
Add apply example to README
1 parent bedbab3 commit 34ea173

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@ aims to parse anything accepted by the `git apply` command.
1212
```golang
1313
patch, err := os.Open("changes.patch")
1414
if err != nil {
15-
log.Fatalf(err)
15+
log.Fatal(err)
1616
}
1717

18+
// files is a slice of *gitdiff.File describing the files changed in the patch
19+
// preamble is a string of the content of the patch before the first file
1820
files, preamble, err := gitdiff.Parse(patch)
1921
if err != nil {
20-
log.Fatalf(err)
22+
log.Fatal(err)
2123
}
2224

23-
// files is a slice of *gitdiff.File describing the files changed in the patch
24-
// preamble is a string of the content of the patch before the first file
25+
code, err := os.Open("code.go")
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
30+
// apply the changes in the patch to a source file
31+
var output bytes.Buffer
32+
if err := gitdiff.NewApplier(code).ApplyFile(&output, files[0]); err != nil {
33+
log.Fatal(err)
34+
}
2535
```
2636

2737
## Development Status

0 commit comments

Comments
 (0)