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

Re: [PATCH 1/2] add '%d' pretty format specifier to show decoration

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Sep 3, 2008, 22:06 UTC
Message-ID
<48BF0A5A.2040502@lsrfire.ath.cx>
In-Reply-To
<7vsksh9c9m.fsf@gitster.siamese.dyndns.org>
Junio C Hamano schrieb:
Show 5 quoted lines
> If the concensus is that we do the simpler variant _now_ and leave
> extending it for later, I think it is Ok to pick any one reasonable
> default/simple format, and including the parentheses (with leading SP)
> would be one reasonable default, certainly more reasonable than not
> including the parentheses at all.

Yes, but can we please use the existing parser and not add another one just to find out if some initialization is needed? Also, format_commit_message() is called from git archive. In order to keep all the placeholders working on each call site, it's better to control everything from there.

The patch below does that by using a static variable to remember if the decorations have already been loaded. That's not too unreasonable, I think, because we currently have no way of unloading them.

The patch should be split up and contains no documentation updates.  It
  - moves add_name_decoration() and add_ref_decoration() to log-tree.c,
    next to the variable name_decoration they are working with,
  - exports add_ref_decoration(),
  - adds load_ref_decorations(), which loads all refs as decorations,
    unless they have already been loaded,
  - adds the placeholder %d, with parentheses and leading space (or
    empty if no decoration is present).

Thanks, René

 builtin-log.c |   25 -------------------------
 log-tree.c    |   36 ++++++++++++++++++++++++++++++++++++
 log-tree.h    |    3 +++
 pretty.c      |   25 +++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 25 deletions(-)
diff --git a/builtin-log.c b/builtin-log.c
index 1d3c5cb..ba1ef8d 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -25,31 +25,6 @@ static int default_show_root = 1;
 static const char *fmt_patch_subject_prefix = "PATCH";
 static const char *fmt_pretty;
 
-static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
-{
-	int plen = strlen(prefix);
-	int nlen = strlen(name);
-	struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
-	memcpy(res->name, prefix, plen);
-	memcpy(res->name + plen, name, nlen + 1);
-	res->next = add_decoration(&name_decoration, obj, res);
-}
-
-static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
-{
-	struct object *obj = parse_object(sha1);
-	if (!obj)
-		return 0;
-	add_name_decoration("", refname, obj);
-	while (obj->type == OBJ_TAG) {
-		obj = ((struct tag *)obj)->tagged;
-		if (!obj)
-			break;
-		add_name_decoration("tag: ", refname, obj);
-	}
-	return 0;
-}
-
 static void cmd_log_init(int argc, const char **argv, const char *prefix,
 		      struct rev_info *rev)
 {
diff --git a/log-tree.c b/log-tree.c
index 30cd5bb..083d08a 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -1,12 +1,48 @@
 #include "cache.h"
 #include "diff.h"
 #include "commit.h"
+#include "tag.h"
 #include "graph.h"
 #include "log-tree.h"
 #include "reflog-walk.h"
+#include "refs.h"
 
 struct decoration name_decoration = { "object names" };
 
+static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
+{
+	int plen = strlen(prefix);
+	int nlen = strlen(name);
+	struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
+	memcpy(res->name, prefix, plen);
+	memcpy(res->name + plen, name, nlen + 1);
+	res->next = add_decoration(&name_decoration, obj, res);
+}
+
+int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
+{
+	struct object *obj = parse_object(sha1);
+	if (!obj)
+		return 0;
+	add_name_decoration("", refname, obj);
+	while (obj->type == OBJ_TAG) {
+		obj = ((struct tag *)obj)->tagged;
+		if (!obj)
+			break;
+		add_name_decoration("tag: ", refname, obj);
+	}
+	return 0;
+}
+
+void load_ref_decorations(void)
+{
+	static int loaded;
+	if (!loaded) {
+		loaded = 1;
+		for_each_ref(add_ref_decoration, NULL);
+	}
+}
+
 static void show_parents(struct commit *commit, int abbrev)
 {
 	struct commit_list *p;
diff --git a/log-tree.h b/log-tree.h
index 59ba4c4..beeb86e 100644
--- a/log-tree.h
+++ b/log-tree.h
@@ -18,4 +18,7 @@ void log_write_email_headers(struct rev_info *opt, const char *name,
 			     const char **extra_headers_p,
 			     int *need_8bit_cte_p);
 
+int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
+void load_ref_decorations(void);
+
 #endif
diff --git a/pretty.c b/pretty.c
index a29c290..4a31e31 100644
--- a/pretty.c
+++ b/pretty.c
@@ -5,6 +5,7 @@
 #include "revision.h"
 #include "string-list.h"
 #include "mailmap.h"
+#include "log-tree.h"
 
 static char *user_format;
 
@@ -423,6 +424,7 @@ struct format_commit_context {
 	struct chunk abbrev_commit_hash;
 	struct chunk abbrev_tree_hash;
 	struct chunk abbrev_parent_hashes;
+	struct chunk decoration;
 };
 
 static int add_again(struct strbuf *sb, struct chunk *chunk)
@@ -481,6 +483,23 @@ static void parse_commit_header(struct format_commit_context *context)
 	context->commit_header_parsed = 1;
 }
 
+static void format_decoration(struct strbuf *sb, const struct commit *commit)
+{
+	struct name_decoration *d;
+	const char *prefix = " (";
+
+	load_ref_decorations();
+	d = lookup_decoration(&name_decoration, &commit->object);
+	while (d) {
+		strbuf_addstr(sb, prefix);
+		prefix = ", ";
+		strbuf_addstr(sb, d->name);
+		d = d->next;
+	}
+	if (prefix[0] == ',')
+		strbuf_addch(sb, ')');
+}
+
 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
                                void *context)
 {
@@ -520,6 +539,12 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 			return 3;
 		} else
 			return 0;
+	case 'd':
+		if (add_again(sb, &c->decoration))
+			return 1;
+		format_decoration(sb, commit);
+		c->decoration.len = sb->len - c->decoration.off;
+		return 1;
 	}
 
 	/* these depend on the commit */
Previous: Jeff KingNext: Jeff King
Message 7 of 23 in “add '%d' pretty format specifier to show decoration”
  1. 1/2 add '%d' pretty format specifier to show decorationMichael Dressel, Sep 3, 2008
  2. Jeff KingSep 3, 2008
  3. Junio C HamanoSep 3, 2008
  4. Jeff KingSep 3, 2008
  5. Junio C HamanoSep 3, 2008
  6. Jeff KingSep 3, 2008
  7. René ScharfeSep 3, 2008
  8. Jeff KingSep 4, 2008
  9. René ScharfeSep 4, 2008
  10. 1/3 log: add load_ref_decorations()René Scharfe, Sep 4, 2008
  11. 2/3 move load_ref_decorations() to log-tree.c and export itRené Scharfe, Sep 4, 2008
  12. 3/3 add '%d' pretty format specifier to show decorationRené Scharfe, Sep 4, 2008
  13. Jeff KingSep 5, 2008
  14. Junio C HamanoSep 5, 2008
  15. Jeff KingSep 5, 2008
  16. Junio C HamanoSep 5, 2008
  17. Michael DresselSep 5, 2008
  18. Michael DresselSep 9, 2008
  19. René ScharfeSep 9, 2008
  20. René ScharfeSep 5, 2008
  21. Jeff KingSep 3, 2008
  22. Michael DresselSep 3, 2008
  23. Jeff KingSep 3, 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.