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

[PATCH 1/5] rerere: rerere_created_at() and has_resolution() abstraction

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 22, 2008, 09:47 UTC
Message-ID
<7vod5tol6r.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<7vskvd9kai.fsf@gitster.siamese.dyndns.org>

There were too many places in the code how an entry in the rerere database looks like, and the garbage_collect() function that iterates over subdirectories of the rr-cache directory was the worse offender.

Introduce two helper functions, rerere_created_at() and has_resolution(), to abstract out the logic a bit better.

Incidentally this fixes a small memory leak in garbage_collect() function. The path list to collect the entries to be pruned were defined to strdup the paths but the caller was feeding a path after doing an extra copy. Because the list does not have to be sorted by conflict signature hash, we use path_list_append() instead of path_list_insert().

While we are at it, make a conflicted hunk comparision in handle_file() a bit easier to read.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * So this is a series to handle the first point in the message I am
   replying to.
 builtin-rerere.c |   57 ++++++++++++++++++++++++-----------------------------
 1 files changed, 26 insertions(+), 31 deletions(-)
diff --git a/builtin-rerere.c b/builtin-rerere.c
index 85222d9..610b96a 100644
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
@@ -23,6 +23,18 @@ static const char *rr_path(const char *name, const char *file)
 	return git_path("rr-cache/%s/%s", name, file);
 }
 
