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

[PATCH 1/8] ahead-behind: create empty builtin

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 6, 2023, 14:06 UTC
Message-ID
<0fd18b6d740f1e8a6f62a25947bc3ad49c2674a6.1678111599.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1489.git.1678111598.gitgitgadget@gmail.com>
From: Derrick Stolee <derrickstolee@github.com>

The 'git ahead-behind' builtin _will_ allow users to specify multiple tip revisions relative to a common base and _will_ report the number of commits on each side of the symmetric difference between each tip and the base. However, that algorithm is not implemented yet and instead this change introduces the builtin and the basic boilerplate for a new builtin.

This builtin could be replaced with multiple invocations of 'git rev-list --count <base>..<tip>' (for ahead values) and 'git rev-list --count <tip>..<base>' (for behind values). However, it is important to be able to batch these calls into a single process.

For example, we will be able to track all local branches relative to an upstream branch using an invocation such as

  git for-each-ref --format=%(refname) refs/heads/* |
    git ahead-behind --base=origin/main --stdin

This would report each local branch and how far ahead or behind it is relative to the remote branch 'origin/main'. This could be used to signal some branches are very old and need to be updated via 'git rebase' or deleted. We will see in future changes how such commit counting can be done efficiently within a single process (and a single commit walk) instead of multiple processes.

For now, only 'git ahead-behind -h' works, and the builtin reports failure and shows the usage if the '--base' option is skipped. The documentation is light. These will be updated in the coming changes.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 .gitignore                         |  1 +
 Documentation/git-ahead-behind.txt | 62 ++++++++++++++++++++++++++++++
 Makefile                           |  1 +
 builtin.h                          |  1 +
 builtin/ahead-behind.c             | 30 +++++++++++++++
 git.c                              |  1 +
 t/t4218-ahead-behind.sh            | 17 ++++++++
 7 files changed, 113 insertions(+)
 create mode 100644 Documentation/git-ahead-behind.txt
 create mode 100644 builtin/ahead-behind.c
 create mode 100755 t/t4218-ahead-behind.sh
diff --git a/.gitignore b/.gitignore
index e875c590545..cc064a4817a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,6 +14,7 @@
 /bin-wrappers/
 /git
 /git-add
+/git-ahead-behind
 /git-am
 /git-annotate
 /git-apply
diff --git a/Documentation/git-ahead-behind.txt b/Documentation/git-ahead-behind.txt
new file mode 100644
index 00000000000..0e2f989a1a0
--- /dev/null
+++ b/Documentation/git-ahead-behind.txt
@@ -0,0 +1,62 @@
+git-ahead-behind(1)
+===================
+
+NAME
+----
+git-ahead-behind - Count the commits on each side of a revision range
+
+SYNOPSIS
+--------
+[verse]
+'git ahead-behind' --base=<ref> [ --stdin | <revs> ]
+
+DESCRIPTION
+-----------
+
+Given a list of commit ranges, report the number of commits reachable from
+each of the sides of the range, but not the other. Consider a commit range
+specified as `<base>...<tip>`, allowing for the following definitions:
+
+* The `<tip>` is *ahead* of `<base>` by the number of commits reachable
+  from `<tip>` but not reachable from `<base>`. This is the same as the
+  number of the commits in the range `<base>..<tip>`.
+
+* The `<tip>` is *behind* `<base>` by the number of commits reachable from
+  `<base>` but not reachble from `<tip>`. This is the same as the number
+  of commits in the range `<tip>..<base>`.
+
+The sum of the ahead and behind counts equals the number of commits in the
+symmetric difference, the range `<base>...<tip>`.
+
+Multiple revisions may be specified, and they are all compared against a
+common base revision, as specified by the `--base` option. The values are
+reported to stdout one line at a time as follows:
+
+---
+  <rev> <ahead> <behind>
+---
+
+There will be exactly one line per input revision, but the lines may be
+in an arbitrary order.
+
+
+OPTIONS
+-------
+--base=<ref>::
+	Specify that `<ref>` should be used as a common base for all
+	provided revisions that are not specified in the form of a range.
+
+--stdin::
+	Read revision tips and ranges from stdin instead of from the
+	command-line.
+
+
+SEE ALSO
+--------
+linkgit:git-branch[1]
+linkgit:git-rev-list[1]
+linkgit:git-tag[1]
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index 50ee51fde32..691f84e8d4e 100644
--- a/Makefile
+++ b/Makefile
@@ -1199,6 +1199,7 @@ LIB_OBJS += xdiff-interface.o
 LIB_OBJS += zlib.o
 
 BUILTIN_OBJS += builtin/add.o
+BUILTIN_OBJS += builtin/ahead-behind.o
 BUILTIN_OBJS += builtin/am.o
 BUILTIN_OBJS += builtin/annotate.o
 BUILTIN_OBJS += builtin/apply.o
diff --git a/builtin.h b/builtin.h
index 46cc7897898..1ae168fa3e3 100644
--- a/builtin.h
+++ b/builtin.h
@@ -108,6 +108,7 @@ void setup_auto_pager(const char *cmd, int def);
 int is_builtin(const char *s);
 
 int cmd_add(int argc, const char **argv, const char *prefix);
+int cmd_ahead_behind(int argc, const char **argv, const char *prefix);
 int cmd_am(int argc, const char **argv, const char *prefix);
 int cmd_annotate(int argc, const char **argv, const char *prefix);
 int cmd_apply(int argc, const char **argv, const char *prefix);
diff --git a/builtin/ahead-behind.c b/builtin/ahead-behind.c
new file mode 100644
index 00000000000..a56cc565def
--- /dev/null
+++ b/builtin/ahead-behind.c
@@ -0,0 +1,30 @@
+#include "builtin.h"
+#include "parse-options.h"
+#include "config.h"
+
+static const char * const ahead_behind_usage[] = {
+	N_("git ahead-behind --base=<ref> [ --stdin | <revs> ]"),
+	NULL
+};
+
+int cmd_ahead_behind(int argc, const char **argv, const char *prefix)
+{
+	const char *base_ref = NULL;
+	int from_stdin = 0;
+
+	struct option ahead_behind_opts[] = {
+		OPT_STRING('b', "base", &base_ref, N_("base"), N_("base reference to process")),
+		OPT_BOOL(0 , "stdin", &from_stdin, N_("read rev names from stdin")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, NULL, ahead_behind_opts,
+			     ahead_behind_usage, PARSE_OPT_KEEP_UNKNOWN_OPT);
+
+	if (!base_ref)
+		usage_with_options(ahead_behind_usage, ahead_behind_opts);
+
+	git_config(git_default_config, NULL);
+
+	return 0;
+}
diff --git a/git.c b/git.c
index 6171fd6769d..64e3d493561 100644
--- a/git.c
+++ b/git.c
@@ -467,6 +467,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 
 static struct cmd_struct commands[] = {
 	{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+	{ "ahead-behind", cmd_ahead_behind, RUN_SETUP },
 	{ "am", cmd_am, RUN_SETUP | NEED_WORK_TREE },
 	{ "annotate", cmd_annotate, RUN_SETUP },
 	{ "apply", cmd_apply, RUN_SETUP_GENTLY },
diff --git a/t/t4218-ahead-behind.sh b/t/t4218-ahead-behind.sh
new file mode 100755
index 00000000000..bc08f1207a0
--- /dev/null
+++ b/t/t4218-ahead-behind.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+test_description='git ahead-behind command-line options'
+
+. ./test-lib.sh
+
+test_expect_success 'git ahead-behind -h' '
+	test_must_fail git ahead-behind -h >out &&
+	grep "usage:" out
+'
+
+test_expect_success 'git ahead-behind without --base' '
+	test_must_fail git ahead-behind HEAD 2>err &&
+	grep "usage:" err
+'
+
+test_done
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Junio C Hamano
Message 2 of 90 in “ahead-behind: new builtin for counting multiple commit ranges”
  1. 0/8 ahead-behind: new builtin for counting multiple commit rangesDerrick Stolee via GitGitGadget, Mar 6, 2023
  2. 1/8 ahead-behind: create empty builtinDerrick Stolee via GitGitGadget, Mar 6, 2023
  3. Junio C HamanoMar 6, 2023
  4. Taylor BlauMar 7, 2023
  5. Derrick StoleeMar 8, 2023
  6. Junio C HamanoMar 8, 2023
  7. 2/8 ahead-behind: parse tip referencesDerrick Stolee via GitGitGadget, Mar 6, 2023
  8. Taylor BlauMar 7, 2023
  9. 3/8 ahead-behind: implement --ignore-missing optionDerrick Stolee via GitGitGadget, Mar 6, 2023
  10. Taylor BlauMar 7, 2023
  11. 4/8 commit-graph: combine generation computationsDerrick Stolee via GitGitGadget, Mar 6, 2023
  12. 5/8 commit-graph: return generation from memoryDerrick Stolee via GitGitGadget, Mar 6, 2023
  13. 6/8 commit-graph: introduce `ensure_generations_valid()`Taylor Blau via GitGitGadget, Mar 6, 2023
  14. Junio C HamanoMar 6, 2023
  15. Taylor BlauMar 7, 2023
  16. 7/8 ahead-behind: implement ahead_behind() logicDerrick Stolee via GitGitGadget, Mar 6, 2023
  17. Taylor BlauMar 7, 2023
  18. Derrick StoleeMar 9, 2023
  19. 8/8 ahead-behind: add --contains modeDerrick Stolee via GitGitGadget, Mar 6, 2023
  20. Junio C HamanoMar 6, 2023
  21. Derrick StoleeMar 6, 2023
  22. Junio C HamanoMar 6, 2023
  23. Taylor BlauMar 7, 2023
  24. Jeff KingMar 9, 2023
  25. Junio C HamanoMar 9, 2023
  26. Taylor BlauMar 7, 2023
  27. 0/8 ref-filter: ahead/behind counting, faster --merged optionDerrick Stolee via GitGitGadget, Mar 10, 2023
  28. 1/8 for-each-ref: add --stdin optionDerrick Stolee via GitGitGadget, Mar 10, 2023
  29. Junio C HamanoMar 10, 2023
  30. Phillip WoodMar 13, 2023
  31. Derrick StoleeMar 13, 2023
  32. Taylor BlauMar 13, 2023
  33. Ævar Arnfjörð BjarmasonMar 15, 2023
  34. Jeff KingMar 15, 2023
  35. Jeff KingMar 15, 2023
  36. Junio C HamanoMar 15, 2023
  37. Jeff KingMar 15, 2023
  38. 2/8 for-each-ref: explicitly test no matchesDerrick Stolee via GitGitGadget, Mar 10, 2023
  39. 3/8 commit-graph: combine generation computationsDerrick Stolee via GitGitGadget, Mar 10, 2023
  40. 4/8 commit-graph: return generation from memoryDerrick Stolee via GitGitGadget, Mar 10, 2023
  41. 5/8 commit-graph: introduce `ensure_generations_valid()`Taylor Blau via GitGitGadget, Mar 10, 2023
  42. 6/8 commit-reach: implement ahead_behind() logicDerrick Stolee via GitGitGadget, Mar 10, 2023
  43. Ævar Arnfjörð BjarmasonMar 15, 2023
  44. Junio C HamanoMar 15, 2023
  45. Derrick StoleeMar 15, 2023
  46. 8/8 commit-reach: add tips_reachable_from_bases()Derrick Stolee via GitGitGadget, Mar 10, 2023
  47. Ævar Arnfjörð BjarmasonMar 15, 2023
  48. Derrick StoleeMar 15, 2023
  49. Derrick StoleeMar 15, 2023
  50. 7/8 for-each-ref: add ahead-behind format atomDerrick Stolee via GitGitGadget, Mar 10, 2023
  51. Junio C HamanoMar 10, 2023
  52. Ævar Arnfjörð BjarmasonMar 15, 2023
  53. Junio C HamanoMar 15, 2023
  54. Derrick StoleeMar 15, 2023
  55. Derrick StoleeMar 15, 2023
  56. Junio C HamanoMar 10, 2023
  57. Derrick StoleeMar 10, 2023
  58. Jeff KingMar 15, 2023
  59. Derrick StoleeMar 15, 2023
  60. Junio C HamanoMar 15, 2023
  61. Ævar Arnfjörð BjarmasonMar 15, 2023
  62. Derrick StoleeMar 15, 2023
  63. 0/8 ref-filter: ahead/behind counting, faster --merged optionDerrick Stolee via GitGitGadget, Mar 15, 2023
  64. 1/8 for-each-ref: add --stdin optionDerrick Stolee via GitGitGadget, Mar 15, 2023
  65. Jeff KingMar 15, 2023
  66. Junio C HamanoMar 15, 2023
  67. Jonathan TanMar 15, 2023
  68. 2/8 for-each-ref: explicitly test no matchesDerrick Stolee via GitGitGadget, Mar 15, 2023
  69. 5/8 commit-graph: introduce `ensure_generations_valid()`Taylor Blau via GitGitGadget, Mar 15, 2023
  70. 3/8 commit-graph: combine generation computationsDerrick Stolee via GitGitGadget, Mar 15, 2023
  71. Jonathan TanMar 15, 2023
  72. Derrick StoleeMar 17, 2023
  73. 6/8 commit-reach: implement ahead_behind() logicDerrick Stolee via GitGitGadget, Mar 15, 2023
  74. Jonathan TanMar 15, 2023
  75. Derrick StoleeMar 17, 2023
  76. 4/8 commit-graph: return generation from memoryDerrick Stolee via GitGitGadget, Mar 15, 2023
  77. Jonathan TanMar 15, 2023
  78. 8/8 commit-reach: add tips_reachable_from_bases()Derrick Stolee via GitGitGadget, Mar 15, 2023
  79. 7/8 for-each-ref: add ahead-behind format atomDerrick Stolee via GitGitGadget, Mar 15, 2023
  80. 0/9 ref-filter: ahead/behind counting, faster --merged optionDerrick Stolee via GitGitGadget, Mar 20, 2023
  81. 1/9 for-each-ref: add --stdin optionDerrick Stolee via GitGitGadget, Mar 20, 2023
  82. 2/9 for-each-ref: explicitly test no matchesDerrick Stolee via GitGitGadget, Mar 20, 2023
  83. 4/9 commit-graph: simplify compute_generation_numbers()Derrick Stolee via GitGitGadget, Mar 20, 2023
  84. 3/9 commit-graph: refactor compute_topological_levels()Derrick Stolee via GitGitGadget, Mar 20, 2023
  85. 5/9 commit-graph: return generation from memoryDerrick Stolee via GitGitGadget, Mar 20, 2023
  86. 6/9 commit-graph: introduce `ensure_generations_valid()`Taylor Blau via GitGitGadget, Mar 20, 2023
  87. 7/9 commit-reach: implement ahead_behind() logicDerrick Stolee via GitGitGadget, Mar 20, 2023
  88. Jonathan TanMar 20, 2023
  89. 9/9 commit-reach: add tips_reachable_from_bases()Derrick Stolee via GitGitGadget, Mar 20, 2023
  90. 8/9 for-each-ref: add ahead-behind format atomDerrick Stolee via GitGitGadget, Mar 20, 2023

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.