git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 7/7] pickaxe: factor out pickaxe

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Oct 6, 2011, 16:50 UTC
Message-ID
<4E8DDC54.2040805@lsrfire.ath.cx>
In-Reply-To
<4E8DD065.3040607@lsrfire.ath.cx>

Move the duplicate diff queue loop into its own function that accepts a match function: has_changes() for -S and diff_grep() for -G.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 diffcore-pickaxe.c |  110 ++++++++++++++++++++-------------------------------
 1 files changed, 43 insertions(+), 67 deletions(-)
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
index 226fa0c..380a837 100644
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -8,6 +8,46 @@
 #include "xdiff-interface.h"
 #include "kwset.h"
 
+typedef int (*pickaxe_fn)(struct diff_filepair *p, struct diff_options *o, regex_t *regexp, kwset_t kws);
+
+static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
+		    regex_t *regexp, kwset_t kws, pickaxe_fn fn)
+{
+	int i;
+	struct diff_queue_struct outq;
+
+	DIFF_QUEUE_CLEAR(&outq);
+
+	if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
+		/* Showing the whole changeset if needle exists */
+		for (i = 0; i < q->nr; i++) {
+			struct diff_filepair *p = q->queue[i];
+			if (fn(p, o, regexp, kws))
+				return; /* do not munge the queue */
+		}
+
+		/*
+		 * Otherwise we will clear the whole queue by copying
+		 * the empty outq at the end of this function, but
+		 * first clear the current entries in the queue.
+		 */
+		for (i = 0; i < q->nr; i++)
+			diff_free_filepair(q->queue[i]);
+	} else {
+		/* Showing only the filepairs that has the needle */
+		for (i = 0; i < q->nr; i++) {
+			struct diff_filepair *p = q->queue[i];
+			if (fn(p, o, regexp, kws))
+				diff_q(&outq, p);
+			else
+				diff_free_filepair(p);
+		}
+	}
+
+	free(q->queue);
+	*q = outq;
+}
+
 struct diffgrep_cb {
 	regex_t *regexp;
 	int hit;
@@ -96,12 +136,8 @@ static int diff_grep(struct diff_filepair *p, struct diff_options *o,
 
 static void diffcore_pickaxe_grep(struct diff_options *o)
 {
-	struct diff_queue_struct *q = &diff_queued_diff;
-	int i, err;
+	int err;
 	regex_t regex;
-	struct diff_queue_struct outq;
-	outq.queue = NULL;
-	outq.nr = outq.alloc = 0;
 
 	err = regcomp(&regex, o->pickaxe, REG_EXTENDED | REG_NEWLINE);
 	if (err) {
@@ -111,36 +147,8 @@ static void diffcore_pickaxe_grep(struct diff_options *o)
 		die("invalid log-grep regex: %s", errbuf);
 	}
 
-	if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
-		/* Showing the whole changeset if needle exists */
-		for (i = 0; i < q->nr; i++) {
-			struct diff_filepair *p = q->queue[i];
-			if (diff_grep(p, o, &regex, NULL))
-				goto out; /* do not munge the queue */
-		}
+	pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
 
-		/*
-		 * Otherwise we will clear the whole queue by copying
-		 * the empty outq at the end of this function, but
-		 * first clear the current entries in the queue.
-		 */
-		for (i = 0; i < q->nr; i++)
-			diff_free_filepair(q->queue[i]);
-	} else {
-		/* Showing only the filepairs that has the needle */
-		for (i = 0; i < q->nr; i++) {
-			struct diff_filepair *p = q->queue[i];
-			if (diff_grep(p, o, &regex, NULL))
-				diff_q(&outq, p);
-			else
-				diff_free_filepair(p);
-		}
-	}
-
-	free(q->queue);
-	*q = outq;
-
- out:
 	regfree(&regex);
 	return;
 }
@@ -213,13 +221,9 @@ static void diffcore_pickaxe_count(struct diff_options *o)
 {
 	const char *needle = o->pickaxe;
 	int opts = o->pickaxe_opts;
-	struct diff_queue_struct *q = &diff_queued_diff;
 	unsigned long len = strlen(needle);
-	int i;
 	regex_t regex, *regexp = NULL;
 	kwset_t kws = NULL;
-	struct diff_queue_struct outq;
-	DIFF_QUEUE_CLEAR(&outq);
 
 	if (opts & DIFF_PICKAXE_REGEX) {
 		int err;
@@ -238,36 +242,8 @@ static void diffcore_pickaxe_count(struct diff_options *o)
 		kwsprep(kws);
 	}
 
-	if (opts & DIFF_PICKAXE_ALL) {
-		/* Showing the whole changeset if needle exists */
-		for (i = 0; i < q->nr; i++) {
-			struct diff_filepair *p = q->queue[i];
-			if (has_changes(p, o, regexp, kws))
-				goto out; /* do not munge the queue */
-		}
-
-		/* otherwise we will clear the whole queue
-		 * by copying the empty outq at the end of this
-		 * function, but first clear the current entries
-		 * in the queue.
-		 */
-		for (i = 0; i < q->nr; i++)
-			diff_free_filepair(q->queue[i]);
-	}
-	else
-		/* Showing only the filepairs that has the needle */
-		for (i = 0; i < q->nr; i++) {
-			struct diff_filepair *p = q->queue[i];
-			if (has_changes(p, o, regexp, kws))
-				diff_q(&outq, p);
-			else
-				diff_free_filepair(p);
-		}
-
-	free(q->queue);
-	*q = outq;
+	pickaxe(&diff_queued_diff, o, regexp, kws, has_changes);
 
- out:
 	if (opts & DIFF_PICKAXE_REGEX)
 		regfree(&regex);
 	else
-- 
1.7.7
Previous: René ScharfeNext: Junio C Hamano
Message 8 of 9 in “pickaxe: plug memory leaks, deduplicate code”
  1. 0/7 pickaxe: plug memory leaks, deduplicate codeRené Scharfe, Oct 6, 2011
  2. 1/7 pickaxe: plug diff filespec leak with empty needleRené Scharfe, Oct 6, 2011
  3. 2/7 pickaxe: plug regex leakRené Scharfe, Oct 6, 2011
  4. 3/7 pickaxe: plug regex/kws leakRené Scharfe, Oct 6, 2011
  5. 4/7 pickaxe: factor out has_changesRené Scharfe, Oct 6, 2011
  6. 5/7 pickaxe: pass diff_options to contains and has_changesRené Scharfe, Oct 6, 2011
  7. 6/7 pickaxe: give diff_grep the same signature as has_changesRené Scharfe, Oct 6, 2011
  8. 7/7 pickaxe: factor out pickaxeRené Scharfe, Oct 6, 2011
  9. Junio C HamanoOct 7, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.