2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
11 #include "attr_file.h"
16 #include "submodule.h"
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))
23 static git_diff_delta
*diff_delta__alloc(
28 git_diff_delta
*delta
= git__calloc(1, sizeof(git_diff_delta
));
32 delta
->old_file
.path
= git_pool_strdup(&diff
->pool
, path
);
33 if (delta
->old_file
.path
== NULL
) {
38 delta
->new_file
.path
= delta
->old_file
.path
;
40 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_REVERSE
)) {
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 */
47 delta
->status
= status
;
52 static int diff_insert_delta(
53 git_diff
*diff
, git_diff_delta
*delta
, const char *matched_pathspec
)
57 if (diff
->opts
.notify_cb
) {
58 error
= diff
->opts
.notify_cb(
59 diff
, delta
, matched_pathspec
, diff
->opts
.notify_payload
);
64 if (error
> 0) /* positive value means to skip this delta */
66 else /* negative value means to cancel diff */
67 return giterr_set_after_callback_function(error
, "git_diff");
71 if ((error
= git_vector_insert(&diff
->deltas
, delta
)) < 0)
77 static bool diff_pathspec_match(
78 const char **matched_pathspec
, git_diff
*diff
, const char *path
)
80 /* The iterator has filtered out paths for us, so the fact that we're
81 * seeing this patch means that it must match the given path list.
83 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_DISABLE_PATHSPEC_MATCH
)) {
84 *matched_pathspec
= path
;
88 return git_pathspec__match(
89 &diff
->pathspec
, path
, false,
90 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_IGNORE_CASE
),
91 matched_pathspec
, NULL
);
94 static int diff_delta__from_one(
97 const git_index_entry
*oitem
,
98 const git_index_entry
*nitem
)
100 const git_index_entry
*entry
= nitem
;
101 bool has_old
= false;
102 git_diff_delta
*delta
;
103 const char *matched_pathspec
;
105 assert((oitem
!= NULL
) ^ (nitem
!= NULL
));
112 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_REVERSE
))
115 if ((entry
->flags
& GIT_IDXENTRY_VALID
) != 0)
118 if (status
== GIT_DELTA_IGNORED
&&
119 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_INCLUDE_IGNORED
))
122 if (status
== GIT_DELTA_UNTRACKED
&&
123 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_INCLUDE_UNTRACKED
))
126 if (status
== GIT_DELTA_UNREADABLE
&&
127 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_INCLUDE_UNREADABLE
))
130 if (!diff_pathspec_match(&matched_pathspec
, diff
, entry
->path
))
133 delta
= diff_delta__alloc(diff
, status
, entry
->path
);
134 GITERR_CHECK_ALLOC(delta
);
136 /* This fn is just for single-sided diffs */
137 assert(status
!= GIT_DELTA_MODIFIED
);
141 delta
->old_file
.mode
= entry
->mode
;
142 delta
->old_file
.size
= entry
->file_size
;
143 delta
->old_file
.flags
|= GIT_DIFF_FLAG_EXISTS
;
144 git_oid_cpy(&delta
->old_file
.id
, &entry
->id
);
145 } else /* ADDED, IGNORED, UNTRACKED */ {
146 delta
->new_file
.mode
= entry
->mode
;
147 delta
->new_file
.size
= entry
->file_size
;
148 delta
->new_file
.flags
|= GIT_DIFF_FLAG_EXISTS
;
149 git_oid_cpy(&delta
->new_file
.id
, &entry
->id
);
152 delta
->old_file
.flags
|= GIT_DIFF_FLAG_VALID_ID
;
154 if (has_old
|| !git_oid_iszero(&delta
->new_file
.id
))
155 delta
->new_file
.flags
|= GIT_DIFF_FLAG_VALID_ID
;
157 return diff_insert_delta(diff
, delta
, matched_pathspec
);
160 static int diff_delta__from_two(
163 const git_index_entry
*old_entry
,
165 const git_index_entry
*new_entry
,
167 const git_oid
*new_id
,
168 const char *matched_pathspec
)
170 const git_oid
*old_id
= &old_entry
->id
;
171 git_diff_delta
*delta
;
172 const char *canonical_path
= old_entry
->path
;
174 if (status
== GIT_DELTA_UNMODIFIED
&&
175 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_INCLUDE_UNMODIFIED
))
179 new_id
= &new_entry
->id
;
181 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_REVERSE
)) {
182 uint32_t temp_mode
= old_mode
;
183 const git_index_entry
*temp_entry
= old_entry
;
184 const git_oid
*temp_id
= old_id
;
186 old_entry
= new_entry
;
187 new_entry
= temp_entry
;
189 new_mode
= temp_mode
;
194 delta
= diff_delta__alloc(diff
, status
, canonical_path
);
195 GITERR_CHECK_ALLOC(delta
);
198 if (!git_index_entry_is_conflict(old_entry
)) {
199 delta
->old_file
.size
= old_entry
->file_size
;
200 delta
->old_file
.mode
= old_mode
;
201 git_oid_cpy(&delta
->old_file
.id
, old_id
);
202 delta
->old_file
.flags
|= GIT_DIFF_FLAG_VALID_ID
|
203 GIT_DIFF_FLAG_EXISTS
;
206 if (!git_index_entry_is_conflict(new_entry
)) {
207 git_oid_cpy(&delta
->new_file
.id
, new_id
);
208 delta
->new_file
.size
= new_entry
->file_size
;
209 delta
->new_file
.mode
= new_mode
;
210 delta
->old_file
.flags
|= GIT_DIFF_FLAG_EXISTS
;
211 delta
->new_file
.flags
|= GIT_DIFF_FLAG_EXISTS
;
213 if (!git_oid_iszero(&new_entry
->id
))
214 delta
->new_file
.flags
|= GIT_DIFF_FLAG_VALID_ID
;
217 return diff_insert_delta(diff
, delta
, matched_pathspec
);
220 static git_diff_delta
*diff_delta__last_for_item(
222 const git_index_entry
*item
)
224 git_diff_delta
*delta
= git_vector_last(&diff
->deltas
);
228 switch (delta
->status
) {
229 case GIT_DELTA_UNMODIFIED
:
230 case GIT_DELTA_DELETED
:
231 if (git_oid__cmp(&delta
->old_file
.id
, &item
->id
) == 0)
234 case GIT_DELTA_ADDED
:
235 if (git_oid__cmp(&delta
->new_file
.id
, &item
->id
) == 0)
238 case GIT_DELTA_UNREADABLE
:
239 case GIT_DELTA_UNTRACKED
:
240 if (diff
->strcomp(delta
->new_file
.path
, item
->path
) == 0 &&
241 git_oid__cmp(&delta
->new_file
.id
, &item
->id
) == 0)
244 case GIT_DELTA_MODIFIED
:
245 if (git_oid__cmp(&delta
->old_file
.id
, &item
->id
) == 0 ||
246 git_oid__cmp(&delta
->new_file
.id
, &item
->id
) == 0)
256 static char *diff_strdup_prefix(git_pool
*pool
, const char *prefix
)
258 size_t len
= strlen(prefix
);
260 /* append '/' at end if needed */
261 if (len
> 0 && prefix
[len
- 1] != '/')
262 return git_pool_strcat(pool
, prefix
, "/");
264 return git_pool_strndup(pool
, prefix
, len
+ 1);
267 GIT_INLINE(const char *) diff_delta__path(const git_diff_delta
*delta
)
269 const char *str
= delta
->old_file
.path
;
272 delta
->status
== GIT_DELTA_ADDED
||
273 delta
->status
== GIT_DELTA_RENAMED
||
274 delta
->status
== GIT_DELTA_COPIED
)
275 str
= delta
->new_file
.path
;
280 const char *git_diff_delta__path(const git_diff_delta
*delta
)
282 return diff_delta__path(delta
);
285 int git_diff_delta__cmp(const void *a
, const void *b
)
287 const git_diff_delta
*da
= a
, *db
= b
;
288 int val
= strcmp(diff_delta__path(da
), diff_delta__path(db
));
289 return val
? val
: ((int)da
->status
- (int)db
->status
);
292 int git_diff_delta__casecmp(const void *a
, const void *b
)
294 const git_diff_delta
*da
= a
, *db
= b
;
295 int val
= strcasecmp(diff_delta__path(da
), diff_delta__path(db
));
296 return val
? val
: ((int)da
->status
- (int)db
->status
);
299 GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta
*delta
)
301 return delta
->old_file
.path
?
302 delta
->old_file
.path
: delta
->new_file
.path
;
305 int git_diff_delta__i2w_cmp(const void *a
, const void *b
)
307 const git_diff_delta
*da
= a
, *db
= b
;
308 int val
= strcmp(diff_delta__i2w_path(da
), diff_delta__i2w_path(db
));
309 return val
? val
: ((int)da
->status
- (int)db
->status
);
312 int git_diff_delta__i2w_casecmp(const void *a
, const void *b
)
314 const git_diff_delta
*da
= a
, *db
= b
;
315 int val
= strcasecmp(diff_delta__i2w_path(da
), diff_delta__i2w_path(db
));
316 return val
? val
: ((int)da
->status
- (int)db
->status
);
319 bool git_diff_delta__should_skip(
320 const git_diff_options
*opts
, const git_diff_delta
*delta
)
322 uint32_t flags
= opts
? opts
->flags
: 0;
324 if (delta
->status
== GIT_DELTA_UNMODIFIED
&&
325 (flags
& GIT_DIFF_INCLUDE_UNMODIFIED
) == 0)
328 if (delta
->status
== GIT_DELTA_IGNORED
&&
329 (flags
& GIT_DIFF_INCLUDE_IGNORED
) == 0)
332 if (delta
->status
== GIT_DELTA_UNTRACKED
&&
333 (flags
& GIT_DIFF_INCLUDE_UNTRACKED
) == 0)
336 if (delta
->status
== GIT_DELTA_UNREADABLE
&&
337 (flags
& GIT_DIFF_INCLUDE_UNREADABLE
) == 0)
344 static const char *diff_mnemonic_prefix(
345 git_iterator_type_t type
, bool left_side
)
347 const char *pfx
= "";
350 case GIT_ITERATOR_TYPE_EMPTY
: pfx
= "c"; break;
351 case GIT_ITERATOR_TYPE_TREE
: pfx
= "c"; break;
352 case GIT_ITERATOR_TYPE_INDEX
: pfx
= "i"; break;
353 case GIT_ITERATOR_TYPE_WORKDIR
: pfx
= "w"; break;
354 case GIT_ITERATOR_TYPE_FS
: pfx
= left_side
? "1" : "2"; break;
358 /* note: without a deeper look at pathspecs, there is no easy way
359 * to get the (o)bject / (w)ork tree mnemonics working...
365 static int diff_entry_cmp(const void *a
, const void *b
)
367 const git_index_entry
*entry_a
= a
;
368 const git_index_entry
*entry_b
= b
;
370 return strcmp(entry_a
->path
, entry_b
->path
);
373 static int diff_entry_icmp(const void *a
, const void *b
)
375 const git_index_entry
*entry_a
= a
;
376 const git_index_entry
*entry_b
= b
;
378 return strcasecmp(entry_a
->path
, entry_b
->path
);
381 static void diff_set_ignore_case(git_diff
*diff
, bool ignore_case
)
384 diff
->opts
.flags
&= ~GIT_DIFF_IGNORE_CASE
;
386 diff
->strcomp
= git__strcmp
;
387 diff
->strncomp
= git__strncmp
;
388 diff
->pfxcomp
= git__prefixcmp
;
389 diff
->entrycomp
= diff_entry_cmp
;
391 git_vector_set_cmp(&diff
->deltas
, git_diff_delta__cmp
);
393 diff
->opts
.flags
|= GIT_DIFF_IGNORE_CASE
;
395 diff
->strcomp
= git__strcasecmp
;
396 diff
->strncomp
= git__strncasecmp
;
397 diff
->pfxcomp
= git__prefixcmp_icase
;
398 diff
->entrycomp
= diff_entry_icmp
;
400 git_vector_set_cmp(&diff
->deltas
, git_diff_delta__casecmp
);
403 git_vector_sort(&diff
->deltas
);
406 static git_diff
*diff_list_alloc(
407 git_repository
*repo
,
408 git_iterator
*old_iter
,
409 git_iterator
*new_iter
)
411 git_diff_options dflt
= GIT_DIFF_OPTIONS_INIT
;
412 git_diff
*diff
= git__calloc(1, sizeof(git_diff
));
416 assert(repo
&& old_iter
&& new_iter
);
418 GIT_REFCOUNT_INC(diff
);
420 diff
->old_src
= old_iter
->type
;
421 diff
->new_src
= new_iter
->type
;
422 memcpy(&diff
->opts
, &dflt
, sizeof(diff
->opts
));
424 if (git_vector_init(&diff
->deltas
, 0, git_diff_delta__cmp
) < 0 ||
425 git_pool_init(&diff
->pool
, 1, 0) < 0) {
430 /* Use case-insensitive compare if either iterator has
431 * the ignore_case bit set */
432 diff_set_ignore_case(
434 git_iterator_ignore_case(old_iter
) ||
435 git_iterator_ignore_case(new_iter
));
440 static int diff_list_apply_options(
442 const git_diff_options
*opts
)
444 git_config
*cfg
= NULL
;
445 git_repository
*repo
= diff
->repo
;
446 git_pool
*pool
= &diff
->pool
;
450 /* copy user options (except case sensitivity info from iterators) */
451 bool icase
= DIFF_FLAG_IS_SET(diff
, GIT_DIFF_IGNORE_CASE
);
452 memcpy(&diff
->opts
, opts
, sizeof(diff
->opts
));
453 DIFF_FLAG_SET(diff
, GIT_DIFF_IGNORE_CASE
, icase
);
455 /* initialize pathspec from options */
456 if (git_pathspec__vinit(&diff
->pathspec
, &opts
->pathspec
, pool
) < 0)
460 /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
461 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_INCLUDE_TYPECHANGE_TREES
))
462 diff
->opts
.flags
|= GIT_DIFF_INCLUDE_TYPECHANGE
;
464 /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
465 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_SHOW_UNTRACKED_CONTENT
))
466 diff
->opts
.flags
|= GIT_DIFF_INCLUDE_UNTRACKED
;
468 /* load config values that affect diff behavior */
469 if ((val
= git_repository_config_snapshot(&cfg
, repo
)) < 0)
472 if (!git_config__cvar(&val
, cfg
, GIT_CVAR_SYMLINKS
) && val
)
473 diff
->diffcaps
= diff
->diffcaps
| GIT_DIFFCAPS_HAS_SYMLINKS
;
475 if (!git_config__cvar(&val
, cfg
, GIT_CVAR_IGNORESTAT
) && val
)
476 diff
->diffcaps
= diff
->diffcaps
| GIT_DIFFCAPS_IGNORE_STAT
;
478 if ((diff
->opts
.flags
& GIT_DIFF_IGNORE_FILEMODE
) == 0 &&
479 !git_config__cvar(&val
, cfg
, GIT_CVAR_FILEMODE
) && val
)
480 diff
->diffcaps
= diff
->diffcaps
| GIT_DIFFCAPS_TRUST_MODE_BITS
;
482 if (!git_config__cvar(&val
, cfg
, GIT_CVAR_TRUSTCTIME
) && val
)
483 diff
->diffcaps
= diff
->diffcaps
| GIT_DIFFCAPS_TRUST_CTIME
;
485 /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
487 /* Set GIT_DIFFCAPS_TRUST_NANOSECS on a platform basis */
488 diff
->diffcaps
= diff
->diffcaps
| GIT_DIFFCAPS_TRUST_NANOSECS
;
490 /* If not given explicit `opts`, check `diff.xyz` configs */
492 int context
= git_config__get_int_force(cfg
, "diff.context", 3);
493 diff
->opts
.context_lines
= context
>= 0 ? (uint32_t)context
: 3;
495 /* add other defaults here */
498 /* Reverse src info if diff is reversed */
499 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_REVERSE
)) {
500 git_iterator_type_t tmp_src
= diff
->old_src
;
501 diff
->old_src
= diff
->new_src
;
502 diff
->new_src
= tmp_src
;
505 /* Unset UPDATE_INDEX unless diffing workdir and index */
506 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_UPDATE_INDEX
) &&
507 (!(diff
->old_src
== GIT_ITERATOR_TYPE_WORKDIR
||
508 diff
->new_src
== GIT_ITERATOR_TYPE_WORKDIR
) ||
509 !(diff
->old_src
== GIT_ITERATOR_TYPE_INDEX
||
510 diff
->new_src
== GIT_ITERATOR_TYPE_INDEX
)))
511 diff
->opts
.flags
&= ~GIT_DIFF_UPDATE_INDEX
;
513 /* if ignore_submodules not explicitly set, check diff config */
514 if (diff
->opts
.ignore_submodules
<= 0) {
515 git_config_entry
*entry
;
516 git_config__lookup_entry(&entry
, cfg
, "diff.ignoresubmodules", true);
518 if (entry
&& git_submodule_parse_ignore(
519 &diff
->opts
.ignore_submodules
, entry
->value
) < 0)
521 git_config_entry_free(entry
);
524 /* if either prefix is not set, figure out appropriate value */
525 if (!diff
->opts
.old_prefix
|| !diff
->opts
.new_prefix
) {
526 const char *use_old
= DIFF_OLD_PREFIX_DEFAULT
;
527 const char *use_new
= DIFF_NEW_PREFIX_DEFAULT
;
529 if (git_config__get_bool_force(cfg
, "diff.noprefix", 0))
530 use_old
= use_new
= "";
531 else if (git_config__get_bool_force(cfg
, "diff.mnemonicprefix", 0)) {
532 use_old
= diff_mnemonic_prefix(diff
->old_src
, true);
533 use_new
= diff_mnemonic_prefix(diff
->new_src
, false);
536 if (!diff
->opts
.old_prefix
)
537 diff
->opts
.old_prefix
= use_old
;
538 if (!diff
->opts
.new_prefix
)
539 diff
->opts
.new_prefix
= use_new
;
542 /* strdup prefix from pool so we're not dependent on external data */
543 diff
->opts
.old_prefix
= diff_strdup_prefix(pool
, diff
->opts
.old_prefix
);
544 diff
->opts
.new_prefix
= diff_strdup_prefix(pool
, diff
->opts
.new_prefix
);
546 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_REVERSE
)) {
547 const char *tmp_prefix
= diff
->opts
.old_prefix
;
548 diff
->opts
.old_prefix
= diff
->opts
.new_prefix
;
549 diff
->opts
.new_prefix
= tmp_prefix
;
552 git_config_free(cfg
);
554 /* check strdup results for error */
555 return (!diff
->opts
.old_prefix
|| !diff
->opts
.new_prefix
) ? -1 : 0;
558 static void diff_list_free(git_diff
*diff
)
560 git_vector_free_deep(&diff
->deltas
);
562 git_pathspec__vfree(&diff
->pathspec
);
563 git_pool_clear(&diff
->pool
);
565 git__memzero(diff
, sizeof(*diff
));
569 void git_diff_free(git_diff
*diff
)
574 GIT_REFCOUNT_DEC(diff
, diff_list_free
);
577 void git_diff_addref(git_diff
*diff
)
579 GIT_REFCOUNT_INC(diff
);
582 int git_diff__oid_for_file(
589 git_index_entry entry
;
591 memset(&entry
, 0, sizeof(entry
));
593 entry
.file_size
= size
;
594 entry
.path
= (char *)path
;
596 return git_diff__oid_for_entry(out
, diff
, &entry
, mode
, NULL
);
599 int git_diff__oid_for_entry(
602 const git_index_entry
*src
,
604 const git_oid
*update_match
)
607 git_buf full_path
= GIT_BUF_INIT
;
608 git_index_entry entry
= *src
;
609 git_filter_list
*fl
= NULL
;
611 memset(out
, 0, sizeof(*out
));
613 if (git_buf_joinpath(
614 &full_path
, git_repository_workdir(diff
->repo
), entry
.path
) < 0)
620 diff
->perf
.stat_calls
++;
622 if (p_stat(full_path
.ptr
, &st
) < 0) {
623 error
= git_path_set_error(errno
, entry
.path
, "stat");
624 git_buf_free(&full_path
);
628 git_index_entry__init_from_stat(
629 &entry
, &st
, (diff
->diffcaps
& GIT_DIFFCAPS_TRUST_MODE_BITS
) != 0);
632 /* calculate OID for file if possible */
633 if (S_ISGITLINK(mode
)) {
636 if (!git_submodule_lookup(&sm
, diff
->repo
, entry
.path
)) {
637 const git_oid
*sm_oid
= git_submodule_wd_id(sm
);
639 git_oid_cpy(out
, sm_oid
);
640 git_submodule_free(sm
);
642 /* if submodule lookup failed probably just in an intermediate
643 * state where some init hasn't happened, so ignore the error
647 } else if (S_ISLNK(mode
)) {
648 error
= git_odb__hashlink(out
, full_path
.ptr
);
649 diff
->perf
.oid_calculations
++;
650 } else if (!git__is_sizet(entry
.file_size
)) {
651 giterr_set(GITERR_OS
, "File size overflow (for 32-bits) on '%s'",
654 } else if (!(error
= git_filter_list_load(
655 &fl
, diff
->repo
, NULL
, entry
.path
,
656 GIT_FILTER_TO_ODB
, GIT_FILTER_ALLOW_UNSAFE
)))
658 int fd
= git_futils_open_ro(full_path
.ptr
);
662 error
= git_odb__hashfd_filtered(
663 out
, fd
, (size_t)entry
.file_size
, GIT_OBJ_BLOB
, fl
);
665 diff
->perf
.oid_calculations
++;
668 git_filter_list_free(fl
);
671 /* update index for entry if requested */
672 if (!error
&& update_match
&& git_oid_equal(out
, update_match
)) {
674 git_index_entry updated_entry
;
676 memcpy(&updated_entry
, &entry
, sizeof(git_index_entry
));
677 updated_entry
.mode
= mode
;
678 git_oid_cpy(&updated_entry
.id
, out
);
680 if (!(error
= git_repository_index__weakptr(&idx
, diff
->repo
))) {
681 error
= git_index_add(idx
, &updated_entry
);
682 diff
->index_updated
= true;
686 git_buf_free(&full_path
);
690 static bool diff_time_eq(
691 const git_index_time
*a
, const git_index_time
*b
, bool use_nanos
)
693 return a
->seconds
== b
->seconds
&&
694 (!use_nanos
|| a
->nanoseconds
== b
->nanoseconds
);
698 git_repository
*repo
;
699 git_iterator
*old_iter
;
700 git_iterator
*new_iter
;
701 const git_index_entry
*oitem
;
702 const git_index_entry
*nitem
;
705 #define MODE_BITS_MASK 0000777
707 static int maybe_modified_submodule(
711 diff_in_progress
*info
)
715 unsigned int sm_status
= 0;
716 git_submodule_ignore_t ign
= diff
->opts
.ignore_submodules
;
718 *status
= GIT_DELTA_UNMODIFIED
;
720 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_IGNORE_SUBMODULES
) ||
721 ign
== GIT_SUBMODULE_IGNORE_ALL
)
724 if ((error
= git_submodule_lookup(
725 &sub
, diff
->repo
, info
->nitem
->path
)) < 0) {
727 /* GIT_EEXISTS means dir with .git in it was found - ignore it */
728 if (error
== GIT_EEXISTS
) {
735 if (ign
<= 0 && git_submodule_ignore(sub
) == GIT_SUBMODULE_IGNORE_ALL
)
737 else if ((error
= git_submodule__status(
738 &sm_status
, NULL
, NULL
, found_oid
, sub
, ign
)) < 0)
739 /* return error below */;
741 /* check IS_WD_UNMODIFIED because this case is only used
742 * when the new side of the diff is the working directory
744 else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status
))
745 *status
= GIT_DELTA_MODIFIED
;
747 /* now that we have a HEAD OID, check if HEAD moved */
748 else if ((sm_status
& GIT_SUBMODULE_STATUS_IN_WD
) != 0 &&
749 !git_oid_equal(&info
->oitem
->id
, found_oid
))
750 *status
= GIT_DELTA_MODIFIED
;
752 git_submodule_free(sub
);
756 static int maybe_modified(
758 diff_in_progress
*info
)
761 git_delta_t status
= GIT_DELTA_MODIFIED
;
762 const git_index_entry
*oitem
= info
->oitem
;
763 const git_index_entry
*nitem
= info
->nitem
;
764 unsigned int omode
= oitem
->mode
;
765 unsigned int nmode
= nitem
->mode
;
766 bool new_is_workdir
= (info
->new_iter
->type
== GIT_ITERATOR_TYPE_WORKDIR
);
767 bool modified_uncertain
= false;
768 const char *matched_pathspec
;
771 if (!diff_pathspec_match(&matched_pathspec
, diff
, oitem
->path
))
774 memset(&noid
, 0, sizeof(noid
));
776 /* on platforms with no symlinks, preserve mode of existing symlinks */
777 if (S_ISLNK(omode
) && S_ISREG(nmode
) && new_is_workdir
&&
778 !(diff
->diffcaps
& GIT_DIFFCAPS_HAS_SYMLINKS
))
781 /* on platforms with no execmode, just preserve old mode */
782 if (!(diff
->diffcaps
& GIT_DIFFCAPS_TRUST_MODE_BITS
) &&
783 (nmode
& MODE_BITS_MASK
) != (omode
& MODE_BITS_MASK
) &&
785 nmode
= (nmode
& ~MODE_BITS_MASK
) | (omode
& MODE_BITS_MASK
);
787 /* if one side is a conflict, mark the whole delta as conflicted */
788 if (git_index_entry_is_conflict(oitem
) ||
789 git_index_entry_is_conflict(nitem
)) {
790 status
= GIT_DELTA_CONFLICTED
;
792 /* support "assume unchanged" (poorly, b/c we still stat everything) */
793 } else if ((oitem
->flags
& GIT_IDXENTRY_VALID
) != 0) {
794 status
= GIT_DELTA_UNMODIFIED
;
796 /* support "skip worktree" index bit */
797 } else if ((oitem
->flags_extended
& GIT_IDXENTRY_SKIP_WORKTREE
) != 0) {
798 status
= GIT_DELTA_UNMODIFIED
;
800 /* if basic type of file changed, then split into delete and add */
801 } else if (GIT_MODE_TYPE(omode
) != GIT_MODE_TYPE(nmode
)) {
802 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_INCLUDE_TYPECHANGE
)) {
803 status
= GIT_DELTA_TYPECHANGE
;
806 else if (nmode
== GIT_FILEMODE_UNREADABLE
) {
807 if (!(error
= diff_delta__from_one(diff
, GIT_DELTA_DELETED
, oitem
, NULL
)))
808 error
= diff_delta__from_one(diff
, GIT_DELTA_UNREADABLE
, NULL
, nitem
);
813 if (!(error
= diff_delta__from_one(diff
, GIT_DELTA_DELETED
, oitem
, NULL
)))
814 error
= diff_delta__from_one(diff
, GIT_DELTA_ADDED
, NULL
, nitem
);
818 /* if oids and modes match (and are valid), then file is unmodified */
819 } else if (git_oid_equal(&oitem
->id
, &nitem
->id
) &&
821 !git_oid_iszero(&oitem
->id
)) {
822 status
= GIT_DELTA_UNMODIFIED
;
824 /* if we have an unknown OID and a workdir iterator, then check some
825 * circumstances that can accelerate things or need special handling
827 } else if (git_oid_iszero(&nitem
->id
) && new_is_workdir
) {
828 bool use_ctime
= ((diff
->diffcaps
& GIT_DIFFCAPS_TRUST_CTIME
) != 0);
829 bool use_nanos
= ((diff
->diffcaps
& GIT_DIFFCAPS_TRUST_NANOSECS
) != 0);
831 git_iterator_index(&index
, info
->new_iter
);
833 status
= GIT_DELTA_UNMODIFIED
;
835 if (S_ISGITLINK(nmode
)) {
836 if ((error
= maybe_modified_submodule(&status
, &noid
, diff
, info
)) < 0)
840 /* if the stat data looks different, then mark modified - this just
841 * means that the OID will be recalculated below to confirm change
843 else if (omode
!= nmode
|| oitem
->file_size
!= nitem
->file_size
) {
844 status
= GIT_DELTA_MODIFIED
;
846 (oitem
->file_size
<= 0 && nitem
->file_size
> 0);
848 else if (!diff_time_eq(&oitem
->mtime
, &nitem
->mtime
, use_nanos
) ||
850 !diff_time_eq(&oitem
->ctime
, &nitem
->ctime
, use_nanos
)) ||
851 oitem
->ino
!= nitem
->ino
||
852 oitem
->uid
!= nitem
->uid
||
853 oitem
->gid
!= nitem
->gid
||
854 (index
&& nitem
->mtime
.seconds
>= index
->stamp
.mtime
))
856 status
= GIT_DELTA_MODIFIED
;
857 modified_uncertain
= true;
860 /* if mode is GITLINK and submodules are ignored, then skip */
861 } else if (S_ISGITLINK(nmode
) &&
862 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_IGNORE_SUBMODULES
)) {
863 status
= GIT_DELTA_UNMODIFIED
;
866 /* if we got here and decided that the files are modified, but we
867 * haven't calculated the OID of the new item, then calculate it now
869 if (modified_uncertain
&& git_oid_iszero(&nitem
->id
)) {
870 const git_oid
*update_check
=
871 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_UPDATE_INDEX
) && omode
== nmode
?
874 if ((error
= git_diff__oid_for_entry(
875 &noid
, diff
, nitem
, nmode
, update_check
)) < 0)
878 /* if oid matches, then mark unmodified (except submodules, where
879 * the filesystem content may be modified even if the oid still
880 * matches between the index and the workdir HEAD)
882 if (omode
== nmode
&& !S_ISGITLINK(omode
) &&
883 git_oid_equal(&oitem
->id
, &noid
))
884 status
= GIT_DELTA_UNMODIFIED
;
887 /* If we want case changes, then break this into a delete of the old
888 * and an add of the new so that consumers can act accordingly (eg,
889 * checkout will update the case on disk.)
891 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_IGNORE_CASE
) &&
892 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_INCLUDE_CASECHANGE
) &&
893 strcmp(oitem
->path
, nitem
->path
) != 0) {
895 if (!(error
= diff_delta__from_one(diff
, GIT_DELTA_DELETED
, oitem
, NULL
)))
896 error
= diff_delta__from_one(diff
, GIT_DELTA_ADDED
, NULL
, nitem
);
901 return diff_delta__from_two(
902 diff
, status
, oitem
, omode
, nitem
, nmode
,
903 git_oid_iszero(&noid
) ? NULL
: &noid
, matched_pathspec
);
906 static bool entry_is_prefixed(
908 const git_index_entry
*item
,
909 const git_index_entry
*prefix_item
)
913 if (!item
|| diff
->pfxcomp(item
->path
, prefix_item
->path
) != 0)
916 pathlen
= strlen(prefix_item
->path
);
918 return (prefix_item
->path
[pathlen
- 1] == '/' ||
919 item
->path
[pathlen
] == '\0' ||
920 item
->path
[pathlen
] == '/');
923 static int iterator_current(
924 const git_index_entry
**entry
,
925 git_iterator
*iterator
)
929 if ((error
= git_iterator_current(entry
, iterator
)) == GIT_ITEROVER
) {
937 static int iterator_advance(
938 const git_index_entry
**entry
,
939 git_iterator
*iterator
)
941 const git_index_entry
*prev_entry
= *entry
;
944 /* if we're looking for conflicts, we only want to report
945 * one conflict for each file, instead of all three sides.
946 * so if this entry is a conflict for this file, and the
947 * previous one was a conflict for the same file, skip it.
949 while ((error
= git_iterator_advance(entry
, iterator
)) == 0) {
950 if (!(iterator
->flags
& GIT_ITERATOR_INCLUDE_CONFLICTS
) ||
951 !git_index_entry_is_conflict(prev_entry
) ||
952 !git_index_entry_is_conflict(*entry
))
955 cmp
= (iterator
->flags
& GIT_ITERATOR_IGNORE_CASE
) ?
956 strcasecmp(prev_entry
->path
, (*entry
)->path
) :
957 strcmp(prev_entry
->path
, (*entry
)->path
);
963 if (error
== GIT_ITEROVER
) {
971 static int iterator_advance_into(
972 const git_index_entry
**entry
,
973 git_iterator
*iterator
)
977 if ((error
= git_iterator_advance_into(entry
, iterator
)) == GIT_ITEROVER
) {
985 static int iterator_advance_over_with_status(
986 const git_index_entry
**entry
,
987 git_iterator_status_t
*status
,
988 git_iterator
*iterator
)
992 if ((error
= git_iterator_advance_over_with_status(
993 entry
, status
, iterator
)) == GIT_ITEROVER
) {
1001 static int handle_unmatched_new_item(
1002 git_diff
*diff
, diff_in_progress
*info
)
1005 const git_index_entry
*nitem
= info
->nitem
;
1006 git_delta_t delta_type
= GIT_DELTA_UNTRACKED
;
1007 bool contains_oitem
;
1009 /* check if this is a prefix of the other side */
1010 contains_oitem
= entry_is_prefixed(diff
, info
->oitem
, nitem
);
1012 /* update delta_type if this item is conflicted */
1013 if (git_index_entry_is_conflict(nitem
))
1014 delta_type
= GIT_DELTA_CONFLICTED
;
1016 /* update delta_type if this item is ignored */
1017 else if (git_iterator_current_is_ignored(info
->new_iter
))
1018 delta_type
= GIT_DELTA_IGNORED
;
1020 if (nitem
->mode
== GIT_FILEMODE_TREE
) {
1021 bool recurse_into_dir
= contains_oitem
;
1023 /* check if user requests recursion into this type of dir */
1024 recurse_into_dir
= contains_oitem
||
1025 (delta_type
== GIT_DELTA_UNTRACKED
&&
1026 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_RECURSE_UNTRACKED_DIRS
)) ||
1027 (delta_type
== GIT_DELTA_IGNORED
&&
1028 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_RECURSE_IGNORED_DIRS
));
1030 /* do not advance into directories that contain a .git file */
1031 if (recurse_into_dir
&& !contains_oitem
) {
1032 git_buf
*full
= NULL
;
1033 if (git_iterator_current_workdir_path(&full
, info
->new_iter
) < 0)
1035 if (full
&& git_path_contains(full
, DOT_GIT
)) {
1036 /* TODO: warning if not a valid git repository */
1037 recurse_into_dir
= false;
1041 /* still have to look into untracked directories to match core git -
1042 * with no untracked files, directory is treated as ignored
1044 if (!recurse_into_dir
&&
1045 delta_type
== GIT_DELTA_UNTRACKED
&&
1046 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS
))
1048 git_diff_delta
*last
;
1049 git_iterator_status_t untracked_state
;
1051 /* attempt to insert record for this directory */
1052 if ((error
= diff_delta__from_one(diff
, delta_type
, NULL
, nitem
)) != 0)
1055 /* if delta wasn't created (because of rules), just skip ahead */
1056 last
= diff_delta__last_for_item(diff
, nitem
);
1058 return iterator_advance(&info
->nitem
, info
->new_iter
);
1060 /* iterate into dir looking for an actual untracked file */
1061 if ((error
= iterator_advance_over_with_status(
1062 &info
->nitem
, &untracked_state
, info
->new_iter
)) < 0)
1065 /* if we found nothing that matched our pathlist filter, exclude */
1066 if (untracked_state
== GIT_ITERATOR_STATUS_FILTERED
) {
1067 git_vector_pop(&diff
->deltas
);
1071 /* if we found nothing or just ignored items, update the record */
1072 if (untracked_state
== GIT_ITERATOR_STATUS_IGNORED
||
1073 untracked_state
== GIT_ITERATOR_STATUS_EMPTY
) {
1074 last
->status
= GIT_DELTA_IGNORED
;
1076 /* remove the record if we don't want ignored records */
1077 if (DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_INCLUDE_IGNORED
)) {
1078 git_vector_pop(&diff
->deltas
);
1086 /* try to advance into directory if necessary */
1087 if (recurse_into_dir
) {
1088 error
= iterator_advance_into(&info
->nitem
, info
->new_iter
);
1090 /* if real error or no error, proceed with iteration */
1091 if (error
!= GIT_ENOTFOUND
)
1095 /* if directory is empty, can't advance into it, so either skip
1099 return iterator_advance(&info
->nitem
, info
->new_iter
);
1100 delta_type
= GIT_DELTA_IGNORED
;
1104 else if (delta_type
== GIT_DELTA_IGNORED
&&
1105 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_RECURSE_IGNORED_DIRS
) &&
1106 git_iterator_current_tree_is_ignored(info
->new_iter
))
1107 /* item contained in ignored directory, so skip over it */
1108 return iterator_advance(&info
->nitem
, info
->new_iter
);
1110 else if (info
->new_iter
->type
!= GIT_ITERATOR_TYPE_WORKDIR
) {
1111 if (delta_type
!= GIT_DELTA_CONFLICTED
)
1112 delta_type
= GIT_DELTA_ADDED
;
1115 else if (nitem
->mode
== GIT_FILEMODE_COMMIT
) {
1116 /* ignore things that are not actual submodules */
1117 if (git_submodule_lookup(NULL
, info
->repo
, nitem
->path
) != 0) {
1119 delta_type
= GIT_DELTA_IGNORED
;
1121 /* if this contains a tracked item, treat as normal TREE */
1122 if (contains_oitem
) {
1123 error
= iterator_advance_into(&info
->nitem
, info
->new_iter
);
1124 if (error
!= GIT_ENOTFOUND
)
1128 return iterator_advance(&info
->nitem
, info
->new_iter
);
1133 else if (nitem
->mode
== GIT_FILEMODE_UNREADABLE
) {
1134 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED
))
1135 delta_type
= GIT_DELTA_UNTRACKED
;
1137 delta_type
= GIT_DELTA_UNREADABLE
;
1140 /* Actually create the record for this item if necessary */
1141 if ((error
= diff_delta__from_one(diff
, delta_type
, NULL
, nitem
)) != 0)
1144 /* If user requested TYPECHANGE records, then check for that instead of
1145 * just generating an ADDED/UNTRACKED record
1147 if (delta_type
!= GIT_DELTA_IGNORED
&&
1148 DIFF_FLAG_IS_SET(diff
, GIT_DIFF_INCLUDE_TYPECHANGE_TREES
) &&
1151 /* this entry was prefixed with a tree - make TYPECHANGE */
1152 git_diff_delta
*last
= diff_delta__last_for_item(diff
, nitem
);
1154 last
->status
= GIT_DELTA_TYPECHANGE
;
1155 last
->old_file
.mode
= GIT_FILEMODE_TREE
;
1159 return iterator_advance(&info
->nitem
, info
->new_iter
);
1162 static int handle_unmatched_old_item(
1163 git_diff
*diff
, diff_in_progress
*info
)
1165 git_delta_t delta_type
= GIT_DELTA_DELETED
;
1168 /* update delta_type if this item is conflicted */
1169 if (git_index_entry_is_conflict(info
->oitem
))
1170 delta_type
= GIT_DELTA_CONFLICTED
;
1172 if ((error
= diff_delta__from_one(diff
, delta_type
, info
->oitem
, NULL
)) < 0)
1175 /* if we are generating TYPECHANGE records then check for that
1176 * instead of just generating a DELETE record
1178 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_INCLUDE_TYPECHANGE_TREES
) &&
1179 entry_is_prefixed(diff
, info
->nitem
, info
->oitem
))
1181 /* this entry has become a tree! convert to TYPECHANGE */
1182 git_diff_delta
*last
= diff_delta__last_for_item(diff
, info
->oitem
);
1184 last
->status
= GIT_DELTA_TYPECHANGE
;
1185 last
->new_file
.mode
= GIT_FILEMODE_TREE
;
1188 /* If new_iter is a workdir iterator, then this situation
1189 * will certainly be followed by a series of untracked items.
1190 * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
1192 if (S_ISDIR(info
->nitem
->mode
) &&
1193 DIFF_FLAG_ISNT_SET(diff
, GIT_DIFF_RECURSE_UNTRACKED_DIRS
))
1194 return iterator_advance(&info
->nitem
, info
->new_iter
);
1197 return iterator_advance(&info
->oitem
, info
->old_iter
);
1200 static int handle_matched_item(
1201 git_diff
*diff
, diff_in_progress
*info
)
1205 if ((error
= maybe_modified(diff
, info
)) < 0)
1208 if (!(error
= iterator_advance(&info
->oitem
, info
->old_iter
)))
1209 error
= iterator_advance(&info
->nitem
, info
->new_iter
);
1214 int git_diff__from_iterators(
1215 git_diff
**diff_ptr
,
1216 git_repository
*repo
,
1217 git_iterator
*old_iter
,
1218 git_iterator
*new_iter
,
1219 const git_diff_options
*opts
)
1222 diff_in_progress info
;
1227 diff
= diff_list_alloc(repo
, old_iter
, new_iter
);
1228 GITERR_CHECK_ALLOC(diff
);
1231 info
.old_iter
= old_iter
;
1232 info
.new_iter
= new_iter
;
1234 /* make iterators have matching icase behavior */
1235 if (DIFF_FLAG_IS_SET(diff
, GIT_DIFF_IGNORE_CASE
)) {
1236 if ((error
= git_iterator_set_ignore_case(old_iter
, true)) < 0 ||
1237 (error
= git_iterator_set_ignore_case(new_iter
, true)) < 0)
1241 /* finish initialization */
1242 if ((error
= diff_list_apply_options(diff
, opts
)) < 0)
1245 if ((error
= iterator_current(&info
.oitem
, old_iter
)) < 0 ||
1246 (error
= iterator_current(&info
.nitem
, new_iter
)) < 0)
1249 /* run iterators building diffs */
1250 while (!error
&& (info
.oitem
|| info
.nitem
)) {
1251 int cmp
= info
.oitem
?
1252 (info
.nitem
? diff
->entrycomp(info
.oitem
, info
.nitem
) : -1) : 1;
1254 /* create DELETED records for old items not matched in new */
1256 error
= handle_unmatched_old_item(diff
, &info
);
1258 /* create ADDED, TRACKED, or IGNORED records for new items not
1259 * matched in old (and/or descend into directories as needed)
1262 error
= handle_unmatched_new_item(diff
, &info
);
1264 /* otherwise item paths match, so create MODIFIED record
1265 * (or ADDED and DELETED pair if type changed)
1268 error
= handle_matched_item(diff
, &info
);
1271 diff
->perf
.stat_calls
+= old_iter
->stat_calls
+ new_iter
->stat_calls
;
1277 git_diff_free(diff
);
1282 #define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
1283 git_iterator *a = NULL, *b = NULL; \
1284 char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
1285 git_pathspec_prefix(&opts->pathspec) : NULL; \
1286 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
1287 b_opts = GIT_ITERATOR_OPTIONS_INIT; \
1288 a_opts.flags = FLAGS_FIRST; \
1289 a_opts.start = pfx; \
1291 b_opts.flags = FLAGS_SECOND; \
1292 b_opts.start = pfx; \
1294 GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
1295 if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
1296 a_opts.pathlist.strings = opts->pathspec.strings; \
1297 a_opts.pathlist.count = opts->pathspec.count; \
1298 b_opts.pathlist.strings = opts->pathspec.strings; \
1299 b_opts.pathlist.count = opts->pathspec.count; \
1301 if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
1302 error = git_diff__from_iterators(diff, repo, a, b, opts); \
1303 git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
1306 int git_diff_tree_to_tree(
1308 git_repository
*repo
,
1311 const git_diff_options
*opts
)
1313 git_iterator_flag_t iflag
= GIT_ITERATOR_DONT_IGNORE_CASE
;
1316 assert(diff
&& repo
);
1318 /* for tree to tree diff, be case sensitive even if the index is
1319 * currently case insensitive, unless the user explicitly asked
1320 * for case insensitivity
1322 if (opts
&& (opts
->flags
& GIT_DIFF_IGNORE_CASE
) != 0)
1323 iflag
= GIT_ITERATOR_IGNORE_CASE
;
1325 DIFF_FROM_ITERATORS(
1326 git_iterator_for_tree(&a
, old_tree
, &a_opts
), iflag
,
1327 git_iterator_for_tree(&b
, new_tree
, &b_opts
), iflag
1333 static int diff_load_index(git_index
**index
, git_repository
*repo
)
1335 int error
= git_repository_index__weakptr(index
, repo
);
1337 /* reload the repository index when user did not pass one in */
1338 if (!error
&& git_index_read(*index
, false) < 0)
1344 int git_diff_tree_to_index(
1346 git_repository
*repo
,
1349 const git_diff_options
*opts
)
1351 git_iterator_flag_t iflag
= GIT_ITERATOR_DONT_IGNORE_CASE
|
1352 GIT_ITERATOR_INCLUDE_CONFLICTS
;
1353 bool index_ignore_case
= false;
1356 assert(diff
&& repo
);
1358 if (!index
&& (error
= diff_load_index(&index
, repo
)) < 0)
1361 index_ignore_case
= index
->ignore_case
;
1363 DIFF_FROM_ITERATORS(
1364 git_iterator_for_tree(&a
, old_tree
, &a_opts
), iflag
,
1365 git_iterator_for_index(&b
, index
, &b_opts
), iflag
1368 /* if index is in case-insensitive order, re-sort deltas to match */
1369 if (!error
&& index_ignore_case
)
1370 diff_set_ignore_case(*diff
, true);
1375 int git_diff_index_to_workdir(
1377 git_repository
*repo
,
1379 const git_diff_options
*opts
)
1383 assert(diff
&& repo
);
1385 if (!index
&& (error
= diff_load_index(&index
, repo
)) < 0)
1388 DIFF_FROM_ITERATORS(
1389 git_iterator_for_index(&a
, index
, &a_opts
),
1390 GIT_ITERATOR_INCLUDE_CONFLICTS
,
1392 git_iterator_for_workdir(&b
, repo
, index
, NULL
, &b_opts
),
1393 GIT_ITERATOR_DONT_AUTOEXPAND
1396 if (!error
&& DIFF_FLAG_IS_SET(*diff
, GIT_DIFF_UPDATE_INDEX
) && (*diff
)->index_updated
)
1397 error
= git_index_write(index
);
1402 int git_diff_tree_to_workdir(
1404 git_repository
*repo
,
1406 const git_diff_options
*opts
)
1411 assert(diff
&& repo
);
1413 if ((error
= git_repository_index__weakptr(&index
, repo
)))
1416 DIFF_FROM_ITERATORS(
1417 git_iterator_for_tree(&a
, old_tree
, &a_opts
), 0,
1418 git_iterator_for_workdir(&b
, repo
, index
, old_tree
, &b_opts
), GIT_ITERATOR_DONT_AUTOEXPAND
1424 int git_diff_tree_to_workdir_with_index(
1426 git_repository
*repo
,
1428 const git_diff_options
*opts
)
1431 git_diff
*d1
= NULL
, *d2
= NULL
;
1432 git_index
*index
= NULL
;
1434 assert(diff
&& repo
);
1436 if ((error
= diff_load_index(&index
, repo
)) < 0)
1439 if (!(error
= git_diff_tree_to_index(&d1
, repo
, old_tree
, index
, opts
)) &&
1440 !(error
= git_diff_index_to_workdir(&d2
, repo
, index
, opts
)))
1441 error
= git_diff_merge(d1
, d2
);
1454 int git_diff_index_to_index(
1456 git_repository
*repo
,
1457 git_index
*old_index
,
1458 git_index
*new_index
,
1459 const git_diff_options
*opts
)
1463 assert(diff
&& old_index
&& new_index
);
1465 DIFF_FROM_ITERATORS(
1466 git_iterator_for_index(&a
, old_index
, &a_opts
), GIT_ITERATOR_DONT_IGNORE_CASE
,
1467 git_iterator_for_index(&b
, new_index
, &b_opts
), GIT_ITERATOR_DONT_IGNORE_CASE
1470 /* if index is in case-insensitive order, re-sort deltas to match */
1471 if (!error
&& (old_index
->ignore_case
|| new_index
->ignore_case
))
1472 diff_set_ignore_case(*diff
, true);
1477 size_t git_diff_num_deltas(const git_diff
*diff
)
1480 return diff
->deltas
.length
;
1483 size_t git_diff_num_deltas_of_type(const git_diff
*diff
, git_delta_t type
)
1485 size_t i
, count
= 0;
1486 const git_diff_delta
*delta
;
1490 git_vector_foreach(&diff
->deltas
, i
, delta
) {
1491 count
+= (delta
->status
== type
);
1497 const git_diff_delta
*git_diff_get_delta(const git_diff
*diff
, size_t idx
)
1500 return git_vector_get(&diff
->deltas
, idx
);
1503 int git_diff_is_sorted_icase(const git_diff
*diff
)
1505 return (diff
->opts
.flags
& GIT_DIFF_IGNORE_CASE
) != 0;
1508 int git_diff_get_perfdata(git_diff_perfdata
*out
, const git_diff
*diff
)
1511 GITERR_CHECK_VERSION(out
, GIT_DIFF_PERFDATA_VERSION
, "git_diff_perfdata");
1512 out
->stat_calls
= diff
->perf
.stat_calls
;
1513 out
->oid_calculations
= diff
->perf
.oid_calculations
;
1517 int git_diff__paired_foreach(
1520 int (*cb
)(git_diff_delta
*h2i
, git_diff_delta
*i2w
, void *payload
),
1524 git_diff_delta
*h2i
, *i2w
;
1525 size_t i
, j
, i_max
, j_max
;
1526 int (*strcomp
)(const char *, const char *) = git__strcmp
;
1527 bool h2i_icase
, i2w_icase
, icase_mismatch
;
1529 i_max
= head2idx
? head2idx
->deltas
.length
: 0;
1530 j_max
= idx2wd
? idx2wd
->deltas
.length
: 0;
1531 if (!i_max
&& !j_max
)
1534 /* At some point, tree-to-index diffs will probably never ignore case,
1535 * even if that isn't true now. Index-to-workdir diffs may or may not
1536 * ignore case, but the index filename for the idx2wd diff should
1537 * still be using the canonical case-preserving name.
1539 * Therefore the main thing we need to do here is make sure the diffs
1540 * are traversed in a compatible order. To do this, we temporarily
1541 * resort a mismatched diff to get the order correct.
1543 * In order to traverse renames in the index->workdir, we need to
1544 * ensure that we compare the index name on both sides, so we
1545 * always sort by the old name in the i2w list.
1547 h2i_icase
= head2idx
!= NULL
&&
1548 (head2idx
->opts
.flags
& GIT_DIFF_IGNORE_CASE
) != 0;
1550 i2w_icase
= idx2wd
!= NULL
&&
1551 (idx2wd
->opts
.flags
& GIT_DIFF_IGNORE_CASE
) != 0;
1554 (head2idx
!= NULL
&& idx2wd
!= NULL
&& h2i_icase
!= i2w_icase
);
1556 if (icase_mismatch
&& h2i_icase
) {
1557 git_vector_set_cmp(&head2idx
->deltas
, git_diff_delta__cmp
);
1558 git_vector_sort(&head2idx
->deltas
);
1561 if (i2w_icase
&& !icase_mismatch
) {
1562 strcomp
= git__strcasecmp
;
1564 git_vector_set_cmp(&idx2wd
->deltas
, git_diff_delta__i2w_casecmp
);
1565 git_vector_sort(&idx2wd
->deltas
);
1566 } else if (idx2wd
!= NULL
) {
1567 git_vector_set_cmp(&idx2wd
->deltas
, git_diff_delta__i2w_cmp
);
1568 git_vector_sort(&idx2wd
->deltas
);
1571 for (i
= 0, j
= 0; i
< i_max
|| j
< j_max
; ) {
1572 h2i
= head2idx
? GIT_VECTOR_GET(&head2idx
->deltas
, i
) : NULL
;
1573 i2w
= idx2wd
? GIT_VECTOR_GET(&idx2wd
->deltas
, j
) : NULL
;
1575 cmp
= !i2w
? -1 : !h2i
? 1 :
1576 strcomp(h2i
->new_file
.path
, i2w
->old_file
.path
);
1580 } else if (cmp
> 0) {
1586 if ((error
= cb(h2i
, i2w
, payload
)) != 0) {
1587 giterr_set_after_callback(error
);
1592 /* restore case-insensitive delta sort */
1593 if (icase_mismatch
&& h2i_icase
) {
1594 git_vector_set_cmp(&head2idx
->deltas
, git_diff_delta__casecmp
);
1595 git_vector_sort(&head2idx
->deltas
);
1598 /* restore idx2wd sort by new path */
1599 if (idx2wd
!= NULL
) {
1600 git_vector_set_cmp(&idx2wd
->deltas
,
1601 i2w_icase
? git_diff_delta__casecmp
: git_diff_delta__cmp
);
1602 git_vector_sort(&idx2wd
->deltas
);
1608 int git_diff__commit(
1610 git_repository
*repo
,
1611 const git_commit
*commit
,
1612 const git_diff_options
*opts
)
1614 git_commit
*parent
= NULL
;
1615 git_diff
*commit_diff
= NULL
;
1616 git_tree
*old_tree
= NULL
, *new_tree
= NULL
;
1620 if ((parents
= git_commit_parentcount(commit
)) > 1) {
1621 char commit_oidstr
[GIT_OID_HEXSZ
+ 1];
1624 giterr_set(GITERR_INVALID
, "Commit %s is a merge commit",
1625 git_oid_tostr(commit_oidstr
, GIT_OID_HEXSZ
+ 1, git_commit_id(commit
)));
1630 if ((error
= git_commit_parent(&parent
, commit
, 0)) < 0 ||
1631 (error
= git_commit_tree(&old_tree
, parent
)) < 0)
1634 if ((error
= git_commit_tree(&new_tree
, commit
)) < 0 ||
1635 (error
= git_diff_tree_to_tree(&commit_diff
, repo
, old_tree
, new_tree
, opts
)) < 0)
1638 *diff
= commit_diff
;
1641 git_tree_free(new_tree
);
1642 git_tree_free(old_tree
);
1643 git_commit_free(parent
);
1648 int git_diff_format_email__append_header_tobuf(
1651 const git_signature
*author
,
1652 const char *summary
,
1654 size_t total_patches
,
1655 bool exclude_patchno_marker
)
1657 char idstr
[GIT_OID_HEXSZ
+ 1];
1658 char date_str
[GIT_DATE_RFC2822_SZ
];
1661 git_oid_fmt(idstr
, id
);
1662 idstr
[GIT_OID_HEXSZ
] = '\0';
1664 if ((error
= git__date_rfc2822_fmt(date_str
, sizeof(date_str
), &author
->when
)) < 0)
1667 error
= git_buf_printf(out
,
1668 "From %s Mon Sep 17 00:00:00 2001\n" \
1673 author
->name
, author
->email
,
1679 if (!exclude_patchno_marker
) {
1680 if (total_patches
== 1) {
1681 error
= git_buf_puts(out
, "[PATCH] ");
1683 error
= git_buf_printf(out
, "[PATCH %"PRIuZ
"/%"PRIuZ
"] ", patch_no
, total_patches
);
1690 error
= git_buf_printf(out
, "%s\n\n", summary
);
1695 int git_diff_format_email__append_patches_tobuf(
1702 deltas
= git_diff_num_deltas(diff
);
1704 for (i
= 0; i
< deltas
; ++i
) {
1705 git_patch
*patch
= NULL
;
1707 if ((error
= git_patch_from_diff(&patch
, diff
, i
)) >= 0)
1708 error
= git_patch_to_buf(out
, patch
);
1710 git_patch_free(patch
);
1719 int git_diff_format_email(
1722 const git_diff_format_email_options
*opts
)
1724 git_diff_stats
*stats
= NULL
;
1725 char *summary
= NULL
, *loc
= NULL
;
1727 unsigned int format_flags
= 0;
1731 assert(out
&& diff
&& opts
);
1732 assert(opts
->summary
&& opts
->id
&& opts
->author
);
1734 GITERR_CHECK_VERSION(opts
, GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION
, "git_format_email_options");
1736 if ((ignore_marker
= opts
->flags
& GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER
) == false) {
1737 if (opts
->patch_no
> opts
->total_patches
) {
1738 giterr_set(GITERR_INVALID
, "patch %"PRIuZ
" out of range. max %"PRIuZ
, opts
->patch_no
, opts
->total_patches
);
1742 if (opts
->patch_no
== 0) {
1743 giterr_set(GITERR_INVALID
, "invalid patch no %"PRIuZ
". should be >0", opts
->patch_no
);
1748 /* the summary we receive may not be clean.
1749 * it could potentially contain new line characters
1750 * or not be set, sanitize, */
1751 if ((loc
= strpbrk(opts
->summary
, "\r\n")) != NULL
) {
1754 if ((offset
= (loc
- opts
->summary
)) == 0) {
1755 giterr_set(GITERR_INVALID
, "summary is empty");
1760 GITERR_CHECK_ALLOC_ADD(&allocsize
, offset
, 1);
1761 summary
= git__calloc(allocsize
, sizeof(char));
1762 GITERR_CHECK_ALLOC(summary
);
1764 strncpy(summary
, opts
->summary
, offset
);
1767 error
= git_diff_format_email__append_header_tobuf(out
,
1768 opts
->id
, opts
->author
, summary
== NULL
? opts
->summary
: summary
,
1769 opts
->patch_no
, opts
->total_patches
, ignore_marker
);
1774 format_flags
= GIT_DIFF_STATS_FULL
| GIT_DIFF_STATS_INCLUDE_SUMMARY
;
1776 if ((error
= git_buf_puts(out
, "---\n")) < 0 ||
1777 (error
= git_diff_get_stats(&stats
, diff
)) < 0 ||
1778 (error
= git_diff_stats_to_buf(out
, stats
, format_flags
, 0)) < 0 ||
1779 (error
= git_buf_putc(out
, '\n')) < 0 ||
1780 (error
= git_diff_format_email__append_patches_tobuf(out
, diff
)) < 0)
1783 error
= git_buf_puts(out
, "--\nlibgit2 " LIBGIT2_VERSION
"\n\n");
1787 git_diff_stats_free(stats
);
1792 int git_diff_commit_as_email(
1794 git_repository
*repo
,
1797 size_t total_patches
,
1798 git_diff_format_email_flags_t flags
,
1799 const git_diff_options
*diff_opts
)
1801 git_diff
*diff
= NULL
;
1802 git_diff_format_email_options opts
= GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT
;
1805 assert (out
&& repo
&& commit
);
1808 opts
.patch_no
= patch_no
;
1809 opts
.total_patches
= total_patches
;
1810 opts
.id
= git_commit_id(commit
);
1811 opts
.summary
= git_commit_summary(commit
);
1812 opts
.author
= git_commit_author(commit
);
1814 if ((error
= git_diff__commit(&diff
, repo
, commit
, diff_opts
)) < 0)
1817 error
= git_diff_format_email(out
, diff
, &opts
);
1819 git_diff_free(diff
);
1823 int git_diff_init_options(git_diff_options
*opts
, unsigned int version
)
1825 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
1826 opts
, version
, git_diff_options
, GIT_DIFF_OPTIONS_INIT
);
1830 int git_diff_find_init_options(
1831 git_diff_find_options
*opts
, unsigned int version
)
1833 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
1834 opts
, version
, git_diff_find_options
, GIT_DIFF_FIND_OPTIONS_INIT
);
1838 int git_diff_format_email_init_options(
1839 git_diff_format_email_options
*opts
, unsigned int version
)
1841 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
1842 opts
, version
, git_diff_format_email_options
,
1843 GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT
);