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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Dec 22, 2021 at 05:06 UTC
fcd2c3d9d854712e7fbb8e7be5a809029aab0a84
3 files changed
+68
-35
builtin/reflog.c
+47
-9
@@ -45,6 +45,7 @@ struct expire_reflog_policy_cb {
45
struct cmd_reflog_expire_cb cmd;
46
struct commit *tip_commit;
47
struct commit_list *tips;
48
+ unsigned int dry_run:1;
49
};
50
51
struct worktree_reflogs {
@@ -319,6 +320,28 @@ static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *no
320
return 0;
321
}
322
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
+
345
static int push_tip_to_list(const char *refname, const struct object_id *oid,
346
int flags, void *cb_data)
347
{
@@ -539,6 +562,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
562
int i, status, do_all, all_worktrees = 1;
563
int explicit_expiry = 0;
564
unsigned int flags = 0;
565
+ int verbose = 0;
566
+ reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
567
568
default_reflog_expire_unreachable = now - 30 * 24 * 3600;
569
default_reflog_expire = now - 90 * 24 * 3600;
@@ -576,7 +601,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
601
else if (!strcmp(arg, "--single-worktree"))
602
all_worktrees = 0;
603
else if (!strcmp(arg, "--verbose"))
579
- flags |= EXPIRE_REFLOGS_VERBOSE;
604
+ verbose = 1;
605
else if (!strcmp(arg, "--")) {
606
i++;
607
break;
@@ -587,6 +612,9 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
612
break;
613
}
614
615
+ if (verbose)
616
+ should_prune_fn = should_expire_reflog_ent_verbose;
617
+
618
/*
619
* We can trust the commits and objects reachable from refs
620
* 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)
627
revs.do_not_die_on_missing_tree = 1;
628
revs.ignore_missing = 1;
629
revs.ignore_missing_links = 1;
602
- if (flags & EXPIRE_REFLOGS_VERBOSE)
630
+ if (verbose)
631
printf(_("Marking reachable objects..."));
632
mark_reachable_objects(&revs, 0, 0, NULL);
605
- if (flags & EXPIRE_REFLOGS_VERBOSE)
633
+ if (verbose)
634
putchar('\n');
635
}
636
@@ -624,12 +652,15 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
652
free_worktrees(worktrees);
653
654
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
+ };
659
660
set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string);
661
status |= reflog_expire(item->string, flags,
662
reflog_expiry_prepare,
632
- should_expire_reflog_ent,
663
+ should_prune_fn,
664
reflog_expiry_cleanup,
665
&cb);
666
}
@@ -647,7 +678,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
678
set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
679
status |= reflog_expire(ref, flags,
680
reflog_expiry_prepare,
650
- should_expire_reflog_ent,
681
+ should_prune_fn,
682
reflog_expiry_cleanup,
683
&cb);
684
free(ref);
@@ -670,6 +701,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
701
struct cmd_reflog_expire_cb cmd = { 0 };
702
int i, status = 0;
703
unsigned int flags = 0;
704
+ int verbose = 0;
705
+ reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
706
707
for (i = 1; i < argc; i++) {
708
const char *arg = argv[i];
@@ -680,7 +713,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
713
else if (!strcmp(arg, "--updateref"))
714
flags |= EXPIRE_REFLOGS_UPDATE_REF;
715
else if (!strcmp(arg, "--verbose"))
683
- flags |= EXPIRE_REFLOGS_VERBOSE;
716
+ verbose = 1;
717
else if (!strcmp(arg, "--")) {
718
i++;
719
break;
@@ -691,6 +724,9 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
724
break;
725
}
726
727
+ if (verbose)
728
+ should_prune_fn = should_expire_reflog_ent_verbose;
729
+
730
if (argc - i < 1)
731
return error(_("no reflog specified to delete"));
732
@@ -698,7 +734,9 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
734
const char *spec = strstr(argv[i], "@{");
735
char *ep, *ref;
736
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
+ };
740
741
if (!spec) {
742
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)
761
cb.cmd = cmd;
762
status |= reflog_expire(ref, flags,
763
reflog_expiry_prepare,
726
- should_expire_reflog_ent,
764
+ should_prune_fn,
765
reflog_expiry_cleanup,
766
&cb);
767
free(ref);
refs.h
+1
-2
@@ -820,8 +820,7 @@ enum ref_type ref_type(const char *refname);
820
enum expire_reflog_flags {
821
EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
822
EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
823
- EXPIRE_REFLOGS_VERBOSE = 1 << 2,
824
- EXPIRE_REFLOGS_REWRITE = 1 << 3
823
+ EXPIRE_REFLOGS_REWRITE = 1 << 2,
824
};
825
826
/*
refs/files-backend.c
+20
-24
@@ -3086,11 +3086,12 @@ cleanup:
3086
}
3087
3088
struct expire_reflog_cb {
3089
- unsigned int flags;
3089
reflog_expiry_should_prune_fn *should_prune_fn;
3090
void *policy_cb;
3091
FILE *newlog;
3092
struct object_id last_kept_oid;
3093
+ unsigned int rewrite:1,
3094
+ dry_run:1;
3095
};
3096
3097
static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
@@ -3098,33 +3099,27 @@ static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
3099
const char *message, void *cb_data)
3100
{
3101
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;
3103
3103
- if (cb->flags & EXPIRE_REFLOGS_REWRITE)
3104
+ if (cb->rewrite)
3105
ooid = &cb->last_kept_oid;
3106
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
+
3117
return 0;
3118
}
3119
3120
static int files_reflog_expire(struct ref_store *ref_store,
3121
const char *refname,
3127
- unsigned int flags,
3122
+ unsigned int expire_flags,
3123
reflog_expiry_prepare_fn prepare_fn,
3124
reflog_expiry_should_prune_fn should_prune_fn,
3125
reflog_expiry_cleanup_fn cleanup_fn,
@@ -3142,7 +3137,8 @@ static int files_reflog_expire(struct ref_store *ref_store,
3137
const struct object_id *oid;
3138
3139
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);
3142
cb.policy_cb = policy_cb_data;
3143
cb.should_prune_fn = should_prune_fn;
3144
@@ -3178,7 +3174,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
3174
3175
files_reflog_path(refs, &log_file_sb, refname);
3176
log_file = strbuf_detach(&log_file_sb, NULL);
3181
- if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3177
+ if (!cb.dry_run) {
3178
/*
3179
* Even though holding $GIT_DIR/logs/$reflog.lock has
3180
* no locking implications, we use the lock_file
@@ -3205,7 +3201,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
3201
refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
3202
(*cleanup_fn)(cb.policy_cb);
3203
3208
- if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3204
+ if (!cb.dry_run) {
3205
/*
3206
* It doesn't make sense to adjust a reference pointed
3207
* to by a symbolic ref based on expiring entries in
@@ -3215,7 +3211,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
3211
*/
3212
int update = 0;
3213
3218
- if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3214
+ if ((expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3215
!is_null_oid(&cb.last_kept_oid)) {
3216
int ignore_errno;
3217
int type;