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

[PATCH 3/7] pretty: Add F and L format specifiers

From
ATAleks Todorov <aleks.todorov.1337@gmail.com>
Date
Mar 1, 2025, 21:45 UTC
Message-ID
<20250301214652.536439-4-aleks.todorov.1337@gmail.com>
In-Reply-To
<20250301214652.536439-1-aleks.todorov.1337@gmail.com>

Add the F (filename) and L (line number) format specifiers. These are to be used with blame only and therefore only work with blame.

Signed-off-by: Aleks Todorov <aleks.todorov.1337@gmail.com>
---
 pretty.c | 14 ++++++++++++++
 pretty.h |  5 +++++
 2 files changed, 19 insertions(+)
diff --git a/pretty.c b/pretty.c
index 0bc8ad8a9a..d2b7284854 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1446,6 +1446,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	const char *arg, *eol;
 	size_t res;
 	char **slot;
+	char line_number[16];
 
 	/* these are independent of the commit */
 	res = strbuf_expand_literal(sb, placeholder);
@@ -1549,6 +1550,19 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	if (!commit->object.parsed)
 		parse_object(the_repository, &commit->object.oid);
 
+	if (c->pretty_ctx->is_blame) {
+		switch (placeholder[0]) {
+		case 'F':		/* filename */
+			strbuf_addstr(sb, c->pretty_ctx->filename);
+			return 1;
+		case 'L':		/* line number */
+			snprintf(line_number, sizeof(line_number), "%zu",
+				 c->pretty_ctx->line);
+			strbuf_addstr(sb, line_number);
+			return 1;
+		}
+	}
+
 	switch (placeholder[0]) {
 	case 'H':		/* commit hash */
 		strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
diff --git a/pretty.h b/pretty.h
index df267afe4a..eb54070ecb 100644
--- a/pretty.h
+++ b/pretty.h
@@ -51,6 +51,11 @@ struct pretty_print_context {
 	unsigned encode_email_headers:1;
 	struct pretty_print_describe_status *describe_status;
 
+	/* Blame fields */
+	bool is_blame;
+	const char *filename;
+	size_t line;
+
 	/*
 	 * Fields below here are manipulated internally by pp_* functions and
 	 * should not be counted on by callers.
-- 
2.43.0
Previous: Aleks TodorovNext: Aleks Todorov
Message 4 of 8 in “Add Format Specifier for Blame”
  1. 0/7 Add Format Specifier for BlameAleks Todorov, Mar 1, 2025
  2. 1/7 builtin/blame: Accept a user-specified formatAleks Todorov, Mar 1, 2025
  3. 2/7 builtin/blame: Add blame.format config optionAleks Todorov, Mar 1, 2025
  4. 3/7 pretty: Add F and L format specifiersAleks Todorov, Mar 1, 2025
  5. 4/7 builtin/blame: Expand format when availableAleks Todorov, Mar 1, 2025
  6. 5/7 t8015: Add tests for git blame -FAleks Todorov, Mar 1, 2025
  7. 6/7 Documentation: Move placeholders to separate fileAleks Todorov, Mar 1, 2025
  8. 7/7 Documentation: Add docs for blame formatAleks Todorov, Mar 1, 2025

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.