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

[PATCH 2/6] pretty: Add %D for script-friendly decoration

From
Greg Price <price@mit.edu>
Date
Nov 18, 2009, 23:22 UTC
Message-ID
<9d9e18b8c062380025f52a6ff992fae51a17f5d1.1309133817.git.greg@quora.com>
In-Reply-To
<cover.1309133817.git.greg@quora.com>

When in a script or porcelain one wants to identify what refs point to which commits in a series, the functionality of 'git log --decorate' is extremely useful. This is available with the %d format code in a form optimized for humans, but for scripts a more raw format is better. Make such a format available through a new format code %D.

Signed-off-by: Greg Price <price@mit.edu>
---
 Documentation/pretty-formats.txt |    1 +
 pretty.c                         |   33 +++++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 561cc9f..217f11b 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -124,6 +124,7 @@ The placeholders are:
 - '%ct': committer date, UNIX timestamp
 - '%ci': committer date, ISO 8601 format
 - '%d': ref names, like the --decorate option of linkgit:git-log[1]
+- '%D': full ref names, like the --decorate=full option of linkgit:git-log[1]
 - '%e': encoding
 - '%s': subject
 - '%f': sanitized subject line, suitable for a filename
diff --git a/pretty.c b/pretty.c
index f45eb54..0c7e723 100644
--- a/pretty.c
+++ b/pretty.c
@@ -776,21 +776,35 @@ static void parse_commit_message(struct format_commit_context *c)
 	c->commit_message_parsed = 1;
 }
 
-static void format_decoration(struct strbuf *sb, const struct commit *commit)
+
+static void format_decoration(struct strbuf *sb, const struct commit *commit,
+			      int decoration_style, const char *affixes[3])
 {
 	struct name_decoration *d;
-	const char *prefix = " (";
+	const char *affix = affixes[0];
 
-	load_ref_decorations(DECORATE_SHORT_REFS);
+	load_ref_decorations(decoration_style);
 	d = lookup_decoration(&name_decoration, &commit->object);
 	while (d) {
-		strbuf_addstr(sb, prefix);
-		prefix = ", ";
+		strbuf_addstr(sb, affix);
+		affix = affixes[1];
 		strbuf_addstr(sb, d->name);
 		d = d->next;
 	}
-	if (prefix[0] == ',')
-		strbuf_addch(sb, ')');
+	if (affix == affixes[1])
+		strbuf_addstr(sb, affixes[2]);
+}
+
+static void format_decoration_short(struct strbuf *sb, const struct commit *commit)
+{
+	const char *affixes[3] = {" (", ", ", ")"};
+	format_decoration(sb, commit, DECORATE_SHORT_REFS, affixes);
+}
+
+static void format_decoration_full(struct strbuf *sb, const struct commit *commit)
+{
+	const char *affixes[3] = {"", " ", ""};
+	format_decoration(sb, commit, DECORATE_FULL_REFS, affixes);
 }
 
 static void strbuf_wrap(struct strbuf *sb, size_t pos,
@@ -947,7 +961,10 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 		strbuf_addstr(sb, get_revision_mark(NULL, commit));
 		return 1;
 	case 'd':
-		format_decoration(sb, commit);
+		format_decoration_short(sb, commit);
+		return 1;
+	case 'D':
+		format_decoration_full(sb, commit);
 		return 1;
 	case 'g':		/* reflog info */
 		switch(placeholder[1]) {
-- 
1.7.5.4
Previous: Greg PriceNext: Junio C Hamano
Message 5 of 24 in “rebase: command "ref" and options --rewrite-{refs,heads,tags}”
  1. 0/6 rebase: command "ref" and options --rewrite-{refs,heads,tags}Greg Price, Jun 27, 2011
  2. 1/6 rebase -i: Add the "ref" commandGreg Price, Oct 10, 2009
  3. Junio C HamanoJun 27, 2011
  4. Greg PriceJun 28, 2011
  5. 2/6 pretty: Add %D for script-friendly decorationGreg Price, Nov 18, 2009
  6. Junio C HamanoJun 27, 2011
  7. 4/6 rebase: --rewrite-{refs,heads,tags} to pull refs along with branchGreg Price, Nov 18, 2009
  8. Phil HordJun 27, 2011
  9. Greg PriceJun 28, 2011
  10. 3/6 for-each-ref: --stdin to match specified refs against patternGreg Price, Jan 7, 2010
  11. 5/6 t/lib-rebase.sh: pass through ref commandsGreg Price, Jan 25, 2010
  12. 6/6 rebase --rewrite-refs: testsGreg Price, Jan 25, 2010
  13. Phil HordJun 27, 2011
  14. Greg PriceJun 28, 2011
  15. Junio C HamanoJun 27, 2011
  16. Greg PriceJun 28, 2011
  17. Junio C HamanoJun 27, 2011
  18. Greg PriceJun 28, 2011
  19. Greg PriceJun 28, 2011
  20. Ramkumar RamachandraJun 30, 2011
  21. Sverre RabbelierAug 3, 2011
  22. Greg PriceAug 3, 2011
  23. Junio C HamanoJun 27, 2011
  24. Greg PriceJun 28, 2011

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.