vendor/libgit2/src/blame.c in rugged-0.27.9 vs vendor/libgit2/src/blame.c in rugged-0.27.10

- old
+ new

@@ -12,10 +12,11 @@ #include "git2/revwalk.h" #include "git2/tree.h" #include "git2/diff.h" #include "git2/blob.h" #include "git2/signature.h" +#include "git2/mailmap.h" #include "util.h" #include "repository.h" #include "blame_git.h" @@ -38,11 +39,16 @@ static int hunk_cmp(const void *_a, const void *_b) { git_blame_hunk *a = (git_blame_hunk*)_a, *b = (git_blame_hunk*)_b; - return a->final_start_line_number - b->final_start_line_number; + if (a->final_start_line_number > b->final_start_line_number) + return 1; + else if (a->final_start_line_number < b->final_start_line_number) + return -1; + else + return 0; } static bool hunk_ends_at_or_before_line(git_blame_hunk *hunk, size_t line) { return line >= (hunk->final_start_line_number + hunk->lines_in_hunk - 1); @@ -130,10 +136,16 @@ { git_blame_free(gbr); return NULL; } + if (opts.flags & GIT_BLAME_USE_MAILMAP && + git_mailmap_from_repository(&gbr->mailmap, repo) < 0) { + git_blame_free(gbr); + return NULL; + } + return gbr; } void git_blame_free(git_blame *blame) { @@ -148,10 +160,12 @@ git_vector_free_deep(&blame->paths); git_array_clear(blame->line_index); + git_mailmap_free(blame->mailmap); + git__free(blame->path); git_blob_free(blame->final_blob); git__free(blame); } @@ -188,11 +202,11 @@ if (!in) in = &dummy; memcpy(out, in, sizeof(git_blame_options)); /* No newest_commit => HEAD */ - if (git_oid_iszero(&out->newest_commit)) { + if (git_oid_is_zero(&out->newest_commit)) { if (git_reference_name_to_id(&out->newest_commit, repo, "HEAD") < 0) { return -1; } } @@ -252,47 +266,48 @@ * https://github.com/gitster/git/blob/be5c9fb9049ed470e7005f159bb923a5f4de1309/builtin/blame.c#L1760-L1789 */ static int index_blob_lines(git_blame *blame) { const char *buf = blame->final_buf; - git_off_t len = blame->final_buf_size; + size_t len = blame->final_buf_size; int num = 0, incomplete = 0, bol = 1; size_t *i; if (len && buf[len-1] != '\n') incomplete++; /* incomplete line at the end */ while (len--) { if (bol) { i = git_array_alloc(blame->line_index); - GITERR_CHECK_ALLOC(i); + GIT_ERROR_CHECK_ALLOC(i); *i = buf - blame->final_buf; bol = 0; } if (*buf++ == '\n') { num++; bol = 1; } } i = git_array_alloc(blame->line_index); - GITERR_CHECK_ALLOC(i); + GIT_ERROR_CHECK_ALLOC(i); *i = buf - blame->final_buf; blame->num_lines = num + incomplete; return blame->num_lines; } -static git_blame_hunk* hunk_from_entry(git_blame__entry *e) +static git_blame_hunk* hunk_from_entry(git_blame__entry *e, git_blame *blame) { git_blame_hunk *h = new_hunk( e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); if (!h) return NULL; git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit)); - git_signature_dup(&h->final_signature, git_commit_author(e->suspect->commit)); - git_signature_dup(&h->orig_signature, git_commit_author(e->suspect->commit)); + git_commit_author_with_mailmap( + &h->final_signature, e->suspect->commit, blame->mailmap); + git_signature_dup(&h->orig_signature, h->final_signature); h->boundary = e->is_boundary ? 1 : 0; return h; } static int load_blob(git_blame *blame) @@ -303,11 +318,11 @@ error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit); if (error < 0) goto cleanup; error = git_object_lookup_bypath((git_object**)&blame->final_blob, - (git_object*)blame->final, blame->path, GIT_OBJ_BLOB); + (git_object*)blame->final, blame->path, GIT_OBJECT_BLOB); cleanup: return error; } @@ -318,15 +333,22 @@ git_blame__origin *o; if ((error = load_blob(blame)) < 0 || (error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0) goto cleanup; + + if (git_blob_rawsize(blame->final_blob) > SIZE_MAX) { + git_error_set(GIT_ERROR_NOMEMORY, "blob is too large to blame"); + error = -1; + goto cleanup; + } + blame->final_buf = git_blob_rawcontent(blame->final_blob); - blame->final_buf_size = git_blob_rawsize(blame->final_blob); + blame->final_buf_size = (size_t)git_blob_rawsize(blame->final_blob); ent = git__calloc(1, sizeof(git_blame__entry)); - GITERR_CHECK_ALLOC(ent); + GIT_ERROR_CHECK_ALLOC(ent); ent->num_lines = index_blob_lines(blame); ent->lno = blame->options.min_line - 1; ent->num_lines = ent->num_lines - blame->options.min_line + 1; if (blame->options.max_line > 0) @@ -339,11 +361,11 @@ error = git_blame__like_git(blame, blame->options.flags); cleanup: for (ent = blame->ent; ent; ) { git_blame__entry *e = ent->next; - git_blame_hunk *h = hunk_from_entry(ent); + git_blame_hunk *h = hunk_from_entry(ent, blame); git_vector_insert(&blame->hunks, h); git_blame__free_entry(ent); ent = e; @@ -369,11 +391,11 @@ assert(out && repo && path); if ((error = normalize_options(&normOptions, options, repo)) < 0) goto on_error; blame = git_blame__alloc(repo, normOptions, path); - GITERR_CHECK_ALLOC(blame); + GIT_ERROR_CHECK_ALLOC(blame); if ((error = load_blob(blame)) < 0) goto on_error; if ((error = blame_internal(blame)) < 0) @@ -391,11 +413,11 @@ * Buffer blaming *******************************************************************************/ static bool hunk_is_bufferblame(git_blame_hunk *hunk) { - return git_oid_iszero(&hunk->final_commit_id); + return git_oid_is_zero(&hunk->final_commit_id); } static int buffer_hunk_cb( const git_diff_delta *delta, const git_diff_hunk *hunk, @@ -411,18 +433,18 @@ blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byline(blame, wedge_line); if (!blame->current_hunk) { /* Line added at the end of the file */ blame->current_hunk = new_hunk(wedge_line, 0, wedge_line, blame->path); - GITERR_CHECK_ALLOC(blame->current_hunk); + GIT_ERROR_CHECK_ALLOC(blame->current_hunk); git_vector_insert(&blame->hunks, blame->current_hunk); } else if (!hunk_starts_at_or_after_line(blame->current_hunk, wedge_line)){ /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ blame->current_hunk = split_hunk_in_vector(&blame->hunks, blame->current_hunk, wedge_line - blame->current_hunk->orig_start_line_number, true); - GITERR_CHECK_ALLOC(blame->current_hunk); + GIT_ERROR_CHECK_ALLOC(blame->current_hunk); } return 0; } @@ -447,11 +469,11 @@ shift_hunks_by(&blame->hunks, blame->current_diff_line+1, 1); } else { /* Create a new buffer-blame hunk with this line */ shift_hunks_by(&blame->hunks, blame->current_diff_line, 1); blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path); - GITERR_CHECK_ALLOC(blame->current_hunk); + GIT_ERROR_CHECK_ALLOC(blame->current_hunk); git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL); } blame->current_diff_line++; } @@ -488,16 +510,16 @@ diffopts.context_lines = 0; assert(out && reference && buffer && buffer_len); blame = git_blame__alloc(reference->repository, reference->options, reference->path); - GITERR_CHECK_ALLOC(blame); + GIT_ERROR_CHECK_ALLOC(blame); /* Duplicate all of the hunk structures in the reference blame */ git_vector_foreach(&reference->hunks, i, hunk) { git_blame_hunk *h = dup_hunk(hunk); - GITERR_CHECK_ALLOC(h); + GIT_ERROR_CHECK_ALLOC(h); git_vector_insert(&blame->hunks, h); } /* Diff to the reference blob */ @@ -507,11 +529,16 @@ *out = blame; return 0; } -int git_blame_init_options(git_blame_options *opts, unsigned int version) +int git_blame_options_init(git_blame_options *opts, unsigned int version) { GIT_INIT_STRUCTURE_FROM_TEMPLATE( opts, version, git_blame_options, GIT_BLAME_OPTIONS_INIT); return 0; +} + +int git_blame_init_options(git_blame_options *opts, unsigned int version) +{ + return git_blame_options_init(opts, version); }