Skip to content

Commit d318a39

Browse files
committed
Merge branch 'jk/maint-reflog-walk-count-vs-time'
Gives a better DWIM behaviour for --pretty=format:%gd, "stash list", and "log -g", depending on how the starting point ("master" vs "master@{0}" vs "master@{now}") and date formatting options (e.g. "--date=iso") are given on the command line. By Jeff King (4) and Junio C Hamano (1) * jk/maint-reflog-walk-count-vs-time: reflog-walk: tell explicit --date=default from not having --date at all reflog-walk: always make HEAD@{0} show indexed selectors reflog-walk: clean up "flag" field of commit_reflog struct log: respect date_mode_explicit with --format:%gd t1411: add more selector index/date tests
2 parents 65029d8 + 55ccf85 commit d318a39

File tree

7 files changed

+82
-16
lines changed

7 files changed

+82
-16
lines changed

builtin/rev-list.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static void show_commit(struct commit *commit, void *data)
109109
struct pretty_print_context ctx = {0};
110110
ctx.abbrev = revs->abbrev;
111111
ctx.date_mode = revs->date_mode;
112+
ctx.date_mode_explicit = revs->date_mode_explicit;
112113
ctx.fmt = revs->commit_format;
113114
pretty_print_commit(&ctx, commit, &buf);
114115
if (revs->graph) {

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct pretty_print_context {
8484
const char *after_subject;
8585
int preserve_subject;
8686
enum date_mode date_mode;
87+
unsigned date_mode_explicit:1;
8788
int need_8bit_cte;
8889
int show_notes;
8990
struct reflog_walk_info *reflog_info;

log-tree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,9 @@ void show_log(struct rev_info *opt)
629629
* graph info here.
630630
*/
631631
show_reflog_message(opt->reflog_info,
632-
opt->commit_format == CMIT_FMT_ONELINE,
633-
opt->date_mode_explicit ?
634-
opt->date_mode :
635-
DATE_NORMAL);
632+
opt->commit_format == CMIT_FMT_ONELINE,
633+
opt->date_mode,
634+
opt->date_mode_explicit);
636635
if (opt->commit_format == CMIT_FMT_ONELINE)
637636
return;
638637
}
@@ -652,6 +651,7 @@ void show_log(struct rev_info *opt)
652651
if (ctx.need_8bit_cte >= 0)
653652
ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
654653
ctx.date_mode = opt->date_mode;
654+
ctx.date_mode_explicit = opt->date_mode_explicit;
655655
ctx.abbrev = opt->diffopt.abbrev;
656656
ctx.after_subject = extra_headers;
657657
ctx.preserve_subject = opt->preserve_subject;

pretty.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
10101010
get_reflog_selector(sb,
10111011
c->pretty_ctx->reflog_info,
10121012
c->pretty_ctx->date_mode,
1013+
c->pretty_ctx->date_mode_explicit,
10131014
(placeholder[1] == 'd'));
10141015
return 2;
10151016
case 's': /* reflog message */

reflog-walk.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ static void add_commit_info(struct commit *commit, void *util,
126126
}
127127

128128
struct commit_reflog {
129-
int flag, recno;
129+
int recno;
130+
enum selector_type {
131+
SELECTOR_NONE,
132+
SELECTOR_INDEX,
133+
SELECTOR_DATE
134+
} selector;
130135
struct complete_reflogs *reflogs;
131136
};
132137

@@ -150,6 +155,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
150155
struct complete_reflogs *reflogs;
151156
char *branch, *at = strchr(name, '@');
152157
struct commit_reflog *commit_reflog;
158+
enum selector_type selector = SELECTOR_NONE;
153159

154160
if (commit->object.flags & UNINTERESTING)
155161
die ("Cannot walk reflogs for %s", name);
@@ -162,7 +168,10 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
162168
if (*ep != '}') {
163169
recno = -1;
164170
timestamp = approxidate(at + 2);
171+
selector = SELECTOR_DATE;
165172
}
173+
else
174+
selector = SELECTOR_INDEX;
166175
} else
167176
recno = 0;
168177

@@ -200,7 +209,6 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
200209

201210
commit_reflog = xcalloc(sizeof(struct commit_reflog), 1);
202211
if (recno < 0) {
203-
commit_reflog->flag = 1;
204212
commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
205213
if (commit_reflog->recno < 0) {
206214
free(branch);
@@ -209,6 +217,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
209217
}
210218
} else
211219
commit_reflog->recno = reflogs->nr - recno - 1;
220+
commit_reflog->selector = selector;
212221
commit_reflog->reflogs = reflogs;
213222

214223
add_commit_info(commit, commit_reflog, &info->reflogs);
@@ -247,7 +256,7 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
247256

