@@ -151,20 +151,137 @@ int error = git_repository_open_ext(&repo, "/tmp/…",
151
151
152
152
## Diff
153
153
154
- ### Worktree to Index
154
+ ### Index to Workdir
155
155
156
- ``` c
156
+ Like ` git diff ` .
157
157
158
+ ``` c
159
+ git_diff *diff;
160
+ int error = git_diff_index_to_workdir(&diff, repo, NULL , NULL );
158
161
```
159
162
163
+ ([ ` git_diff_index_to_workdir ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_index_to_workdir ) )
164
+
160
165
### HEAD to Index
161
166
167
+ Like ` git diff --cached ` .
168
+
162
169
``` 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));
163
175
176
+ git_diff *diff;
177
+ error = git_diff_tree_to_index(&diff, repo, tree, NULL , NULL );
164
178
```
165
179
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
+
166
203
### Commit to Its Parent
167
204
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
+
168
285
```c
169
286
170
287
```
0 commit comments