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

[PATCH] "checkout-cache -m" writes unmerged contents for each stage.

From
Junio C Hamano <junkio@cox.net>
Date
Apr 17, 2005, 23:03 UTC
Message-ID
<7v1x99vwdg.fsf@assigned-by-dhcp.cox.net>

This is the alternative strategy I mentioned in my previous message. A new -m option to checkout-cache causes it to store contents of unmerged paths in path~1~, path~2~, and path~3~.

To be applied on top of the previous patch I re-sent:
    [PATCH] checkout-cache -a should not extract unmerged stages.
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 checkout-cache.c |   93 ++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 76 insertions(+), 17 deletions(-)

checkout-cache.c: 596471a2f98b80a622488cf04edf9e95ce8666b1 --- checkout-cache.c +++ checkout-cache.c 2005-04-17 15:49:15.000000000 -0700

@@ -34,7 +34,7 @@
  */
 #include "cache.h"
 
-static int force = 0, quiet = 0;
+static int force = 0, merge = 0, quiet = 0;
 
 static void create_directories(const char *path)
 {
@@ -65,6 +65,31 @@ static int create_file(const char *path,
 	return fd;
 }
 
+/* Returns the pathname itself for a merged entry
+ * and pathname~N~ for an unmerged one.
+ * Do not free the value you get from this function.
+ */
+static char *ce_name_with_stage(struct cache_entry *ce)
+{
+	/* (CE_NAMEMASK+1) is the max length of a name.
+	 * We are adding 3 bytes for ~N~ and we need a terminating NUL
+	 * hence +5.
+	 */
+	static char name[CE_NAMEMASK + 5];
+	int stage = ce_stage(ce);
+	if (! stage)
+		return ce->name;
+	else {
+		int pos = ce_namelen(ce);
+		strcpy(name, ce->name);
+		name[pos++] = '~';
+		name[pos++] = '0' + stage;
+		name[pos++] = '~';
+		name[pos] = 0;
+		return name;
+	}
+}
+
 static int write_entry(struct cache_entry *ce)
 {
 	int fd;
@@ -72,13 +97,20 @@ static int write_entry(struct cache_entr
 	unsigned long size;
 	long wrote;
 	char type[20];
+	char *name;
 
 	new = read_sha1_file(ce->sha1, type, &size);
 	if (!new || strcmp(type, "blob")) {
 		return error("checkout-cache: unable to read sha1 file of %s (%s)",
 			ce->name, sha1_to_hex(ce->sha1));
 	}
-	fd = create_file(ce->name, ntohl(ce->ce_mode));
+	name = ce_name_with_stage(ce);
+	if (!quiet && name != ce->name)
+		fprintf(stderr,
+			"checkout-cache: storing stage %d of %s in %s\n",
+			ce_stage(ce), ce->name, name);
+
+	fd = create_file(name, ntohl(ce->ce_mode));
 	if (fd < 0) {
 		free(new);
 		return error("checkout-cache: unable to create %s (%s)",
@@ -117,19 +149,39 @@ static int checkout_entry(struct cache_e
 	return write_entry(ce);
 }
 
+static int checkout_unmerged(int pos)
+{
+	int i, err;
+	struct cache_entry *ce = active_cache[pos];
+
+	for (err = 0, i = pos;
+	     (i < active_nr &&
+	      !strcmp(active_cache[i]->name, ce->name));
+	     i++)
+		err |= checkout_entry(active_cache[i]);
+	return err;
+}
+
 static int checkout_file(const char *name)
 {
 	int pos = cache_name_pos(name, strlen(name));
 	if (pos < 0) {
-		if (!quiet) {
-			pos = -pos - 1;
-			fprintf(stderr,
-				"checkout-cache: %s is %s.\n",
-				name,
-				(pos < active_nr &&
-				 !strcmp(active_cache[pos]->name, name)) ?
-				"unmerged" : "not in the cache");
+		pos = -pos - 1;
+		if (pos < active_nr &&
+		    !strcmp(active_cache[pos]->name, name)) {
+			if (merge)
+				return checkout_unmerged(pos);
+			else if (! quiet) {
+				fprintf(stderr,
+					"checkout-cache: %s is unmerged.\n",
+					name);
+				return -1;
+			}
 		}
+		else if (! quiet)
+			fprintf(stderr,
+				"checkout-cache: %s is not in the cache.\n",
+				name);
 		return -1;
 	}
 	return checkout_entry(active_cache[pos]);
@@ -137,22 +189,25 @@ static int checkout_file(const char *nam
 
 static int checkout_all(void)
 {
-	struct cache_entry *unmerge_skipping = NULL;
 	int i;
 
 	for (i = 0; i < active_nr ; i++) {
 		struct cache_entry *ce = active_cache[i];
 		if (ce_stage(ce)) {
-			if (!unmerge_skipping ||
-			    strcmp(unmerge_skipping->name, ce->name))
+			if (!merge)
 				fprintf(stderr,
 					"checkout-cache: needs merge %s\n",
 					ce->name);
-			unmerge_skipping = ce;
-			continue;
+			while (i < active_nr &&
+			       !strcmp(ce->name, active_cache[i]->name)) {
+				if (merge) {
+					checkout_entry(active_cache[i]);
+				}
+				i++;
+			}
+			i--;
 		}
-		unmerge_skipping = NULL;
-		if (checkout_entry(ce) < 0)
+		else if (checkout_entry(ce) < 0)
 			return -1;
 	}
 	return 0;
@@ -181,6 +236,10 @@ int main(int argc, char **argv)
 				force = 1;
 				continue;
 			}
+			if (!strcmp(arg, "-m")) {
+				merge = 1;
+				continue;
+			}
 			if (!strcmp(arg, "-q")) {
 				quiet = 1;
 				continue;
Next: Linus Torvalds
Message 1 of 6 in “"checkout-cache -m" writes unmerged contents for each stage.”
  1. "checkout-cache -m" writes unmerged contents for each stage.Junio C Hamano, Apr 17, 2005
  2. Linus TorvaldsApr 18, 2005
  3. Junio C HamanoApr 18, 2005
  4. Linus TorvaldsApr 18, 2005
  5. Junio C HamanoApr 18, 2005
  6. Linus TorvaldsApr 18, 2005

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.