Commit 66137ff6ea9e516e0fa840134393d5a81d5b86e9

Patrick Steinhardt 2020-04-19T12:08:24

Merge pull request #5383 from ognarb/feature/blame-ignore-whitespace Feature: Allow blame to ignore whitespace change

diff --git a/include/git2/blame.h b/include/git2/blame.h
index 73f6cf9..f42c815 100644
--- a/include/git2/blame.h
+++ b/include/git2/blame.h
@@ -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;
 
 /**
diff --git a/src/blame_git.c b/src/blame_git.c
index a9157c4..073137a 100644
--- a/src/blame_git.c
+++ b/src/blame_git.c
@@ -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;
@@ -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 */
diff --git a/tests/blame/simple.c b/tests/blame/simple.c
index 16e7bc4..6b13ccc 100644
--- a/tests/blame/simple.c
+++ b/tests/blame/simple.c
@@ -239,6 +239,24 @@ void test_blame_simple__can_restrict_lines_min(void)
 }
 
 /*
+ * $ 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
  * commit    V  author     timestamp                  V
diff --git a/tests/resources/blametest.git/objects/1d/81379086fd6d91ee027e883cf6f4703a107dfb b/tests/resources/blametest.git/objects/1d/81379086fd6d91ee027e883cf6f4703a107dfb
new file mode 100644
index 0000000..a35dd56
Binary files /dev/null and b/tests/resources/blametest.git/objects/1d/81379086fd6d91ee027e883cf6f4703a107dfb differ
diff --git a/tests/resources/blametest.git/objects/34/73c3e21e76492d09b80b7c75569edc275dffcf b/tests/resources/blametest.git/objects/34/73c3e21e76492d09b80b7c75569edc275dffcf
new file mode 100644
index 0000000..887deff
Binary files /dev/null and b/tests/resources/blametest.git/objects/34/73c3e21e76492d09b80b7c75569edc275dffcf differ
diff --git a/tests/resources/blametest.git/objects/40/fcae7fb84378fdb037dc6a3ccbb33669c3f26d b/tests/resources/blametest.git/objects/40/fcae7fb84378fdb037dc6a3ccbb33669c3f26d
new file mode 100644
index 0000000..27fc76b
Binary files /dev/null and b/tests/resources/blametest.git/objects/40/fcae7fb84378fdb037dc6a3ccbb33669c3f26d differ
diff --git a/tests/resources/blametest.git/objects/46/ef45f4ae55c1f5dca64b9e1d7ca77c1798069b b/tests/resources/blametest.git/objects/46/ef45f4ae55c1f5dca64b9e1d7ca77c1798069b
new file mode 100644
index 0000000..487ac57
Binary files /dev/null and b/tests/resources/blametest.git/objects/46/ef45f4ae55c1f5dca64b9e1d7ca77c1798069b differ
diff --git a/tests/resources/blametest.git/objects/4b/0ca755f5bfd69ed6074f268b05bb0542a42c68 b/tests/resources/blametest.git/objects/4b/0ca755f5bfd69ed6074f268b05bb0542a42c68
new file mode 100644
index 0000000..698f5b1
Binary files /dev/null and b/tests/resources/blametest.git/objects/4b/0ca755f5bfd69ed6074f268b05bb0542a42c68 differ
diff --git a/tests/resources/blametest.git/objects/4d/8400b7ce2d15ef5045c2775ed33e82a326786e b/tests/resources/blametest.git/objects/4d/8400b7ce2d15ef5045c2775ed33e82a326786e
new file mode 100644
index 0000000..6e754df
Binary files /dev/null and b/tests/resources/blametest.git/objects/4d/8400b7ce2d15ef5045c2775ed33e82a326786e differ
diff --git a/tests/resources/blametest.git/objects/6b/52ee554131a5e7bacd15553fbd22408c5a8a6f b/tests/resources/blametest.git/objects/6b/52ee554131a5e7bacd15553fbd22408c5a8a6f
new file mode 100644
index 0000000..b53aa04
Binary files /dev/null and b/tests/resources/blametest.git/objects/6b/52ee554131a5e7bacd15553fbd22408c5a8a6f differ
diff --git a/tests/resources/blametest.git/objects/70/2c7aa5250abc42be69ef78ee8fa47a346cb2ce b/tests/resources/blametest.git/objects/70/2c7aa5250abc42be69ef78ee8fa47a346cb2ce
new file mode 100644
index 0000000..1f6f2da
Binary files /dev/null and b/tests/resources/blametest.git/objects/70/2c7aa5250abc42be69ef78ee8fa47a346cb2ce differ
diff --git a/tests/resources/blametest.git/objects/77/c796837eb003c81d2cd8a6577ef4e7edc61222 b/tests/resources/blametest.git/objects/77/c796837eb003c81d2cd8a6577ef4e7edc61222
new file mode 100644
index 0000000..ae5f740
Binary files /dev/null and b/tests/resources/blametest.git/objects/77/c796837eb003c81d2cd8a6577ef4e7edc61222 differ
diff --git a/tests/resources/blametest.git/objects/7e/135d94af53b6c5edbae6a77df8a0f09375e823 b/tests/resources/blametest.git/objects/7e/135d94af53b6c5edbae6a77df8a0f09375e823
new file mode 100644
index 0000000..b374ed1
Binary files /dev/null and b/tests/resources/blametest.git/objects/7e/135d94af53b6c5edbae6a77df8a0f09375e823 differ
diff --git a/tests/resources/blametest.git/objects/92/5bddd7a536a66eecb32faa41abd5bc9c192311 b/tests/resources/blametest.git/objects/92/5bddd7a536a66eecb32faa41abd5bc9c192311
new file mode 100644
index 0000000..aba34fa
Binary files /dev/null and b/tests/resources/blametest.git/objects/92/5bddd7a536a66eecb32faa41abd5bc9c192311 differ
diff --git a/tests/resources/blametest.git/objects/a3/4ead35680be7b9704fc4c6d750d182e228e02b b/tests/resources/blametest.git/objects/a3/4ead35680be7b9704fc4c6d750d182e228e02b
new file mode 100644
index 0000000..121934e
Binary files /dev/null and b/tests/resources/blametest.git/objects/a3/4ead35680be7b9704fc4c6d750d182e228e02b differ
diff --git a/tests/resources/blametest.git/objects/a4/641ad869ffad601aa8347e0770e949bb6d90df b/tests/resources/blametest.git/objects/a4/641ad869ffad601aa8347e0770e949bb6d90df
new file mode 100644
index 0000000..e54827c
Binary files /dev/null and b/tests/resources/blametest.git/objects/a4/641ad869ffad601aa8347e0770e949bb6d90df differ
diff --git a/tests/resources/blametest.git/objects/ba/9089263dce882885ad84513f31495bf9d31132 b/tests/resources/blametest.git/objects/ba/9089263dce882885ad84513f31495bf9d31132
new file mode 100644
index 0000000..4610c26
Binary files /dev/null and b/tests/resources/blametest.git/objects/ba/9089263dce882885ad84513f31495bf9d31132 differ
diff --git a/tests/resources/blametest.git/objects/c3/8d3c99946b74173f9b037279f07d505195563f b/tests/resources/blametest.git/objects/c3/8d3c99946b74173f9b037279f07d505195563f
new file mode 100644
index 0000000..04fd9cc
Binary files /dev/null and b/tests/resources/blametest.git/objects/c3/8d3c99946b74173f9b037279f07d505195563f differ
diff --git a/tests/resources/blametest.git/objects/c4/c13c153a611418325c70d6e630fed373546c4d b/tests/resources/blametest.git/objects/c4/c13c153a611418325c70d6e630fed373546c4d
new file mode 100644
index 0000000..ba52060
Binary files /dev/null and b/tests/resources/blametest.git/objects/c4/c13c153a611418325c70d6e630fed373546c4d differ
diff --git a/tests/resources/blametest.git/objects/d2/bc4f27cbb72260eeec350087d81a60a122efe9 b/tests/resources/blametest.git/objects/d2/bc4f27cbb72260eeec350087d81a60a122efe9
new file mode 100644
index 0000000..b4d6c6d
Binary files /dev/null and b/tests/resources/blametest.git/objects/d2/bc4f27cbb72260eeec350087d81a60a122efe9 differ
diff --git a/tests/resources/blametest.git/objects/d3/c7316f0075debfe53b25e58f56b0a4b46e18c3 b/tests/resources/blametest.git/objects/d3/c7316f0075debfe53b25e58f56b0a4b46e18c3
new file mode 100644
index 0000000..73f00ee
--- /dev/null
+++ b/tests/resources/blametest.git/objects/d3/c7316f0075debfe53b25e58f56b0a4b46e18c3
@@ -0,0 +1,4 @@
+xmΣHSݚb*Q`c7lwyH)?)RPF1]. @YĕEEK WsKB\@Hg9UP|!/p"!D$a
+LeN3Ч	 ^O9RL[1]P +P`@>sR@EF?oZmͽk߿J֌
\ΰ0ߜ[$YR4M߫9?{.)ui>$9{s
+XwOi&E4<VWr}fJewH}eo
}ȼ1<r'Gބ"';LkDNoխs0݃4+n
XUy%UUalk򰬧Mrzvuv̐C{n6t?^I2:gNG+zϮ=W=#̩HbNj솇}q(\)p׽C\r%+e"ng $w3z>MF`eRN΃9*=O&]t6{aR~piSNÒ	:@u*B֍89^r)ܴV
+	:lM1`|\kKJh HpHzs%Rv94v_MA(E+QfQsgAJ6F/xc+ߟհqiK苬_'ьGUeQy?!k{+0ƒ}7|c^Cż/FקX( 29z\6M`ԣbϏsݙF1Ӑcޢt?y2CIe	osI
\ No newline at end of file
diff --git a/tests/resources/blametest.git/objects/d6/7268771ef5244f4aa224df29d4e4ae0bed2fd8 b/tests/resources/blametest.git/objects/d6/7268771ef5244f4aa224df29d4e4ae0bed2fd8
new file mode 100644
index 0000000..ee48478
Binary files /dev/null and b/tests/resources/blametest.git/objects/d6/7268771ef5244f4aa224df29d4e4ae0bed2fd8 differ
diff --git a/tests/resources/blametest.git/objects/d6/afeea2c4657c743dedab24a8a62da96f63547d b/tests/resources/blametest.git/objects/d6/afeea2c4657c743dedab24a8a62da96f63547d
new file mode 100644
index 0000000..f3e52be
Binary files /dev/null and b/tests/resources/blametest.git/objects/d6/afeea2c4657c743dedab24a8a62da96f63547d differ
diff --git a/tests/resources/blametest.git/objects/d9/3e87a0863c7ec5e772f99e72ca9efddf0ca718 b/tests/resources/blametest.git/objects/d9/3e87a0863c7ec5e772f99e72ca9efddf0ca718
new file mode 100644
index 0000000..e1c0f00
Binary files /dev/null and b/tests/resources/blametest.git/objects/d9/3e87a0863c7ec5e772f99e72ca9efddf0ca718 differ
diff --git a/tests/resources/blametest.git/objects/f0/5190494260c2f6b6d045ac9bf27cb6d7e0abcc b/tests/resources/blametest.git/objects/f0/5190494260c2f6b6d045ac9bf27cb6d7e0abcc
new file mode 100644
index 0000000..6df7d34
Binary files /dev/null and b/tests/resources/blametest.git/objects/f0/5190494260c2f6b6d045ac9bf27cb6d7e0abcc differ
diff --git a/tests/resources/blametest.git/objects/f4/f4b926582a2c23c6e3ba05309eaa89244c1d68 b/tests/resources/blametest.git/objects/f4/f4b926582a2c23c6e3ba05309eaa89244c1d68
new file mode 100644
index 0000000..32c1cdf
Binary files /dev/null and b/tests/resources/blametest.git/objects/f4/f4b926582a2c23c6e3ba05309eaa89244c1d68 differ
diff --git a/tests/resources/blametest.git/objects/fa/01940156471352d5483b4f26b7c849dfaa7eef b/tests/resources/blametest.git/objects/fa/01940156471352d5483b4f26b7c849dfaa7eef
new file mode 100644
index 0000000..b392e44
Binary files /dev/null and b/tests/resources/blametest.git/objects/fa/01940156471352d5483b4f26b7c849dfaa7eef differ
diff --git a/tests/resources/blametest.git/refs/heads/master b/tests/resources/blametest.git/refs/heads/master
index 994877a..9d17158 100644
--- a/tests/resources/blametest.git/refs/heads/master
+++ b/tests/resources/blametest.git/refs/heads/master
@@ -1 +1 @@
-836bc00b06cb60eb0f629e237ad2b58adb2cfc7e
+d93e87a0863c7ec5e772f99e72ca9efddf0ca718