git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v3 17/18] rerere: use "struct rerere_id" instead of "char *" for conflict ID
Date: Fri, 17 Jul 2015 15:24:39 -0700	[thread overview]
Message-ID: <1437171880-21590-18-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1437171880-21590-1-git-send-email-gitster@pobox.com>

This gives a thin abstraction between the conflict ID that is a hash
value obtained by inspecting the conflicts and the name of the
directory under $GIT_DIR/rr-cache/, in which the previous resolution
is recorded to be replayed.  The plan is to make sure that the
presense of the directory does not imply the presense of a previous
resolution and vice-versa, and later allow us to have more than one
pair of <preimage, postimage> for a given conflict ID.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/rerere.c |  2 +-
 rerere.c         | 99 +++++++++++++++++++++++++++++++++++++++++---------------
 rerere.h         | 12 ++++++-
 3 files changed, 85 insertions(+), 28 deletions(-)

diff --git a/builtin/rerere.c b/builtin/rerere.c
index 81730bb..fd229a7 100644
--- a/builtin/rerere.c
+++ b/builtin/rerere.c
@@ -103,7 +103,7 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
 	} else if (!strcmp(argv[0], "diff"))
 		for (i = 0; i < merge_rr.nr; i++) {
 			const char *path = merge_rr.items[i].string;
-			const char *id = (const char *)merge_rr.items[i].util;
+			const struct rerere_id *id = merge_rr.items[i].util;
 			diff_two(rerere_path(id, "preimage"), path, path, path);
 		}
 	else
diff --git a/rerere.c b/rerere.c
index e68469a..469d8a8 100644
--- a/rerere.c
+++ b/rerere.c
@@ -22,17 +22,43 @@ static int rerere_autoupdate;
 
 static char *merge_rr_path;
 
-const char *rerere_path(const char *id, const char *file)
+static void free_rerere_id(struct string_list_item *item)
 {
-	return git_path("rr-cache/%s/%s", id, file);
+	free(item->util);
+}
+
+static const char *rerere_id_hex(const struct rerere_id *id)
+{
+	return id->hex;
+}
+
+const char *rerere_path(const struct rerere_id *id, const char *file)
+{
+	if (file)
+		return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
+	else
+		return git_path("rr-cache/%s", rerere_id_hex(id));
 }
 
-static int has_rerere_resolution(const char *id)
+static int has_rerere_resolution(const struct rerere_id *id)
 {
 	struct stat st;
+
 	return !stat(rerere_path(id, "postimage"), &st);
 }
 
+static struct rerere_id *new_rerere_id_hex(char *hex)
+{
+	struct rerere_id *id = xmalloc(sizeof(*id));
+	strcpy(id->hex, hex);
+	return id;
+}
+
+static struct rerere_id *new_rerere_id(unsigned char *sha1)
+{
+	return new_rerere_id_hex(sha1_to_hex(sha1));
+}
+
 /*
  * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
  * "conflict ID", a HT and pathname, terminated with a NUL, and is
@@ -50,6 +76,7 @@ static void read_rr(struct string_list *rr)
 	while (!strbuf_getwholeline(&buf, in, '\0')) {
 		char *path;
 		unsigned char sha1[20];
+		struct rerere_id *id;
 
 		/* There has to be the hash, tab, path and then NUL */
 		if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
@@ -59,8 +86,8 @@ static void read_rr(struct string_list *rr)
 			die("corrupt MERGE_RR");
 		buf.buf[40] = '\0';
 		path = buf.buf + 41;
-
-		string_list_insert(rr, path)->util = xstrdup(buf.buf);
+		id = new_rerere_id_hex(buf.buf);
+		string_list_insert(rr, path)->util = id;
 	}
 	strbuf_release(&buf);
 	fclose(in);
@@ -73,12 +100,15 @@ static int write_rr(struct string_list *rr, int out_fd)
 	int i;
 	for (i = 0; i < rr->nr; i++) {
 		struct strbuf buf = STRBUF_INIT;
+		struct rerere_id *id;
 
 		assert(rr->items[i].util != RERERE_RESOLVED);
-		if (!rr->items[i].util)
+
+		id = rr->items[i].util;
+		if (!id)
 			continue;
 		strbuf_addf(&buf, "%s\t%s%c",
-			    (char *)rr->items[i].util,
+			    rerere_id_hex(id),
 			    rr->items[i].string, 0);
 		if (write_in_full(out_fd, buf.buf, buf.len) != buf.len)
 			die("unable to write rerere record");
@@ -521,7 +551,7 @@ int rerere_remaining(struct string_list *merge_rr)
 			struct string_list_item *it;
 			it = string_list_lookup(merge_rr, (const char *)e->name);
 			if (it != NULL) {
-				free(it->util);
+				free_rerere_id(it);
 				it->util = RERERE_RESOLVED;
 			}
 		}
