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

[PATCH 14/14] shortlog: match commit trailers with --ident

From
Jeff King <peff@peff.net>
Date
Dec 29, 2015, 07:38 UTC
Message-ID
<20151229073832.GN8842@sigill.intra.peff.net>
In-Reply-To
<20151229071847.GA8726@sigill.intra.peff.net>

If a project uses commit trailers, this patch lets you use shortlog to see who is performing each action. For example, running:

  git shortlog -ns --ident=reviewed-by

in git.git shows who has reviewed. You can even use a custom format to see things like who has helped whom:

  git shortlog --format="...helped %an (%ad)" --ident=helped-by

This does run a bit more slowly than a normal author-grouping (about 33% slower in my tests). Some of that is natural, because we have to spend time parsing the trailers. But I suspect a fair bit of that could be cut off by optimizing the trailer code, which involves quite a few more copies of the data than we actually need.

Still, it is certainly fast enough to be usable, and optimization can come later.

Signed-off-by: Jeff King <peff@peff.net>
---
 Documentation/git-shortlog.txt |  9 +++++++++
 builtin/shortlog.c             | 38 +++++++++++++++++++++++++++++++++++---
 shortlog.h                     |  4 +++-
 t/t4201-shortlog.sh            |  9 +++++++++
 4 files changed, 56 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-shortlog.txt b/Documentation/git-shortlog.txt
index a89a01e..16080c4 100644
--- a/Documentation/git-shortlog.txt
+++ b/Documentation/git-shortlog.txt
@@ -54,6 +54,15 @@ OPTIONS
 +
  - `author`, commits are grouped by author (this is the default)
  - `committer`, commits are grouped by committer
+ - any other value, the `<type>` is interpreted as a case-insensitive
+   commit message trailer (see linkgit:git-interpret-trailers[1]). For
+   example, if your project uses `Reviewed-by` trailers, you might want
+   to see who has been reviewing with
+   `git shortlog -ns --ident=reviewed-by`.
++
+Note that commits that do not include the trailer will not be counted.
+Likewise, commits with multiple trailers (e.g., multiple signoffs) may
+count more than once.
 
 -w[<width>[,<indent1>[,<indent2>]]]::
 	Linewrap the output by wrapping each line at `width`.  The first
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index 39da2d4..f774c84 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -8,6 +8,7 @@
 #include "mailmap.h"
 #include "shortlog.h"
 #include "parse-options.h"
+#include "trailer.h"
 
 static char const * const shortlog_usage[] = {
 	N_("git shortlog [<options>] [<revision-range>] [[--] [<path>...]]"),
@@ -126,6 +127,8 @@ static void read_from_stdin(struct shortlog *log)
 	case SHORTLOG_IDENT_COMMITTER:
 		ident_header = "Commit: ";
 		break;
+	case SHORTLOG_IDENT_TRAILER:
+		die(_("sorry, using a trailer --ident with stdin is not supported"));
 	}
 
 	while (strbuf_getline(&ident, stdin, '\n') != EOF) {
@@ -149,6 +152,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
 	struct strbuf ident = STRBUF_INIT;
 	struct strbuf oneline = STRBUF_INIT;
 	struct pretty_print_context ctx = {0};
+	char *oneline_str;
 
 	ctx.fmt = CMIT_FMT_USERFORMAT;
 	ctx.abbrev = log->abbrev;
@@ -174,6 +178,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
 			return;
 		}
 		break;
+	case SHORTLOG_IDENT_TRAILER:
+		/*
+		 * We might have multiple matches, so deal with it in the loop
+		 * below.
+		 */
+		break;
 	}
 
 	if (!log->summary) {
@@ -183,7 +193,26 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
 			format_commit_message(commit, "%s", &oneline, &ctx);
 	}
 
-	insert_one_record(log, ident.buf, oneline.len ? oneline.buf : "<none>");
+	oneline_str = oneline.len ? oneline.buf : "<none>";
+	if (log->ident_type != SHORTLOG_IDENT_TRAILER)
+		insert_one_record(log, ident.buf, oneline_str);
+	else {
+		struct strbuf msg = STRBUF_INIT;
+		struct trailer_parse_context tp;
+		int i;
+
+		format_commit_message(commit, "%B", &msg, &ctx);
+		trailer_parse_init(&tp, &msg);
+		for (i = tp.start; i < tp.end; i++) {
+			const char *v = trailer_parse_match(&tp, i, log->trailer);
+			if (!v)
+				continue;
+			insert_one_record(log, v, oneline_str);
+		}
+		trailer_parse_clear(&tp);
+		strbuf_release(&msg);
+	}
+
 	strbuf_release(&ident);
 	strbuf_release(&oneline);
 }
