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

[PATCH 1/2] log-tree: make format_decorations more flexible

From
Harry Jeffery <harry@exec64.co.uk>
Date
Sep 10, 2014, 21:58 UTC
Message-ID
<5410C998.5060701@exec64.co.uk>

The prefix, separator and suffix for decorations are hard-coded. Make format_decorations more flexible by having the caller specify the prefix, separator and suffix.

Signed-off-by: Harry Jeffery <harry@exec64.co.uk>
---
  log-tree.c | 16 +++++++++-------
  log-tree.h |  2 +-
  pretty.c   |  2 +-
  3 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/log-tree.c b/log-tree.c
index 95e9b1d..860694c 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -184,9 +184,11 @@ static void show_children(struct rev_info *opt, 
struct commit *commit, int abbre
   */
  void format_decorations(struct strbuf *sb,
  			const struct commit *commit,
-			int use_color)
+			int use_color,
+			const char* prefix,
+			const char* sep,
+			const char* suffix)
  {
-	const char *prefix;
  	struct name_decoration *decoration;
  	const char *color_commit =
  		diff_get_color(use_color, DIFF_COMMIT);
@@ -196,20 +198,20 @@ void format_decorations(struct strbuf *sb,
  	decoration = lookup_decoration(&name_decoration, &commit->object);
  	if (!decoration)
  		return;
-	prefix = " (";
+	strbuf_addstr(sb, prefix);
  	while (decoration) {
  		strbuf_addstr(sb, color_commit);
-		strbuf_addstr(sb, prefix);
  		strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
  		if (decoration->type == DECORATION_REF_TAG)
  			strbuf_addstr(sb, "tag: ");
  		strbuf_addstr(sb, decoration->name);
+		if(decoration->next)
+			strbuf_addstr(sb, sep);
  		strbuf_addstr(sb, color_reset);
-		prefix = ", ";
  		decoration = decoration->next;
  	}
  	strbuf_addstr(sb, color_commit);
-	strbuf_addch(sb, ')');
+	strbuf_addstr(sb, suffix);
  	strbuf_addstr(sb, color_reset);
  }

@@ -221,7 +223,7 @@ void show_decorations(struct rev_info *opt, struct 
commit *commit)
  		printf("\t%s", (char *) commit->util);
  	if (!opt->show_decorations)
  		return;
-	format_decorations(&sb, commit, opt->diffopt.use_color);
+	format_decorations(&sb, commit, opt->diffopt.use_color, " (", ", ", ")");
  	fputs(sb.buf, stdout);
  	strbuf_release(&sb);
  }
diff --git a/log-tree.h b/log-tree.h
index d6ecd4d..4816911 100644
--- a/log-tree.h
+++ b/log-tree.h
@@ -13,7 +13,7 @@ int log_tree_diff_flush(struct rev_info *);
  int log_tree_commit(struct rev_info *, struct commit *);
  int log_tree_opt_parse(struct rev_info *, const char **, int);
  void show_log(struct rev_info *opt);
-void format_decorations(struct strbuf *sb, const struct commit *commit, 
int use_color);
+void format_decorations(struct strbuf *sb, const struct commit *commit, 
int use_color, const char* prefix, const char* sep, const char* suffix);
  void show_decorations(struct rev_info *opt, struct commit *commit);
  void log_write_email_headers(struct rev_info *opt, struct commit *commit,
  			     const char **subject_p,
diff --git a/pretty.c b/pretty.c
index 44b9f64..e4dc093 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1195,7 +1195,7 @@ static size_t format_commit_one(struct strbuf *sb, 
/* in UTF-8 */
  		return 1;
  	case 'd':
  		load_ref_decorations(DECORATE_SHORT_REFS);
-		format_decorations(sb, commit, c->auto_color);
+		format_decorations(sb, commit, c->auto_color, " (", ", ", ")");
  		return 1;
  	case 'g':		/* reflog info */
  		switch(placeholder[1]) {
-- 
2.1.0
Next: Harry Jeffery
Message 1 of 6 in “log-tree: make format_decorations more flexible”
  1. 1/2 log-tree: make format_decorations more flexibleHarry Jeffery, Sep 10, 2014
  2. 2/2 pretty: add %D format specifierHarry Jeffery, Sep 10, 2014
  3. Junio C HamanoSep 11, 2014
  4. Harry JefferySep 11, 2014
  5. Junio C HamanoSep 11, 2014
  6. Junio C HamanoSep 11, 2014

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.