diff options
Diffstat (limited to 'src/blame.c')
-rw-r--r-- | src/blame.c | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/src/blame.c b/src/blame.c index 219a6bfe4..eb977c287 100644 --- a/src/blame.c +++ b/src/blame.c @@ -20,12 +20,15 @@ static int hunk_byfinalline_search_cmp(const void *key, const void *entry) { - uint16_t lineno = (uint16_t)*(size_t*)key; git_blame_hunk *hunk = (git_blame_hunk*)entry; - if (lineno < hunk->final_start_line_number) + size_t lineno = *(size_t*)key; + size_t lines_in_hunk = (size_t)hunk->lines_in_hunk; + size_t final_start_line_number = (size_t)hunk->final_start_line_number; + + if (lineno < final_start_line_number) return -1; - if (lineno >= hunk->final_start_line_number + hunk->lines_in_hunk) + if (lineno >= final_start_line_number + lines_in_hunk) return 1; return 0; } @@ -76,8 +79,8 @@ static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id); git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id); newhunk->boundary = hunk->boundary; - newhunk->final_signature = git_signature_dup(hunk->final_signature); - newhunk->orig_signature = git_signature_dup(hunk->orig_signature); + git_signature_dup(&newhunk->final_signature, hunk->final_signature); + git_signature_dup(&newhunk->orig_signature, hunk->orig_signature); return newhunk; } @@ -95,7 +98,7 @@ static void shift_hunks_by(git_vector *v, size_t start_line, int shift_by) { size_t i; - if (!git_vector_bsearch2( &i, v, hunk_byfinalline_search_cmp, &start_line)) { + if (!git_vector_bsearch2(&i, v, hunk_byfinalline_search_cmp, &start_line)) { for (; i < v->length; i++) { git_blame_hunk *hunk = (git_blame_hunk*)v->contents[i]; hunk->final_start_line_number += shift_by; @@ -108,17 +111,22 @@ git_blame* git_blame__alloc( git_blame_options opts, const char *path) { - git_blame *gbr = (git_blame*)git__calloc(1, sizeof(git_blame)); - if (!gbr) { - giterr_set_oom(); + git_blame *gbr = git__calloc(1, sizeof(git_blame)); + if (!gbr) return NULL; - } - git_vector_init(&gbr->hunks, 8, hunk_cmp); - git_vector_init(&gbr->paths, 8, paths_cmp); + gbr->repository = repo; gbr->options = opts; - gbr->path = git__strdup(path); - git_vector_insert(&gbr->paths, git__strdup(path)); + + if (git_vector_init(&gbr->hunks, 8, hunk_cmp) < 0 || + git_vector_init(&gbr->paths, 8, paths_cmp) < 0 || + (gbr->path = git__strdup(path)) == NULL || + git_vector_insert(&gbr->paths, git__strdup(path)) < 0) + { + git_blame_free(gbr); + return NULL; + } + return gbr; } @@ -126,7 +134,6 @@ void git_blame_free(git_blame *blame) { size_t i; git_blame_hunk *hunk; - char *path; if (!blame) return; @@ -134,13 +141,11 @@ void git_blame_free(git_blame *blame) free_hunk(hunk); git_vector_free(&blame->hunks); - git_vector_foreach(&blame->paths, i, path) - git__free(path); - git_vector_free(&blame->paths); + git_vector_free_deep(&blame->paths); git_array_clear(blame->line_index); - git__free((void*)blame->path); + git__free(blame->path); git_blob_free(blame->final_blob); git__free(blame); } @@ -159,10 +164,10 @@ const git_blame_hunk *git_blame_get_hunk_byindex(git_blame *blame, uint32_t inde const git_blame_hunk *git_blame_get_hunk_byline(git_blame *blame, uint32_t lineno) { - size_t i; + size_t i, new_lineno = (size_t)lineno; assert(blame); - if (!git_vector_bsearch2( &i, &blame->hunks, hunk_byfinalline_search_cmp, &lineno)) { + if (!git_vector_bsearch2(&i, &blame->hunks, hunk_byfinalline_search_cmp, &new_lineno)) { return git_blame_get_hunk_byindex(blame, (uint32_t)i); } @@ -239,7 +244,7 @@ static int index_blob_lines(git_blame *blame) git_off_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--) { @@ -260,13 +265,15 @@ static int index_blob_lines(git_blame *blame) blame->num_lines = num + incomplete; return blame->num_lines; } - + static git_blame_hunk* hunk_from_entry(git_blame__entry *e) { git_blame_hunk *h = new_hunk( e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); - h->final_signature = git_signature_dup(git_commit_author(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)); h->boundary = e->is_boundary ? 1 : 0; return h; } @@ -282,8 +289,6 @@ static int load_blob(git_blame *blame) goto cleanup; error = git_object_lookup_bypath((git_object**)&blame->final_blob, (git_object*)blame->final, blame->path, GIT_OBJ_BLOB); - if (error < 0) - goto cleanup; cleanup: return error; @@ -448,7 +453,7 @@ int git_blame_buffer( git_blame **out, git_blame *reference, const char *buffer, - uint32_t buffer_len) + size_t buffer_len) { git_blame *blame; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; @@ -474,3 +479,10 @@ int git_blame_buffer( *out = blame; return 0; } + +int git_blame_init_options(git_blame_options *opts, unsigned int version) +{ + GIT_INIT_STRUCTURE_FROM_TEMPLATE( + opts, version, git_blame_options, GIT_BLAME_OPTIONS_INIT); + return 0; +} |