Skip to content

Commit 402aab2

Browse files
committed
Diff examples
1 parent 1ee20cd commit 402aab2

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

css/libgit2.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,3 +2023,11 @@ img {
20232023
.toc-h4 {
20242024
margin-left: 30px;
20252025
}
2026+
2027+
.toc-h1,
2028+
.toc-h2,
2029+
.toc-h3,
2030+
.toc-h4 {
2031+
padding-left: 2em;
2032+
text-indent: -2em;
2033+
}

docs/guides/101-samples/index.md

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,137 @@ int error = git_repository_open_ext(&repo, "/tmp/…",
151151

152152
## Diff
153153

154-
### Worktree to Index
154+
### Index to Workdir
155155

156-
```c
156+
Like `git diff`.
157157

158+
```c
159+
git_diff *diff;
160+
int error = git_diff_index_to_workdir(&diff, repo, NULL, NULL);
158161
```
159162

163+
([`git_diff_index_to_workdir`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_index_to_workdir))
164+
160165
### HEAD to Index
161166

167+
Like `git diff --cached`.
168+
162169
```c
170+
git_object *obj;
171+
int error = git_revparse_single(&obj, repo, "HEAD^{tree}");
172+
173+
git_tree *tree;
174+
error = git_tree_lookup(&tree, repo, git_object_id(obj));
163175

176+
git_diff *diff;
177+
error = git_diff_tree_to_index(&diff, repo, tree, NULL, NULL);
164178
```
165179

180+
([`git_revparse_single`](http://libgit2.github.com/libgit2/#HEAD/group/revparse/git_revparse_single),
181+
[`git_tree_lookup`](http://libgit2.github.com/libgit2/#HEAD/group/tree/git_tree_lookup),
182+
[`git_diff_tree_to_index`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_tree_to_index))
183+
184+
### HEAD to Workdir
185+
186+
Like `git diff HEAD`.
187+
188+
```c
189+
git_object *obj;
190+
int error = git_revparse_single(&obj, repo, "HEAD^{tree}");
191+
192+
git_tree *tree;
193+
error = git_tree_lookup(&tree, repo, git_object_id(obj));
194+
195+
git_diff *diff;
196+
error = git_diff_tree_to_workdir_with_index(&diff, repo, tree, NULL);
197+
```
198+
199+
([`git_revparse_single`](http://libgit2.github.com/libgit2/#HEAD/group/revparse/git_revparse_single),
200+
[`git_tree_lookup`](http://libgit2.github.com/libgit2/#HEAD/group/tree/git_tree_lookup),
201+
[`git_diff_tree_to_workdir_with_index`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_tree_to_workdir_with_index))
202+
166203
### Commit to Its Parent
167204

205+
Like `git show <commit>`.
206+
207+
```c
208+
git_object *obj;
209+
int error = git_revparse_single(&obj, repo, "committish");
210+
211+
git_commit *commit;
212+
error = git_commit_lookup(&commit, repo, git_object_id(obj));
213+
214+
git_commit *parent;
215+
error = git_commit_parent(&parent, commit, 0);
216+
217+
git_tree *commit_tree, *parent_tree;
218+
error = git_commit_tree(&commit_tree, commit);
219+
error = git_commit_tree(&parent_tree, parent);
220+
221+
git_diff *diff;
222+
error = git_diff_tree_to_tree(
223+
&diff, repo, commit_tree, parent_tree, NULL);
224+
```
225+
226+
([`git_revparse_single`](http://libgit2.github.com/libgit2/#HEAD/group/revparse/git_revparse_single),
227+
[`git_commit_lookup`](http://libgit2.github.com/libgit2/#HEAD/group/commit/git_commit_lookup),
228+
[`git_commit_parent`](http://libgit2.github.com/libgit2/#HEAD/group/commit/git_commit_parent),
229+
[`git_commit_tree`](http://libgit2.github.com/libgit2/#HEAD/group/commit/git_commit_tree),
230+
[`git_diff_tree_to_tree`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_tree_to_tree))
231+
232+
233+
### Rename detection
234+
235+
```c
236+
git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
237+
opts.flags = GIT_DIFF_FIND_RENAMES |
238+
GIT_DIFF_FIND_COPIES |
239+
GIT_DIFF_FIND_FOR_UNTRACKED;
240+
241+
int error = git_diff_find_similar(diff, &opts);
242+
```
243+
244+
([`git_diff_find_options`](http://libgit2.github.com/libgit2/#HEAD/type/git_diff_find_options),
245+
[`git_diff_find_similar`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_find_similar))
246+
247+
### Iterating Deltas
248+
249+
```c
250+
int each_file_cb(const git_diff_delta *delta,
251+
float progress,
252+
void *payload)
253+
{
254+
/**/
255+
}
256+
257+
int each_hunk_cb(const git_diff_delta *delta,
258+
const git_diff_hunk *hunk,
259+
void *payload)
260+
{
261+
/**/
262+
}
263+
264+
int each_line_cb(const git_diff_delta *delta,
265+
const git_diff_hunk *hunk,
266+
const git_diff_line *line,
267+
void *payload)
268+
{
269+
/**/
270+
}
271+
272+
int error = git_diff_foreach(diff,
273+
each_file_cb,
274+
each_hunk_cb,
275+
each_line_cb,
276+
NULL);
277+
```
278+
279+
([`git_diff_foreach`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_foreach),
280+
[`git_diff_file_cb`](http://libgit2.github.com/libgit2/#HEAD/type/git_diff_file_cb),
281+
[`git_diff_hunk_cb`](http://libgit2.github.com/libgit2/#HEAD/type/git_diff_hunk_cb))
282+
283+
### Generating a Patch
284+
168285
```c
169286
170287
```

0 commit comments

Comments
 (0)