Skip to content

Commit fcd2c3d

Browse files
avargitster
authored andcommitted
reflog + refs-backend: move "verbose" out of the backend
Move the handling of the "verbose" flag entirely out of "refs/files-backend.c" and into "builtin/reflog.c". This allows the backend to stop knowing about the EXPIRE_REFLOGS_VERBOSE flag. The expire_reflog_ent() function shouldn't need to deal with the implementation detail of whether or not we're emitting verbose output, by doing this the --verbose output becomes backend-agnostic, so reftable will get the same output. I think the output is rather bad currently, and should e.g. be implemented with some better future mode of progress.[ch], but that's a topic for another improvement. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c28875 commit fcd2c3d

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

builtin/reflog.c

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct expire_reflog_policy_cb {
4545
struct cmd_reflog_expire_cb cmd;
4646
struct commit *tip_commit;
4747
struct commit_list *tips;
48+
unsigned int dry_run:1;
4849
};
4950

5051
struct worktree_reflogs {
@@ -319,6 +320,28 @@ static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *no
319320
return 0;
320321
}
321322

323+
static int should_expire_reflog_ent_verbose(struct object_id *ooid,
324+
struct object_id *noid,
325+
const char *email,
326+
timestamp_t timestamp, int tz,
327+
const char *message, void *cb_data)
328+
{
329+
struct expire_reflog_policy_cb *cb = cb_data;
330+
int expire;
331+
332+
expire = should_expire_reflog_ent(ooid, noid, email, timestamp, tz,
333+
message, cb);
334+
335+
if (!expire)
336+
printf("keep %s", message);
337+
else if (cb->dry_run)
338+
printf("would prune %s", message);
339+
else
340+
printf("prune %s", message);
341+
342+
return expire;
343+
}
344+
322345
static int push_tip_to_list(const char *refname, const struct object_id *oid,
323346
int flags, void *cb_data)
324347
{
@@ -539,6 +562,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
539562
int i, status, do_all, all_worktrees = 1;
540563
int explicit_expiry = 0;
541564
unsigned int flags = 0;
565+
int verbose = 0;
566+
reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
542567

543568
default_reflog_expire_unreachable = now - 30 * 24 * 3600;
544569
default_reflog_expire = now - 90 * 24 * 3600;
@@ -576,7 +601,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
576601
else if (!strcmp(arg, "--single-worktree"))
577602
all_worktrees = 0;
578603
else if (!strcmp(arg, "--verbose"))
579-
flags |= EXPIRE_REFLOGS_VERBOSE;
604+
verbose = 1;
580605
else if (!strcmp(arg, "--")) {
581606
i++;
582607
break;
@@ -587,6 +612,9 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
587612
break;
588613
}
589614

615+
if (verbose)
616+
should_prune_fn = should_expire_reflog_ent_verbose;
617+
590618
/*
591619
* We can trust the commits and objects reachable from refs
592620
* even in older repository. We cannot trust what's reachable
@@ -599,10 +627,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
599627
revs.do_not_die_on_missing_tree = 1;
600628
revs.ignore_missing = 1;
601629
revs.ignore_missing_links = 1;
602-
if (flags & EXPIRE_REFLOGS_VERBOSE)
630+
if (verbose)
603631
printf(_("Marking reachable objects..."));
604632
mark_reachable_objects(&revs, 0, 0, NULL);
605-
if (flags & EXPIRE_REFLOGS_VERBOSE)
633+
if (verbose)
606634
putchar('\n');
607635
}
608636

@@ -624,12 +652,15 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
624652
free_worktrees(worktrees);
625653

626654
for_each_string_list_item(item, &collected.reflogs) {
627-
struct expire_reflog_policy_cb cb = { .cmd = cmd };
655+
struct expire_reflog_policy_cb cb = {
656+
.cmd = cmd,
657+
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
658+
};
628659

629660
set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string);
630661
status |= reflog_expire(item->string, flags,
631662
reflog_expiry_prepare,
632-
should_expire_reflog_ent,
663+
should_prune_fn,
633664
reflog_expiry_cleanup,
634665
&cb);
635666
}
@@ -647,7 +678,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
647678
set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
648679
status |= reflog_expire(ref, flags,
649680
reflog_expiry_prepare,
650-
should_expire_reflog_ent,
681+
should_prune_fn,
651682
reflog_expiry_cleanup,
652683
&cb);
653684
free(ref);
@@ -670,6 +701,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
670701
struct cmd_reflog_expire_cb cmd = { 0 };
671702
int i, status = 0;
672703
unsigned int flags = 0;
704+
int verbose = 0;
705+
reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
673706

674707
for (i = 1; i < argc; i++) {
675708
const char *arg = argv[i];
@@ -680,7 +713,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
680713
else if (!strcmp(arg, "--updateref"))
681714
flags |= EXPIRE_REFLOGS_UPDATE_REF;
682715
else if (!strcmp(arg, "--verbose"))
683-
flags |= EXPIRE_REFLOGS_VERBOSE;
716+
verbose = 1;
684717
else if (!strcmp(arg, "--")) {
685718
i++;
686719
break;
@@ -691,14 +724,19 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
691724
break;
692725
}
693726

727+
if (verbose)
728+
should_prune_fn = should_expire_reflog_ent_verbose;
729+
694730
if (argc - i < 1)
695731
return error(_("no reflog specified to delete"));
696732

697733
for ( ; i < argc; i++) {
698734
const char *spec = strstr(argv[i], "@{");
699735
char *ep, *ref;
700736
int recno;
701-
struct expire_reflog_policy_cb cb = { 0 };
737+
struct expire_reflog_policy_cb cb = {
738+
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
739+
};
702740

703741
if (!spec) {
704742
status |= error(_("not a reflog: %s"), argv[i]);
@@ -723,7 +761,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
723761
cb.cmd = cmd;
724762
status |= reflog_expire(ref, flags,
725763
reflog_expiry_prepare,
726-
should_expire_reflog_ent,
764+
should_prune_fn,
727765
reflog_expiry_cleanup,
728766
&cb);
729767
free(ref);

refs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,7 @@ enum ref_type ref_type(const char *refname);
820820
enum expire_reflog_flags {
821821
EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
822822
EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
823-
EXPIRE_REFLOGS_VERBOSE = 1 << 2,
824-
EXPIRE_REFLOGS_REWRITE = 1 << 3
823+
EXPIRE_REFLOGS_REWRITE = 1 << 2,
825824
};
826825

827826
/*

refs/files-backend.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,45 +3086,40 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
30863086
}
30873087

30883088
struct expire_reflog_cb {
3089-
unsigned int flags;
30903089
reflog_expiry_should_prune_fn *should_prune_fn;
30913090
void *policy_cb;
30923091
FILE *newlog;
30933092
struct object_id last_kept_oid;
3093+
unsigned int rewrite:1,
3094+
dry_run:1;
30943095
};
30953096

30963097
static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
30973098
const char *email, timestamp_t timestamp, int tz,
30983099
const char *message, void *cb_data)
30993100
{
31003101
struct expire_reflog_cb *cb = cb_data;
3101-
struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3102+
reflog_expiry_should_prune_fn *fn = cb->should_prune_fn;
31023103

3103-
if (cb->flags & EXPIRE_REFLOGS_REWRITE)
3104+
if (cb->rewrite)
31043105
ooid = &cb->last_kept_oid;
31053106

3106-
if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,
3107-
message, policy_cb)) {
3108-
if (cb->flags & EXPIRE_REFLOGS_DRY_RUN)
3109-
printf("would prune %s", message);
3110-
else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3111-
printf("prune %s", message);
3112-
} else {
3113-
if (!(cb->flags & EXPIRE_REFLOGS_DRY_RUN)) {
3114-
fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
3115-
oid_to_hex(ooid), oid_to_hex(noid),
3116-
email, timestamp, tz, message);
3117-
oidcpy(&cb->last_kept_oid, noid);
3118-
}
3119-
if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3120-
printf("keep %s", message);
3121-
}
3107+
if (fn(ooid, noid, email, timestamp, tz, message, cb->policy_cb))
3108+
return 0;
3109+
3110+
if (cb->dry_run)
3111+
return 0; /* --dry-run */
3112+
3113+
fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", oid_to_hex(ooid),
3114+
oid_to_hex(noid), email, timestamp, tz, message);
3115+
oidcpy(&cb->last_kept_oid, noid);
3116+
31223117
return 0;
31233118
}
31243119

