Re: [PATCH] git-reset.txt: Use commit~1 notation over commit^

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] git-reset.txt: Use commit~1 notation over commit^

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:50:09

Jeff King [off-list ref] writes:
quoted hunk
On Thu, Dec 02, 2010 at 09:55:03AM -0800, Junio C Hamano wrote:
quoted
Perhaps we need to also fix "git name-rev master^" which currently does
not try to reduce "master~1" to "master^".
This patch does it:
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index c946a82..417bae5 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -142,8 +142,12 @@ static const char *get_rev_name(const struct object *o)
 		int len = strlen(n->tip_name);
 		if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
 			len -= 2;
-		snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
-				n->generation);
+		if (n->generation == 1)
+			snprintf(buffer, sizeof(buffer), "%.*s^", len,
+				 n->tip_name);
+		else
+			snprintf(buffer, sizeof(buffer), "%.*s~%d", len,
+				 n->tip_name, n->generation);
 
 		return buffer;
 	}
but I am not sure the results are always more readable. I think "foo^"
is perhaps nicer than "foo~1". But in more complex examples, I kind of
think the ~1 is easier to read. E.g.:

  # old
  $ git name-rev 9904fadf
  9904fadf tags/v1.7.3-rc2~1^2~1

  # new
  $ git name-rev 9904fadf
  9904fadf tags/v1.7.3-rc2~1^2^
Curious.  Why does the "first take the first parent of rc2" is left as-is,
while "then lastly take its parent" does get shortened?
Somehow the visual appearance of "^2^" ends up being more confusing to
me than ~1^2~1, I guess because in the latter there is a regular set of
modifier-number pairs.

But I admit that is just my subjective opinion.

-Peff

Re: [PATCH] git-reset.txt: Use commit~1 notation over commit^

From: Jeff King <hidden>
Date: 2016-06-15 22:50:09

On Thu, Dec 02, 2010 at 10:54:03AM -0800, Junio C Hamano wrote:
quoted
but I am not sure the results are always more readable. I think "foo^"
is perhaps nicer than "foo~1". But in more complex examples, I kind of
think the ~1 is easier to read. E.g.:

  # old
  $ git name-rev 9904fadf
  9904fadf tags/v1.7.3-rc2~1^2~1

  # new
  $ git name-rev 9904fadf
  9904fadf tags/v1.7.3-rc2~1^2^
Curious.  Why does the "first take the first parent of rc2" is left as-is,
while "then lastly take its parent" does get shortened?
Because my patch is crappy and I didn't spend more than 30 seconds
testing it? :)

It needs a similar change elsewhere, so the full patch should be:
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index c946a82..03d7bce 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -64,9 +64,15 @@ copy_data:
 
 			if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
 				len -= 2;
-			if (generation > 0)
-				sprintf(new_name, "%.*s~%d^%d", len, tip_name,
+			if (generation > 0) {
+				int w = sprintf(new_name, "%.*s", len, tip_name);
+				if (generation == 1)
+					sprintf(new_name + w, "^^%d",
+						parent_number);
+				else
+					sprintf(new_name + w, "~%d^%d",
 						generation, parent_number);
+			}
 			else
 				sprintf(new_name, "%.*s^%d", len, tip_name,
 						parent_number);
@@ -142,8 +148,12 @@ static const char *get_rev_name(const struct object *o)
 		int len = strlen(n->tip_name);
 		if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
 			len -= 2;
-		snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
-				n->generation);
+		if (n->generation == 1)
+			snprintf(buffer, sizeof(buffer), "%.*s^", len,
+				 n->tip_name);
+		else
+			snprintf(buffer, sizeof(buffer), "%.*s~%d", len,
+				 n->tip_name, n->generation);
 
 		return buffer;
 	}
However, IMHO that looks even worse:

  $ git name-rev 9904fadf
  9904fadf tags/v1.7.3-rc2^^2^

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