Merge branch 'ab/reflog-prep' into jc/reflog-parse-options

* ab/reflog-prep:
  reflog + refs-backend: move "verbose" out of the backend
  refs files-backend: assume cb->newlog if !EXPIRE_REFLOGS_DRY_RUN
  reflog: reduce scope of "struct rev_info"
  reflog expire: don't use lookup_commit_reference_gently()
  reflog expire: refactor & use "tip_commit" only for UE_NORMAL
  reflog expire: use "switch" over enum values
  reflog: change one->many worktree->refnames to use a string_list
  reflog expire: narrow scope of "cb" in cmd_reflog_expire()
  reflog delete: narrow scope of "cmd" passed to count_reflog_ent()
This commit is contained in:
Junio C Hamano 2022-01-04 13:56:32 -08:00
Родитель dcc0cd074f fcd2c3d9d8
Коммит e391a45102
3 изменённых файлов: 150 добавлений и 120 удалений

Просмотреть файл

@ -28,7 +28,6 @@ static timestamp_t default_reflog_expire;
static timestamp_t default_reflog_expire_unreachable;
struct cmd_reflog_expire_cb {
struct rev_info revs;
int stalefix;
timestamp_t expire_total;
timestamp_t expire_unreachable;
@ -46,18 +45,12 @@ struct expire_reflog_policy_cb {
struct cmd_reflog_expire_cb cmd;
struct commit *tip_commit;
struct commit_list *tips;
unsigned int dry_run:1;
};
struct collected_reflog {
struct object_id oid;
char reflog[FLEX_ARRAY];
};
struct collect_reflog_cb {
struct collected_reflog **e;
int alloc;
int nr;
struct worktree *wt;
struct worktree_reflogs {
struct worktree *worktree;
struct string_list reflogs;
};
/* Remember to update object flag allocation in object.h */
@ -310,10 +303,15 @@ static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *no
return 1;
if (timestamp < cb->cmd.expire_unreachable) {
if (cb->unreachable_expire_kind == UE_ALWAYS)
return 1;
if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
switch (cb->unreachable_expire_kind) {
case UE_ALWAYS:
return 1;
case UE_NORMAL:
case UE_HEAD:
if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
return 1;
break;
}
}
if (cb->cmd.recno && --(cb->cmd.recno) == 0)
@ -322,6 +320,28 @@ static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *no
return 0;
}
static int should_expire_reflog_ent_verbose(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_policy_cb *cb = cb_data;
int expire;
expire = should_expire_reflog_ent(ooid, noid, email, timestamp, tz,
message, cb);
if (!expire)
printf("keep %s", message);
else if (cb->dry_run)
printf("would prune %s", message);
else
printf("prune %s", message);
return expire;
}
static int push_tip_to_list(const char *refname, const struct object_id *oid,
int flags, void *cb_data)
{
@ -355,75 +375,71 @@ static void reflog_expiry_prepare(const char *refname,
void *cb_data)
{
struct expire_reflog_policy_cb *cb = cb_data;
struct commit_list *elem;
struct commit *commit = NULL;
if (!cb->cmd.expire_unreachable || is_head(refname)) {
cb->tip_commit = NULL;
cb->unreachable_expire_kind = UE_HEAD;
} else {
cb->tip_commit = lookup_commit_reference_gently(the_repository,
oid, 1);
if (!cb->tip_commit)
cb->unreachable_expire_kind = UE_ALWAYS;
else
cb->unreachable_expire_kind = UE_NORMAL;
commit = lookup_commit(the_repository, oid);
cb->unreachable_expire_kind = commit ? UE_NORMAL : UE_ALWAYS;
}
if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
cb->unreachable_expire_kind = UE_ALWAYS;
cb->mark_list = NULL;
cb->tips = NULL;
if (cb->unreachable_expire_kind != UE_ALWAYS) {
if (cb->unreachable_expire_kind == UE_HEAD) {
struct commit_list *elem;
for_each_ref(push_tip_to_list, &cb->tips);
for (elem = cb->tips; elem; elem = elem->next)
commit_list_insert(elem->item, &cb->mark_list);
} else {
commit_list_insert(cb->tip_commit, &cb->mark_list);
}
cb->mark_limit = cb->cmd.expire_total;
mark_reachable(cb);
switch (cb->unreachable_expire_kind) {
case UE_ALWAYS:
return;
case UE_HEAD:
for_each_ref(push_tip_to_list, &cb->tips);
for (elem = cb->tips; elem; elem = elem->next)
commit_list_insert(elem->item, &cb->mark_list);
break;
case UE_NORMAL:
commit_list_insert(commit, &cb->mark_list);
/* For reflog_expiry_cleanup() below */
cb->tip_commit = commit;
}
cb->mark_limit = cb->cmd.expire_total;
mark_reachable(cb);
}
static void reflog_expiry_cleanup(void *cb_data)
{
struct expire_reflog_policy_cb *cb = cb_data;
struct commit_list *elem;
if (cb->unreachable_expire_kind != UE_ALWAYS) {
if (cb->unreachable_expire_kind == UE_HEAD) {
struct commit_list *elem;
for (elem = cb->tips; elem; elem = elem->next)
clear_commit_marks(elem->item, REACHABLE);
free_commit_list(cb->tips);
} else {
clear_commit_marks(cb->tip_commit, REACHABLE);
}
switch (cb->unreachable_expire_kind) {
case UE_ALWAYS:
return;
case UE_HEAD:
for (elem = cb->tips; elem; elem = elem->next)
clear_commit_marks(elem->item, REACHABLE);
free_commit_list(cb->tips);
break;
case UE_NORMAL:
clear_commit_marks(cb->tip_commit, REACHABLE);
break;
}
}
static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
{
struct collected_reflog *e;
struct collect_reflog_cb *cb = cb_data;
struct worktree_reflogs *cb = cb_data;
struct worktree *worktree = cb->worktree;
struct strbuf newref = STRBUF_INIT;
/*
* Avoid collecting the same shared ref multiple times because
* they are available via all worktrees.
*/
if (!cb->wt->is_current && ref_type(ref) == REF_TYPE_NORMAL)
if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
return 0;
strbuf_worktree_ref(cb->wt, &newref, ref);
FLEX_ALLOC_STR(e, reflog, newref.buf);
strbuf_release(&newref);
strbuf_worktree_ref(worktree, &newref, ref);
string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
oidcpy(&e->oid, oid);
ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
cb->e[cb->nr++] = e;
return 0;
}
@ -541,11 +557,13 @@ static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, c
static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
{
struct expire_reflog_policy_cb cb;
struct cmd_reflog_expire_cb cmd = { 0 };
timestamp_t now = time(NULL);
int i, status, do_all, all_worktrees = 1;
int explicit_expiry = 0;
unsigned int flags = 0;
int verbose = 0;
reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
default_reflog_expire_unreachable = now - 30 * 24 * 3600;
default_reflog_expire = now - 90 * 24 * 3600;
@ -553,10 +571,9 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
save_commit_buffer = 0;
do_all = status = 0;
memset(&cb, 0, sizeof(cb));
cb.cmd.expire_total = default_reflog_expire;
cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
cmd.expire_total = default_reflog_expire;
cmd.expire_unreachable = default_reflog_expire_unreachable;
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
@ -564,17 +581,17 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
flags |= EXPIRE_REFLOGS_DRY_RUN;
else if (skip_prefix(arg, "--expire=", &arg)) {
if (parse_expiry_date(arg, &cb.cmd.expire_total))
if (parse_expiry_date(arg, &cmd.expire_total))
die(_("'%s' is not a valid timestamp"), arg);
explicit_expiry |= EXPIRE_TOTAL;
}
else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
if (parse_expiry_date(arg, &cmd.expire_unreachable))
die(_("'%s' is not a valid timestamp"), arg);
explicit_expiry |= EXPIRE_UNREACH;
}
else if (!strcmp(arg, "--stale-fix"))
cb.cmd.stalefix = 1;
cmd.stalefix = 1;
else if (!strcmp(arg, "--rewrite"))
flags |= EXPIRE_REFLOGS_REWRITE;
else if (!strcmp(arg, "--updateref"))
@ -584,7 +601,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;
@ -595,54 +612,65 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
break;
}
if (verbose)
should_prune_fn = should_expire_reflog_ent_verbose;
/*
* We can trust the commits and objects reachable from refs
* even in older repository. We cannot trust what's reachable
* from reflog if the repository was pruned with older git.
*/
if (cb.cmd.stalefix) {
repo_init_revisions(the_repository, &cb.cmd.revs, prefix);
cb.cmd.revs.do_not_die_on_missing_tree = 1;
cb.cmd.revs.ignore_missing = 1;
cb.cmd.revs.ignore_missing_links = 1;
if (flags & EXPIRE_REFLOGS_VERBOSE)
if (cmd.stalefix) {
struct rev_info revs;
repo_init_revisions(the_repository, &revs, prefix);
revs.do_not_die_on_missing_tree = 1;
revs.ignore_missing = 1;
revs.ignore_missing_links = 1;
if (verbose)
printf(_("Marking reachable objects..."));
mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
if (flags & EXPIRE_REFLOGS_VERBOSE)
mark_reachable_objects(&revs, 0, 0, NULL);
if (verbose)
putchar('\n');
}
if (do_all) {
struct collect_reflog_cb collected;
struct worktree_reflogs collected = {
.reflogs = STRING_LIST_INIT_DUP,
};
struct string_list_item *item;
struct worktree **worktrees, **p;
int i;
memset(&collected, 0, sizeof(collected));
worktrees = get_worktrees();
for (p = worktrees; *p; p++) {
if (!all_worktrees && !(*p)->is_current)
continue;
collected.wt = *p;
collected.worktree = *p;
refs_for_each_reflog(get_worktree_ref_store(*p),
collect_reflog, &collected);
}
free_worktrees(worktrees);
for (i = 0; i < collected.nr; i++) {
struct collected_reflog *e = collected.e[i];
set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
status |= reflog_expire(e->reflog, flags,
for_each_string_list_item(item, &collected.reflogs) {
struct expire_reflog_policy_cb cb = {
.cmd = cmd,
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
};
set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string);
status |= reflog_expire(item->string, flags,
reflog_expiry_prepare,
should_expire_reflog_ent,
should_prune_fn,
reflog_expiry_cleanup,
&cb);
free(e);
}
free(collected.e);
string_list_clear(&collected.reflogs, 0);
}
for (; i < argc; i++) {
char *ref;
struct expire_reflog_policy_cb cb = { .cmd = cmd };
if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
status |= error(_("%s points nowhere!"), argv[i]);
continue;
@ -650,7 +678,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
status |= reflog_expire(ref, flags,
reflog_expiry_prepare,
should_expire_reflog_ent,
should_prune_fn,
reflog_expiry_cleanup,
&cb);
free(ref);
@ -662,19 +690,19 @@ static int count_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_policy_cb *cb = cb_data;
if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
cb->cmd.recno++;
struct cmd_reflog_expire_cb *cb = cb_data;
if (!cb->expire_total || timestamp < cb->expire_total)
cb->recno++;
return 0;
}
static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
{
struct expire_reflog_policy_cb cb;
struct cmd_reflog_expire_cb cmd = { 0 };
int i, status = 0;
unsigned int flags = 0;
memset(&cb, 0, sizeof(cb));
int verbose = 0;
reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
@ -685,7 +713,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;
@ -696,6 +724,9 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
break;
}
if (verbose)
should_prune_fn = should_expire_reflog_ent_verbose;
if (argc - i < 1)
return error(_("no reflog specified to delete"));
@ -703,6 +734,9 @@ 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 = {
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
};
if (!spec) {
status |= error(_("not a reflog: %s"), argv[i]);
@ -716,17 +750,18 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
recno = strtoul(spec + 2, &ep, 10);
if (*ep == '}') {
cb.cmd.recno = -recno;
for_each_reflog_ent(ref, count_reflog_ent, &cb);
cmd.recno = -recno;
for_each_reflog_ent(ref, count_reflog_ent, &cmd);
} else {
cb.cmd.expire_total = approxidate(spec + 2);
for_each_reflog_ent(ref, count_reflog_ent, &cb);
cb.cmd.expire_total = 0;
cmd.expire_total = approxidate(spec + 2);
for_each_reflog_ent(ref, count_reflog_ent, &cmd);
cmd.expire_total = 0;
}
cb.cmd = cmd;
status |= reflog_expire(ref, flags,
reflog_expiry_prepare,
should_expire_reflog_ent,
should_prune_fn,
reflog_expiry_cleanup,
&cb);
free(ref);

3
refs.h
Просмотреть файл

@ -820,8 +820,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,
};
/*

Просмотреть файл

@ -3086,11 +3086,12 @@ cleanup:
}
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,
@ -3098,33 +3099,27 @@ static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
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->newlog)
printf("would prune %s", message);
else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
printf("prune %s", message);
} else {
if (cb->newlog) {
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,
@ -3142,7 +3137,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;
@ -3178,7 +3174,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
@ -3205,7 +3201,7 @@ 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
@ -3215,7 +3211,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
*/
int update = 0;
if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
if ((expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
!is_null_oid(&cb.last_kept_oid)) {
int ignore_errno;
int type;