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

[PATCH v2] git-log: add a --since-as-filter option

From
Miklos Vajna <vmiklos@vmiklos.hu>
Date
Apr 2, 2022, 10:09 UTC
Message-ID
<Ykggy4poryul8uyG@vmiklos.hu>
In-Reply-To
<YkdwbUqM45T06R00@vmiklos.hu>

This is similar to --since, but it will filter out not matching commits, rather than stopping at the first not matching commit.

This is useful if you e.g. want to list the commits from the last year, but one odd commit has a bad commit date and that would hide lots of earlier commits in that range.

The behavior of --since is left unchanged, since it's valid to depend on its current behavior.

Signed-off-by: Miklos Vajna <vmiklos@vmiklos.hu>
---
Hi,
On Fri, Apr 01, 2022 at 11:36:48PM +0200, Miklos Vajna <vmiklos@vmiklos.hu> wrote:
> Here is a patch that does this.
Oops, forgot the sign-off, fixed now.
Regards,
Miklos
 Documentation/rev-list-options.txt |  5 +++++
 revision.c                         | 10 ++++++++++
 revision.h                         |  1 +
 t/t4217-log-limit.sh               | 32 ++++++++++++++++++++++++++++++
 4 files changed, 48 insertions(+)
 create mode 100755 t/t4217-log-limit.sh
diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index fd4f4e26c9..ba01b1ba06 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -25,6 +25,11 @@ ordering and formatting options, such as `--reverse`.
 --after=<date>::
 	Show commits more recent than a specific date.
 
+--since-as-filter=<date>::
+	Show all commits more recent than a specific date. This visits all
+	commits in the range, rather than stopping at the first commit which is older
+	than a specific date.
+
 --until=<date>::
 --before=<date>::
 	Show commits older than a specific date.
diff --git a/revision.c b/revision.c
index 2646b78990..ebc95319d6 100644
--- a/revision.c
+++ b/revision.c
@@ -1440,6 +1440,9 @@ static int limit_list(struct rev_info *revs)
 		if (revs->min_age != -1 && (commit->date > revs->min_age) &&
 		    !revs->line_level_traverse)
 			continue;
+		if (revs->max_age_as_filter != -1 && (commit->date < revs->max_age_as_filter) &&
+		    !revs->line_level_traverse)
+			continue;
 		date = commit->date;
 		p = &commit_list_insert(commit, p)->next;
 
@@ -1838,6 +1841,7 @@ void repo_init_revisions(struct repository *r,
 	revs->dense = 1;
 	revs->prefix = prefix;
 	revs->max_age = -1;
+	revs->max_age_as_filter = -1;
 	revs->min_age = -1;
 	revs->skip_count = -1;
 	revs->max_count = -1;
@@ -2218,6 +2222,9 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 	} else if ((argcount = parse_long_opt("since", argv, &optarg))) {
 		revs->max_age = approxidate(optarg);
 		return argcount;
+	} else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
+		revs->max_age_as_filter = approxidate(optarg);
+		return argcount;
 	} else if ((argcount = parse_long_opt("after", argv, &optarg))) {
 		revs->max_age = approxidate(optarg);
 		return argcount;
@@ -3862,6 +3869,9 @@ enum commit_action get_commit_action(struct rev_info *revs, struct commit *commi
 	if (revs->min_age != -1 &&
 	    comparison_date(revs, commit) > revs->min_age)
 			return commit_ignore;
+	if (revs->max_age_as_filter != -1 &&
+	    comparison_date(revs, commit) < revs->max_age_as_filter)
+			return commit_ignore;
 	if (revs->min_parents || (revs->max_parents >= 0)) {
 		int n = commit_list_count(commit->parents);
 		if ((n < revs->min_parents) ||
diff --git a/revision.h b/revision.h
index 5bc59c7bfe..e80c148b19 100644
--- a/revision.h
+++ b/revision.h
@@ -263,6 +263,7 @@ struct rev_info {
 	int skip_count;
 	int max_count;
 	timestamp_t max_age;
+	timestamp_t max_age_as_filter;
 	timestamp_t min_age;
 	int min_parents;
 	int max_parents;
diff --git a/t/t4217-log-limit.sh b/t/t4217-log-limit.sh
new file mode 100755
index 0000000000..5b7d30d5ad
--- /dev/null
+++ b/t/t4217-log-limit.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='git log with filter options limiting the output'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+GIT_TEST_COMMIT_GRAPH=0
+GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
+
+test_expect_success 'setup test' '
+	git init &&
+	echo a > file &&
+	git add file &&
+	GIT_COMMITTER_DATE="2022-02-01 0:00" git commit -m init &&
+	echo a >> file &&
+	git add file &&
+	GIT_COMMITTER_DATE="2021-01-01 0:00" git commit -m second &&
+	echo a >> file &&
+	git add file &&
+	GIT_COMMITTER_DATE="2022-03-01 0:00" git commit -m third
+'
+
+test_expect_success 'git log --since-as-filter' '
+	git log --since-as-filter="2022-01-01" --pretty="format:%s" > actual &&
+	test_i18ngrep init actual &&
+	! test_i18ngrep second actual &&
+	test_i18ngrep third actual
+'
+
+test_done
-- 
2.34.1
Previous: Miklos VajnaNext: Miklos Vajna
Message 7 of 24 in “git log --since to not stop after first old commit?”
  1. Miklos VajnaApr 1, 2022
  2. Ævar Arnfjörð BjarmasonApr 1, 2022
  3. Miklos VajnaApr 1, 2022
  4. Ævar Arnfjörð BjarmasonApr 1, 2022
  5. Junio C HamanoApr 1, 2022
  6. git-log: add a --since-as-filter optionMiklos Vajna, Apr 1, 2022
  7. git-log: add a --since-as-filter optionMiklos Vajna, Apr 2, 2022
  8. Miklos VajnaApr 7, 2022
  9. Junio C HamanoApr 8, 2022
  10. Junio C HamanoApr 8, 2022
  11. git-log: add a --since=... --as-filter optionMiklos Vajna, Apr 8, 2022
  12. Ævar Arnfjörð BjarmasonApr 12, 2022
  13. git-log: add a --since=... --as-filter optionMiklos Vajna, Apr 15, 2022
  14. Junio C HamanoApr 15, 2022
  15. log: "--as-filter" option adjusts how "--since" cut-off worksMiklos Vajna, Apr 16, 2022
  16. Miklos VajnaApr 22, 2022
  17. Miklos VajnaApr 11, 2022
  18. demerphqApr 11, 2022
  19. Junio C HamanoApr 11, 2022
  20. Junio C HamanoApr 22, 2022
  21. log: "--since-as-filter" option is a non-terminating "--since" variantMiklos Vajna, Apr 22, 2022
  22. Junio C HamanoApr 22, 2022
  23. log: "--since-as-filter" option is a non-terminating "--since" variantMiklos Vajna, Apr 23, 2022
  24. Junio C HamanoApr 22, 2022

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.