31253120
static int files_reflog_expire(struct ref_store *ref_store,
31263121
const char *refname,
3127-
unsigned int flags,
3122+
unsigned int expire_flags,
31283123
reflog_expiry_prepare_fn prepare_fn,
31293124
reflog_expiry_should_prune_fn should_prune_fn,
31303125
reflog_expiry_cleanup_fn cleanup_fn,
@@ -3142,7 +3137,8 @@ static int files_reflog_expire(struct ref_store *ref_store,
31423137
const struct object_id *oid;
31433138

31443139
memset(&cb, 0, sizeof(cb));
3145-
cb.flags = flags;
3140+
cb.rewrite = !!(expire_flags & EXPIRE_REFLOGS_REWRITE);
3141+
cb.dry_run = !!(expire_flags & EXPIRE_REFLOGS_DRY_RUN);
31463142
cb.policy_cb = policy_cb_data;
31473143
cb.should_prune_fn = should_prune_fn;
31483144

@@ -3178,7 +3174,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
31783174

31793175
files_reflog_path(refs, &log_file_sb, refname);
31803176
log_file = strbuf_detach(&log_file_sb, NULL);
3181-
if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3177+
if (!cb.dry_run) {
31823178
/*
31833179
* Even though holding $GIT_DIR/logs/$reflog.lock has
31843180
* no locking implications, we use the lock_file
@@ -3205,7 +3201,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
32053201
refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
32063202
(*cleanup_fn)(cb.policy_cb);
32073203

3208-
if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3204+
if (!cb.dry_run) {
32093205
/*
32103206
* It doesn't make sense to adjust a reference pointed
32113207
* to by a symbolic ref based on expiring entries in
@@ -3215,7 +3211,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
32153211
*/
32163212
int update = 0;
32173213

3218-
if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3214+
if ((expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
32193215
!is_null_oid(&cb.last_kept_oid)) {
32203216
int ignore_errno;
32213217
int type;

0 commit comments

Comments
 (0)