Commit c2f602f8 by Russell Belfer

Fix line numbering for patches with eofnl

When a patch contained an eofnl change (i.e. the last line either
gained or lost a newline), the oldno and newno line number values
for the lines in the last hunk of the patch were not useful.  This
makes them behave in a more expected manner.
parent d63eec69
...@@ -930,11 +930,13 @@ static int diff_patch_line_cb( ...@@ -930,11 +930,13 @@ static int diff_patch_line_cb(
switch (line_origin) { switch (line_origin) {
case GIT_DIFF_LINE_ADDITION: case GIT_DIFF_LINE_ADDITION:
case GIT_DIFF_LINE_DEL_EOFNL:
line->oldno = -1; line->oldno = -1;
line->newno = patch->newno; line->newno = patch->newno;
patch->newno += line->lines; patch->newno += line->lines;
break; break;
case GIT_DIFF_LINE_DELETION: case GIT_DIFF_LINE_DELETION:
case GIT_DIFF_LINE_ADD_EOFNL:
line->oldno = patch->oldno; line->oldno = patch->oldno;
line->newno = -1; line->newno = -1;
patch->oldno += line->lines; patch->oldno += line->lines;
......
...@@ -226,6 +226,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) ...@@ -226,6 +226,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
size_t hdrlen, hunklen, textlen; size_t hdrlen, hunklen, textlen;
char origin; char origin;
int oldno, newno; int oldno, newno;
git_buf old_content = GIT_BUF_INIT, actual = GIT_BUF_INIT;
const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n"; const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n";
g_repo = cl_git_sandbox_init("renames"); g_repo = cl_git_sandbox_init("renames");
...@@ -233,6 +234,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) ...@@ -233,6 +234,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_new(&cfg));
git_repository_set_config(g_repo, cfg); git_repository_set_config(g_repo, cfg);
cl_git_pass(
git_futils_readbuffer(&old_content, "renames/songof7cities.txt"));
cl_git_rewritefile("renames/songof7cities.txt", new_content); cl_git_rewritefile("renames/songof7cities.txt", new_content);
cl_git_pass(git_repository_head_tree(&head, g_repo)); cl_git_pass(git_repository_head_tree(&head, g_repo));
...@@ -263,21 +267,24 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) ...@@ -263,21 +267,24 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
cl_git_pass(git_diff_patch_get_line_in_hunk( cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 0)); &origin, &text, &textlen, &oldno, &newno, patch, 0, 0));
cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
cl_assert(strncmp("Ivory their outposts were--the guardrooms of them gilded,\n", text, textlen) == 0); cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("Ivory their outposts were--the guardrooms of them gilded,\n", actual.ptr);
cl_assert_equal_i(6, oldno); cl_assert_equal_i(6, oldno);
cl_assert_equal_i(6, newno); cl_assert_equal_i(6, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk( cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 3)); &origin, &text, &textlen, &oldno, &newno, patch, 0, 3));
cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
cl_assert(strncmp("All the world went softly when it walked before my Cities--\n", text, textlen) == 0); cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("All the world went softly when it walked before my Cities--\n", actual.ptr);
cl_assert_equal_i(9, oldno); cl_assert_equal_i(9, oldno);
cl_assert_equal_i(-1, newno); cl_assert_equal_i(-1, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk( cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 12)); &origin, &text, &textlen, &oldno, &newno, patch, 0, 12));
cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
cl_assert(strncmp("This is some new text;\n", text, textlen) == 0); cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("This is some new text;\n", actual.ptr);
cl_assert_equal_i(-1, oldno); cl_assert_equal_i(-1, oldno);
cl_assert_equal_i(9, newno); cl_assert_equal_i(9, newno);
...@@ -298,26 +305,103 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) ...@@ -298,26 +305,103 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
cl_git_pass(git_diff_patch_get_line_in_hunk( cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 1, 0)); &origin, &text, &textlen, &oldno, &newno, patch, 1, 0));
cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
cl_assert(strncmp("My rulers and their treasure and their unborn populations,\n", text, textlen) == 0); cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("My rulers and their treasure and their unborn populations,\n", actual.ptr);
cl_assert_equal_i(31, oldno); cl_assert_equal_i(31, oldno);
cl_assert_equal_i(25, newno); cl_assert_equal_i(25, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk( cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 1, 3)); &origin, &text, &textlen, &oldno, &newno, patch, 1, 3));
cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
cl_assert(strncmp("The Daughters of the Palace whom they cherished in my Cities,\n", text, textlen) == 0); cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("The Daughters of the Palace whom they cherished in my Cities,\n", actual.ptr);
cl_assert_equal_i(34, oldno); cl_assert_equal_i(34, oldno);
cl_assert_equal_i(-1, newno); cl_assert_equal_i(-1, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk( cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 1, 12)); &origin, &text, &textlen, &oldno, &newno, patch, 1, 12));
cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
cl_assert(strncmp("Another replacement;\n", text, textlen) == 0); cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("Another replacement;\n", actual.ptr);
cl_assert_equal_i(-1, oldno); cl_assert_equal_i(-1, oldno);
cl_assert_equal_i(28, newno); cl_assert_equal_i(28, newno);
git_diff_patch_free(patch); git_diff_patch_free(patch);
git_diff_list_free(diff); git_diff_list_free(diff);
/* Let's check line numbers when there is no newline */
git_buf_rtrim(&old_content);
cl_git_rewritefile("renames/songof7cities.txt", old_content.ptr);
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, head, &opt));
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0));
cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);
cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(patch));
/* check hunk 0 */
cl_git_pass(
git_diff_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0));
cl_assert_equal_i(6, (int)hunklen);
cl_assert_equal_i(46, (int)range->old_start);
cl_assert_equal_i(4, (int)range->old_lines);
cl_assert_equal_i(46, (int)range->new_start);
cl_assert_equal_i(4, (int)range->new_lines);
cl_assert_equal_i(6, (int)git_diff_patch_num_lines_in_hunk(patch, 0));
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 1));
cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("And the horses and the chariots fleeing from them as of old!\n", actual.ptr);
cl_assert_equal_i(47, oldno);
cl_assert_equal_i(47, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 2));
cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("\n", actual.ptr);
cl_assert_equal_i(48, oldno);
cl_assert_equal_i(48, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 3));
cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s(" -- Rudyard Kipling\n", actual.ptr);
cl_assert_equal_i(49, oldno);
cl_assert_equal_i(-1, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 4));
cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s(" -- Rudyard Kipling", actual.ptr);
cl_assert_equal_i(-1, oldno);
cl_assert_equal_i(49, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 5));
cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)origin);
cl_git_pass(git_buf_set(&actual, text, textlen));
cl_assert_equal_s("\n\\ No newline at end of file\n", actual.ptr);
cl_assert_equal_i(-1, oldno);
cl_assert_equal_i(49, newno);
git_diff_patch_free(patch);
git_diff_list_free(diff);
git_buf_free(&actual);
git_buf_free(&old_content);
git_tree_free(head); git_tree_free(head);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -355,6 +439,38 @@ static void check_single_patch_stats( ...@@ -355,6 +439,38 @@ static void check_single_patch_stats(
git__free(text); git__free(text);
} }
/* walk lines in hunk with basic sanity checks */
for (; hunks > 0; --hunks) {
size_t i, max_i;
int lastoldno = -1, oldno, lastnewno = -1, newno;
char origin;
max_i = git_diff_patch_num_lines_in_hunk(patch, hunks - 1);
for (i = 0; i < max_i; ++i) {
int expected = 1;
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, NULL, NULL, &oldno, &newno, patch, hunks - 1, i));
if (origin == GIT_DIFF_LINE_ADD_EOFNL ||
origin == GIT_DIFF_LINE_DEL_EOFNL ||
origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
expected = 0;
if (oldno >= 0) {
if (lastoldno >= 0)
cl_assert_equal_i(expected, oldno - lastoldno);
lastoldno = oldno;
}
if (newno >= 0) {
if (lastnewno >= 0)
cl_assert_equal_i(expected, newno - lastnewno);
lastnewno = newno;
}
}
}
git_diff_patch_free(patch); git_diff_patch_free(patch);
git_diff_list_free(diff); git_diff_list_free(diff);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment