From f6f7537cfd8041d407a4f3b6c350b012975be116 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 12 Nov 2013 15:24:05 -0800 Subject: [PATCH 1/5] Add some failing blame test cases --- tests-clar/blame/buffer.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests-clar/blame/buffer.c b/tests-clar/blame/buffer.c index 69b2d5440..5da004652 100644 --- a/tests-clar/blame/buffer.c +++ b/tests-clar/blame/buffer.c @@ -19,6 +19,8 @@ void test_blame_buffer__cleanup(void) void test_blame_buffer__added_line(void) { + git_blame_hunk *hunk; + const char *buffer = "\ EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ @@ -39,6 +41,10 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame)); check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "000000", "b.txt"); + + hunk = git_blame_get_hunk_byline(g_bufferblame, 16); + cl_assert(hunk); + cl_assert_equal_s("Ben Straub", hunk->final_signature->name); } void test_blame_buffer__deleted_line(void) @@ -128,3 +134,33 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 1, 0, "00000000", "b.txt"); check_blame_hunk_index(g_repo, g_bufferblame, 4, 8, 3, 0, "63d671eb", "b.txt"); } + +void test_blame_buffer__add_line_at_end(void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CCCCCCCCCCCCCCCCC\n\ +abc\n\ +\n"; + cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); + + cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame)); + check_blame_hunk_index(g_repo, g_bufferblame, 0, 1, 4, 0, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 1, 5, 1, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 5, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 11, 5, 0, "aa06ecca", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 16, 1, 0, "00000000", "b.txt"); + +} From 089297b2cd4f50a2218130f9f3a0ddceb86b8697 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 12 Nov 2013 15:24:59 -0800 Subject: [PATCH 2/5] Duplicate all fields of a blame hunk --- src/blame.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/blame.c b/src/blame.c index 01d3fc03c..b562b5b98 100644 --- a/src/blame.c +++ b/src/blame.c @@ -75,6 +75,11 @@ static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) hunk->orig_path); 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; + if (hunk->final_signature) + newhunk->final_signature = git_signature_dup(hunk->final_signature); + if (hunk->orig_signature) + newhunk->orig_signature = git_signature_dup(hunk->orig_signature); return newhunk; } From 9db56cc4a78ee7799450cd3838af12124ccc4e8e Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 12 Nov 2013 18:57:16 -0800 Subject: [PATCH 3/5] Fix buffer blame with new lines at end of file --- src/blame.c | 15 ++++++--------- tests-clar/blame/buffer.c | 5 +++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/blame.c b/src/blame.c index b562b5b98..c5b7083ad 100644 --- a/src/blame.c +++ b/src/blame.c @@ -386,9 +386,13 @@ static int buffer_hunk_cb( wedge_line = (hunk->old_lines == 0) ? hunk->new_start : hunk->old_start; blame->current_diff_line = wedge_line; - /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byline(blame, wedge_line); - if (!hunk_starts_at_or_after_line(blame->current_hunk, 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); + 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); } @@ -409,13 +413,6 @@ static int buffer_line_cb( GIT_UNUSED(hunk); GIT_UNUSED(line); -#ifdef DO_DEBUG - { - char *str = git__substrdup(content, content_len); - git__free(str); - } -#endif - if (line->origin == GIT_DIFF_LINE_ADDITION) { if (hunk_is_bufferblame(blame->current_hunk) && hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) { diff --git a/tests-clar/blame/buffer.c b/tests-clar/blame/buffer.c index 5da004652..82085c3d5 100644 --- a/tests-clar/blame/buffer.c +++ b/tests-clar/blame/buffer.c @@ -152,8 +152,9 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +\n\ abc\n\ -\n"; +def\n"; cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame)); @@ -161,6 +162,6 @@ abc\n\ check_blame_hunk_index(g_repo, g_bufferblame, 1, 5, 1, 1, "b99f7ac0", "b.txt"); check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 5, 0, "63d671eb", "b.txt"); check_blame_hunk_index(g_repo, g_bufferblame, 3, 11, 5, 0, "aa06ecca", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 4, 16, 1, 0, "00000000", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 16, 2, 0, "00000000", "b.txt"); } From b20c40a8d6127a3ee9b807b2243e41bc8e85124c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 12 Nov 2013 19:02:28 -0800 Subject: [PATCH 4/5] Don't leak memory when duplicating a NULL signature --- src/blame.c | 6 ++---- src/signature.c | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/blame.c b/src/blame.c index c5b7083ad..219a6bfe4 100644 --- a/src/blame.c +++ b/src/blame.c @@ -76,10 +76,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; - if (hunk->final_signature) - newhunk->final_signature = git_signature_dup(hunk->final_signature); - if (hunk->orig_signature) - newhunk->orig_signature = git_signature_dup(hunk->orig_signature); + newhunk->final_signature = git_signature_dup(hunk->final_signature); + newhunk->orig_signature = git_signature_dup(hunk->orig_signature); return newhunk; } diff --git a/src/signature.c b/src/signature.c index 52ca2b375..ec51a42e9 100644 --- a/src/signature.c +++ b/src/signature.c @@ -84,8 +84,12 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema git_signature *git_signature_dup(const git_signature *sig) { - git_signature *new = git__calloc(1, sizeof(git_signature)); + git_signature *new; + if (sig == NULL) + return NULL; + + new = git__calloc(1, sizeof(git_signature)); if (new == NULL) return NULL; From 4320bec09bff4596ed0647eb2fcbedf19995f631 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 12 Nov 2013 19:02:34 -0800 Subject: [PATCH 5/5] Minor cleanup --- tests-clar/blame/buffer.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests-clar/blame/buffer.c b/tests-clar/blame/buffer.c index 82085c3d5..912ee9846 100644 --- a/tests-clar/blame/buffer.c +++ b/tests-clar/blame/buffer.c @@ -19,7 +19,7 @@ void test_blame_buffer__cleanup(void) void test_blame_buffer__added_line(void) { - git_blame_hunk *hunk; + const git_blame_hunk *hunk; const char *buffer = "\ EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ @@ -135,7 +135,7 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC check_blame_hunk_index(g_repo, g_bufferblame, 4, 8, 3, 0, "63d671eb", "b.txt"); } -void test_blame_buffer__add_line_at_end(void) +void test_blame_buffer__add_lines_at_end(void) { const char *buffer = "\ EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ @@ -163,5 +163,4 @@ def\n"; check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 5, 0, "63d671eb", "b.txt"); check_blame_hunk_index(g_repo, g_bufferblame, 3, 11, 5, 0, "aa06ecca", "b.txt"); check_blame_hunk_index(g_repo, g_bufferblame, 4, 16, 2, 0, "00000000", "b.txt"); - }