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

[PATCH 0/7] Add Format Specifier for Blame

From
ATAleks Todorov <aleks.todorov.1337@gmail.com>
Date
Mar 1, 2025, 21:44 UTC
Message-ID
<20250301214652.536439-1-aleks.todorov.1337@gmail.com>
Hi,

As previously discussed, this patch series implements a `--format` option for `git-blame`, re-using the existing implementation from `git-log`.

My motivation for this is that I use git blame quite often when debugging, and find myself checking the commit subject for a large number of lines that I look at. Instead, it would speed up my workflow if I could see the commit subject next to each line in the blame output.

Since this is a personal preference and each git user probably has their own personal preference for what kind of output they want to see, as per Junio's suggestion, by implementing a generic format option, we serve a variety of use cases all at the same time.

For context, we can now use a command like:
  git blame builtin/blame.c -F '%h %<(17,trunc)%F (%<(20,trunc)%an %ai %>(4)%L) '
to see output like:
  df8738116f7 builtin/blame.c   (Ævar Arnfjörð Bjar.. 2022-10-13 17:39:20 +0200   40) static char annotate_usage[] = N_("git annotate [<options>] [<rev-opts>] [<rev>] [--] <file>");
  5817da01434 builtin-blame.c   (Pierre Habouzit      2008-07-08 15:19:34 +0200   41)
  5817da01434 builtin-blame.c   (Pierre Habouzit      2008-07-08 15:19:34 +0200   42) static const char *blame_opt_usage[] = {
  5817da01434 builtin-blame.c   (Pierre Habouzit      2008-07-08 15:19:34 +0200   43)     blame_usage,
  5817da01434 builtin-blame.c   (Pierre Habouzit      2008-07-08 15:19:34 +0200   44)     "",

However, one key feature I could not find an easy way to implement was to include a length specifier that auto-detects the minimum column width required to display a placeholder without trimming the output. As a result, I could not re-implement the existing options using the formatting syntax.

Nevertheless, these patches serve my purpose, are self-contained, and could be extended in the future to complete the plan.

I've made my best effort to include clear atomic commits, write conforming code, include tests, and add documentation. However, as this is my first submission here, I expect that I may have missed something - so please let me know what can be improved and whether this patch series is suitable for integration.

A short logical summary of the patches:
  1. Add --format / -F option
  2. Add format config option
  3. Add %F and %L format specifiers (blame-only) to allow the filename
     and line number to be emitted
  4. Call into pretty.c to expand the provided format string
  5. Add tests
  6. Refactor documentation structure
  7. Add documentation specific to `git blame --format`
Patches are based on main.

Thanks, Aleks

Aleks Todorov (7):
  builtin/blame: Accept a user-specified format
  builtin/blame: Add blame.format config option
  pretty: Add F and L format specifiers
  builtin/blame: Expand format when available
  t8015: Add tests for git blame -F
  Documentation: Move placeholders to separate file
  Documentation: Add docs for blame format
 Documentation/config/blame.adoc   |   5 +
 Documentation/git-blame.adoc      |  16 ++
 Documentation/placeholders.adoc   | 242 ++++++++++++++++++++++++++++++
 Documentation/pretty-formats.adoc | 239 +----------------------------
 builtin/blame.c                   | 119 +++++++++------
 pretty.c                          |  14 ++
 pretty.h                          |   5 +
 t/meson.build                     |   1 +
 t/t8015-blame-format.sh           |  39 +++++
 9 files changed, 395 insertions(+), 285 deletions(-)
 create mode 100644 Documentation/placeholders.adoc
 create mode 100755 t/t8015-blame-format.sh
-- 
2.43.0
Next: Aleks Todorov
Message 1 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.