vendor/libgit2/src/diff_generate.c in rugged-0.28.4 vs vendor/libgit2/src/diff_generate.c in rugged-0.28.4.1

- old
+ new

@@ -7,11 +7,11 @@ #include "diff_generate.h" #include "diff.h" #include "patch_generate.h" -#include "futils.h" +#include "fileops.h" #include "config.h" #include "attr_file.h" #include "filter.h" #include "pathspec.h" #include "index.h" @@ -177,11 +177,11 @@ delta->new_file.id_abbrev = GIT_OID_HEXSZ; } delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; - if (has_old || !git_oid_is_zero(&delta->new_file.id)) + if (has_old || !git_oid_iszero(&delta->new_file.id)) delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; return diff_insert_delta(diff, delta, matched_pathspec); } @@ -238,11 +238,11 @@ delta->new_file.size = new_entry->file_size; delta->new_file.mode = new_mode; delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS; - if (!git_oid_is_zero(&new_entry->id)) + if (!git_oid_iszero(&new_entry->id)) delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; } return diff_insert_delta(diff, delta, matched_pathspec); } @@ -470,21 +470,21 @@ /* load config values that affect diff behavior */ if ((val = git_repository_config_snapshot(&cfg, repo)) < 0) return val; - if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_SYMLINKS) && val) + if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val) diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS; - if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_IGNORESTAT) && val) + if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val) diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT; if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 && - !git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_FILEMODE) && val) + !git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val) diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS; - if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_TRUSTCTIME) && val) + if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val) diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME; /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */ /* If not given explicit `opts`, check `diff.xyz` configs */ @@ -558,15 +558,15 @@ int git_diff__oid_for_file( git_oid *out, git_diff *diff, const char *path, uint16_t mode, - git_object_size_t size) + git_off_t size) { git_index_entry entry; - if (size > UINT32_MAX) { + if (size < 0 || size > UINT32_MAX) { git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", path); return -1; } memset(&entry, 0, sizeof(entry)); @@ -795,17 +795,17 @@ } /* if oids and modes match (and are valid), then file is unmodified */ } else if (git_oid_equal(&oitem->id, &nitem->id) && omode == nmode && - !git_oid_is_zero(&oitem->id)) { + !git_oid_iszero(&oitem->id)) { status = GIT_DELTA_UNMODIFIED; /* if we have an unknown OID and a workdir iterator, then check some * circumstances that can accelerate things or need special handling */ - } else if (git_oid_is_zero(&nitem->id) && new_is_workdir) { + } else if (git_oid_iszero(&nitem->id) && new_is_workdir) { bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0); git_index *index = git_iterator_index(info->new_iter); status = GIT_DELTA_UNMODIFIED; @@ -841,11 +841,11 @@ } /* if we got here and decided that the files are modified, but we * haven't calculated the OID of the new item, then calculate it now */ - if (modified_uncertain && git_oid_is_zero(&nitem->id)) { + if (modified_uncertain && git_oid_iszero(&nitem->id)) { const git_oid *update_check = DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ? &oitem->id : NULL; if ((error = git_diff__oid_for_entry( @@ -875,11 +875,11 @@ return error; } return diff_delta__from_two( diff, status, oitem, omode, nitem, nmode, - git_oid_is_zero(&noid) ? NULL : &noid, matched_pathspec); + git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec); } static bool entry_is_prefixed( git_diff_generated *diff, const git_index_entry *item, @@ -1260,49 +1260,43 @@ git_diff_free(&diff->base); return error; } -static int diff_prepare_iterator_opts(char **prefix, git_iterator_options *a, int aflags, - git_iterator_options *b, int bflags, - const git_diff_options *opts) -{ - GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); +#define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \ + git_iterator *a = NULL, *b = NULL; \ + char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \ + git_pathspec_prefix(&opts->pathspec) : NULL; \ + git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \ + b_opts = GIT_ITERATOR_OPTIONS_INIT; \ + a_opts.flags = FLAGS_FIRST; \ + a_opts.start = pfx; \ + a_opts.end = pfx; \ + b_opts.flags = FLAGS_SECOND; \ + b_opts.start = pfx; \ + b_opts.end = pfx; \ + GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \ + if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \ + a_opts.pathlist.strings = opts->pathspec.strings; \ + a_opts.pathlist.count = opts->pathspec.count; \ + b_opts.pathlist.strings = opts->pathspec.strings; \ + b_opts.pathlist.count = opts->pathspec.count; \ + } \ + if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \ + error = git_diff__from_iterators(&diff, repo, a, b, opts); \ + git__free(pfx); git_iterator_free(a); git_iterator_free(b); \ +} while (0) - *prefix = NULL; - - if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { - a->pathlist.strings = opts->pathspec.strings; - a->pathlist.count = opts->pathspec.count; - b->pathlist.strings = opts->pathspec.strings; - b->pathlist.count = opts->pathspec.count; - } else if (opts) { - *prefix = git_pathspec_prefix(&opts->pathspec); - GIT_ERROR_CHECK_ALLOC(prefix); - } - - a->flags = aflags; - b->flags = bflags; - a->start = b->start = *prefix; - a->end = b->end = *prefix; - - return 0; -} - int git_diff_tree_to_tree( git_diff **out, git_repository *repo, git_tree *old_tree, git_tree *new_tree, const git_diff_options *opts) { - git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE; - git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, - b_opts = GIT_ITERATOR_OPTIONS_INIT; - git_iterator *a = NULL, *b = NULL; git_diff *diff = NULL; - char *prefix = NULL; + git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE; int error = 0; assert(out && repo); *out = NULL; @@ -1312,23 +1306,17 @@ * for case insensitivity */ if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0) iflag = GIT_ITERATOR_IGNORE_CASE; - if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, iflag, &b_opts, iflag, opts)) < 0 || - (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || - (error = git_iterator_for_tree(&b, new_tree, &b_opts)) < 0 || - (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) - goto out; + DIFF_FROM_ITERATORS( + git_iterator_for_tree(&a, old_tree, &a_opts), iflag, + git_iterator_for_tree(&b, new_tree, &b_opts), iflag + ); - *out = diff; - diff = NULL; -out: - git_iterator_free(a); - git_iterator_free(b); - git_diff_free(diff); - git__free(prefix); + if (!error) + *out = diff; return error; } static int diff_load_index(git_index **index, git_repository *repo) @@ -1347,17 +1335,13 @@ git_repository *repo, git_tree *old_tree, git_index *index, const git_diff_options *opts) { + git_diff *diff = NULL; git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_CONFLICTS; - git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, - b_opts = GIT_ITERATOR_OPTIONS_INIT; - git_iterator *a = NULL, *b = NULL; - git_diff *diff = NULL; - char *prefix = NULL; bool index_ignore_case = false; int error = 0; assert(out && repo); @@ -1366,107 +1350,84 @@ if (!index && (error = diff_load_index(&index, repo)) < 0) return error; index_ignore_case = index->ignore_case; - if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, iflag, &b_opts, iflag, opts)) < 0 || - (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || - (error = git_iterator_for_index(&b, repo, index, &b_opts)) < 0 || - (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) - goto out; + DIFF_FROM_ITERATORS( + git_iterator_for_tree(&a, old_tree, &a_opts), iflag, + git_iterator_for_index(&b, repo, index, &b_opts), iflag + ); /* if index is in case-insensitive order, re-sort deltas to match */ - if (index_ignore_case) + if (!error && index_ignore_case) git_diff__set_ignore_case(diff, true); - *out = diff; - diff = NULL; -out: - git_iterator_free(a); - git_iterator_free(b); - git_diff_free(diff); - git__free(prefix); + if (!error) + *out = diff; return error; } int git_diff_index_to_workdir( git_diff **out, git_repository *repo, git_index *index, const git_diff_options *opts) { - git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, - b_opts = GIT_ITERATOR_OPTIONS_INIT; - git_iterator *a = NULL, *b = NULL; git_diff *diff = NULL; - char *prefix = NULL; int error = 0; assert(out && repo); *out = NULL; if (!index && (error = diff_load_index(&index, repo)) < 0) return error; - if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_INCLUDE_CONFLICTS, - &b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts)) < 0 || - (error = git_iterator_for_index(&a, repo, index, &a_opts)) < 0 || - (error = git_iterator_for_workdir(&b, repo, index, NULL, &b_opts)) < 0 || - (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) - goto out; + DIFF_FROM_ITERATORS( + git_iterator_for_index(&a, repo, index, &a_opts), + GIT_ITERATOR_INCLUDE_CONFLICTS, - if ((diff->opts.flags & GIT_DIFF_UPDATE_INDEX) && ((git_diff_generated *)diff)->index_updated) - if ((error = git_index_write(index)) < 0) - goto out; + git_iterator_for_workdir(&b, repo, index, NULL, &b_opts), + GIT_ITERATOR_DONT_AUTOEXPAND + ); - *out = diff; - diff = NULL; -out: - git_iterator_free(a); - git_iterator_free(b); - git_diff_free(diff); - git__free(prefix); + if (!error && (diff->opts.flags & GIT_DIFF_UPDATE_INDEX) != 0 && + ((git_diff_generated *)diff)->index_updated) + error = git_index_write(index); + if (!error) + *out = diff; + return error; } int git_diff_tree_to_workdir( git_diff **out, git_repository *repo, git_tree *old_tree, const git_diff_options *opts) { - git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, - b_opts = GIT_ITERATOR_OPTIONS_INIT; - git_iterator *a = NULL, *b = NULL; git_diff *diff = NULL; - char *prefix = NULL; git_index *index; - int error; + int error = 0; assert(out && repo); *out = NULL; - if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, 0, - &b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts) < 0) || - (error = git_repository_index__weakptr(&index, repo)) < 0 || - (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || - (error = git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts)) < 0 || - (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) - goto out; + if ((error = git_repository_index__weakptr(&index, repo))) + return error; - *out = diff; - diff = NULL; -out: - git_iterator_free(a); - git_iterator_free(b); - git_diff_free(diff); - git__free(prefix); + DIFF_FROM_ITERATORS( + git_iterator_for_tree(&a, old_tree, &a_opts), 0, + git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND + ); + if (!error) + *out = diff; + return error; } int git_diff_tree_to_workdir_with_index( git_diff **out, @@ -1505,38 +1466,27 @@ git_repository *repo, git_index *old_index, git_index *new_index, const git_diff_options *opts) { - git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, - b_opts = GIT_ITERATOR_OPTIONS_INIT; - git_iterator *a = NULL, *b = NULL; - git_diff *diff = NULL; - char *prefix = NULL; - int error; + git_diff *diff; + int error = 0; assert(out && old_index && new_index); *out = NULL; - if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_DONT_IGNORE_CASE, - &b_opts, GIT_ITERATOR_DONT_IGNORE_CASE, opts) < 0) || - (error = git_iterator_for_index(&a, repo, old_index, &a_opts)) < 0 || - (error = git_iterator_for_index(&b, repo, new_index, &b_opts)) < 0 || - (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0) - goto out; + DIFF_FROM_ITERATORS( + git_iterator_for_index(&a, repo, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE, + git_iterator_for_index(&b, repo, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE + ); /* if index is in case-insensitive order, re-sort deltas to match */ - if (old_index->ignore_case || new_index->ignore_case) + if (!error && (old_index->ignore_case || new_index->ignore_case)) git_diff__set_ignore_case(diff, true); - *out = diff; - diff = NULL; -out: - git_iterator_free(a); - git_iterator_free(b); - git_diff_free(diff); - git__free(prefix); + if (!error) + *out = diff; return error; } int git_diff__paired_foreach(