Skip to content

Commit

Permalink
blame: add option to ignore whitespace changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlSchwan committed Apr 14, 2020
1 parent 918a7d1 commit 9830ab3
Show file tree
Hide file tree
Showing 28 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/git2/blame.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ typedef enum {
* to canonical real names and email addresses. The mailmap will be read
* from the working directory, or HEAD in a bare repository. */
GIT_BLAME_USE_MAILMAP = (1<<5),
/** Ignore whitespace differences */
GIT_BLAME_IGNORE_WHITESPACE = (1<<6),
} git_blame_flag_t;

/**
Expand Down
9 changes: 6 additions & 3 deletions src/blame_git.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,14 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
b->size -= trimmed - recovered;
}

static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data)
static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data, git_blame_options *options)
{
xpparam_t xpp = {0};
xdemitconf_t xecfg = {0};
xdemitcb_t ecb = {0};
xpparam_t xpp = {0};

if (options->flags & GIT_BLAME_IGNORE_WHITESPACE)
xpp.flags |= XDF_IGNORE_WHITESPACE;

xecfg.hunk_func = my_emit;
ecb.priv = cb_data;
Expand Down Expand Up @@ -409,7 +412,7 @@ static int pass_blame_to_parent(
fill_origin_blob(parent, &file_p);
fill_origin_blob(target, &file_o);

if (diff_hunks(file_p, file_o, &d) < 0)
if (diff_hunks(file_p, file_o, &d, &blame->options) < 0)
return -1;

/* The reset (i.e. anything after tlno) are the same as the parent */
Expand Down
18 changes: 18 additions & 0 deletions tests/blame/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ void test_blame_simple__can_restrict_lines_min(void)
check_blame_hunk_index(g_repo, g_blame, 1, 11, 5, 0, "aa06ecca", "b.txt");
}

/*
* $ git blame -n c.txt
* orig line no final line no
* commit V author timestamp V
* 702c7aa5 1 (Carl Schwan 2020-01-29 01:52:31 +0100 4
*/
void test_blame_simple__can_ignore_whitespace_change(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;

cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));

opts.flags |= GIT_BLAME_IGNORE_WHITESPACE;
cl_git_pass(git_blame_file(&g_blame, g_repo, "c.txt", &opts));
cl_assert_equal_i(1, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "702c7aa5", "c.txt");
}

/*
* $ git blame -n b.txt -L ,6
* orig line no final line no
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
xm��ΣH���S�ݚ�b�*��Q��`�c7��l ���w���yH)?)R�PF1].� ������@�� YĕEEK���� Ws���KB����\� @H��g9�U�P| �!/p"�!�D$��a
Le��N3Ч �� �^O��9��RL��[1]�P +�P�`�@��>�s�R�@��E��F�� ?��o�Z��m�ͽk�߿J֌�\��ΰ�0�ߜ��[�$Y��R�4M߫�9?�{�����.��)�ui�>$�9{�s
�X�w��O�i�&E�4�<�VWr}fJ�ewH��}���eo����}ȼ1<�r�'���G��ބ��"�';L�k�DN�oխ�s�0�݃4+�n��X�U�y�%�UU�a�lk�򰬧Mrz�v��uv̐C�{���n6t�?���^I2:�gNG+�z�Ϯ��=W�=#̩����HbNj솇}��q( �����\)p׽��C���\r�%+������e�"�n�g $w�3z�>M���F`�e�R�N�΃��9���*=O&]�t6�{�a���R~���piS�NÒ �:�@�u*�B�֍����8��9^�r�)ܴV
������� �:�l��M1`���|\k�K���J�h ��Hp�Hzs%��R����v94��v_MA�(�E+Q��f�QsgA��J�6�F��/�xc��+ߟհ�q�i���K苬�_��'��ьG�Ue�Q���y?!�k��{+0ƒ}7|c�^C�ż��/FקX�( 29z�\6�M`ԣb�ϏsݙF1���Ӑcޢ���t��?y���2���CIe �o�s����I�
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/resources/blametest.git/refs/heads/master
Original file line number Diff line number Diff line change
@@ -1 +1 @@
836bc00b06cb60eb0f629e237ad2b58adb2cfc7e
d93e87a0863c7ec5e772f99e72ca9efddf0ca718

0 comments on commit 9830ab3

Please sign in to comment.