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

[PATCH 2/2] log: do not shorten decoration names too early

From
Junio C Hamano <gitster@pobox.com>
Date
May 13, 2015, 19:40 UTC
Message-ID
<xmqqoalo9sgc.fsf_-_@gitster.dls.corp.google.com>
In-Reply-To
<xmqqsib09z8y.fsf@gitster.dls.corp.google.com>

The DECORATE_SHORT_REFS option given to load_ref_decorations() affects the way a copy of the refname is stored for each decorated commit, and this forces later steps like current_pointed_by_HEAD() to adjust their behaviour based on this initial settings.

Instead, we can always store the full refname and then shorten them when producing the output.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * [1/2] is just the earlier "this should fix it" patch, with
   adjustments to the existing tests.
   I suspect that it may be a good idea to lose the decoration_flags
   from load_ref_decorations() and instead make that a new parameter
   to format_decorations().  That way, the caller could decide which
   ones to use.  It is not unconceivable to extend "log --format=%d"
   that shows the decoration in the style given by --decorate arg
   and let the callers specify two additional formats (i.e. decorate
   always short, decorate always in full), and for that kind of
   work, this patch will become a prerequisite.
 log-tree.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
diff --git a/log-tree.c b/log-tree.c
index 92259bc..c931615 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -94,6 +94,8 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
 	struct object *obj;
 	enum decoration_type type = DECORATION_NONE;
 
+	assert(cb_data == NULL);
+
 	if (starts_with(refname, "refs/replace/")) {
 		unsigned char original_sha1[20];
 		if (!check_replace_refs)
@@ -123,8 +125,6 @@ static int add_ref_decoration(const char *refname, const unsigned char *sha1, in
 	else if (!strcmp(refname, "HEAD"))
 		type = DECORATION_REF_HEAD;
 
-	if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
-		refname = prettify_refname(refname);
 	add_name_decoration(type, refname, obj);
 	while (obj->type == OBJ_TAG) {
 		obj = ((struct tag *)obj)->tagged;
@@ -151,8 +151,8 @@ void load_ref_decorations(int flags)
 	if (!decoration_loaded) {
 		decoration_loaded = 1;
 		decoration_flags = flags;
-		for_each_ref(add_ref_decoration, &flags);
-		head_ref(add_ref_decoration, &flags);
+		for_each_ref(add_ref_decoration, NULL);
+		head_ref(add_ref_decoration, NULL);
 		for_each_commit_graft(add_graft_decoration, NULL);
 	}
 }
@@ -199,18 +199,8 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
 	if (!(rru_flags & REF_ISSYMREF))
 		return NULL;
 
-	if ((decoration_flags == DECORATE_SHORT_REFS)) {
-		if (!skip_prefix(branch_name, "refs/heads/", &branch_name))
-			return NULL;
-	} else {
-		/*
-		 * Each decoration has a refname in full; keep
-		 * branch_name also in full, but still make sure
-		 * HEAD is a reasonable ref.
-		 */
-		if (!starts_with(branch_name, "refs/"))
-			return NULL;
-	}
+	if (!starts_with(branch_name, "refs/"))
+		return NULL;
 
 	/* OK, do we have that ref in the list? */
 	for (list = decoration; list; list = list->next)
@@ -222,6 +212,14 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
 	return NULL;
 }
 
+static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
+{
+	if (decoration_flags == DECORATE_SHORT_REFS)
+		strbuf_addstr(sb, prettify_refname(decoration->name));
+	else
+		strbuf_addstr(sb, decoration->name);
+}
+
 /*
  * The caller makes sure there is no funny color before calling.
  * format_decorations_extended makes sure the same after return.
@@ -259,7 +257,7 @@ void format_decorations_extended(struct strbuf *sb,
 			if (decoration->type == DECORATION_REF_TAG)
 				strbuf_addstr(sb, "tag: ");
 
-			strbuf_addstr(sb, decoration->name);
+			show_name(sb, decoration);
 
 			if (current_and_HEAD &&
 			    decoration->type == DECORATION_REF_HEAD) {
@@ -268,7 +266,7 @@ void format_decorations_extended(struct strbuf *sb,
 				strbuf_addstr(sb, " -> ");
 				strbuf_addstr(sb, color_reset);
 				strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
-				strbuf_addstr(sb, current_and_HEAD->name);
+				show_name(sb, current_and_HEAD);
 			}
 			strbuf_addstr(sb, color_reset);
 
Previous: Junio C HamanoNext: Jeff King
Message 6 of 17 in “"HEAD -> branch" decoration doesn't work with "--decorate=full"”
  1. Michael HaggertyMay 13, 2015
  2. Junio C HamanoMay 13, 2015
  3. Michael J GruberMay 13, 2015
  4. Junio C HamanoMay 13, 2015
  5. Junio C HamanoMay 13, 2015
  6. 2/2 log: do not shorten decoration names too earlyJunio C Hamano, May 13, 2015
  7. Jeff KingMay 14, 2015
  8. Junio C HamanoMay 14, 2015
  9. Jeff KingMay 14, 2015
  10. Junio C HamanoMay 14, 2015
  11. Jeff KingMay 14, 2015
  12. Junio C HamanoMay 14, 2015
  13. Jeff KingMay 14, 2015
  14. Junio C HamanoMay 14, 2015
  15. Jeff KingMay 14, 2015
  16. Junio C HamanoMay 22, 2015
  17. Jeff KingMay 22, 2015

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.