+static time_t rerere_created_at(const char *name)
+{
+	struct stat st;
+	return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
+}
+
+static int has_resolution(const char *name)
+{
+	struct stat st;
+	return !stat(rr_path(name, "postimage"), &st);
+}
+
 static void read_rr(struct path_list *rr)
 {
 	unsigned char sha1[20];
@@ -98,13 +110,10 @@ static int handle_file(const char *path,
 		else if (!prefixcmp(buf, "======="))
 			hunk = 2;
 		else if (!prefixcmp(buf, ">>>>>>> ")) {
-			int cmp = strbuf_cmp(&one, &two);
-
+			if (strbuf_cmp(&one, &two) > 0)
+				strbuf_swap(&one, &two);
 			hunk_no++;
 			hunk = 0;
-			if (cmp > 0) {
-				strbuf_swap(&one, &two);
-			}
 			if (out) {
 				fputs("<<<<<<<\n", out);
 				fwrite(one.buf, one.len, 1, out);
@@ -201,33 +210,24 @@ static void unlink_rr_item(const char *name)
 static void garbage_collect(struct path_list *rr)
 {
 	struct path_list to_remove = { NULL, 0, 0, 1 };
-	char buf[1024];
 	DIR *dir;
 	struct dirent *e;
-	int len, i, cutoff;
+	int i, cutoff;
 	time_t now = time(NULL), then;
 
-	strlcpy(buf, git_path("rr-cache"), sizeof(buf));
-	len = strlen(buf);
-	dir = opendir(buf);
-	strcpy(buf + len++, "/");
+	dir = opendir(git_path("rr-cache"));
 	while ((e = readdir(dir))) {
 		const char *name = e->d_name;
-		struct stat st;
-		if (name[0] == '.' && (name[1] == '\0' ||
-					(name[1] == '.' && name[2] == '\0')))
+		if (name[0] == '.' &&
+		    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
 			continue;
-		i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
-		strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
-		if (stat(buf, &st))
+		then = rerere_created_at(name);
+		if (!then)
 			continue;
-		then = st.st_mtime;
-		strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
-		cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
-		if (then < now - cutoff * 86400) {
-			buf[len + i] = '\0';
-			path_list_insert(xstrdup(name), &to_remove);
-		}
+		cutoff = (has_resolution(name)
+			  ? cutoff_resolve : cutoff_noresolve);
+		if (then < now - cutoff * 86400)
+			path_list_append(name, &to_remove);
 	}
 	for (i = 0; i < to_remove.nr; i++)
 		unlink_rr_item(to_remove.items[i].path);
@@ -306,13 +306,11 @@ static int do_plain_rerere(struct path_list *rr, int fd)
 	 */
 
 	for (i = 0; i < rr->nr; i++) {
-		struct stat st;
 		int ret;
 		const char *path = rr->items[i].path;
 		const char *name = (const char *)rr->items[i].util;
 
-		if (!stat(rr_path(name, "preimage"), &st) &&
-				!stat(rr_path(name, "postimage"), &st)) {
+		if (has_resolution(name)) {
 			if (!merge(name, path)) {
 				fprintf(stderr, "Resolved '%s' using "
 						"previous resolution.\n", path);
@@ -410,11 +408,8 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
 		return do_plain_rerere(&merge_rr, fd);
 	else if (!strcmp(argv[1], "clear")) {
 		for (i = 0; i < merge_rr.nr; i++) {
-			struct stat st;
 			const char *name = (const char *)merge_rr.items[i].util;
-			if (!stat(git_path("rr-cache/%s", name), &st) &&
-					S_ISDIR(st.st_mode) &&
-					stat(rr_path(name, "postimage"), &st))
+			if (!has_resolution(name))
 				unlink_rr_item(name);
 		}
 		unlink(merge_rr_path);
-- 
1.5.6.12.g73f03
Previous: Junio C HamanoNext: Junio C Hamano
Message 17 of 45 in “git-rerere observations and feature suggestions”
  1. Ingo MolnarJun 16, 2008
  2. Mike HommeyJun 16, 2008
  3. Pierre HabouzitJun 16, 2008
  4. Pierre HabouzitJun 16, 2008
  5. Sverre RabbelierJun 16, 2008
  6. Karl HasselströmJun 17, 2008
  7. David KastrupJun 16, 2008
  8. Theodore TsoJun 16, 2008
  9. David KastrupJun 16, 2008
  10. Ingo MolnarJun 16, 2008
  11. Junio C HamanoJun 16, 2008
  12. Ingo MolnarJun 16, 2008
  13. Junio C HamanoJun 16, 2008
  14. Junio C HamanoJun 16, 2008
  15. Ingo MolnarJun 16, 2008
  16. Junio C HamanoJun 16, 2008
  17. 1/5 rerere: rerere_created_at() and has_resolution() abstractionJunio C Hamano, Jun 22, 2008
  18. 2/5 git-rerere: detect unparsable conflictsJunio C Hamano, Jun 22, 2008
  19. 3/5 rerere: remove dubious "tail_optimization"Junio C Hamano, Jun 22, 2008
  20. 4/5 t4200: fix rerere testJunio C Hamano, Jun 22, 2008
  21. 5/5 rerere.autoupdateJunio C Hamano, Jun 22, 2008
  22. Ingo MolnarJun 18, 2008
  23. Miklos VajnaJun 18, 2008
  24. Ingo MolnarJun 18, 2008
  25. Miklos VajnaJun 18, 2008
  26. Ingo MolnarJun 18, 2008
  27. Jakub NarebskiJun 18, 2008
  28. Miklos VajnaJun 18, 2008
  29. Karl HasselströmJun 19, 2008
  30. Miklos VajnaJun 19, 2008
  31. Junio C HamanoJun 19, 2008
  32. Karl HasselströmJun 19, 2008
  33. Miklos VajnaJun 19, 2008
  34. Karl HasselströmJun 19, 2008
  35. Miklos VajnaJun 19, 2008
  36. Karl HasselströmJun 19, 2008
  37. Junio C HamanoJun 16, 2008
  38. Ingo MolnarJun 16, 2008
  39. Ingo MolnarJun 23, 2008
  40. Peter ZijlstraJun 23, 2008
  41. Peter ZijlstraJun 23, 2008
  42. Jeff KingJun 23, 2008
  43. Ingo MolnarJun 23, 2008
  44. Jakub NarebskiJun 16, 2008
  45. Johannes SchindelinJun 17, 2008

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.