@@ -539,7 +569,7 @@ int rerere_remaining(struct string_list *merge_rr)
  * Returns 0 for successful replay of recorded resolution, or non-zero
  * for failure.
  */
-static int merge(const char *id, const char *path)
+static int merge(const struct rerere_id *id, const char *path)
 {
 	int ret;
 	mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
@@ -573,8 +603,8 @@ static int merge(const char *id, const char *path)
 		 */
 		if (utime(rerere_path(id, "postimage"), NULL) < 0)
 			warning("failed utime() on %s: %s",
-					rerere_path(id, "postimage"),
-					strerror(errno));
+				rerere_path(id, "postimage"),
+				strerror(errno));
 
 		/* Update "path" with the resolution */
 		f = fopen(path, "w");
@@ -631,7 +661,7 @@ static void do_rerere_one_path(struct string_list_item *rr_item,
 			       struct string_list *update)
 {
 	const char *path = rr_item->string;
-	const char *id = (const char *)rr_item->util;
+	const struct rerere_id *id = rr_item->util;
 
 	/* Is there a recorded resolution we could attempt to apply? */
 	if (has_rerere_resolution(id)) {
@@ -651,7 +681,7 @@ static void do_rerere_one_path(struct string_list_item *rr_item,
 	} else {
 		return;
 	}
-	free(rr_item->util);
+	free_rerere_id(rr_item);
 	rr_item->util = NULL;
 }
 
@@ -670,10 +700,10 @@ static int do_plain_rerere(struct string_list *rr, int fd)
 	 * initial run would catch all and register their preimages.
 	 */
 	for (i = 0; i < conflict.nr; i++) {
+		struct rerere_id *id;
 		unsigned char sha1[20];
-		char *id;
-		int ret;
 		const char *path = conflict.items[i].string;
+		int ret;
 
 		if (string_list_has_string(rr, path))
 			continue;
@@ -686,7 +716,8 @@ static int do_plain_rerere(struct string_list *rr, int fd)
 		ret = handle_file(path, sha1, NULL);
 		if (ret < 1)
 			continue;
-		id = xstrdup(sha1_to_hex(sha1));
+
+		id = new_rerere_id(sha1);
 		string_list_insert(rr, path)->util = id;
 
 		/*
@@ -697,7 +728,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
 		 * NEEDSWORK: make sure "gc" does not remove
 		 * preimage without removing the directory.
 		 */
-		if (mkdir_in_gitdir(git_path("rr-cache/%s", id)))
+		if (mkdir_in_gitdir(rerere_path(id, NULL)))
 			continue;
 
 		/*
@@ -779,7 +810,7 @@ int rerere(int flags)
 static int rerere_forget_one_path(const char *path, struct string_list *rr)
 {
 	const char *filename;
-	char *id;
+	struct rerere_id *id;
 	unsigned char sha1[20];
 	int ret;
 	struct string_list_item *item;
@@ -793,7 +824,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
 		return error("Could not parse conflict hunks in '%s'", path);
 
 	/* Nuke the recorded resolution for the conflict */
-	id = xstrdup(sha1_to_hex(sha1));
+	id = new_rerere_id(sha1);
 	filename = rerere_path(id, "postimage");
 	if (unlink(filename))
 		return (errno == ENOENT
@@ -813,7 +844,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
 	 * conflict when the user is done.
 	 */
 	item = string_list_insert(rr, path);
-	free(item->util);
+	free_rerere_id(item);
 	item->util = id;
 	fprintf(stderr, "Forgot resolution for %s\n", path);
 	return 0;
@@ -850,22 +881,38 @@ int rerere_forget(struct pathspec *pathspec)
 /*
  * Garbage collection support
  */
-static time_t rerere_created_at(const char *id)
+
+/*
+ * Note that this is not reentrant but is used only one-at-a-time
+ * so it does not matter right now.
+ */
+static struct rerere_id *dirname_to_id(const char *name)
+{
+	static struct rerere_id id;
+	strcpy(id.hex, name);
+	return &id;
+}
+
+static time_t rerere_created_at(const char *dir_name)
 {
 	struct stat st;
+	struct rerere_id *id = dirname_to_id(dir_name);
+
 	return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
 }
 
-static time_t rerere_last_used_at(const char *id)
+static time_t rerere_last_used_at(const char *dir_name)
 {
 	struct stat st;
+	struct rerere_id *id = dirname_to_id(dir_name);
+
 	return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
 }
 
 /*
  * Remove the recorded resolution for a given conflict ID
  */
-static void unlink_rr_item(const char *id)
+static void unlink_rr_item(struct rerere_id *id)
 {
 	unlink(rerere_path(id, "thisimage"));
 	unlink(rerere_path(id, "preimage"));
@@ -875,7 +922,7 @@ static void unlink_rr_item(const char *id)
 	 * assume that we already have preimage recorded in
 	 * do_plain_rerere()?
 	 */
-	rmdir(git_path("rr-cache/%s", id));
+	rmdir(rerere_path(id, NULL));
 }
 
 void rerere_gc(struct string_list *rr)
@@ -914,7 +961,7 @@ void rerere_gc(struct string_list *rr)
 	closedir(dir);
 	/* ... and then remove them one-by-one */
 	for (i = 0; i < to_remove.nr; i++)
-		unlink_rr_item(to_remove.items[i].string);
+		unlink_rr_item(dirname_to_id(to_remove.items[i].string));
 	string_list_clear(&to_remove, 0);
 }
 
@@ -930,7 +977,7 @@ void rerere_clear(struct string_list *merge_rr)
 	int i;
 
 	for (i = 0; i < merge_rr->nr; i++) {
-		const char *id = (const char *)merge_rr->items[i].util;
+		struct rerere_id *id = merge_rr->items[i].util;
 		if (!has_rerere_resolution(id))
 			unlink_rr_item(id);
 	}
diff --git a/rerere.h b/rerere.h
index f998eba..ce545d0 100644
--- a/rerere.h
+++ b/rerere.h
@@ -15,9 +15,19 @@ struct pathspec;
  */
 extern void *RERERE_RESOLVED;
 
+struct rerere_id {
+	char hex[41];
+};
+
 extern int setup_rerere(struct string_list *, int);
 extern int rerere(int);
-extern const char *rerere_path(const char *id, const char *file);
+/*
+ * Given the conflict ID and the name of a "file" used for replaying
+ * the recorded resolution (e.g. "preimage", "postimage"), return the
+ * path to that filesystem entity.  With "file" specified with NULL,
+ * return the path to the directory that houses these files.
+ */
+extern const char *rerere_path(const struct rerere_id *, const char *file);
 extern int rerere_forget(struct pathspec *);
 extern int rerere_remaining(struct string_list *);
 extern void rerere_clear(struct string_list *);
-- 
2.5.0-rc2-340-g0cccc16

  parent reply	other threads:[~2015-07-17 22:25 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-01  6:04 [PATCH v2 00/13] "rerere" minor clean-up Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 01/13] rerere: fix an off-by-one non-bug Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 02/13] rerere: plug conflict ID leaks Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 03/13] rerere: lift PATH_MAX limitation Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 04/13] rerere: write out each record of MERGE_RR in one go Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 05/13] rerere: report autoupdated paths only after actually updating them Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 06/13] rerere: drop want_sp parameter from is_cmarker() Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 07/13] rerere: stop looping unnecessarily Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 08/13] rerere: explain the rerere I/O abstraction Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 09/13] rerere: explain MERGE_RR management helpers Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 10/13] rerere: explain the primary codepath Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 11/13] rerere: explain "rerere forget" codepath Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 12/13] rerere: explain the remainder Junio C Hamano
