Skip to content

Commit 9830ab3

Browse files
committed
blame: add option to ignore whitespace changes
1 parent 918a7d1 commit 9830ab3

28 files changed

+31
-4
lines changed

include/git2/blame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ typedef enum {
4747
* to canonical real names and email addresses. The mailmap will be read
4848
* from the working directory, or HEAD in a bare repository. */
4949
GIT_BLAME_USE_MAILMAP = (1<<5),
50+
/** Ignore whitespace differences */
51+
GIT_BLAME_IGNORE_WHITESPACE = (1<<6),
5052
} git_blame_flag_t;
5153

5254
/**

src/blame_git.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,14 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
365365
b->size -= trimmed - recovered;
366366
}
367367

368-
static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data)
368+
static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data, git_blame_options *options)
369369
{
370-
xpparam_t xpp = {0};
371370
xdemitconf_t xecfg = {0};
372371
xdemitcb_t ecb = {0};
372+
xpparam_t xpp = {0};
373+
374+
if (options->flags & GIT_BLAME_IGNORE_WHITESPACE)
375+
xpp.flags |= XDF_IGNORE_WHITESPACE;
373376

374377
xecfg.hunk_func = my_emit;
375378
ecb.priv = cb_data;
@@ -409,7 +412,7 @@ static int pass_blame_to_parent(
409412
fill_origin_blob(parent, &file_p);
410413
fill_origin_blob(target, &file_o);
411414

412-
if (diff_hunks(file_p, file_o, &d) < 0)
415+
if (diff_hunks(file_p, file_o, &d, &blame->options) < 0)
413416
return -1;
414417

415418
/* The reset (i.e. anything after tlno) are the same as the parent */

tests/blame/simple.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,24 @@ void test_blame_simple__can_restrict_lines_min(void)
238238
check_blame_hunk_index(g_repo, g_blame, 1, 11, 5, 0, "aa06ecca", "b.txt");
239239
}
240240

241+
/*
242+
* $ git blame -n c.txt
243+
* orig line no final line no
244+
* commit V author timestamp V
245+
* 702c7aa5 1 (Carl Schwan 2020-01-29 01:52:31 +0100 4
246+
*/
247+
void test_blame_simple__can_ignore_whitespace_change(void)
248+
{
249+
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
250+
251+
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
252+
253+
opts.flags |= GIT_BLAME_IGNORE_WHITESPACE;
254+
cl_git_pass(git_blame_file(&g_blame, g_repo, "c.txt", &opts));
255+
cl_assert_equal_i(1, git_blame_get_hunk_count(g_blame));
256+
check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "702c7aa5", "c.txt");
257+
}
258+
241259
/*
242260
* $ git blame -n b.txt -L ,6
243261
* orig line no final line no
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.

0 commit comments

Comments
 (0)