diff options
| author | Vicent Martà <vicent@github.com> | 2013-05-24 01:13:48 -0700 |
|---|---|---|
| committer | Vicent Martà <vicent@github.com> | 2013-05-24 01:13:48 -0700 |
| commit | 30caf0cf2942412981b7662e50a2e9242afa2cce (patch) | |
| tree | cbfb8e0a543dbce5f11230dad85e8309bf90037c /src | |
| parent | 87a56fe015d263e592242157fa6c88badf46fa18 (diff) | |
| parent | 49f70f2c370e649c3a0970b6aadd6f5cdf4f2701 (diff) | |
| download | libgit2-30caf0cf2942412981b7662e50a2e9242afa2cce.tar.gz | |
Merge pull request #1595 from arrbee/even-more-rename-fixes
Even more rename detection fixes
Diffstat (limited to 'src')
| -rw-r--r-- | src/checkout.c | 29 | ||||
| -rw-r--r-- | src/diff.c | 15 | ||||
| -rw-r--r-- | src/diff.h | 12 | ||||
| -rw-r--r-- | src/diff_output.c | 133 | ||||
| -rw-r--r-- | src/diff_tform.c | 561 | ||||
| -rw-r--r-- | src/fileops.c | 26 | ||||
| -rw-r--r-- | src/fileops.h | 4 | ||||
| -rw-r--r-- | src/oid.c | 46 | ||||
| -rw-r--r-- | src/util.h | 7 |
9 files changed, 579 insertions, 254 deletions
diff --git a/src/checkout.c b/src/checkout.c index 5820f626a..c28fcdee0 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -676,33 +676,26 @@ static int buffer_to_file( int file_open_flags, mode_t file_mode) { - int fd, error; + int error; if ((error = git_futils_mkpath2file(path, dir_mode)) < 0) return error; - if ((fd = p_open(path, file_open_flags, file_mode)) < 0) { - giterr_set(GITERR_OS, "Could not open '%s' for writing", path); - return fd; - } - - if ((error = p_write(fd, git_buf_cstr(buffer), git_buf_len(buffer))) < 0) { - giterr_set(GITERR_OS, "Could not write to '%s'", path); - (void)p_close(fd); - } else { - if ((error = p_close(fd)) < 0) - giterr_set(GITERR_OS, "Error while closing '%s'", path); + if ((error = git_futils_writebuffer( + buffer, path, file_open_flags, file_mode)) < 0) + return error; - if ((error = p_stat(path, st)) < 0) - giterr_set(GITERR_OS, "Error while statting '%s'", path); + if (st != NULL && (error = p_stat(path, st)) < 0) { + giterr_set(GITERR_OS, "Error while statting '%s'", path); + return error; } - if (!error && - (file_mode & 0100) != 0 && - (error = p_chmod(path, file_mode)) < 0) + if ((file_mode & 0100) != 0 && (error = p_chmod(path, file_mode)) < 0) { giterr_set(GITERR_OS, "Failed to set permissions on '%s'", path); + return error; + } - return error; + return 0; } static int blob_content_to_file( diff --git a/src/diff.c b/src/diff.c index d93506984..d2389f103 100644 --- a/src/diff.c +++ b/src/diff.c @@ -231,10 +231,23 @@ static char *diff_strdup_prefix(git_pool *pool, const char *prefix) return git_pool_strndup(pool, prefix, len + 1); } +GIT_INLINE(const char *) diff_delta__path(const git_diff_delta *delta) +{ + const char *str = delta->old_file.path; + + if (!str || + delta->status == GIT_DELTA_ADDED || + delta->status == GIT_DELTA_RENAMED || + delta->status == GIT_DELTA_COPIED) + str = delta->new_file.path; + + return str; +} + int git_diff_delta__cmp(const void *a, const void *b) { const git_diff_delta *da = a, *db = b; - int val = strcmp(da->old_file.path, db->old_file.path); + int val = strcmp(diff_delta__path(da), diff_delta__path(db)); return val ? val : ((int)da->status - (int)db->status); } diff --git a/src/diff.h b/src/diff.h index 16df431ed..ac8ab2aed 100644 --- a/src/diff.h +++ b/src/diff.h @@ -34,10 +34,18 @@ enum { GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */ GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */ GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */ - GIT_DIFF_FLAG__TO_DELETE = (1 << 11), /* delete entry during rename det. */ - GIT_DIFF_FLAG__TO_SPLIT = (1 << 12), /* split entry during rename det. */ + + GIT_DIFF_FLAG__TO_DELETE = (1 << 16), /* delete entry during rename det. */ + GIT_DIFF_FLAG__TO_SPLIT = (1 << 17), /* split entry during rename det. */ + GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18), + GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19), + GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20), }; +#define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF) + +#define GIT_DIFF__VERBOSE (1 << 30) + struct git_diff_list { git_refcount rc; git_repository *repo; diff --git a/src/diff_output.c b/src/diff_output.c index be1ff56e7..8dd110cbf 100644 --- a/src/diff_output.c +++ b/src/diff_output.c @@ -236,9 +236,8 @@ static int get_blob_content( char oidstr[GIT_OID_HEXSZ+1]; git_buf content = GIT_BUF_INIT; - git_oid_fmt(oidstr, &file->oid); - oidstr[GIT_OID_HEXSZ] = 0; - git_buf_printf(&content, "Subproject commit %s\n", oidstr ); + git_oid_tostr(oidstr, sizeof(oidstr), &file->oid); + git_buf_printf(&content, "Subproject commit %s\n", oidstr); map->data = git_buf_detach(&content); map->len = strlen(map->data); @@ -318,14 +317,13 @@ static int get_workdir_sm_content( } } - git_oid_fmt(oidstr, &file->oid); - oidstr[GIT_OID_HEXSZ] = '\0'; + git_oid_tostr(oidstr, sizeof(oidstr), &file->oid); if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) sm_status_text = "-dirty"; - git_buf_printf(&content, "Subproject commit %s%s\n", - oidstr, sm_status_text); + git_buf_printf( + &content, "Subproject commit %s%s\n", oidstr, sm_status_text); map->data = git_buf_detach(&content); map->len = strlen(map->data); @@ -1021,8 +1019,33 @@ typedef struct { git_diff_data_cb print_cb; void *payload; git_buf *buf; + int oid_strlen; } diff_print_info; +static int diff_print_info_init( + diff_print_info *pi, + git_buf *out, git_diff_list *diff, git_diff_data_cb cb, void *payload) +{ + assert(diff && diff->repo); + + pi->diff = diff; + pi->print_cb = cb; + pi->payload = payload; + pi->buf = out; + + if (git_repository__cvar(&pi->oid_strlen, diff->repo, GIT_CVAR_ABBREV) < 0) + return -1; + + pi->oid_strlen += 1; /* for NUL byte */ + + if (pi->oid_strlen < 2) + pi->oid_strlen = 2; + else if (pi->oid_strlen > GIT_OID_HEXSZ + 1) + pi->oid_strlen = GIT_OID_HEXSZ + 1; + + return 0; +} + static char pick_suffix(int mode) { if (S_ISDIR(mode)) @@ -1106,34 +1129,79 @@ int git_diff_print_compact( git_buf buf = GIT_BUF_INIT; diff_print_info pi; - pi.diff = diff; - pi.print_cb = print_cb; - pi.payload = payload; - pi.buf = &buf; - - error = git_diff_foreach(diff, print_compact, NULL, NULL, &pi); + if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload))) + error = git_diff_foreach(diff, print_compact, NULL, NULL, &pi); git_buf_free(&buf); return error; } -static int print_oid_range(diff_print_info *pi, const git_diff_delta *delta) +static int print_raw( + const git_diff_delta *delta, float progress, void *data) { - int abbrevlen; + diff_print_info *pi = data; + char code = git_diff_status_char(delta->status); char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1]; - if (git_repository__cvar(&abbrevlen, pi->diff->repo, GIT_CVAR_ABBREV) < 0) + GIT_UNUSED(progress); + + if (code == ' ') + return 0; + + git_buf_clear(pi->buf); + + git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.oid); + git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.oid); + + git_buf_printf( + pi->buf, ":%06o %06o %s... %s... %c", + delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code); + + if (delta->similarity > 0) + git_buf_printf(pi->buf, "%03u", delta->similarity); + + if (delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED) + git_buf_printf( + pi->buf, "\t%s %s\n", delta->old_file.path, delta->new_file.path); + else + git_buf_printf( + pi->buf, "\t%s\n", delta->old_file.path ? + delta->old_file.path : delta->new_file.path); + + if (git_buf_oom(pi->buf)) return -1; - abbrevlen += 1; /* for NUL byte */ - if (abbrevlen < 2) - abbrevlen = 2; - else if (abbrevlen > (int)sizeof(start_oid)) - abbrevlen = (int)sizeof(start_oid); + if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, + git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) + return callback_error(); + + return 0; +} - git_oid_tostr(start_oid, abbrevlen, &delta->old_file.oid); - git_oid_tostr(end_oid, abbrevlen, &delta->new_file.oid); +int git_diff_print_raw( + git_diff_list *diff, + git_diff_data_cb print_cb, + void *payload) +{ + int error; + git_buf buf = GIT_BUF_INIT; + diff_print_info pi; + + if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload))) + error = git_diff_foreach(diff, print_raw, NULL, NULL, &pi); + + git_buf_free(&buf); + + return error; +} + +static int print_oid_range(diff_print_info *pi, const git_diff_delta *delta) +{ + char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1]; + + git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.oid); + git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.oid); /* TODO: Match git diff more closely */ if (delta->old_file.mode == delta->new_file.mode) { @@ -1289,13 +1357,9 @@ int git_diff_print_patch( git_buf buf = GIT_BUF_INIT; diff_print_info pi; - pi.diff = diff; - pi.print_cb = print_cb; - pi.payload = payload; - pi.buf = &buf; - - error = git_diff_foreach( - diff, print_patch_file, print_patch_hunk, print_patch_line, &pi); + if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload))) + error = git_diff_foreach( + diff, print_patch_file, print_patch_hunk, print_patch_line, &pi); git_buf_free(&buf); @@ -1736,12 +1800,9 @@ int git_diff_patch_print( assert(patch && print_cb); - pi.diff = patch->diff; - pi.print_cb = print_cb; - pi.payload = payload; - pi.buf = &temp; - - error = print_patch_file(patch->delta, 0, &pi); + if (!(error = diff_print_info_init( + &pi, &temp, patch->diff, print_cb, payload))) + error = print_patch_file(patch->delta, 0, &pi); for (h = 0; h < patch->hunks_size && !error; ++h) { diff_patch_hunk *hunk = &patch->hunks[h]; diff --git a/src/diff_tform.c b/src/diff_tform.c index 84650a37b..bc3acae1d 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -18,12 +18,15 @@ static git_diff_delta *diff_delta__dup( return NULL; memcpy(delta, d, sizeof(git_diff_delta)); + GIT_DIFF_FLAG__CLEAR_INTERNAL(delta->flags); - delta->old_file.path = git_pool_strdup(pool, d->old_file.path); - if (delta->old_file.path == NULL) - goto fail; + if (d->old_file.path != NULL) { + delta->old_file.path = git_pool_strdup(pool, d->old_file.path); + if (delta->old_file.path == NULL) + goto fail; + } - if (d->new_file.path != d->old_file.path) { + if (d->new_file.path != d->old_file.path && d->new_file.path != NULL) { delta->new_file.path = git_pool_strdup(pool, d->new_file.path); if (delta->new_file.path == NULL) goto fail; @@ -220,7 +223,7 @@ int git_diff_find_similar__calc_similarity( #define DEFAULT_THRESHOLD 50 #define DEFAULT_BREAK_REWRITE_THRESHOLD 60 -#define DEFAULT_TARGET_LIMIT 200 +#define DEFAULT_RENAME_LIMIT 200 static int normalize_find_opts( git_diff_list *diff, @@ -253,12 +256,25 @@ static int normalize_find_opts( /* some flags imply others */ + if (opts->flags & GIT_DIFF_FIND_EXACT_MATCH_ONLY) { + /* if we are only looking for exact matches, then don't turn + * MODIFIED items into ADD/DELETE pairs because it's too picky + */ + opts->flags &= ~(GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES); + + /* similarly, don't look for self-rewrites to split */ + opts->flags &= ~GIT_DIFF_FIND_RENAMES_FROM_REWRITES; + } + if (opts->flags & GIT_DIFF_FIND_RENAMES_FROM_REWRITES) opts->flags |= GIT_DIFF_FIND_RENAMES; if (opts->flags & GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED) opts->flags |= GIT_DIFF_FIND_COPIES; + if (opts->flags & GIT_DIFF_BREAK_REWRITES) + opts->flags |= GIT_DIFF_FIND_REWRITES; + #define USE_DEFAULT(X) ((X) == 0 || (X) > 100) if (USE_DEFAULT(opts->rename_threshold)) @@ -275,15 +291,15 @@ static int normalize_find_opts( #undef USE_DEFAULT - if (!opts->target_limit) { + if (!opts->rename_limit) { int32_t limit = 0; - opts->target_limit = DEFAULT_TARGET_LIMIT; + opts->rename_limit = DEFAULT_RENAME_LIMIT; if (git_config_get_int32(&limit, cfg, "diff.renameLimit") < 0) giterr_clear(); else if (limit > 0) - opts->target_limit = limit; + opts->rename_limit = limit; } /* assign the internal metric with whitespace flag as payload */ @@ -307,11 +323,12 @@ static int normalize_find_opts( return 0; } -static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size) +static int apply_splits_and_deletes( + git_diff_list *diff, size_t expected_size, bool actually_split) { git_vector onto = GIT_VECTOR_INIT; size_t i; - git_diff_delta *delta; + git_diff_delta *delta, *deleted; if (git_vector_init(&onto, expected_size, git_diff_delta__cmp) < 0) return -1; @@ -321,9 +338,11 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size) if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0) continue; - if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) { - git_diff_delta *deleted = diff_delta__dup(delta, &diff->pool); - if (!deleted) + if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0 && actually_split) { + delta->similarity = 0; + + /* make new record for DELETED side of split */ + if (!(deleted = diff_delta__dup(delta, &diff->pool))) goto on_error; deleted->status = GIT_DELTA_DELETED; @@ -334,32 +353,46 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size) if (git_vector_insert(&onto, deleted) < 0) goto on_error; - delta->status = GIT_DELTA_ADDED; + if (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) + delta->status = GIT_DELTA_UNTRACKED; + else + delta->status = GIT_DELTA_ADDED; memset(&delta->old_file, 0, sizeof(delta->old_file)); delta->old_file.path = delta->new_file.path; delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID; } + /* clean up delta before inserting into new list */ + GIT_DIFF_FLAG__CLEAR_INTERNAL(delta->flags); + + if (delta->status != GIT_DELTA_COPIED && + delta->status != GIT_DELTA_RENAMED && + (delta->status != GIT_DELTA_MODIFIED || actually_split)) + delta->similarity = 0; + + /* insert into new list */ if (git_vector_insert(&onto, delta) < 0) goto on_error; } /* cannot return an error past this point */ - git_vector_foreach(&diff->deltas, i, delta) + + /* free deltas from old list that didn't make it to the new one */ + git_vector_foreach(&diff->deltas, i, delta) { if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0) git__free(delta); + } /* swap new delta list into place */ - git_vector_sort(&onto); git_vector_swap(&diff->deltas, &onto); git_vector_free(&onto); + git_vector_sort(&diff->deltas); return 0; on_error: git_vector_foreach(&onto, i, delta) git__free(delta); - git_vector_free(&onto); return -1; @@ -373,13 +406,13 @@ GIT_INLINE(git_diff_file *) similarity_get_file(git_diff_list *diff, size_t idx) static int similarity_calc( git_diff_list *diff, - git_diff_find_options *opts, + const git_diff_find_options *opts, size_t file_idx, void **cache) { int error = 0; git_diff_file *file = similarity_get_file(diff, file_idx); - git_iterator_type_t src = (file_idx & 1) ? diff->old_src : diff->new_src; + git_iterator_type_t src = (file_idx & 1) ? diff->new_src : diff->old_src; if (src == GIT_ITERATOR_TYPE_WORKDIR) { /* compute hashsig from file */ git_buf path = GIT_BUF_INIT; @@ -422,22 +455,56 @@ static int similarity_calc( return error; } +#define FLAG_SET(opts,flag_name) (((opts)->flags & flag_name) != 0) + +/* - score < 0 means files cannot be compared + * - score >= 100 means files are exact match + * - score == 0 means files are completely different + */ static int similarity_measure( + int *score, git_diff_list *diff, - git_diff_find_options *opts, + const git_diff_find_options *opts, void **cache, size_t a_idx, size_t b_idx) { - int score = 0; git_diff_file *a_file = similarity_get_file(diff, a_idx); git_diff_file *b_file = similarity_get_file(diff, b_idx); + bool exact_match = FLAG_SET(opts, GIT_DIFF_FIND_EXACT_MATCH_ONLY); + + *score = -1; + /* don't try to compare files of different types */ if (GIT_MODE_TYPE(a_file->mode) != GIT_MODE_TYPE(b_file->mode)) return 0; - if (git_oid__cmp(&a_file->oid, &b_file->oid) == 0) - return 100; + /* if exact match is requested, force calculation of missing OIDs */ + if (exact_match) { + if (git_oid_iszero(&a_file->oid) && + diff->old_src == GIT_ITERATOR_TYPE_WORKDIR && + !git_diff__oid_for_file(diff->repo, a_file->path, + a_file->mode, a_file->size, &a_file->oid)) + a_file->flags |= GIT_DIFF_FLAG_VALID_OID; + + if (git_oid_iszero(&b_file->oid) && + diff->new_src == GIT_ITERATOR_TYPE_WORKDIR && + !git_diff__oid_for_file(diff->repo, b_file->path, + b_file->mode, b_file->size, &b_file->oid)) + b_file->flags |= GIT_DIFF_FLAG_VALID_OID; + } + + /* check OID match as a quick test */ + if (git_oid__cmp(&a_file->oid, &b_file->oid) == 0) { + *score = 100; + return 0; + } + + /* don't calculate signatures if we are doing exact match */ + if (exact_match) { + *score = 0; + return 0; + } /* update signature cache if needed */ if (!cache[a_idx] && similarity_calc(diff, opts, a_idx, cache) < 0) @@ -450,231 +517,369 @@ static int similarity_measure( return 0; /* compare signatures */ - if (opts->metric->similarity( - &score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0) - return -1; + return opts->metric->similarity( + score, cache[a_idx], cache[b_idx], opts->metric->payload); +} + +static int calc_self_similarity( + git_diff_list *diff, + const git_diff_find_options *opts, + size_t delta_idx, + void **cache) +{ + int error, similarity = -1; + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + if ((delta->flags & GIT_DIFF_FLAG__HAS_SELF_SIMILARITY) != 0) + return 0; + + error = similarity_measure( + &similarity, diff, opts, cache, 2 * delta_idx, 2 * delta_idx + 1); + if (error < 0) + return error; + + if (similarity >= 0) { + delta->similarity = (uint32_t)similarity; + delta->flags |= GIT_DIFF_FLAG__HAS_SELF_SIMILARITY; + } + + return 0; +} + +static bool is_rename_target( + git_diff_list *diff, + const git_diff_find_options *opts, + size_t delta_idx, + void **cache) +{ + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + /* skip things that aren't plain blobs */ + if (!GIT_MODE_ISBLOB(delta->new_file.mode)) + return false; + + /* only consider ADDED, RENAMED, COPIED, and split MODIFIED as + * targets; maybe include UNTRACKED and IGNORED if requested. + */ + switch (delta->status) { + case GIT_DELTA_UNMODIFIED: + case GIT_DELTA_DELETED: + return false; + + case GIT_DELTA_MODIFIED: + if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) && + !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES)) + return false; + + if (calc_self_similarity(diff, opts, delta_idx, cache) < 0) + return false; + + if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) && + delta->similarity < opts->break_rewrite_threshold) { + delta->flags |= GIT_DIFF_FLAG__TO_SPLIT; + break; + } + if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) && + delta->similarity < opts->rename_from_rewrite_threshold) + break; + + return false; + + case GIT_DELTA_UNTRACKED: + case GIT_DELTA_IGNORED: + if (!FLAG_SET(opts, GIT_DIFF_FIND_FOR_UNTRACKED)) + return false; + break; + + default: /* all other status values should be checked */ + break; + } + + delta->flags |= GIT_DIFF_FLAG__IS_RENAME_TARGET; + return true; +} + +static bool is_rename_source( + git_diff_list *diff, + const git_diff_find_options *opts, + size_t delta_idx, + void **cache) +{ + git_diff_delta *delta = GIT_VECTOR_GET(&diff->deltas, delta_idx); + + /* skip things that aren't blobs */ + if (!GIT_MODE_ISBLOB(delta->old_file.mode)) + return false; + + switch (delta->status) { + case GIT_DELTA_ADDED: + case GIT_DELTA_UNTRACKED: + case GIT_DELTA_IGNORED: + return false; + + case GIT_DELTA_DELETED: + case GIT_DELTA_TYPECHANGE: + break; + + case GIT_DELTA_UNMODIFIED: + if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED)) + return false; + break; + + default: /* MODIFIED, RENAMED, COPIED */ + /* if we're finding copies, this could be a source */ + if (FLAG_SET(opts, GIT_DIFF_FIND_COPIES)) + break; + + /* otherwise, this is only a source if we can split it */ + if (!FLAG_SET(opts, GIT_DIFF_FIND_REWRITES) && + !FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES)) + return false; + + if (calc_self_similarity(diff, opts, delta_idx, cache) < 0) + return false; + + if (FLAG_SET(opts, GIT_DIFF_BREAK_REWRITES) && + delta->similarity < opts->break_rewrite_threshold) { + delta->flags |= GIT_DIFF_FLAG__TO_SPLIT; + break; + } + + if (FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) && + delta->similarity < opts->rename_from_rewrite_threshold) + break; - /* clip score */ - if (score < 0) - score = 0; - else if (score > 100) - score = 100; + return false; + } - return score; + delta->flags |= GIT_DIFF_FLAG__IS_RENAME_SOURCE; + return true; } -#define FLAG_SET(opts,flag_name) ((opts.flags & flag_name) != 0) +GIT_INLINE(bool) delta_is_split(git_diff_delta *delta) +{ + return (delta->status == GIT_DELTA_TYPECHANGE || + (delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0); +} + +GIT_INLINE(bool) delta_is_new_only(git_diff_delta *delta) +{ + return (delta->status == GIT_DELTA_ADDED || + delta->status == GIT_DELTA_UNTRACKED || + delta->status == GIT_DELTA_IGNORED); +} + +typedef struct { + uint32_t idx; + uint32_t similarity; +} diff_find_match; int git_diff_find_similar( git_diff_list *diff, git_diff_find_options *given_opts) { - size_t i, j, cache_size, *matches; + size_t i, j, cache_size; int error = 0, similarity; git_diff_delta *from, *to; git_diff_find_options opts; - size_t tried_targets, num_rewrites = 0; - void **cache; + size_t num_rewrites = 0, num_updates = 0; + void **cache; /* cache of similarity metric file signatures */ + diff_find_match *matches; /* cache of best matches */ if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0) return error; - /* TODO: maybe abort if deltas.length > target_limit ??? */ + /* TODO: maybe abort if deltas.length > rename_limit ??? */ + if (!git__is_uint32(diff->deltas.length)) + return 0; cache_size = diff->deltas.length * 2; /* must store b/c length may change */ cache = git__calloc(cache_size, sizeof(void *)); GITERR_CHECK_ALLOC(cache); - matches = git__calloc(diff->deltas.length, sizeof(size_t)); + matches = git__calloc(diff->deltas.length, sizeof(diff_find_match)); GITERR_CHECK_ALLOC(matches); - /* first break MODIFIED records that are too different (if requested) */ - - if (FLAG_SET(opts, GIT_DIFF_FIND_AND_BREAK_REWRITES)) { - git_vector_foreach(&diff->deltas, i, from) { - if (from->status != GIT_DELTA_MODIFIED) - continue; - - similarity = similarity_measure( - diff, &opts, cache, 2 * i, 2 * i + 1); - - if (similarity < 0) { - error = similarity; - goto cleanup; - } - - if ((unsigned int)similarity < opts.break_rewrite_threshold) { - from->flags |= GIT_DIFF_FLAG__TO_SPLIT; - num_rewrites++; - } - } - } - /* next find the most similar delta for each rename / copy candidate */ - git_vector_foreach(&diff->deltas, i, from) { - tried_targets = 0; + git_vector_foreach(&diff->deltas, i, to) { + size_t tried_sources = 0; - /* skip things that aren't blobs */ - if (GIT_MODE_TYPE(from->old_file.mode) != - GIT_MODE_TYPE(GIT_FILEMODE_BLOB)) - continue; - - /* don't check UNMODIFIED files as source unless given option */ - if (from->status == GIT_DELTA_UNMODIFIED && - !FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED)) - continue; + matches[i].idx = i; + matches[i].similarity = 0; - /* skip all but DELETED files unless copy detection is on */ - if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES) && - from->status != GIT_DELTA_DELETED && - (from->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0) + /* skip things that are not rename targets */ + if (!is_rename_target(diff, &opts, i, cache)) continue; - git_vector_foreach(&diff->deltas, j, to) { + git_vector_foreach(&diff->deltas, j, from) { if (i == j) continue; - /* skip things that aren't blobs */ - if (GIT_MODE_TYPE(to->new_file.mode) != - GIT_MODE_TYPE(GIT_FILEMODE_BLOB)) + /* skip things that are not rename sources */ + if (!is_rename_source(diff, &opts, j, cache)) continue; - switch (to->status) { - case GIT_DELTA_ADDED: - case GIT_DELTA_UNTRACKED: - case GIT_DELTA_RENAMED: - case GIT_DELTA_COPIED: + /* cap on maximum targets we'll examine (per "to" file) */ + if (++tried_sources > opts.rename_limit) break; - case GIT_DELTA_MODIFIED: - if ((to->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0) - continue; - break; - default: - /* only the above status values should be checked */ - continue; - } - /* cap on maximum files we'll examine (per "from" file) */ - if (++tried_targets > opts.target_limit) - break; - - /* calculate similarity and see if this pair beats the - * similarity score of the current best pair. - */ - similarity = similarity_measure( - diff, &opts, cache, 2 * i, 2 * j + 1); - - if (similarity < 0) { - error = similarity; + /* calculate similarity for this pair and find best match */ + if ((error = similarity_measure( + &similarity, diff, &opts, cache, 2 * j, 2 * i + 1)) < 0) goto cleanup; + + if (similarity < 0) { /* not actually comparable */ + --tried_sources; + continue; } - if (to->similarity < (unsigned int)similarity) { - to->similarity = (unsigned int)similarity; - matches[j] = i + 1; + if (matches[i].similarity < (uint32_t)similarity) { + matches[i].similarity = (uint32_t)similarity; + matches[i].idx = j; } } } /* next rewrite the diffs with renames / copies */ - git_vector_foreach(&diff->deltas, j, to) { - if (!matches[j]) { - assert(to->similarity == 0); + git_vector_foreach(&diff->deltas, i, to) { + + /* check if this delta was matched to another one */ + if ((similarity = (int)matches[i].similarity) <= 0) continue; - } + assert(to && (to->flags & GIT_DIFF_FLAG__IS_RENAME_TARGET) != 0); + + from = GIT_VECTOR_GET(&diff->deltas, matches[i].idx); + assert(from && (from->flags & GIT_DIFF_FLAG__IS_RENAME_SOURCE) != 0); - i = matches[j] - 1; - from = GIT_VECTOR_GET(&diff->deltas, i); - assert(from); - - /* four possible outcomes here: - * 1. old DELETED and if over rename threshold, - * new becomes RENAMED and old goes away - * 2. old SPLIT and if over rename threshold, - * new becomes RENAMED and old becomes ADDED (clear SPLIT) - * 3. old was MODIFIED but FIND_RENAMES_FROM_REWRITES is on and - * old is more similar to new than it is to itself, in which - * case, new becomes RENAMED and old becomed ADDED - * 4. otherwise if over copy threshold, new becomes COPIED + /* possible scenarios: + * 1. from DELETE to ADD/UNTRACK/IGNORE = RENAME + * 2. from DELETE to SPLIT/TYPECHANGE = RENAME + DELETE + * 3. from SPLIT/TYPECHANGE to ADD/UNTRACK/IGNORE = ADD + RENAME + * 4. from SPLIT/TYPECHANGE to SPLIT/TYPECHANGE = RENAME + SPLIT + * 5. from OTHER to ADD/UNTRACK/IGNORE = OTHER + COPY */ if (from->status == GIT_DELTA_DELETED) { - if (to->similarity < opts.rename_threshold) { - to->similarity = 0; - continue; - } - to->status = GIT_DELTA_RENAMED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); + if (delta_is_new_only(to)) { - from->flags |= GIT_DIFF_FLAG__TO_DELETE; - num_rewrites++; + if (similarity < (int)opts.rename_threshold) + continue; - continue; - } + from->status = GIT_DELTA_RENAMED; + from->similarity = (uint32_t)similarity; + memcpy(&from->new_file, &to->new_file, sizeof(from->new_file)); - if (from->status == GIT_DELTA_MODIFIED && - (from->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) - { - if (to->similarity < opts.rename_threshold) { - to->similarity = 0; - continue; - } + to->flags |= GIT_DIFF_FLAG__TO_DELETE; - to->status = GIT_DELTA_RENAMED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); + num_rewrites++; + } else { + assert(delta_is_split(to)); - from->status = GIT_DELTA_ADDED; - from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; - memset(&from->old_file, 0, sizeof(from->old_file)); - num_rewrites--; + if (similarity < (int)opts.rename_from_rewrite_threshold) + continue; - continue; - } + from->status = GIT_DELTA_RENAMED; + from->similarity = (uint32_t)similarity; + memcpy(&from->new_file, &to->new_file, sizeof(from->new_file)); - if (from->status == GIT_DELTA_MODIFIED && - FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) && - to->similarity > opts.rename_threshold) - { - similarity = similarity_measure( - diff, &opts, cache, 2 * i, 2 * i + 1); + to->status = GIT_DELTA_DELETED; + memset(&to->new_file, 0, sizeof(to->new_file)); + to->new_file.path = to->old_file.path; + to->new_file.flags |= GIT_DIFF_FLAG_VALID_OID; + if ((to->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) { + to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; + num_rewrites--; + } - if (similarity < 0) { - error = similarity; - goto cleanup; + num_updates++; } + } - if ((unsigned int)similarity < opts.rename_from_rewrite_threshold) { - to->status = GIT_DELTA_RENAMED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); + else if (delta_is_split(from)) { + git_diff_file swap; - from->status = GIT_DELTA_ADDED; - memset(&from->old_file, 0, sizeof(from->old_file)); - from->old_file.path = to->old_file.path; - from->old_file.flags |= GIT_DIFF_FLAG_VALID_OID; + if (delta_is_new_only(to)) { - continue; + if (similarity < (int)opts.rename_threshold) + continue; + + memcpy(&swap, &from->new_file, sizeof(swap)); + + from->status = GIT_DELTA_RENAMED; + from->similarity = (uint32_t)similarity; + memcpy(&from->new_file, &to->new_file, sizeof(from->new_file)); + if ((from->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) { + from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; + num_rewrites--; + } + + to->status = (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ? + GIT_DELTA_UNTRACKED : GIT_DELTA_ADDED; + memcpy(&to->new_file, &swap, sizeof(to->new_file)); + to->old_file.path = to->new_file.path; + + num_updates++; + } else { + assert(delta_is_split(from)); + + if (similarity < (int)opts.rename_from_rewrite_threshold) + continue; + + memcpy(&swap, &to->new_file, sizeof(swap)); + + to->status = GIT_DELTA_RENAMED; + to->similarity = (uint32_t)similarity; + memcpy(&to->new_file, &from->new_file, sizeof(to->new_file)); + if ((to->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) { + to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; + num_rewrites--; + } + + memcpy(&from->new_file, &swap, sizeof(from->new_file)); + if ((from->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0) { + from->flags |= GIT_DIFF_FLAG__TO_SPLIT; + num_rewrites++; + } + + /* in the off chance that we've just swapped the new + * element into the correct place, clear the SPLIT flag + */ + if (matches[matches[i].idx].idx == i && + matches[matches[i].idx].similarity > + opts.rename_from_rewrite_threshold) { + + from->status = GIT_DELTA_RENAMED; + from->similarity = + (uint32_t)matches[matches[i].idx].similarity; + matches[matches[i].idx].similarity = 0; + from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; + num_rewrites--; + } + + num_updates++; } } - if (to->similarity < opts.copy_threshold) { - to->similarity = 0; - continue; - } + else if (delta_is_new_only(to)) { + if (!FLAG_SET(&opts, GIT_DIFF_FIND_COPIES) || + similarity < (int)opts.copy_threshold) + continue; - /* convert "to" to a COPIED record */ - to->status = GIT_DELTA_COPIED; - memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); - } + to->status = GIT_DELTA_COPIED; + to->similarity = (uint32_t)similarity; + memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); - if (num_rewrites > 0) { - assert(num_rewrites < diff->deltas.length); + num_updates++; + } + } + if (num_rewrites > 0 || num_updates > 0) error = apply_splits_and_deletes( - diff, diff->deltas.length - num_rewrites); - } + diff, diff->deltas.length - num_rewrites, + FLAG_SET(&opts, GIT_DIFF_BREAK_REWRITES)); cleanup: git__free(matches); diff --git a/src/fileops.c b/src/fileops.c index 98ab8efe3..a3e43214f 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -202,6 +202,32 @@ int git_futils_readbuffer(git_buf *buf, const char *path) return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL); } +int git_futils_writebuffer( + const git_buf *buf, const char *path, int flags, mode_t mode) +{ + int fd, error = 0; + + if (flags <= 0) + flags = O_CREAT | O_TRUNC | O_WRONLY; + if (!mode) + mode = GIT_FILEMODE_BLOB; + + if ((fd = p_open(path, flags, mode)) < 0) { + giterr_set(GITERR_OS, "Could not open '%s' for writing", path); + return fd; + } + + if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) { + giterr_set(GITERR_OS, "Could not write to '%s'", path); + (void)p_close(fd); + } + + if ((error = p_close(fd)) < 0) + giterr_set(GITERR_OS, "Error while closing '%s'", path); + + return error; +} + int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) { if (git_futils_mkpath2file(to, dirmode) < 0) diff --git a/src/fileops.h b/src/fileops.h index 627a6923d..f4e059c83 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -22,6 +22,9 @@ extern int git_futils_readbuffer_updated( git_buf *obj, const char *path, time_t *mtime, size_t *size, int *updated); extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len); +extern int git_futils_writebuffer( + const git_buf *buf, const char *path, int open_flags, mode_t mode); + /** * File utils * @@ -223,6 +226,7 @@ extern git_off_t git_futils_filesize(git_file fd); #define GIT_MODE_PERMS_MASK 0777 #define GIT_CANONICAL_PERMS(MODE) (((MODE) & 0100) ? 0755 : 0644) #define GIT_MODE_TYPE(MODE) ((MODE) & ~GIT_MODE_PERMS_MASK) +#define GIT_MODE_ISBLOB(MODE) (GIT_MODE_TYPE(MODE) == GIT_MODE_TYPE(GIT_FILEMODE_BLOB)) /** * Convert a mode_t from the OS to a legal git mode_t value. @@ -68,12 +68,31 @@ GIT_INLINE(char) *fmt_one(char *str, unsigned int val) return str; } -void git_oid_fmt(char *str, const git_oid *oid) +void git_oid_nfmt(char *str, size_t n, const git_oid *oid) { - size_t i; + size_t i, max_i; + + if (!oid) { + memset(str, 0, n); + return; + } + if (n > GIT_OID_HEXSZ) { + memset(&str[GIT_OID_HEXSZ], 0, n - GIT_OID_HEXSZ); + n = GIT_OID_HEXSZ; + } + + max_i = n / 2; - for (i = 0; i < sizeof(oid->id); i++) + for (i = 0; i < max_i; i++) str = fmt_one(str, oid->id[i]); + + if (n & 1) + *str++ = to_hex[oid->id[i] >> 4]; +} + +void git_oid_fmt(char *str, const git_oid *oid) +{ + git_oid_nfmt(str, GIT_OID_HEXSZ, oid); } void git_oid_pathfmt(char *str, const git_oid *oid) @@ -91,31 +110,20 @@ char *git_oid_allocfmt(const git_oid *oid) char *str = git__malloc(GIT_OID_HEXSZ + 1); if (!str) return NULL; - git_oid_fmt(str, oid); - str[GIT_OID_HEXSZ] = '\0'; + git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid); return str; } char *git_oid_tostr(char *out, size_t n, const git_oid *oid) { - char str[GIT_OID_HEXSZ]; - if (!out || n == 0) return ""; - n--; /* allow room for terminating NUL */ - - if (oid == NULL) - n = 0; - - if (n > 0) { - git_oid_fmt(str, oid); - if (n > GIT_OID_HEXSZ) - n = GIT_OID_HEXSZ; - memcpy(out, str, n); - } + if (n > GIT_OID_HEXSZ + 1) + n = GIT_OID_HEXSZ + 1; - out[n] = '\0'; + git_oid_nfmt(out, n - 1, oid); /* allow room for terminating NUL */ + out[n - 1] = '\0'; return out; } diff --git a/src/util.h b/src/util.h index 6f876d012..5ae87ac10 100644 --- a/src/util.h +++ b/src/util.h @@ -109,6 +109,13 @@ GIT_INLINE(int) git__is_sizet(git_off_t p) return p == (git_off_t)r; } +/** @return true if p fits into the range of a uint32_t */ +GIT_INLINE(int) git__is_uint32(size_t p) +{ + uint32_t r = (uint32_t)p; + return p == (size_t)r; +} + /* 32-bit cross-platform rotl */ #ifdef _MSC_VER /* use built-in method in MSVC */ # define git__rotl(v, s) (uint32_t)_rotl(v, s) |
