Skip to content

Commit

Permalink
reflog + refs-backend: move "verbose" out of the backend
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
avar committed Nov 23, 2021
1 parent feba4ab commit eff40d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
42 changes: 31 additions & 11 deletions builtin/reflog.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct expire_reflog_policy_cb {
struct cmd_reflog_expire_cb cmd;
struct commit *tip_commit;
struct commit_list *tips;
unsigned int dry_run:1,
verbose:1;
};

struct worktree_reflogs {
Expand Down Expand Up @@ -294,29 +296,38 @@ static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *no
struct commit *old_commit, *new_commit;

if (timestamp < cb->cmd.expire_total)
return 1;
goto expire;

old_commit = new_commit = NULL;
if (cb->cmd.stalefix &&
(!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid)))
return 1;
goto expire;

if (timestamp < cb->cmd.expire_unreachable) {
switch (cb->unreachable_expire_kind) {
case UE_ALWAYS:
return 1;
goto expire;
case UE_NORMAL:
case UE_HEAD:
if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
return 1;
goto expire;
break;
}
}

if (cb->cmd.recno && --(cb->cmd.recno) == 0)
return 1;
goto expire;

if (cb->verbose)
printf("keep %s", message);

return 0;
expire:
if (cb->dry_run)
printf("would prune %s", message);
else if (cb->verbose)
printf("prune %s", message);
return 1;
}