@@ -253,8 +282,11 @@ static int parse_ident_option(const struct option *opt, const char *arg, int uns
 		log->ident_type = SHORTLOG_IDENT_AUTHOR;
 	else if (!strcasecmp(arg, "committer"))
 		log->ident_type = SHORTLOG_IDENT_COMMITTER;
-	else
-		die("unknown ident type: %s", arg);
+	else {
+		log->ident_type = SHORTLOG_IDENT_TRAILER;
+		free(log->trailer);
+		log->trailer = xstrdup(arg);
+	}
 
 	return 0;
 }
diff --git a/shortlog.h b/shortlog.h
index a365620..718b49d 100644
--- a/shortlog.h
+++ b/shortlog.h
@@ -16,8 +16,10 @@ struct shortlog {
 
 	enum {
 		SHORTLOG_IDENT_AUTHOR = 0,
-		SHORTLOG_IDENT_COMMITTER
+		SHORTLOG_IDENT_COMMITTER,
+		SHORTLOG_IDENT_TRAILER,
 	} ident_type;
+	char *trailer;
 
 	char *common_repo_prefix;
 	int email;
diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
index 867a7ae..9c00ccb 100755
--- a/t/t4201-shortlog.sh
+++ b/t/t4201-shortlog.sh
@@ -207,4 +207,13 @@ test_expect_success 'shortlog --ident=committer (external)' '
 	test_cmp expect actual
 '
 
+test_expect_success 'shortlog --ident=signed-off-by' '
+	git commit --allow-empty -m foo -s &&
+	cat >expect <<-\EOF &&
+	     1	C O Mitter
+	EOF
+	git shortlog -ns --ident=signed-off-by HEAD >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.7.0.rc3.367.g09631da
Previous: Jeff KingNext: Jeff King
Message 26 of 29 in “counting trailers with shortlogs”
  1. 0/14 counting trailers with shortlogsJeff King, Dec 29, 2015
  2. 01/14 move string functions out of git-compat-utilJeff King, Dec 29, 2015
  3. 02/14 log: refactor add_header to drop some magic numbersJeff King, Dec 29, 2015
  4. Eric SunshineDec 31, 2015
  5. Jeff KingJan 1, 2016
  6. Jeff KingJan 1, 2016
  7. 03/14 strutil: add skip_prefix_icaseJeff King, Dec 29, 2015
  8. Eric SunshineDec 31, 2015
  9. Jeff KingJan 1, 2016
  10. 04/14 shortlog: use skip_prefix_icase to parse "Author" linesJeff King, Dec 29, 2015
  11. Eric SunshineDec 31, 2015
  12. Jeff KingJan 1, 2016
  13. 05/14 shortlog: use strbufs to read from stdinJeff King, Dec 29, 2015
  14. 06/14 shortlog: replace hand-parsing of author with pretty-printerJeff King, Dec 29, 2015
  15. Eric SunshineJan 4, 2016
  16. Jeff KingJan 4, 2016
  17. 07/14 shortlog: optimize "--summary" modeJeff King, Dec 29, 2015
  18. 08/14 shortlog: optimize out useless "<none>" normalizationJeff King, Dec 29, 2015
  19. 09/14 shortlog: optimize out useless string listJeff King, Dec 29, 2015
  20. 10/14 shortlog: change "author" variables to "ident"Jeff King, Dec 29, 2015
  21. 11/14 shortlog: allow grouping by committer identJeff King, Dec 29, 2015
  22. Eric SunshineJan 4, 2016
  23. Jeff KingJan 4, 2016
  24. 12/14 trailer: factor out config readingJeff King, Dec 29, 2015
  25. 13/14 trailer: add interface for parsing commit trailersJeff King, Dec 29, 2015
  26. 14/14 shortlog: match commit trailers with --identJeff King, Dec 29, 2015
  27. Jeff KingDec 29, 2015
  28. Eric SunshineJan 4, 2016
  29. Jeff KingJan 4, 2016

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.