Thread (126 messages) 126 messages, 9 authors, 2024-12-05

Re: [PATCH v6 3/6] set-head: better output for --auto

From: karthik nayak <hidden>
Date: 2024-10-10 21:05:11

Possibly related (same subject, not in this thread)

Bence Ferdinandy [off-list ref] writes:
[snip]
quoted hunk ↗ jump to hunk
diff --git a/builtin/remote.c b/builtin/remote.c
index 353ffd2c43..24f9caf149 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1399,10 +1399,34 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }

+static void report_auto(const char *remote, const char *head_name,
+			struct strbuf *buf_prev) {
Nit: would be nicer if this was called 'report_set_head_auto'
+	struct strbuf buf_prefix = STRBUF_INIT;
+	const char *prev_head = NULL;
+
+	strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
+	skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head);
+
+	if (prev_head && !strcmp(prev_head, head_name))
+		printf("'%s/HEAD' is unchanged and points to '%s'\n",
+			remote, head_name);
+	else if (prev_head)
+		printf("'%s/HEAD' has changed from '%s' and now points to '%s'\n",
+			remote, prev_head, head_name);
+	else if (buf_prev->len == 0)
Nit: we tend to do `if (!buf_prev->len)` in this project
+		printf("'%s/HEAD' is now created and points to '%s'\n",
+			remote, head_name);
+	else
+		printf("'%s/HEAD' used to point to '%s' "
+			"(which is unusual), but now points to '%s'\n",
Isn't "which is unusual" a bit vague? Wouldn't be nicer to say why it is
unusual?
+			remote, buf_prev->buf, head_name);
Let's clear up buf_prefix by calling `strbuf_release` here.
quoted hunk ↗ jump to hunk
+}
+
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT,
+		buf_prev = STRBUF_INIT;
 	char *head_name = NULL;
 	struct ref_store *refs = get_main_ref_store(the_repository);
@@ -1445,15 +1469,17 @@ static int set_head(int argc, const char **argv, const char *prefix)
 		/* make sure it's valid */
 		if (!refs_ref_exists(refs, buf2.buf))
 			result |= error(_("Not a valid ref: %s"), buf2.buf);
-		else if (refs_update_symref(refs, buf.buf, buf2.buf, "remote set-head", NULL))
+		else if (refs_update_symref(refs, buf.buf, buf2.buf, "remote set-head", &buf_prev))
 			result |= error(_("Could not setup %s"), buf.buf);
-		else if (opt_a)
-			printf("%s/HEAD set to %s\n", argv[0], head_name);
+		else if (opt_a) {
+			report_auto(argv[0], head_name, &buf_prev);
+		}
 		free(head_name);
 	}

 	strbuf_release(&buf);
 	strbuf_release(&buf2);
+	strbuf_release(&buf_prev);
 	return result;
 }
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 532035933f..77c12b8709 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -429,12 +429,51 @@ test_expect_success 'set-head --auto' '
 	)
 '

+test_expect_success 'set-head --auto detects creation' '
+	(
+		cd test &&
+		rm .git/refs/remotes/origin/HEAD &&
does this work with reftables too?
+		git remote set-head --auto origin >output &&
+		echo "'\''origin/HEAD'\'' is now created and points to '\''main'\''" >expect &&
Nit: might be cleaner to use `${SQ}` here and below

[snip]

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help