248257
void get_reflog_selector(struct strbuf *sb,
249258
struct reflog_walk_info *reflog_info,
250-
enum date_mode dmode,
259+
enum date_mode dmode, int force_date,
251260
int shorten)
252261
{
253262
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
@@ -267,7 +276,8 @@ void get_reflog_selector(struct strbuf *sb,
267276
}
268277

269278
strbuf_addf(sb, "%s@{", printed_ref);
270-
if (commit_reflog->flag || dmode) {
279+
if (commit_reflog->selector == SELECTOR_DATE ||
280+
(commit_reflog->selector == SELECTOR_NONE && force_date)) {
271281
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
272282
strbuf_addstr(sb, show_date(info->timestamp, info->tz, dmode));
273283
} else {
@@ -308,15 +318,15 @@ const char *get_reflog_ident(struct reflog_walk_info *reflog_info)
308318
}
309319

310320
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
311-
enum date_mode dmode)
321+
enum date_mode dmode, int force_date)
312322
{
313323
if (reflog_info && reflog_info->last_commit_reflog) {
314324
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
315325
struct reflog_info *info;
316326
struct strbuf selector = STRBUF_INIT;
317327

318328
info = &commit_reflog->reflogs->items[commit_reflog->recno+1];
319-
get_reflog_selector(&selector, reflog_info, dmode, 0);
329+
get_reflog_selector(&selector, reflog_info, dmode, force_date, 0);
320330
if (oneline) {
321331
printf("%s: %s", selector.buf, info->message);
322332
}

reflog-walk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ extern int add_reflog_for_walk(struct reflog_walk_info *info,
1111
extern void fake_reflog_parent(struct reflog_walk_info *info,
1212
struct commit *commit);
1313
extern void show_reflog_message(struct reflog_walk_info *info, int,
14-
enum date_mode);
14+
enum date_mode, int force_date);
1515
extern void get_reflog_message(struct strbuf *sb,
1616
struct reflog_walk_info *reflog_info);
1717
extern const char *get_reflog_ident(struct reflog_walk_info *reflog_info);
1818
extern void get_reflog_selector(struct strbuf *sb,
1919
struct reflog_walk_info *reflog_info,
20-
enum date_mode dmode,
20+
enum date_mode dmode, int force_date,
2121
int shorten);
2222

2323
#endif

t/t1411-reflog-show.sh

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,73 @@ test_expect_success 'using @{now} syntax shows reflog date (oneline)' '
6565
'
6666

6767
cat >expect <<'EOF'
68-
Reflog: HEAD@{1112911993 -0700} (C O Mitter <[email protected]>)
68+
HEAD@{Thu Apr 7 15:13:13 2005 -0700}
69+
EOF
70+
test_expect_success 'using @{now} syntax shows reflog date (format=%gd)' '
71+
git log -g -1 --format=%gd HEAD@{now} >actual &&
72+
test_cmp expect actual
73+
'
74+
75+
cat >expect <<'EOF'
76+
Reflog: HEAD@{Thu Apr 7 15:13:13 2005 -0700} (C O Mitter <[email protected]>)
6977
Reflog message: commit (initial): one
7078
EOF
7179
test_expect_success 'using --date= shows reflog date (multiline)' '
72-
git log -g -1 --date=raw >tmp &&
80+
git log -g -1 --date=default >tmp &&
7381
grep ^Reflog <tmp >actual &&
7482
test_cmp expect actual
7583
'
7684

7785
cat >expect <<'EOF'
78-
e46513e HEAD@{1112911993 -0700}: commit (initial): one
86+
e46513e HEAD@{Thu Apr 7 15:13:13 2005 -0700}: commit (initial): one
7987
EOF
8088
test_expect_success 'using --date= shows reflog date (oneline)' '
81-
git log -g -1 --oneline --date=raw >actual &&
89+
git log -g -1 --oneline --date=default >actual &&
90+
test_cmp expect actual
91+
'
92+
93+
cat >expect <<'EOF'
94+
HEAD@{1112911993 -0700}
95+
EOF
96+
test_expect_success 'using --date= shows reflog date (format=%gd)' '
97+
git log -g -1 --format=%gd --date=raw >actual &&
98+
test_cmp expect actual
99+
'
100+
101+
cat >expect <<'EOF'
102+
Reflog: HEAD@{0} (C O Mitter <[email protected]>)
103+
Reflog message: commit (initial): one
104+
EOF
105+
test_expect_success 'log.date does not invoke "--date" magic (multiline)' '
106+
test_config log.date raw &&
107+
git log -g -1 >tmp &&
108+
grep ^Reflog <tmp >actual &&
109+
test_cmp expect actual
110+
'
111+
112+
cat >expect <<'EOF'
113+
e46513e HEAD@{0}: commit (initial): one
114+
EOF
115+
test_expect_success 'log.date does not invoke "--date" magic (oneline)' '
116+
test_config log.date raw &&
117+
git log -g -1 --oneline >actual &&
118+
test_cmp expect actual
119+
'
120+
121+
cat >expect <<'EOF'
122+
HEAD@{0}
123+
EOF
124+
test_expect_success 'log.date does not invoke "--date" magic (format=%gd)' '
125+
test_config log.date raw &&
126+
git log -g -1 --format=%gd >actual &&
127+
test_cmp expect actual
128+
'
129+
130+
cat >expect <<'EOF'
131+
HEAD@{0}
132+
EOF
133+
test_expect_success '--date magic does not override explicit @{0} syntax' '
134+
git log -g -1 --format=%gd --date=raw HEAD@{0} >actual &&
82135
test_cmp expect actual
83136
'
84137

0 commit comments

Comments
 (0)