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

[PATCH] checkout: tone down the "forked status" diagnostic messages

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 20, 2008, 23:05 UTC
Message-ID
<7vy79f2pqk.fsf_-_@gitster.siamese.dyndns.org>
In-Reply-To
<46a038f90802190903g1a19e38i30198b368a800a89@mail.gmail.com>

When checking out a branch that is behind or forked from a branch you are building on top of, we used to show full left-right log but if you already _know_ you have long history since you forked, it is a bit too much.

This tones down the message quite a bit, by only showing the number of commits each side has since they diverged.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This obviously comes on top of the earlier patch of mine,
   which in turn is on top of Daniel's "checkout written in C".
 builtin-checkout.c |  102 ++++++++++++++++++++++++++++++++--------------------
 1 files changed, 63 insertions(+), 39 deletions(-)
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 5291f72..261f67f 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -301,64 +301,88 @@ static void adjust_to_tracking(struct branch_info *new, struct checkout_opts *op
 	char *base;
 	unsigned char sha1[20];
 	struct commit *ours, *theirs;
-	const char *msgfmt;
 	char symmetric[84];
-	int show_log;
+	struct rev_info revs;
+	const char *rev_argv[10];
+	int rev_argc;
+	int num_ours, num_theirs;
+	const char *remote_msg;
 	struct branch *branch = branch_get(NULL);
 
+	/*
+	 * Nothing to report unless we are marked to build on top of
+	 * somebody else.
+	 */
 	if (!branch || !branch->merge)
 		return;
 
-	base = branch->merge[0]->dst;
-
-	ours = new->commit;
-
-	sprintf(symmetric, "%s", sha1_to_hex(ours->object.sha1));
-
 	/*
-	 * Ok, it is tracking base; is it ahead of us?
+	 * If what we used to build on no longer exists, there is
+	 * nothing to report.
 	 */
+	base = branch->merge[0]->dst;
 	if (!resolve_ref(base, sha1, 1, NULL))
 		return;
-	theirs = lookup_commit(sha1);
-
-	sprintf(symmetric + 40, "...%s", sha1_to_hex(sha1));
 
+	theirs = lookup_commit(sha1);
+	ours = new->commit;
 	if (!hashcmp(sha1, ours->object.sha1))
 		return; /* we are the same */
 
-	show_log = 1;
-	if (in_merge_bases(theirs, &ours, 1)) {
-		msgfmt = "You are ahead of the tracked branch '%s'\n";
-		show_log = 0;
+	/* Run "rev-list --left-right ours...theirs" internally... */
+	rev_argc = 0;
+	rev_argv[rev_argc++] = NULL;
+	rev_argv[rev_argc++] = "--left-right";
+	rev_argv[rev_argc++] = symmetric;
+	rev_argv[rev_argc++] = "--";
+	rev_argv[rev_argc] = NULL;
+
+	strcpy(symmetric, sha1_to_hex(ours->object.sha1));
+	strcpy(symmetric + 40, "...");
+	strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
+
+	init_revisions(&revs, NULL);
+	setup_revisions(rev_argc, rev_argv, &revs, NULL);
+	prepare_revision_walk(&revs);
+
+	/* ... and count the commits on each side. */
+	num_ours = 0;
+	num_theirs = 0;
+	while (1) {
+		struct commit *c = get_revision(&revs);
+		if (!c)
+			break;
+		if (c->object.flags & SYMMETRIC_LEFT)
+			num_ours++;
+		else
+			num_theirs++;
 	}
-	else if (in_merge_bases(ours, &theirs, 1))
-		msgfmt = "Your branch can be fast-forwarded to the tracked branch '%s'\n";
-	else
-		msgfmt = "Both your branch and the tracked branch '%s' have own changes, you would eventually need to merge\n";
 
-	if (!prefixcmp(base, "refs/remotes/"))
+	if (!prefixcmp(base, "refs/remotes/")) {
+		remote_msg = " remote";
 		base += strlen("refs/remotes/");
-	fprintf(stderr, msgfmt, base);
-
-	if (show_log) {
-		const char *args[32];
-		int ac;
-
-		ac = 0;
-		args[ac++] = "log";
-		args[ac++] = "--pretty=oneline";
-		args[ac++] = "--abbrev-commit";
-		args[ac++] = "--left-right";
-		args[ac++] = "--boundary";
-		args[ac++] = symmetric;
-		args[ac++] = "--";
-		args[ac] = NULL;
-
-		run_command_v_opt(args, RUN_GIT_CMD);
+	} else {
+		remote_msg = "";
 	}
-}
 
+	if (!num_theirs)
+		printf("Your branch is ahead of the tracked%s branch '%s' "
+		       "by %d commit%s.\n",
+		       remote_msg, base,
+		       num_ours, (num_ours == 1) ? "" : "s");
+	else if (!num_ours)
+		printf("Your branch is behind of the tracked%s branch '%s' "
+		       "by %d commit%s,\n"
+		       "and can be fast-forwarded.\n",
+		       remote_msg, base,
+		       num_theirs, (num_theirs == 1) ? "" : "s");
+	else
+		printf("Your branch and the tracked%s branch '%s' "
+		       "have diverged,\nand respectively "
+		       "have %d and %d different commit(s) each.\n",
+		       remote_msg, base,
+		       num_ours, num_theirs);
+}
 
 static void update_refs_for_switch(struct checkout_opts *opts,
 				   struct branch_info *old,
Previous: Martin LanghoffNext: Jeff King
Message 37 of 71 in “Minor annoyance with git push”
  1. Martin LanghoffFeb 8, 2008
  2. Martin LanghoffFeb 8, 2008
  3. Junio C HamanoFeb 8, 2008
  4. Steffen ProhaskaFeb 9, 2008
  5. Junio C HamanoFeb 10, 2008
  6. Johannes SchindelinFeb 10, 2008
  7. Johannes SchindelinFeb 8, 2008
  8. Martin LanghoffFeb 8, 2008
  9. Mike HommeyFeb 8, 2008
  10. SeanFeb 8, 2008
  11. Steffen ProhaskaFeb 8, 2008
  12. Johannes SchindelinFeb 8, 2008
  13. Martin LanghoffFeb 8, 2008
  14. Johannes SchindelinFeb 8, 2008
  15. Jeff KingFeb 9, 2008
  16. Jeff KingFeb 9, 2008
  17. Johannes SchindelinFeb 9, 2008
  18. Jeff KingFeb 9, 2008
  19. Steffen ProhaskaFeb 9, 2008
  20. Jeff KingFeb 9, 2008
  21. Junio C HamanoFeb 9, 2008
  22. Jeff KingFeb 9, 2008
  23. Martin LanghoffFeb 9, 2008
  24. Johannes SchindelinFeb 9, 2008
  25. Junio C HamanoFeb 10, 2008
  26. Jeff KingFeb 10, 2008
  27. Johannes SchindelinFeb 10, 2008
  28. [RFC] checkout to notice forks (Re: Minor annoyance with git push)Junio C Hamano, Feb 17, 2008
  29. Daniel BarkalowFeb 17, 2008
  30. Junio C HamanoFeb 17, 2008
  31. Daniel BarkalowFeb 17, 2008
  32. Junio C HamanoFeb 17, 2008
  33. Daniel BarkalowFeb 17, 2008
  34. Jeff KingFeb 17, 2008
  35. Santi BéjarFeb 20, 2008
  36. Martin LanghoffFeb 19, 2008
  37. checkout: tone down the "forked status" diagnostic messagesJunio C Hamano, Feb 20, 2008
  38. Jeff KingFeb 21, 2008
  39. checkout: updates to tracking reportJunio C Hamano, Feb 21, 2008
  40. Jay SoffianFeb 21, 2008
  41. Daniel BarkalowFeb 21, 2008
  42. Jay SoffianFeb 21, 2008
  43. Steffen ProhaskaFeb 9, 2008
  44. Johannes SchindelinFeb 9, 2008
  45. Junio C HamanoFeb 10, 2008
  46. Johannes SchindelinFeb 10, 2008
  47. Jeff KingFeb 10, 2008
  48. Johannes SchindelinFeb 10, 2008
  49. Jeff KingFeb 10, 2008
  50. Johannes SchindelinFeb 10, 2008
  51. Jeff KingFeb 10, 2008
  52. Junio C HamanoFeb 20, 2008
  53. Johannes SchindelinFeb 20, 2008
  54. Jay SoffianFeb 20, 2008
  55. Johannes SchindelinFeb 20, 2008
  56. Steven WalterFeb 21, 2008
  57. Johannes SchindelinFeb 22, 2008
  58. Jeff KingFeb 20, 2008
  59. Junio C HamanoFeb 20, 2008
  60. Jeff KingFeb 20, 2008
  61. Jeff KingFeb 20, 2008
  62. Junio C HamanoFeb 20, 2008
  63. Jeff KingFeb 20, 2008
  64. Wincent ColaiutaFeb 10, 2008
  65. Steven WalterFeb 10, 2008
  66. Johannes SchindelinFeb 10, 2008
  67. Johannes SchindelinFeb 10, 2008
  68. Wincent ColaiutaFeb 10, 2008
  69. Jeff KingFeb 10, 2008
  70. Junio C HamanoFeb 10, 2008
  71. Jeff KingFeb 10, 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.