static int push_tip_to_list(const char *refname, const struct object_id *oid,
Expand Down Expand Up @@ -541,6 +552,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
int i, status, do_all, all_worktrees = 1;
int explicit_expiry = 0;
unsigned int flags = 0;
int verbose = 0;

default_reflog_expire_unreachable = now - 30 * 24 * 3600;
default_reflog_expire = now - 90 * 24 * 3600;
Expand Down Expand Up @@ -578,7 +590,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
else if (!strcmp(arg, "--single-worktree"))
all_worktrees = 0;
else if (!strcmp(arg, "--verbose"))
flags |= EXPIRE_REFLOGS_VERBOSE;
verbose = 1;
else if (!strcmp(arg, "--")) {
i++;
break;
Expand All @@ -601,10 +613,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
revs.do_not_die_on_missing_tree = 1;
revs.ignore_missing = 1;
revs.ignore_missing_links = 1;
if (flags & EXPIRE_REFLOGS_VERBOSE)
if (verbose)
printf(_("Marking reachable objects..."));
mark_reachable_objects(&revs, 0, 0, NULL);
if (flags & EXPIRE_REFLOGS_VERBOSE)
if (verbose)
putchar('\n');
}

Expand All @@ -626,7 +638,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
free_worktrees(worktrees);

for_each_string_list_item(item, &collected.reflogs) {
struct expire_reflog_policy_cb cb = { .cmd = cmd };
struct expire_reflog_policy_cb cb = {
.cmd = cmd,
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
.verbose = verbose,
};

set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string);
status |= reflog_expire(item->string, flags,
Expand Down Expand Up @@ -673,6 +689,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
struct cmd_reflog_expire_cb cmd = { 0 };
int i, status = 0;
unsigned int flags = 0;
int verbose = 0;

for (i = 1; i < argc; i++) {
const char *arg = argv[i];
Expand All @@ -683,7 +700,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
else if (!strcmp(arg, "--updateref"))
flags |= EXPIRE_REFLOGS_UPDATE_REF;
else if (!strcmp(arg, "--verbose"))
flags |= EXPIRE_REFLOGS_VERBOSE;
verbose = 1;
else if (!strcmp(arg, "--")) {
i++;
break;
Expand All @@ -701,7 +718,10 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
const char *spec = strstr(argv[i], "@{");
char *ep, *ref;
int recno;
struct expire_reflog_policy_cb cb = { 0 };
struct expire_reflog_policy_cb cb = {
.verbose = verbose,
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
};

if (!spec) {
status |= error(_("not a reflog: %s"), argv[i]);
Expand Down
3 changes: 1 addition & 2 deletions refs.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,7 @@ enum ref_type ref_type(const char *refname);
enum expire_reflog_flags {
EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
EXPIRE_REFLOGS_VERBOSE = 1 << 2,
EXPIRE_REFLOGS_REWRITE = 1 << 3
EXPIRE_REFLOGS_REWRITE = 1 << 2,
};

/*
Expand Down
44 changes: 20 additions & 24 deletions refs/files-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -3039,45 +3039,40 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
}

struct expire_reflog_cb {
unsigned int flags;
reflog_expiry_should_prune_fn *should_prune_fn;
void *policy_cb;
FILE *newlog;
struct object_id last_kept_oid;
unsigned int rewrite:1,
dry_run:1;
};

static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
const char *email, timestamp_t timestamp, int tz,
const char *message, void *cb_data)
{
struct expire_reflog_cb *cb = cb_data;
struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
reflog_expiry_should_prune_fn *fn = cb->should_prune_fn;

if (cb->flags & EXPIRE_REFLOGS_REWRITE)
if (cb->rewrite)
ooid = &cb->last_kept_oid;

if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,
message, policy_cb)) {
if (cb->flags & EXPIRE_REFLOGS_DRY_RUN)
printf("would prune %s", message);
else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
printf("prune %s", message);
} else {
if (!(cb->flags & EXPIRE_REFLOGS_DRY_RUN)) {
fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
oid_to_hex(ooid), oid_to_hex(noid),
email, timestamp, tz, message);
oidcpy(&cb->last_kept_oid, noid);
}
if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
printf("keep %s", message);
}
if (fn(ooid, noid, email, timestamp, tz, message, cb->policy_cb))
return 0;

if (cb->dry_run)
return 0; /* --dry-run */

fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", oid_to_hex(ooid),
oid_to_hex(noid), email, timestamp, tz, message);
oidcpy(&cb->last_kept_oid, noid);

return 0;
}

static int files_reflog_expire(struct ref_store *ref_store,
const char *refname,
unsigned int flags,
unsigned int expire_flags,
reflog_expiry_prepare_fn prepare_fn,
reflog_expiry_should_prune_fn should_prune_fn,
reflog_expiry_cleanup_fn cleanup_fn,
Expand All @@ -3096,7 +3091,8 @@ static int files_reflog_expire(struct ref_store *ref_store,
const struct object_id *oid;

memset(&cb, 0, sizeof(cb));
cb.flags = flags;
cb.rewrite = !!(expire_flags & EXPIRE_REFLOGS_REWRITE);
cb.dry_run = !!(expire_flags & EXPIRE_REFLOGS_DRY_RUN);
cb.policy_cb = policy_cb_data;
cb.should_prune_fn = should_prune_fn;

Expand Down Expand Up @@ -3132,7 +3128,7 @@ static int files_reflog_expire(struct ref_store *ref_store,

files_reflog_path(refs, &log_file_sb, refname);
log_file = strbuf_detach(&log_file_sb, NULL);
if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
if (!cb.dry_run) {
/*
* Even though holding $GIT_DIR/logs/$reflog.lock has
* no locking implications, we use the lock_file
Expand All @@ -3159,15 +3155,15 @@ static int files_reflog_expire(struct ref_store *ref_store,
refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
(*cleanup_fn)(cb.policy_cb);

if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
if (!cb.dry_run) {
/*
* It doesn't make sense to adjust a reference pointed
* to by a symbolic ref based on expiring entries in
* the symbolic reference's reflog. Nor can we update
* a reference if there are no remaining reflog
* entries.
*/
int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
int update = (expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
!(type & REF_ISSYMREF) &&
!is_null_oid(&cb.last_kept_oid);

Expand Down

0 comments on commit eff40d2

Please sign in to comment.