Skip to content

Commit de23944

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d08565b commit de23944

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

reflog-walk.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
264264
return info->email;
265265
}
266266

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+
267279
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
268280
const struct date_mode *dmode, int force_date)
269281
{

reflog-walk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern void show_reflog_message(struct reflog_walk_info *info, int,
1313
extern void get_reflog_message(struct strbuf *sb,
1414
struct reflog_walk_info *reflog_info);
1515
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);
1617
extern void get_reflog_selector(struct strbuf *sb,
1718
struct reflog_walk_info *reflog_info,
1819
const struct date_mode *dmode, int force_date,

revision.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,18 @@ static inline int want_ancestry(const struct rev_info *revs)
29652965
return (revs->rewrite_parents || revs->children.name);
29662966
}
29672967

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+
29682980
enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
29692981
{
29702982
if (commit->object.flags & SHOWN)
@@ -2975,8 +2987,9 @@ enum commit_action get_commit_action(struct rev_info *revs, struct commit *commi
29752987
return commit_show;
29762988
if (commit->object.flags & UNINTERESTING)
29772989
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;
29802993
if (revs->min_parents || (revs->max_parents >= 0)) {
29812994
int n = commit_list_count(commit->parents);
29822995
if ((n < revs->min_parents) ||
@@ -3130,7 +3143,7 @@ static struct commit *get_revision_1(struct rev_info *revs)
31303143
*/
31313144
if (!revs->limited) {
31323145
if (revs->max_age != -1 &&
3133-
(commit->date < revs->max_age))
3146+
comparison_date(revs, commit) < revs->max_age)
31343147
continue;
31353148

31363149
if (revs->reflog_info)

t/t1414-reflog-walk.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ test_expect_success 'date-limiting does not interfere with other logs' '
9191
test_cmp expect.all actual
9292
'
9393

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+
94120
test_expect_success 'walk prefers reflog to ref tip' '
95121
head=$(git rev-parse HEAD) &&
96122
one=$(git rev-parse one) &&

0 commit comments

Comments
 (0)