Commit b8e86c62f7512425e29f340a38152978d869e316

Russell Belfer 2014-01-21T12:00:08

Implement matched pattern extract for fn headers

diff --git a/src/diff_driver.c b/src/diff_driver.c
index 169f7b0..56b5b5a 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -460,16 +460,21 @@ static int diff_context_line__simple(
 static int diff_context_line__pattern_match(
 	git_diff_driver *driver, git_buf *line)
 {
-	size_t i;
+	size_t i, maxi = git_array_size(driver->fn_patterns);
 	regmatch_t pmatch[2];
 
-	for (i = 0; i < git_array_size(driver->fn_patterns); ++i) {
+	for (i = 0; i < maxi; ++i) {
 		git_diff_driver_pattern *pat = git_array_get(driver->fn_patterns, i);
 
 		if (!regexec(&pat->re, line->ptr, 2, pmatch, 0)) {
 			if (pat->flags & REG_NEGATE)
 				return false;
-			/* TODO: use pmatch data to trim line data */
+
+			/* use pmatch data to trim line data */
+			i = (pmatch[1].rm_so >= 0) ? 1 : 0;
+			git_buf_consume(line, git_buf_cstr(line) + pmatch[i].rm_so);
+			git_buf_truncate(line, pmatch[i].rm_eo - pmatch[i].rm_so);
+
 			return true;
 		}
 	}
diff --git a/tests/diff/drivers.c b/tests/diff/drivers.c
index 003a844..1191321 100644
--- a/tests/diff/drivers.c
+++ b/tests/diff/drivers.c
@@ -123,7 +123,7 @@ void test_diff_drivers__patterns(void)
 
 	cl_git_pass(git_repository_config(&cfg, g_repo));
 	cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 0));
-	cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H"));
+	cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H.*$"));
 	git_config_free(cfg);
 
 	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
@@ -196,7 +196,7 @@ void test_diff_drivers__builtins(void)
 	const char *expected_nodriver =
 		"diff --git a/file.html b/file.html\nindex 97b34db..c7dbed3 100644\n--- a/file.html\n+++ b/file.html\n@@ -5,4 +5,4 @@\n     <li>item 1.1</li>\n-    <li>item 1.2</li>\n-    <li>item 1.3</li>\n+    <li>item 1.2 changed</li>\n+    <li>item 1.3 changed</li>\n     <li>item 1.4</li>\n@@ -13,2 +13,3 @@\n     <li>item 1.9</li>\n+  <li>item 1.10 added</li>\n   </ol>\n@@ -23,3 +24,4 @@\n     <li>item 2.6</li>\n-    <li>item 2.7</li>\n+    <li>item 2.7 changed</li>\n+    <li>item 2.7.1 added</li>\n     <li>item 2.8</li>\n@@ -35,4 +37,2 @@\n     <li>item 3.6</li>\n-    <li>item 3.7</li>\n-    <li>item 3.8</li>\n   </ol>\n";
 	const char *expected_driver =
-		"diff --git a/file.html b/file.html\nindex 97b34db..c7dbed3 100644\n--- a/file.html\n+++ b/file.html\n@@ -5,4 +5,4 @@   <h1 id=\"first section\">\n     <li>item 1.1</li>\n-    <li>item 1.2</li>\n-    <li>item 1.3</li>\n+    <li>item 1.2 changed</li>\n+    <li>item 1.3 changed</li>\n     <li>item 1.4</li>\n@@ -13,2 +13,3 @@   <h1 id=\"first section\">\n     <li>item 1.9</li>\n+  <li>item 1.10 added</li>\n   </ol>\n@@ -23,3 +24,4 @@   <h1 id=\"second section\">\n     <li>item 2.6</li>\n-    <li>item 2.7</li>\n+    <li>item 2.7 changed</li>\n+    <li>item 2.7.1 added</li>\n     <li>item 2.8</li>\n@@ -35,4 +37,2 @@   <h1 id=\"third section\">\n     <li>item 3.6</li>\n-    <li>item 3.7</li>\n-    <li>item 3.8</li>\n   </ol>\n";
+		"diff --git a/file.html b/file.html\nindex 97b34db..c7dbed3 100644\n--- a/file.html\n+++ b/file.html\n@@ -5,4 +5,4 @@ <h1 id=\"first section\">\n     <li>item 1.1</li>\n-    <li>item 1.2</li>\n-    <li>item 1.3</li>\n+    <li>item 1.2 changed</li>\n+    <li>item 1.3 changed</li>\n     <li>item 1.4</li>\n@@ -13,2 +13,3 @@ <h1 id=\"first section\">\n     <li>item 1.9</li>\n+  <li>item 1.10 added</li>\n   </ol>\n@@ -23,3 +24,4 @@ <h1 id=\"second section\">\n     <li>item 2.6</li>\n-    <li>item 2.7</li>\n+    <li>item 2.7 changed</li>\n+    <li>item 2.7.1 added</li>\n     <li>item 2.8</li>\n@@ -35,4 +37,2 @@ <h1 id=\"third section\">\n     <li>item 3.6</li>\n-    <li>item 3.7</li>\n-    <li>item 3.8</li>\n   </ol>\n";
 
 	g_repo = cl_git_sandbox_init("empty_standard_repo");