reflog-walk: apply --since/--until to reflog dates

When doing a reflog walk, we use the commit's date to do any date limiting. In earlier versions of Git, this could lead to nonsense results, since a skipped commit would truncate the traversal. So a sequence like: git commit ... git checkout week-old-branch git checkout - git log -g --since=1.day.ago would stop at the week-old-branch, even though the "git commit" entry further back is still interesting. As of the prior commit, which uses a parent-less traversal of the reflog, you get the whole reflog minus any commits whose dates do not match the specified options. This is arguably useful, as you could scan the reflogs for commits that originated in a certain range. But more likely a user doing a reflog walk wants to limit based on the reflog entries themselves. You can simulate --until with: git log -g @{1.day.ago} but there's no way to ask Git to traverse only back to a certain date. E.g.: # show me reflog entries from the past day git log -g --since=1.day.ago This patch teaches the revision machinery to prefer the reflog entry dates to the commit dates when doing a reflog walk. Technically this is a change in behavior that affects plumbing, but the previous behavior was so buggy that it's unlikely anyone was relying on it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 7, 2017 at 05:16 UTC de239446b69f3b453050af8091e07aa5433421cc
4 files changed +55 -3
reflog-walk.c
+12
@@ -264,6 +264,18 @@ const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
264 return info->email;
265 }
266
267 +timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
268 +{
269 + struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
270 + struct reflog_info *info;
271 +
272 + if (!commit_reflog)
273 + return 0;
274 +
275 + info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
276 + return info->timestamp;
277 +}
278 +
279 void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
280 const struct date_mode *dmode, int force_date)
281 {
reflog-walk.h
+1
@@ -13,6 +13,7 @@ extern void show_reflog_message(struct reflog_walk_info *info, int,
13 extern void get_reflog_message(struct strbuf *sb,
14 struct reflog_walk_info *reflog_info);
15 extern const char *get_reflog_ident(struct reflog_walk_info *reflog_info);
16 +extern timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);
17 extern void get_reflog_selector(struct strbuf *sb,
18 struct reflog_walk_info *reflog_info,
19 const struct date_mode *dmode, int force_date,
revision.c
+16 -3
@@ -2965,6 +2965,18 @@ static inline int want_ancestry(const struct rev_info *revs)
2965 return (revs->rewrite_parents || revs->children.name);
2966 }
2967
2968 +/*
2969 + * Return a timestamp to be used for --since/--until comparisons for this
2970 + * commit, based on the revision options.
2971 + */
2972 +static timestamp_t comparison_date(const struct rev_info *revs,
2973 + struct commit *commit)
2974 +{
2975 + return revs->reflog_info ?
2976 + get_reflog_timestamp(revs->reflog_info) :
2977 + commit->date;
2978 +}
2979 +
2980 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2981 {
2982 if (commit->object.flags & SHOWN)
@@ -2975,8 +2987,9 @@ enum commit_action get_commit_action(struct rev_info *revs, struct commit *commi
2987 return commit_show;
2988 if (commit->object.flags & UNINTERESTING)
2989 return commit_ignore;
2978 - if (revs->min_age != -1 && (commit->date > revs->min_age))
2979 - return commit_ignore;
2990 + if (revs->min_age != -1 &&
2991 + comparison_date(revs, commit) > revs->min_age)
2992 + return commit_ignore;
2993 if (revs->min_parents || (revs->max_parents >= 0)) {
2994 int n = commit_list_count(commit->parents);
2995 if ((n < revs->min_parents) ||
@@ -3130,7 +3143,7 @@ static struct commit *get_revision_1(struct rev_info *revs)
3143 */
3144 if (!revs->limited) {
3145 if (revs->max_age != -1 &&
3133 - (commit->date < revs->max_age))
3146 + comparison_date(revs, commit) < revs->max_age)
3147 continue;
3148
3149 if (revs->reflog_info)
t/t1414-reflog-walk.sh
+26
@@ -91,6 +91,32 @@ test_expect_success 'date-limiting does not interfere with other logs' '
91 test_cmp expect.all actual
92 '
93
94 +test_expect_success 'min/max age uses entry date to limit' '
95 + # Flip between commits one and two so each ref update actually
96 + # does something (and does not get optimized out). We know
97 + # that the timestamps of those commits will be before our "min".
98 +
99 + git update-ref -m before refs/heads/minmax one &&
100 +
101 + test_tick &&
102 + min=$test_tick &&
103 + git update-ref -m min refs/heads/minmax two &&
104 +
105 + test_tick &&
106 + max=$test_tick &&
107 + git update-ref -m max refs/heads/minmax one &&
108 +
109 + test_tick &&
110 + git update-ref -m after refs/heads/minmax two &&
111 +
112 + cat >expect <<-\EOF &&
113 + max
114 + min
115 + EOF
116 + git log -g --since=$min --until=$max --format=%gs minmax >actual &&
117 + test_cmp expect actual
118 +'
119 +
120 test_expect_success 'walk prefers reflog to ref tip' '
121 head=$(git rev-parse HEAD) &&
122 one=$(git rev-parse one) &&