]> git.proxmox.com Git - libgit2.git/blob - src/diff.c
Merge pull request #3477 from linquize/inttypes.h
[libgit2.git] / src / diff.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #include "common.h"
8 #include "diff.h"
9 #include "fileops.h"
10 #include "config.h"
11 #include "attr_file.h"
12 #include "filter.h"
13 #include "pathspec.h"
14 #include "index.h"
15 #include "odb.h"
16 #include "submodule.h"
17
18 #define DIFF_FLAG_IS_SET(DIFF,FLAG) (((DIFF)->opts.flags & (FLAG)) != 0)
19 #define DIFF_FLAG_ISNT_SET(DIFF,FLAG) (((DIFF)->opts.flags & (FLAG)) == 0)
20 #define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->opts.flags = \
21 (VAL) ? ((DIFF)->opts.flags | (FLAG)) : ((DIFF)->opts.flags & ~(VAL))
22
23 static git_diff_delta *diff_delta__alloc(
24 git_diff *diff,
25 git_delta_t status,
26 const char *path)
27 {
28 git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
29 if (!delta)
30 return NULL;
31
32 delta->old_file.path = git_pool_strdup(&diff->pool, path);
33 if (delta->old_file.path == NULL) {
34 git__free(delta);
35 return NULL;
36 }
37
38 delta->new_file.path = delta->old_file.path;
39
40 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
41 switch (status) {
42 case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
43 case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
44 default: break; /* leave other status values alone */
45 }
46 }
47 delta->status = status;
48
49 return delta;
50 }
51
52 static int diff_insert_delta(
53 git_diff *diff, git_diff_delta *delta, const char *matched_pathspec)
54 {
55 int error = 0;
56
57 if (diff->opts.notify_cb) {
58 error = diff->opts.notify_cb(
59 diff, delta, matched_pathspec, diff->opts.notify_payload);
60
61 if (error) {
62 git__free(delta);
63
64 if (error > 0) /* positive value means to skip this delta */
65 return 0;
66 else /* negative value means to cancel diff */
67 return giterr_set_after_callback_function(error, "git_diff");
68 }
69 }
70
71 if ((error = git_vector_insert(&diff->deltas, delta)) < 0)
72 git__free(delta);
73
74 return error;
75 }
76
77 static bool diff_pathspec_match(
78 const char **matched_pathspec,
79 git_diff *diff,
80 const git_index_entry *entry)
81 {
82 bool disable_pathspec_match =
83 DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
84
85 /* If we're disabling fnmatch, then the iterator has already applied
86 * the filters to the files for us and we don't have to do anything.
87 * However, this only applies to *files* - the iterator will include
88 * directories that we need to recurse into when not autoexpanding,
89 * so we still need to apply the pathspec match to directories.
90 */
91 if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
92 disable_pathspec_match) {
93 *matched_pathspec = entry->path;
94 return true;
95 }
96
97 return git_pathspec__match(
98 &diff->pathspec, entry->path, disable_pathspec_match,
99 DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
100 matched_pathspec, NULL);
101 }
102
103 static int diff_delta__from_one(
104 git_diff *diff,
105 git_delta_t status,
106 const git_index_entry *oitem,
107 const git_index_entry *nitem)
108 {
109 const git_index_entry *entry = nitem;
110 bool has_old = false;
111 git_diff_delta *delta;
112 const char *matched_pathspec;
113
114 assert((oitem != NULL) ^ (nitem != NULL));
115
116 if (oitem) {
117 entry = oitem;
118 has_old = true;
119 }
120
121 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
122 has_old = !has_old;
123
124 if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
125 return 0;
126
127 if (status == GIT_DELTA_IGNORED &&
128 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
129 return 0;
130
131 if (status == GIT_DELTA_UNTRACKED &&
132 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
133 return 0;
134
135 if (status == GIT_DELTA_UNREADABLE &&
136 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
137 return 0;
138
139 if (!diff_pathspec_match(&matched_pathspec, diff, entry))
140 return 0;
141
142 delta = diff_delta__alloc(diff, status, entry->path);
143 GITERR_CHECK_ALLOC(delta);
144
145 /* This fn is just for single-sided diffs */
146 assert(status != GIT_DELTA_MODIFIED);
147 delta->nfiles = 1;
148
149 if (has_old) {
150 delta->old_file.mode = entry->mode;
151 delta->old_file.size = entry->file_size;
152 delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
153 git_oid_cpy(&delta->old_file.id, &entry->id);
154 } else /* ADDED, IGNORED, UNTRACKED */ {
155 delta->new_file.mode = entry->mode;
156 delta->new_file.size = entry->file_size;
157 delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
158 git_oid_cpy(&delta->new_file.id, &entry->id);
159 }
160
161 delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
162
163 if (has_old || !git_oid_iszero(&delta->new_file.id))
164 delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
165
166 return diff_insert_delta(diff, delta, matched_pathspec);
167 }
168
169 static int diff_delta__from_two(
170 git_diff *diff,
171 git_delta_t status,
172 const git_index_entry *old_entry,
173 uint32_t old_mode,
174 const git_index_entry *new_entry,
175 uint32_t new_mode,
176 const git_oid *new_id,
177 const char *matched_pathspec)
178 {
179 const git_oid *old_id = &old_entry->id;
180 git_diff_delta *delta;
181 const char *canonical_path = old_entry->path;
182
183 if (status == GIT_DELTA_UNMODIFIED &&
184 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
185 return 0;
186
187 if (!new_id)
188 new_id = &new_entry->id;
189
190 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
191 uint32_t temp_mode = old_mode;
192 const git_index_entry *temp_entry = old_entry;
193 const git_oid *temp_id = old_id;
194
195 old_entry = new_entry;
196 new_entry = temp_entry;
197 old_mode = new_mode;
198 new_mode = temp_mode;
199 old_id = new_id;
200 new_id = temp_id;
201 }
202
203 delta = diff_delta__alloc(diff, status, canonical_path);
204 GITERR_CHECK_ALLOC(delta);
205 delta->nfiles = 2;
206
207 if (!git_index_entry_is_conflict(old_entry)) {
208 delta->old_file.size = old_entry->file_size;
209 delta->old_file.mode = old_mode;
210 git_oid_cpy(&delta->old_file.id, old_id);
211 delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
212 GIT_DIFF_FLAG_EXISTS;
213 }
214
215 if (!git_index_entry_is_conflict(new_entry)) {
216 git_oid_cpy(&delta->new_file.id, new_id);
217 delta->new_file.size = new_entry->file_size;
218 delta->new_file.mode = new_mode;
219 delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
220 delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
221
222 if (!git_oid_iszero(&new_entry->id))
223 delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
224 }
225
226 return diff_insert_delta(diff, delta, matched_pathspec);
227 }
228
229 static git_diff_delta *diff_delta__last_for_item(
230 git_diff *diff,
231 const git_index_entry *item)
232 {
233 git_diff_delta *delta = git_vector_last(&diff->deltas);
234 if (!delta)
235 return NULL;
236
237 switch (delta->status) {
238 case GIT_DELTA_UNMODIFIED:
239 case GIT_DELTA_DELETED:
240 if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
241 return delta;
242 break;
243 case GIT_DELTA_ADDED:
244 if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
245 return delta;
246 break;
247 case GIT_DELTA_UNREADABLE:
248 case GIT_DELTA_UNTRACKED:
249 if (diff->strcomp(delta->new_file.path, item->path) == 0 &&
250 git_oid__cmp(&delta->new_file.id, &item->id) == 0)
251 return delta;
252 break;
253 case GIT_DELTA_MODIFIED:
254 if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
255 git_oid__cmp(&delta->new_file.id, &item->id) == 0)
256 return delta;
257 break;
258 default:
259 break;
260 }
261
262 return NULL;
263 }
264
265 static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
266 {
267 size_t len = strlen(prefix);
268
269 /* append '/' at end if needed */
270 if (len > 0 && prefix[len - 1] != '/')
271 return git_pool_strcat(pool, prefix, "/");
272 else
273 return git_pool_strndup(pool, prefix, len + 1);
274 }
275
276 GIT_INLINE(const char *) diff_delta__path(const git_diff_delta *delta)
277 {
278 const char *str = delta->old_file.path;
279
280 if (!str ||
281 delta->status == GIT_DELTA_ADDED ||
282 delta->status == GIT_DELTA_RENAMED ||
283 delta->status == GIT_DELTA_COPIED)
284 str = delta->new_file.path;
285
286 return str;
287 }
288
289 const char *git_diff_delta__path(const git_diff_delta *delta)
290 {
291 return diff_delta__path(delta);
292 }
293
294 int git_diff_delta__cmp(const void *a, const void *b)
295 {
296 const git_diff_delta *da = a, *db = b;
297 int val = strcmp(diff_delta__path(da), diff_delta__path(db));
298 return val ? val : ((int)da->status - (int)db->status);
299 }
300
301 int git_diff_delta__casecmp(const void *a, const void *b)
302 {
303 const git_diff_delta *da = a, *db = b;
304 int val = strcasecmp(diff_delta__path(da), diff_delta__path(db));
305 return val ? val : ((int)da->status - (int)db->status);
306 }
307
308 GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
309 {
310 return delta->old_file.path ?
311 delta->old_file.path : delta->new_file.path;
312 }
313
314 int git_diff_delta__i2w_cmp(const void *a, const void *b)
315 {
316 const git_diff_delta *da = a, *db = b;
317 int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
318 return val ? val : ((int)da->status - (int)db->status);
319 }
320
321 int git_diff_delta__i2w_casecmp(const void *a, const void *b)
322 {
323 const git_diff_delta *da = a, *db = b;
324 int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
325 return val ? val : ((int)da->status - (int)db->status);
326 }
327
328 bool git_diff_delta__should_skip(
329 const git_diff_options *opts, const git_diff_delta *delta)
330 {
331 uint32_t flags = opts ? opts->flags : 0;
332
333 if (delta->status == GIT_DELTA_UNMODIFIED &&
334 (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
335 return true;
336
337 if (delta->status == GIT_DELTA_IGNORED &&
338 (flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
339 return true;
340
341 if (delta->status == GIT_DELTA_UNTRACKED &&
342 (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
343 return true;
344
345 if (delta->status == GIT_DELTA_UNREADABLE &&
346 (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
347 return true;
348
349 return false;
350 }
351
352
353 static const char *diff_mnemonic_prefix(
354 git_iterator_type_t type, bool left_side)
355 {
356 const char *pfx = "";
357
358 switch (type) {
359 case GIT_ITERATOR_TYPE_EMPTY: pfx = "c"; break;
360 case GIT_ITERATOR_TYPE_TREE: pfx = "c"; break;
361 case GIT_ITERATOR_TYPE_INDEX: pfx = "i"; break;
362 case GIT_ITERATOR_TYPE_WORKDIR: pfx = "w"; break;
363 case GIT_ITERATOR_TYPE_FS: pfx = left_side ? "1" : "2"; break;
364 default: break;
365 }
366
367 /* note: without a deeper look at pathspecs, there is no easy way
368 * to get the (o)bject / (w)ork tree mnemonics working...
369 */
370
371 return pfx;
372 }
373
374 static int diff_entry_cmp(const void *a, const void *b)
375 {
376 const git_index_entry *entry_a = a;
377 const git_index_entry *entry_b = b;
378
379 return strcmp(entry_a->path, entry_b->path);
380 }
381
382 static int diff_entry_icmp(const void *a, const void *b)
383 {
384 const git_index_entry *entry_a = a;
385 const git_index_entry *entry_b = b;
386
387 return strcasecmp(entry_a->path, entry_b->path);
388 }
389
390 static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
391 {
392 if (!ignore_case) {
393 diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
394
395 diff->strcomp = git__strcmp;
396 diff->strncomp = git__strncmp;
397 diff->pfxcomp = git__prefixcmp;
398 diff->entrycomp = diff_entry_cmp;
399
400 git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
401 } else {
402 diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
403
404 diff->strcomp = git__strcasecmp;
405 diff->strncomp = git__strncasecmp;
406 diff->pfxcomp = git__prefixcmp_icase;
407 diff->entrycomp = diff_entry_icmp;
408
409 git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
410 }
411
412 git_vector_sort(&diff->deltas);
413 }
414
415 static git_diff *diff_list_alloc(
416 git_repository *repo,
417 git_iterator *old_iter,
418 git_iterator *new_iter)
419 {
420 git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
421 git_diff *diff = git__calloc(1, sizeof(git_diff));
422 if (!diff)
423 return NULL;
424
425 assert(repo && old_iter && new_iter);
426
427 GIT_REFCOUNT_INC(diff);
428 diff->repo = repo;
429 diff->old_src = old_iter->type;
430 diff->new_src = new_iter->type;
431 memcpy(&diff->opts, &dflt, sizeof(diff->opts));
432
433 if (git_vector_init(&diff->deltas, 0, git_diff_delta__cmp) < 0 ||
434 git_pool_init(&diff->pool, 1, 0) < 0) {
435 git_diff_free(diff);
436 return NULL;
437 }
438
439 /* Use case-insensitive compare if either iterator has
440 * the ignore_case bit set */
441 diff_set_ignore_case(
442 diff,
443 git_iterator_ignore_case(old_iter) ||
444 git_iterator_ignore_case(new_iter));
445
446 return diff;
447 }
448
449 static int diff_list_apply_options(
450 git_diff *diff,
451 const git_diff_options *opts)
452 {
453 git_config *cfg = NULL;
454 git_repository *repo = diff->repo;
455 git_pool *pool = &diff->pool;
456 int val;
457
458 if (opts) {
459 /* copy user options (except case sensitivity info from iterators) */
460 bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
461 memcpy(&diff->opts, opts, sizeof(diff->opts));
462 DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
463
464 /* initialize pathspec from options */
465 if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
466 return -1;
467 }
468
469 /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
470 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
471 diff->opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
472
473 /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
474 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
475 diff->opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
476
477 /* load config values that affect diff behavior */
478 if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
479 return val;
480
481 if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val)
482 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_HAS_SYMLINKS;
483
484 if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val)
485 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_IGNORE_STAT;
486
487 if ((diff->opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
488 !git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val)
489 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_MODE_BITS;
490
491 if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val)
492 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_CTIME;
493
494 /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
495
496 /* Don't trust nanoseconds; we do not load nanos from disk */
497 #ifdef GIT_USE_NSEC
498 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_NANOSECS;
499 #endif
500
501 /* If not given explicit `opts`, check `diff.xyz` configs */
502 if (!opts) {
503 int context = git_config__get_int_force(cfg, "diff.context", 3);
504 diff->opts.context_lines = context >= 0 ? (uint32_t)context : 3;
505
506 /* add other defaults here */
507 }
508
509 /* Reverse src info if diff is reversed */
510 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
511 git_iterator_type_t tmp_src = diff->old_src;
512 diff->old_src = diff->new_src;
513 diff->new_src = tmp_src;
514 }
515
516 /* Unset UPDATE_INDEX unless diffing workdir and index */
517 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
518 (!(diff->old_src == GIT_ITERATOR_TYPE_WORKDIR ||
519 diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ||
520 !(diff->old_src == GIT_ITERATOR_TYPE_INDEX ||
521 diff->new_src == GIT_ITERATOR_TYPE_INDEX)))
522 diff->opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
523
524 /* if ignore_submodules not explicitly set, check diff config */
525 if (diff->opts.ignore_submodules <= 0) {
526 git_config_entry *entry;
527 git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
528
529 if (entry && git_submodule_parse_ignore(
530 &diff->opts.ignore_submodules, entry->value) < 0)
531 giterr_clear();
532 git_config_entry_free(entry);
533 }
534
535 /* if either prefix is not set, figure out appropriate value */
536 if (!diff->opts.old_prefix || !diff->opts.new_prefix) {
537 const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
538 const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
539
540 if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
541 use_old = use_new = "";
542 else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
543 use_old = diff_mnemonic_prefix(diff->old_src, true);
544 use_new = diff_mnemonic_prefix(diff->new_src, false);
545 }
546
547 if (!diff->opts.old_prefix)
548 diff->opts.old_prefix = use_old;
549 if (!diff->opts.new_prefix)
550 diff->opts.new_prefix = use_new;
551 }
552
553 /* strdup prefix from pool so we're not dependent on external data */
554 diff->opts.old_prefix = diff_strdup_prefix(pool, diff->opts.old_prefix);
555 diff->opts.new_prefix = diff_strdup_prefix(pool, diff->opts.new_prefix);
556
557 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
558 const char *tmp_prefix = diff->opts.old_prefix;
559 diff->opts.old_prefix = diff->opts.new_prefix;
560 diff->opts.new_prefix = tmp_prefix;
561 }
562
563 git_config_free(cfg);
564
565 /* check strdup results for error */
566 return (!diff->opts.old_prefix || !diff->opts.new_prefix) ? -1 : 0;
567 }
568
569 static void diff_list_free(git_diff *diff)
570 {
571 git_vector_free_deep(&diff->deltas);
572
573 git_pathspec__vfree(&diff->pathspec);
574 git_pool_clear(&diff->pool);
575
576 git__memzero(diff, sizeof(*diff));
577 git__free(diff);
578 }
579
580 void git_diff_free(git_diff *diff)
581 {
582 if (!diff)
583 return;
584
585 GIT_REFCOUNT_DEC(diff, diff_list_free);
586 }
587
588 void git_diff_addref(git_diff *diff)
589 {
590 GIT_REFCOUNT_INC(diff);
591 }
592
593 int git_diff__oid_for_file(
594 git_oid *out,
595 git_diff *diff,
596 const char *path,
597 uint16_t mode,
598 git_off_t size)
599 {
600 git_index_entry entry;
601
602 memset(&entry, 0, sizeof(entry));
603 entry.mode = mode;
604 entry.file_size = size;
605 entry.path = (char *)path;
606
607 return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
608 }
609
610 int git_diff__oid_for_entry(
611 git_oid *out,
612 git_diff *diff,
613 const git_index_entry *src,
614 uint16_t mode,
615 const git_oid *update_match)
616 {
617 int error = 0;
618 git_buf full_path = GIT_BUF_INIT;
619 git_index_entry entry = *src;
620 git_filter_list *fl = NULL;
621
622 memset(out, 0, sizeof(*out));
623
624 if (git_buf_joinpath(
625 &full_path, git_repository_workdir(diff->repo), entry.path) < 0)
626 return -1;
627
628 if (!mode) {
629 struct stat st;
630
631 diff->perf.stat_calls++;
632
633 if (p_stat(full_path.ptr, &st) < 0) {
634 error = git_path_set_error(errno, entry.path, "stat");
635 git_buf_free(&full_path);
636 return error;
637 }
638
639 git_index_entry__init_from_stat(
640 &entry, &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
641 }
642
643 /* calculate OID for file if possible */
644 if (S_ISGITLINK(mode)) {
645 git_submodule *sm;
646
647 if (!git_submodule_lookup(&sm, diff->repo, entry.path)) {
648 const git_oid *sm_oid = git_submodule_wd_id(sm);
649 if (sm_oid)
650 git_oid_cpy(out, sm_oid);
651 git_submodule_free(sm);
652 } else {
653 /* if submodule lookup failed probably just in an intermediate
654 * state where some init hasn't happened, so ignore the error
655 */
656 giterr_clear();
657 }
658 } else if (S_ISLNK(mode)) {
659 error = git_odb__hashlink(out, full_path.ptr);
660 diff->perf.oid_calculations++;
661 } else if (!git__is_sizet(entry.file_size)) {
662 giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'",
663 entry.path);
664 error = -1;
665 } else if (!(error = git_filter_list_load(
666 &fl, diff->repo, NULL, entry.path,
667 GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
668 {
669 int fd = git_futils_open_ro(full_path.ptr);
670 if (fd < 0)
671 error = fd;
672 else {
673 error = git_odb__hashfd_filtered(
674 out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
675 p_close(fd);
676 diff->perf.oid_calculations++;
677 }
678
679 git_filter_list_free(fl);
680 }
681
682 /* update index for entry if requested */
683 if (!error && update_match && git_oid_equal(out, update_match)) {
684 git_index *idx;
685 git_index_entry updated_entry;
686
687 memcpy(&updated_entry, &entry, sizeof(git_index_entry));
688 updated_entry.mode = mode;
689 git_oid_cpy(&updated_entry.id, out);
690
691 if (!(error = git_repository_index__weakptr(&idx, diff->repo))) {
692 error = git_index_add(idx, &updated_entry);
693 diff->index_updated = true;
694 }
695 }
696
697 git_buf_free(&full_path);
698 return error;
699 }
700
701 static bool diff_time_eq(
702 const git_index_time *a, const git_index_time *b, bool use_nanos)
703 {
704 return a->seconds == b->seconds &&
705 (!use_nanos || a->nanoseconds == b->nanoseconds);
706 }
707
708 typedef struct {
709 git_repository *repo;
710 git_iterator *old_iter;
711 git_iterator *new_iter;
712 const git_index_entry *oitem;
713 const git_index_entry *nitem;
714 } diff_in_progress;
715
716 #define MODE_BITS_MASK 0000777
717
718 static int maybe_modified_submodule(
719 git_delta_t *status,
720 git_oid *found_oid,
721 git_diff *diff,
722 diff_in_progress *info)
723 {
724 int error = 0;
725 git_submodule *sub;
726 unsigned int sm_status = 0;
727 git_submodule_ignore_t ign = diff->opts.ignore_submodules;
728
729 *status = GIT_DELTA_UNMODIFIED;
730
731 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
732 ign == GIT_SUBMODULE_IGNORE_ALL)
733 return 0;
734
735 if ((error = git_submodule_lookup(
736 &sub, diff->repo, info->nitem->path)) < 0) {
737
738 /* GIT_EEXISTS means dir with .git in it was found - ignore it */
739 if (error == GIT_EEXISTS) {
740 giterr_clear();
741 error = 0;
742 }
743 return error;
744 }
745
746 if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
747 /* ignore it */;
748 else if ((error = git_submodule__status(
749 &sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
750 /* return error below */;
751
752 /* check IS_WD_UNMODIFIED because this case is only used
753 * when the new side of the diff is the working directory
754 */
755 else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
756 *status = GIT_DELTA_MODIFIED;
757
758 /* now that we have a HEAD OID, check if HEAD moved */
759 else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
760 !git_oid_equal(&info->oitem->id, found_oid))
761 *status = GIT_DELTA_MODIFIED;
762
763 git_submodule_free(sub);
764 return error;
765 }
766
767 static int maybe_modified(
768 git_diff *diff,
769 diff_in_progress *info)
770 {
771 git_oid noid;
772 git_delta_t status = GIT_DELTA_MODIFIED;
773 const git_index_entry *oitem = info->oitem;
774 const git_index_entry *nitem = info->nitem;
775 unsigned int omode = oitem->mode;
776 unsigned int nmode = nitem->mode;
777 bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
778 bool modified_uncertain = false;
779 const char *matched_pathspec;
780 int error = 0;
781
782 if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
783 return 0;
784
785 memset(&noid, 0, sizeof(noid));
786
787 /* on platforms with no symlinks, preserve mode of existing symlinks */
788 if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
789 !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
790 nmode = omode;
791
792 /* on platforms with no execmode, just preserve old mode */
793 if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
794 (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
795 new_is_workdir)
796 nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
797
798 /* if one side is a conflict, mark the whole delta as conflicted */
799 if (git_index_entry_is_conflict(oitem) ||
800 git_index_entry_is_conflict(nitem)) {
801 status = GIT_DELTA_CONFLICTED;
802
803 /* support "assume unchanged" (poorly, b/c we still stat everything) */
804 } else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
805 status = GIT_DELTA_UNMODIFIED;
806
807 /* support "skip worktree" index bit */
808 } else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
809 status = GIT_DELTA_UNMODIFIED;
810
811 /* if basic type of file changed, then split into delete and add */
812 } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
813 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
814 status = GIT_DELTA_TYPECHANGE;
815 }
816
817 else if (nmode == GIT_FILEMODE_UNREADABLE) {
818 if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
819 error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
820 return error;
821 }
822
823 else {
824 if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
825 error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
826 return error;
827 }
828
829 /* if oids and modes match (and are valid), then file is unmodified */
830 } else if (git_oid_equal(&oitem->id, &nitem->id) &&
831 omode == nmode &&
832 !git_oid_iszero(&oitem->id)) {
833 status = GIT_DELTA_UNMODIFIED;
834
835 /* if we have an unknown OID and a workdir iterator, then check some
836 * circumstances that can accelerate things or need special handling
837 */
838 } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
839 bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
840 bool use_nanos = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_NANOSECS) != 0);
841 git_index *index;
842 git_iterator_index(&index, info->new_iter);
843
844 status = GIT_DELTA_UNMODIFIED;
845
846 if (S_ISGITLINK(nmode)) {
847 if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
848 return error;
849 }
850
851 /* if the stat data looks different, then mark modified - this just
852 * means that the OID will be recalculated below to confirm change
853 */
854 else if (omode != nmode || oitem->file_size != nitem->file_size) {
855 status = GIT_DELTA_MODIFIED;
856 modified_uncertain =
857 (oitem->file_size <= 0 && nitem->file_size > 0);
858 }
859 else if (!diff_time_eq(&oitem->mtime, &nitem->mtime, use_nanos) ||
860 (use_ctime &&
861 !diff_time_eq(&oitem->ctime, &nitem->ctime, use_nanos)) ||
862 oitem->ino != nitem->ino ||
863 oitem->uid != nitem->uid ||
864 oitem->gid != nitem->gid ||
865 (index && nitem->mtime.seconds >= index->stamp.mtime))
866 {
867 status = GIT_DELTA_MODIFIED;
868 modified_uncertain = true;
869 }
870
871 /* if mode is GITLINK and submodules are ignored, then skip */
872 } else if (S_ISGITLINK(nmode) &&
873 DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
874 status = GIT_DELTA_UNMODIFIED;
875 }
876
877 /* if we got here and decided that the files are modified, but we
878 * haven't calculated the OID of the new item, then calculate it now
879 */
880 if (modified_uncertain && git_oid_iszero(&nitem->id)) {
881 const git_oid *update_check =
882 DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
883 &oitem->id : NULL;
884
885 if ((error = git_diff__oid_for_entry(
886 &noid, diff, nitem, nmode, update_check)) < 0)
887 return error;
888
889 /* if oid matches, then mark unmodified (except submodules, where
890 * the filesystem content may be modified even if the oid still
891 * matches between the index and the workdir HEAD)
892 */
893 if (omode == nmode && !S_ISGITLINK(omode) &&
894 git_oid_equal(&oitem->id, &noid))
895 status = GIT_DELTA_UNMODIFIED;
896 }
897
898 /* If we want case changes, then break this into a delete of the old
899 * and an add of the new so that consumers can act accordingly (eg,
900 * checkout will update the case on disk.)
901 */
902 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
903 DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
904 strcmp(oitem->path, nitem->path) != 0) {
905
906 if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
907 error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
908
909 return error;
910 }
911
912 return diff_delta__from_two(
913 diff, status, oitem, omode, nitem, nmode,
914 git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec);
915 }
916
917 static bool entry_is_prefixed(
918 git_diff *diff,
919 const git_index_entry *item,
920 const git_index_entry *prefix_item)
921 {
922 size_t pathlen;
923
924 if (!item || diff->pfxcomp(item->path, prefix_item->path) != 0)
925 return false;
926
927 pathlen = strlen(prefix_item->path);
928
929 return (prefix_item->path[pathlen - 1] == '/' ||
930 item->path[pathlen] == '\0' ||
931 item->path[pathlen] == '/');
932 }
933
934 static int iterator_current(
935 const git_index_entry **entry,
936 git_iterator *iterator)
937 {
938 int error;
939
940 if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
941 *entry = NULL;
942 error = 0;
943 }
944
945 return error;
946 }
947
948 static int iterator_advance(
949 const git_index_entry **entry,
950 git_iterator *iterator)
951 {
952 const git_index_entry *prev_entry = *entry;
953 int cmp, error;
954
955 /* if we're looking for conflicts, we only want to report
956 * one conflict for each file, instead of all three sides.
957 * so if this entry is a conflict for this file, and the
958 * previous one was a conflict for the same file, skip it.
959 */
960 while ((error = git_iterator_advance(entry, iterator)) == 0) {
961 if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
962 !git_index_entry_is_conflict(prev_entry) ||
963 !git_index_entry_is_conflict(*entry))
964 break;
965
966 cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
967 strcasecmp(prev_entry->path, (*entry)->path) :
968 strcmp(prev_entry->path, (*entry)->path);
969
970 if (cmp)
971 break;
972 }
973
974 if (error == GIT_ITEROVER) {
975 *entry = NULL;
976 error = 0;
977 }
978
979 return error;
980 }
981
982 static int iterator_advance_into(
983 const git_index_entry **entry,
984 git_iterator *iterator)
985 {
986 int error;
987
988 if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
989 *entry = NULL;
990 error = 0;
991 }
992
993 return error;
994 }
995
996 static int iterator_advance_over_with_status(
997 const git_index_entry **entry,
998 git_iterator_status_t *status,
999 git_iterator *iterator)
1000 {
1001 int error;
1002
1003 if ((error = git_iterator_advance_over_with_status(
1004 entry, status, iterator)) == GIT_ITEROVER) {
1005 *entry = NULL;
1006 error = 0;
1007 }
1008
1009 return error;
1010 }
1011
1012 static int handle_unmatched_new_item(
1013 git_diff *diff, diff_in_progress *info)
1014 {
1015 int error = 0;
1016 const git_index_entry *nitem = info->nitem;
1017 git_delta_t delta_type = GIT_DELTA_UNTRACKED;
1018 bool contains_oitem;
1019
1020 /* check if this is a prefix of the other side */
1021 contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
1022
1023 /* update delta_type if this item is conflicted */
1024 if (git_index_entry_is_conflict(nitem))
1025 delta_type = GIT_DELTA_CONFLICTED;
1026
1027 /* update delta_type if this item is ignored */
1028 else if (git_iterator_current_is_ignored(info->new_iter))
1029 delta_type = GIT_DELTA_IGNORED;
1030
1031 if (nitem->mode == GIT_FILEMODE_TREE) {
1032 bool recurse_into_dir = contains_oitem;
1033
1034 /* check if user requests recursion into this type of dir */
1035 recurse_into_dir = contains_oitem ||
1036 (delta_type == GIT_DELTA_UNTRACKED &&
1037 DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
1038 (delta_type == GIT_DELTA_IGNORED &&
1039 DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
1040
1041 /* do not advance into directories that contain a .git file */
1042 if (recurse_into_dir && !contains_oitem) {
1043 git_buf *full = NULL;
1044 if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
1045 return -1;
1046 if (full && git_path_contains(full, DOT_GIT)) {
1047 /* TODO: warning if not a valid git repository */
1048 recurse_into_dir = false;
1049 }
1050 }
1051
1052 /* still have to look into untracked directories to match core git -
1053 * with no untracked files, directory is treated as ignored
1054 */
1055 if (!recurse_into_dir &&
1056 delta_type == GIT_DELTA_UNTRACKED &&
1057 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
1058 {
1059 git_diff_delta *last;
1060 git_iterator_status_t untracked_state;
1061
1062 /* attempt to insert record for this directory */
1063 if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
1064 return error;
1065
1066 /* if delta wasn't created (because of rules), just skip ahead */
1067 last = diff_delta__last_for_item(diff, nitem);
1068 if (!last)
1069 return iterator_advance(&info->nitem, info->new_iter);
1070
1071 /* iterate into dir looking for an actual untracked file */
1072 if ((error = iterator_advance_over_with_status(
1073 &info->nitem, &untracked_state, info->new_iter)) < 0)
1074 return error;
1075
1076 /* if we found nothing that matched our pathlist filter, exclude */
1077 if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
1078 git_vector_pop(&diff->deltas);
1079 git__free(last);
1080 }
1081
1082 /* if we found nothing or just ignored items, update the record */
1083 if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
1084 untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
1085 last->status = GIT_DELTA_IGNORED;
1086
1087 /* remove the record if we don't want ignored records */
1088 if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
1089 git_vector_pop(&diff->deltas);
1090 git__free(last);
1091 }
1092 }
1093
1094 return 0;
1095 }
1096
1097 /* try to advance into directory if necessary */
1098 if (recurse_into_dir) {
1099 error = iterator_advance_into(&info->nitem, info->new_iter);
1100
1101 /* if real error or no error, proceed with iteration */
1102 if (error != GIT_ENOTFOUND)
1103 return error;
1104 giterr_clear();
1105
1106 /* if directory is empty, can't advance into it, so either skip
1107 * it or ignore it
1108 */
1109 if (contains_oitem)
1110 return iterator_advance(&info->nitem, info->new_iter);
1111 delta_type = GIT_DELTA_IGNORED;
1112 }
1113 }
1114
1115 else if (delta_type == GIT_DELTA_IGNORED &&
1116 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
1117 git_iterator_current_tree_is_ignored(info->new_iter))
1118 /* item contained in ignored directory, so skip over it */
1119 return iterator_advance(&info->nitem, info->new_iter);
1120
1121 else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) {
1122 if (delta_type != GIT_DELTA_CONFLICTED)
1123 delta_type = GIT_DELTA_ADDED;
1124 }
1125
1126 else if (nitem->mode == GIT_FILEMODE_COMMIT) {
1127 /* ignore things that are not actual submodules */
1128 if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
1129 giterr_clear();
1130 delta_type = GIT_DELTA_IGNORED;
1131
1132 /* if this contains a tracked item, treat as normal TREE */
1133 if (contains_oitem) {
1134 error = iterator_advance_into(&info->nitem, info->new_iter);
1135 if (error != GIT_ENOTFOUND)
1136 return error;
1137
1138 giterr_clear();
1139 return iterator_advance(&info->nitem, info->new_iter);
1140 }
1141 }
1142 }
1143
1144 else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
1145 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
1146 delta_type = GIT_DELTA_UNTRACKED;
1147 else
1148 delta_type = GIT_DELTA_UNREADABLE;
1149 }
1150
1151 /* Actually create the record for this item if necessary */
1152 if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
1153 return error;
1154
1155 /* If user requested TYPECHANGE records, then check for that instead of
1156 * just generating an ADDED/UNTRACKED record
1157 */
1158 if (delta_type != GIT_DELTA_IGNORED &&
1159 DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1160 contains_oitem)
1161 {
1162 /* this entry was prefixed with a tree - make TYPECHANGE */
1163 git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
1164 if (last) {
1165 last->status = GIT_DELTA_TYPECHANGE;
1166 last->old_file.mode = GIT_FILEMODE_TREE;
1167 }
1168 }
1169
1170 return iterator_advance(&info->nitem, info->new_iter);
1171 }
1172
1173 static int handle_unmatched_old_item(
1174 git_diff *diff, diff_in_progress *info)
1175 {
1176 git_delta_t delta_type = GIT_DELTA_DELETED;
1177 int error;
1178
1179 /* update delta_type if this item is conflicted */
1180 if (git_index_entry_is_conflict(info->oitem))
1181 delta_type = GIT_DELTA_CONFLICTED;
1182
1183 if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
1184 return error;
1185
1186 /* if we are generating TYPECHANGE records then check for that
1187 * instead of just generating a DELETE record
1188 */
1189 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1190 entry_is_prefixed(diff, info->nitem, info->oitem))
1191 {
1192 /* this entry has become a tree! convert to TYPECHANGE */
1193 git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
1194 if (last) {
1195 last->status = GIT_DELTA_TYPECHANGE;
1196 last->new_file.mode = GIT_FILEMODE_TREE;
1197 }
1198
1199 /* If new_iter is a workdir iterator, then this situation
1200 * will certainly be followed by a series of untracked items.
1201 * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
1202 */
1203 if (S_ISDIR(info->nitem->mode) &&
1204 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
1205 return iterator_advance(&info->nitem, info->new_iter);
1206 }
1207
1208 return iterator_advance(&info->oitem, info->old_iter);
1209 }
1210
1211 static int handle_matched_item(
1212 git_diff *diff, diff_in_progress *info)
1213 {
1214 int error = 0;
1215
1216 if ((error = maybe_modified(diff, info)) < 0)
1217 return error;
1218
1219 if (!(error = iterator_advance(&info->oitem, info->old_iter)))
1220 error = iterator_advance(&info->nitem, info->new_iter);
1221
1222 return error;
1223 }
1224
1225 int git_diff__from_iterators(
1226 git_diff **diff_ptr,
1227 git_repository *repo,
1228 git_iterator *old_iter,
1229 git_iterator *new_iter,
1230 const git_diff_options *opts)
1231 {
1232 int error = 0;
1233 diff_in_progress info;
1234 git_diff *diff;
1235
1236 *diff_ptr = NULL;
1237
1238 diff = diff_list_alloc(repo, old_iter, new_iter);
1239 GITERR_CHECK_ALLOC(diff);
1240
1241 info.repo = repo;
1242 info.old_iter = old_iter;
1243 info.new_iter = new_iter;
1244
1245 /* make iterators have matching icase behavior */
1246 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
1247 if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 ||
1248 (error = git_iterator_set_ignore_case(new_iter, true)) < 0)
1249 goto cleanup;
1250 }
1251
1252 /* finish initialization */
1253 if ((error = diff_list_apply_options(diff, opts)) < 0)
1254 goto cleanup;
1255
1256 if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
1257 (error = iterator_current(&info.nitem, new_iter)) < 0)
1258 goto cleanup;
1259
1260 /* run iterators building diffs */
1261 while (!error && (info.oitem || info.nitem)) {
1262 int cmp = info.oitem ?
1263 (info.nitem ? diff->entrycomp(info.oitem, info.nitem) : -1) : 1;
1264
1265 /* create DELETED records for old items not matched in new */
1266 if (cmp < 0)
1267 error = handle_unmatched_old_item(diff, &info);
1268
1269 /* create ADDED, TRACKED, or IGNORED records for new items not
1270 * matched in old (and/or descend into directories as needed)
1271 */
1272 else if (cmp > 0)
1273 error = handle_unmatched_new_item(diff, &info);
1274
1275 /* otherwise item paths match, so create MODIFIED record
1276 * (or ADDED and DELETED pair if type changed)
1277 */
1278 else
1279 error = handle_matched_item(diff, &info);
1280 }
1281
1282 diff->perf.stat_calls += old_iter->stat_calls + new_iter->stat_calls;
1283
1284 cleanup:
1285 if (!error)
1286 *diff_ptr = diff;
1287 else
1288 git_diff_free(diff);
1289
1290 return error;
1291 }
1292
1293 #define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
1294 git_iterator *a = NULL, *b = NULL; \
1295 char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
1296 git_pathspec_prefix(&opts->pathspec) : NULL; \
1297 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
1298 b_opts = GIT_ITERATOR_OPTIONS_INIT; \
1299 a_opts.flags = FLAGS_FIRST; \
1300 a_opts.start = pfx; \
1301 a_opts.end = pfx; \
1302 b_opts.flags = FLAGS_SECOND; \
1303 b_opts.start = pfx; \
1304 b_opts.end = pfx; \
1305 GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
1306 if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
1307 a_opts.pathlist.strings = opts->pathspec.strings; \
1308 a_opts.pathlist.count = opts->pathspec.count; \
1309 b_opts.pathlist.strings = opts->pathspec.strings; \
1310 b_opts.pathlist.count = opts->pathspec.count; \
1311 } \
1312 if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
1313 error = git_diff__from_iterators(diff, repo, a, b, opts); \
1314 git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
1315 } while (0)
1316
1317 int git_diff_tree_to_tree(
1318 git_diff **diff,
1319 git_repository *repo,
1320 git_tree *old_tree,
1321 git_tree *new_tree,
1322 const git_diff_options *opts)
1323 {
1324 git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
1325 int error = 0;
1326
1327 assert(diff && repo);
1328
1329 /* for tree to tree diff, be case sensitive even if the index is
1330 * currently case insensitive, unless the user explicitly asked
1331 * for case insensitivity
1332 */
1333 if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
1334 iflag = GIT_ITERATOR_IGNORE_CASE;
1335
1336 DIFF_FROM_ITERATORS(
1337 git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
1338 git_iterator_for_tree(&b, new_tree, &b_opts), iflag
1339 );
1340
1341 return error;
1342 }
1343
1344 static int diff_load_index(git_index **index, git_repository *repo)
1345 {
1346 int error = git_repository_index__weakptr(index, repo);
1347
1348 /* reload the repository index when user did not pass one in */
1349 if (!error && git_index_read(*index, false) < 0)
1350 giterr_clear();
1351
1352 return error;
1353 }
1354
1355 int git_diff_tree_to_index(
1356 git_diff **diff,
1357 git_repository *repo,
1358 git_tree *old_tree,
1359 git_index *index,
1360 const git_diff_options *opts)
1361 {
1362 git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
1363 GIT_ITERATOR_INCLUDE_CONFLICTS;
1364 bool index_ignore_case = false;
1365 int error = 0;
1366
1367 assert(diff && repo);
1368
1369 if (!index && (error = diff_load_index(&index, repo)) < 0)
1370 return error;
1371
1372 index_ignore_case = index->ignore_case;
1373
1374 DIFF_FROM_ITERATORS(
1375 git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
1376 git_iterator_for_index(&b, index, &b_opts), iflag
1377 );
1378
1379 /* if index is in case-insensitive order, re-sort deltas to match */
1380 if (!error && index_ignore_case)
1381 diff_set_ignore_case(*diff, true);
1382
1383 return error;
1384 }
1385
1386 int git_diff_index_to_workdir(
1387 git_diff **diff,
1388 git_repository *repo,
1389 git_index *index,
1390 const git_diff_options *opts)
1391 {
1392 int error = 0;
1393
1394 assert(diff && repo);
1395
1396 if (!index && (error = diff_load_index(&index, repo)) < 0)
1397 return error;
1398
1399 DIFF_FROM_ITERATORS(
1400 git_iterator_for_index(&a, index, &a_opts),
1401 GIT_ITERATOR_INCLUDE_CONFLICTS,
1402
1403 git_iterator_for_workdir(&b, repo, index, NULL, &b_opts),
1404 GIT_ITERATOR_DONT_AUTOEXPAND
1405 );
1406
1407 if (!error && DIFF_FLAG_IS_SET(*diff, GIT_DIFF_UPDATE_INDEX) && (*diff)->index_updated)
1408 error = git_index_write(index);
1409
1410 return error;
1411 }
1412
1413 int git_diff_tree_to_workdir(
1414 git_diff **diff,
1415 git_repository *repo,
1416 git_tree *old_tree,
1417 const git_diff_options *opts)
1418 {
1419 int error = 0;
1420 git_index *index;
1421
1422 assert(diff && repo);
1423
1424 if ((error = git_repository_index__weakptr(&index, repo)))
1425 return error;
1426
1427 DIFF_FROM_ITERATORS(
1428 git_iterator_for_tree(&a, old_tree, &a_opts), 0,
1429 git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND
1430 );
1431
1432 return error;
1433 }
1434
1435 int git_diff_tree_to_workdir_with_index(
1436 git_diff **diff,
1437 git_repository *repo,
1438 git_tree *old_tree,
1439 const git_diff_options *opts)
1440 {
1441 int error = 0;
1442 git_diff *d1 = NULL, *d2 = NULL;
1443 git_index *index = NULL;
1444
1445 assert(diff && repo);
1446
1447 if ((error = diff_load_index(&index, repo)) < 0)
1448 return error;
1449
1450 if (!(error = git_diff_tree_to_index(&d1, repo, old_tree, index, opts)) &&
1451 !(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
1452 error = git_diff_merge(d1, d2);
1453
1454 git_diff_free(d2);
1455
1456 if (error) {
1457 git_diff_free(d1);
1458 d1 = NULL;
1459 }
1460
1461 *diff = d1;
1462 return error;
1463 }
1464
1465 int git_diff_index_to_index(
1466 git_diff **diff,
1467 git_repository *repo,
1468 git_index *old_index,
1469 git_index *new_index,
1470 const git_diff_options *opts)
1471 {
1472 int error = 0;
1473
1474 assert(diff && old_index && new_index);
1475
1476 DIFF_FROM_ITERATORS(
1477 git_iterator_for_index(&a, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE,
1478 git_iterator_for_index(&b, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE
1479 );
1480
1481 /* if index is in case-insensitive order, re-sort deltas to match */
1482 if (!error && (old_index->ignore_case || new_index->ignore_case))
1483 diff_set_ignore_case(*diff, true);
1484
1485 return error;
1486 }
1487
1488 size_t git_diff_num_deltas(const git_diff *diff)
1489 {
1490 assert(diff);
1491 return diff->deltas.length;
1492 }
1493
1494 size_t git_diff_num_deltas_of_type(const git_diff *diff, git_delta_t type)
1495 {
1496 size_t i, count = 0;
1497 const git_diff_delta *delta;
1498
1499 assert(diff);
1500
1501 git_vector_foreach(&diff->deltas, i, delta) {
1502 count += (delta->status == type);
1503 }
1504
1505 return count;
1506 }
1507
1508 const git_diff_delta *git_diff_get_delta(const git_diff *diff, size_t idx)
1509 {
1510 assert(diff);
1511 return git_vector_get(&diff->deltas, idx);
1512 }
1513
1514 int git_diff_is_sorted_icase(const git_diff *diff)
1515 {
1516 return (diff->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
1517 }
1518
1519 int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
1520 {
1521 assert(out);
1522 GITERR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
1523 out->stat_calls = diff->perf.stat_calls;
1524 out->oid_calculations = diff->perf.oid_calculations;
1525 return 0;
1526 }
1527
1528 int git_diff__paired_foreach(
1529 git_diff *head2idx,
1530 git_diff *idx2wd,
1531 int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
1532 void *payload)
1533 {
1534 int cmp, error = 0;
1535 git_diff_delta *h2i, *i2w;
1536 size_t i, j, i_max, j_max;
1537 int (*strcomp)(const char *, const char *) = git__strcmp;
1538 bool h2i_icase, i2w_icase, icase_mismatch;
1539
1540 i_max = head2idx ? head2idx->deltas.length : 0;
1541 j_max = idx2wd ? idx2wd->deltas.length : 0;
1542 if (!i_max && !j_max)
1543 return 0;
1544
1545 /* At some point, tree-to-index diffs will probably never ignore case,
1546 * even if that isn't true now. Index-to-workdir diffs may or may not
1547 * ignore case, but the index filename for the idx2wd diff should
1548 * still be using the canonical case-preserving name.
1549 *
1550 * Therefore the main thing we need to do here is make sure the diffs
1551 * are traversed in a compatible order. To do this, we temporarily
1552 * resort a mismatched diff to get the order correct.
1553 *
1554 * In order to traverse renames in the index->workdir, we need to
1555 * ensure that we compare the index name on both sides, so we
1556 * always sort by the old name in the i2w list.
1557 */
1558 h2i_icase = head2idx != NULL &&
1559 (head2idx->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
1560
1561 i2w_icase = idx2wd != NULL &&
1562 (idx2wd->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
1563
1564 icase_mismatch =
1565 (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
1566
1567 if (icase_mismatch && h2i_icase) {
1568 git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
1569 git_vector_sort(&head2idx->deltas);
1570 }
1571
1572 if (i2w_icase && !icase_mismatch) {
1573 strcomp = git__strcasecmp;
1574
1575 git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_casecmp);
1576 git_vector_sort(&idx2wd->deltas);
1577 } else if (idx2wd != NULL) {
1578 git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_cmp);
1579 git_vector_sort(&idx2wd->deltas);
1580 }
1581
1582 for (i = 0, j = 0; i < i_max || j < j_max; ) {
1583 h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
1584 i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
1585
1586 cmp = !i2w ? -1 : !h2i ? 1 :
1587 strcomp(h2i->new_file.path, i2w->old_file.path);
1588
1589 if (cmp < 0) {
1590 i++; i2w = NULL;
1591 } else if (cmp > 0) {
1592 j++; h2i = NULL;
1593 } else {
1594 i++; j++;
1595 }
1596
1597 if ((error = cb(h2i, i2w, payload)) != 0) {
1598 giterr_set_after_callback(error);
1599 break;
1600 }
1601 }
1602
1603 /* restore case-insensitive delta sort */
1604 if (icase_mismatch && h2i_icase) {
1605 git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
1606 git_vector_sort(&head2idx->deltas);
1607 }
1608
1609 /* restore idx2wd sort by new path */
1610 if (idx2wd != NULL) {
1611 git_vector_set_cmp(&idx2wd->deltas,
1612 i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
1613 git_vector_sort(&idx2wd->deltas);
1614 }
1615
1616 return error;
1617 }
1618
1619 int git_diff__commit(
1620 git_diff **diff,
1621 git_repository *repo,
1622 const git_commit *commit,
1623 const git_diff_options *opts)
1624 {
1625 git_commit *parent = NULL;
1626 git_diff *commit_diff = NULL;
1627 git_tree *old_tree = NULL, *new_tree = NULL;
1628 size_t parents;
1629 int error = 0;
1630
1631 if ((parents = git_commit_parentcount(commit)) > 1) {
1632 char commit_oidstr[GIT_OID_HEXSZ + 1];
1633
1634 error = -1;
1635 giterr_set(GITERR_INVALID, "Commit %s is a merge commit",
1636 git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
1637 goto on_error;
1638 }
1639
1640 if (parents > 0)
1641 if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
1642 (error = git_commit_tree(&old_tree, parent)) < 0)
1643 goto on_error;
1644
1645 if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
1646 (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
1647 goto on_error;
1648
1649 *diff = commit_diff;
1650
1651 on_error:
1652 git_tree_free(new_tree);
1653 git_tree_free(old_tree);
1654 git_commit_free(parent);
1655
1656 return error;
1657 }
1658
1659 int git_diff_format_email__append_header_tobuf(
1660 git_buf *out,
1661 const git_oid *id,
1662 const git_signature *author,
1663 const char *summary,
1664 size_t patch_no,
1665 size_t total_patches,
1666 bool exclude_patchno_marker)
1667 {
1668 char idstr[GIT_OID_HEXSZ + 1];
1669 char date_str[GIT_DATE_RFC2822_SZ];
1670 int error = 0;
1671
1672 git_oid_fmt(idstr, id);
1673 idstr[GIT_OID_HEXSZ] = '\0';
1674
1675 if ((error = git__date_rfc2822_fmt(date_str, sizeof(date_str), &author->when)) < 0)
1676 return error;
1677
1678 error = git_buf_printf(out,
1679 "From %s Mon Sep 17 00:00:00 2001\n" \
1680 "From: %s <%s>\n" \
1681 "Date: %s\n" \
1682 "Subject: ",
1683 idstr,
1684 author->name, author->email,
1685 date_str);
1686
1687 if (error < 0)
1688 return error;
1689
1690 if (!exclude_patchno_marker) {
1691 if (total_patches == 1) {
1692 error = git_buf_puts(out, "[PATCH] ");
1693 } else {
1694 error = git_buf_printf(out, "[PATCH %"PRIuZ"/%"PRIuZ"] ", patch_no, total_patches);
1695 }
1696
1697 if (error < 0)
1698 return error;
1699 }
1700
1701 error = git_buf_printf(out, "%s\n\n", summary);
1702
1703 return error;
1704 }
1705
1706 int git_diff_format_email__append_patches_tobuf(
1707 git_buf *out,
1708 git_diff *diff)
1709 {
1710 size_t i, deltas;
1711 int error = 0;
1712
1713 deltas = git_diff_num_deltas(diff);
1714
1715 for (i = 0; i < deltas; ++i) {
1716 git_patch *patch = NULL;
1717
1718 if ((error = git_patch_from_diff(&patch, diff, i)) >= 0)
1719 error = git_patch_to_buf(out, patch);
1720
1721 git_patch_free(patch);
1722
1723 if (error < 0)
1724 break;
1725 }
1726
1727 return error;
1728 }
1729
1730 int git_diff_format_email(
1731 git_buf *out,
1732 git_diff *diff,
1733 const git_diff_format_email_options *opts)
1734 {
1735 git_diff_stats *stats = NULL;
1736 char *summary = NULL, *loc = NULL;
1737 bool ignore_marker;
1738 unsigned int format_flags = 0;
1739 size_t allocsize;
1740 int error;
1741
1742 assert(out && diff && opts);
1743 assert(opts->summary && opts->id && opts->author);
1744
1745 GITERR_CHECK_VERSION(opts, GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, "git_format_email_options");
1746
1747 if ((ignore_marker = opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) == false) {
1748 if (opts->patch_no > opts->total_patches) {
1749 giterr_set(GITERR_INVALID, "patch %"PRIuZ" out of range. max %"PRIuZ, opts->patch_no, opts->total_patches);
1750 return -1;
1751 }
1752
1753 if (opts->patch_no == 0) {
1754 giterr_set(GITERR_INVALID, "invalid patch no %"PRIuZ". should be >0", opts->patch_no);
1755 return -1;
1756 }
1757 }
1758
1759 /* the summary we receive may not be clean.
1760 * it could potentially contain new line characters
1761 * or not be set, sanitize, */
1762 if ((loc = strpbrk(opts->summary, "\r\n")) != NULL) {
1763 size_t offset = 0;
1764
1765 if ((offset = (loc - opts->summary)) == 0) {
1766 giterr_set(GITERR_INVALID, "summary is empty");
1767 error = -1;
1768 goto on_error;
1769 }
1770
1771 GITERR_CHECK_ALLOC_ADD(&allocsize, offset, 1);
1772 summary = git__calloc(allocsize, sizeof(char));
1773 GITERR_CHECK_ALLOC(summary);
1774
1775 strncpy(summary, opts->summary, offset);
1776 }
1777
1778 error = git_diff_format_email__append_header_tobuf(out,
1779 opts->id, opts->author, summary == NULL ? opts->summary : summary,
1780 opts->patch_no, opts->total_patches, ignore_marker);
1781
1782 if (error < 0)
1783 goto on_error;
1784
1785 format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY;
1786
1787 if ((error = git_buf_puts(out, "---\n")) < 0 ||
1788 (error = git_diff_get_stats(&stats, diff)) < 0 ||
1789 (error = git_diff_stats_to_buf(out, stats, format_flags, 0)) < 0 ||
1790 (error = git_buf_putc(out, '\n')) < 0 ||
1791 (error = git_diff_format_email__append_patches_tobuf(out, diff)) < 0)
1792 goto on_error;
1793
1794 error = git_buf_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");
1795
1796 on_error:
1797 git__free(summary);
1798 git_diff_stats_free(stats);
1799
1800 return error;
1801 }
1802
1803 int git_diff_commit_as_email(
1804 git_buf *out,
1805 git_repository *repo,
1806 git_commit *commit,
1807 size_t patch_no,
1808 size_t total_patches,
1809 git_diff_format_email_flags_t flags,
1810 const git_diff_options *diff_opts)
1811 {
1812 git_diff *diff = NULL;
1813 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
1814 int error;
1815
1816 assert (out && repo && commit);
1817
1818 opts.flags = flags;
1819 opts.patch_no = patch_no;
1820 opts.total_patches = total_patches;
1821 opts.id = git_commit_id(commit);
1822 opts.summary = git_commit_summary(commit);
1823 opts.author = git_commit_author(commit);
1824
1825 if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
1826 return error;
1827
1828 error = git_diff_format_email(out, diff, &opts);
1829
1830 git_diff_free(diff);
1831 return error;
1832 }
1833
1834 int git_diff_init_options(git_diff_options *opts, unsigned int version)
1835 {
1836 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
1837 opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
1838 return 0;
1839 }
1840
1841 int git_diff_find_init_options(
1842 git_diff_find_options *opts, unsigned int version)
1843 {
1844 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
1845 opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
1846 return 0;
1847 }
1848
1849 int git_diff_format_email_init_options(
1850 git_diff_format_email_options *opts, unsigned int version)
1851 {
1852 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
1853 opts, version, git_diff_format_email_options,
1854 GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT);
1855 return 0;
1856 }