This might be useful for external programs doing topological sorting or other graph analysis. It's also handy for testing the generation calculation code. Signed-off-by: Jeff King <peff@xxxxxxxx> --- This now includes some basic tests. Documentation/pretty-formats.txt | 1 + pretty.c | 3 ++ t/t6070-commit-generations.sh | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 0 deletions(-) create mode 100755 t/t6070-commit-generations.sh diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt index 561cc9f..c58ab52 100644 --- a/Documentation/pretty-formats.txt +++ b/Documentation/pretty-formats.txt @@ -133,6 +133,7 @@ The placeholders are: - '%gD': reflog selector, e.g., `refs/stash@\{1\}` - '%gd': shortened reflog selector, e.g., `stash@\{1\}` - '%gs': reflog subject +- '%G': generation number (i.e., distance of path to farthest root ancestor) - '%Cred': switch color to red - '%Cgreen': switch color to green - '%Cblue': switch color to blue diff --git a/pretty.c b/pretty.c index f45eb54..8f1b321 100644 --- a/pretty.c +++ b/pretty.c @@ -965,6 +965,9 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder, return 2; } return 0; /* unknown %g placeholder */ + case 'G': + strbuf_addf(sb, "%lu", commit_generation(commit)); + return 1; case 'N': if (c->pretty_ctx->show_notes) { format_display_notes(commit->object.sha1, sb, diff --git a/t/t6070-commit-generations.sh b/t/t6070-commit-generations.sh new file mode 100755 index 0000000..3e0f2ad --- /dev/null +++ b/t/t6070-commit-generations.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +test_description='calculate and cache commit generations' +. ./test-lib.sh + +test_expect_success 'setup history' ' + test_commit one && + test_commit two && + test_commit three && + test_commit four && + git checkout -b other two && + test_commit five && + git checkout master && + git merge other && + test_commit six +' + +cat >expect <<'EOF' +5 six +4 Merge branch 'other' +2 five +3 four +2 three +1 two +0 one +EOF +test_expect_success 'check commit generations' ' + git log --format="%G %s" >actual && + test_cmp expect actual +' + +test_expect_success 'cache file was created' ' + test_path_is_file .git/cache/generations +' + +test_expect_success 'cached values are the same' ' + git log --format="%G %s" >actual && + test_cmp expect actual +' + +test_done -- 1.7.6.37.g989c6 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html