2015-07-01  6:04 ` [PATCH v2 13/13] rerere: refactor "replay" part of do_plain_rerere() Junio C Hamano
2015-07-17 22:24 ` [PATCH v3 00/18] "rerere" preparatory clean-up Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 01/18] rerere: fix an off-by-one non-bug Junio C Hamano
2015-07-24 19:46     ` Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 02/18] rerere: plug conflict ID leaks Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 03/18] rerere: lift PATH_MAX limitation Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 04/18] rerere: write out each record of MERGE_RR in one go Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 05/18] rerere: report autoupdated paths only after actually updating them Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 06/18] rerere: drop want_sp parameter from is_cmarker() Junio C Hamano
2015-07-18  8:24     ` Philip Oakley
2015-07-18  8:47       ` Eric Sunshine
2015-07-17 22:24   ` [PATCH v3 07/18] rerere: stop looping unnecessarily Junio C Hamano
2015-07-24 20:06     ` Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 08/18] rerere: explain the rerere I/O abstraction Junio C Hamano
2015-07-24 20:42     ` Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 09/18] rerere: explain MERGE_RR management helpers Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 10/18] rerere: explain the primary codepath Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 11/18] rerere: explain "rerere forget" codepath Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 12/18] rerere: explain the remainder Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 13/18] rerere: refactor "replay" part of do_plain_rerere() Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 14/18] rerere: further de-dent do_plain_rerere() Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 15/18] rerere: further clarify do_rerere_one_path() Junio C Hamano
2015-07-17 22:24   ` [PATCH v3 16/18] rerere: call conflict-ids IDs Junio C Hamano
2015-07-17 22:24   ` Junio C Hamano [this message]
2015-07-18  8:47     ` [PATCH v3 17/18] rerere: use "struct rerere_id" instead of "char *" for conflict ID Eric Sunshine
2015-07-17 22:24   ` [PATCH v3 18/18] rerere: un-nest merge() further Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1437171880-21590-18-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).