#include "revision.h"
#include "reachable.h"
-/*
- * reflog expire
- */
-
+/* NEEDSWORK: switch to using parse_options */
static const char reflog_expire_usage[] =
-"git reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
+"git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
static const char reflog_delete_usage[] =
-"git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
+"git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
static unsigned long default_reflog_expire;
static unsigned long default_reflog_expire_unreachable;
-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
-};
-
struct cmd_reflog_expire_cb {
struct rev_info revs;
int stalefix;
};
struct expire_reflog_policy_cb {
- FILE *newlog;
enum {
UE_NORMAL,
UE_ALWAYS,
} unreachable_expire_kind;
struct commit_list *mark_list;
unsigned long mark_limit;
- struct cmd_reflog_expire_cb *cmd;
- unsigned char last_kept_sha1[20];
+ struct cmd_reflog_expire_cb cmd;
struct commit *tip_commit;
struct commit_list *tips;
};
-struct expire_reflog_cb {
- unsigned int flags;
- void *policy_cb;
-};
-
struct collected_reflog {
unsigned char sha1[20];
char reflog[FLEX_ARRAY];
struct expire_reflog_policy_cb *cb = cb_data;
struct commit *old, *new;
- if (timestamp < cb->cmd->expire_total)
+ if (timestamp < cb->cmd.expire_total)
return 1;
old = new = NULL;
- if (cb->cmd->stalefix &&
+ if (cb->cmd.stalefix &&
(!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
return 1;
- if (timestamp < cb->cmd->expire_unreachable) {
+ if (timestamp < cb->cmd.expire_unreachable) {
if (cb->unreachable_expire_kind == UE_ALWAYS)
return 1;
if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
return 1;
}
- if (cb->cmd->recno && --(cb->cmd->recno) == 0)
+ if (cb->cmd.recno && --(cb->cmd.recno) == 0)
return 1;
return 0;
}
-static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
- const char *email, unsigned long 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;
-
- if (cb->flags & EXPIRE_REFLOGS_REWRITE)
- osha1 = policy_cb->last_kept_sha1;
-
- if (should_expire_reflog_ent(osha1, nsha1, email, timestamp, tz,
- message, policy_cb)) {
- if (!policy_cb->newlog)
- printf("would prune %s", message);
- else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
- printf("prune %s", message);
- } else {
- if (policy_cb->newlog) {
- char sign = (tz < 0) ? '-' : '+';
- int zone = (tz < 0) ? (-tz) : tz;
- fprintf(policy_cb->newlog, "%s %s %s %lu %c%04d\t%s",
- sha1_to_hex(osha1), sha1_to_hex(nsha1),
- email, timestamp, sign, zone,
- message);
- hashcpy(policy_cb->last_kept_sha1, nsha1);
- }
- if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
- printf("keep %s", message);
- }
- return 0;
-}
-
static int push_tip_to_list(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
{
static void reflog_expiry_prepare(const char *refname,
const unsigned char *sha1,
- struct expire_reflog_policy_cb *cb)
+ void *cb_data)
{
- if (!cb->cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
+ struct expire_reflog_policy_cb *cb = cb_data;
+
+ if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
cb->tip_commit = NULL;
cb->unreachable_expire_kind = UE_HEAD;
} else {
cb->unreachable_expire_kind = UE_NORMAL;
}
- if (cb->cmd->expire_unreachable <= cb->cmd->expire_total)
+ if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
cb->unreachable_expire_kind = UE_ALWAYS;
cb->mark_list = NULL;
} else {
commit_list_insert(cb->tip_commit, &cb->mark_list);
}
- cb->mark_limit = cb->cmd->expire_total;
+ cb->mark_limit = cb->cmd.expire_total;
mark_reachable(cb);
}
}
-static void reflog_expiry_cleanup(struct expire_reflog_policy_cb *cb)
+static void reflog_expiry_cleanup(void *cb_data)
{
+ struct expire_reflog_policy_cb *cb = cb_data;
+
if (cb->unreachable_expire_kind != UE_ALWAYS) {
if (cb->unreachable_expire_kind == UE_HEAD) {
struct commit_list *elem;
}
}
-static int expire_reflog(const char *refname, const unsigned char *sha1,
- unsigned int flags, struct cmd_reflog_expire_cb *cmd)
-{
- static struct lock_file reflog_lock;
- struct expire_reflog_cb cb;
- struct expire_reflog_policy_cb policy_cb;
- struct ref_lock *lock;
- char *log_file;
- int status = 0;
-
- memset(&cb, 0, sizeof(cb));
- memset(&policy_cb, 0, sizeof(policy_cb));
- cb.flags = flags;
- cb.policy_cb = &policy_cb;
-
- /*
- * The reflog file is locked by holding the lock on the
- * reference itself, plus we might need to update the
- * reference if --updateref was specified:
- */
- lock = lock_any_ref_for_update(refname, sha1, 0, NULL);
- if (!lock)
- return error("cannot lock ref '%s'", refname);
- if (!reflog_exists(refname)) {
- unlock_ref(lock);
- return 0;
- }
-
- log_file = git_pathdup("logs/%s", refname);
- if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
- /*
- * Even though holding $GIT_DIR/logs/$reflog.lock has
- * no locking implications, we use the lock_file
- * machinery here anyway because it does a lot of the
- * work we need, including cleaning up if the program
- * exits unexpectedly.
- */
- if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
- struct strbuf err = STRBUF_INIT;
- unable_to_lock_message(log_file, errno, &err);
- error("%s", err.buf);
- strbuf_release(&err);
- goto failure;
- }
- policy_cb.newlog = fdopen_lock_file(&reflog_lock, "w");
- if (!policy_cb.newlog) {
- error("cannot fdopen %s (%s)",
- reflog_lock.filename.buf, strerror(errno));
- goto failure;
- }
- }
-
- policy_cb.cmd = cmd;
-
- reflog_expiry_prepare(refname, sha1, &policy_cb);
- for_each_reflog_ent(refname, expire_reflog_ent, &cb);
- reflog_expiry_cleanup(&policy_cb);
-
- if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
- if (close_lock_file(&reflog_lock)) {
- status |= error("couldn't write %s: %s", log_file,
- strerror(errno));
- } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
- (write_in_full(lock->lock_fd,
- sha1_to_hex(policy_cb.last_kept_sha1), 40) != 40 ||
- write_str_in_full(lock->lock_fd, "\n") != 1 ||
- close_ref(lock) < 0)) {
- status |= error("couldn't write %s",
- lock->lk->filename.buf);
- rollback_lock_file(&reflog_lock);
- } else if (commit_lock_file(&reflog_lock)) {
- status |= error("unable to commit reflog '%s' (%s)",
- log_file, strerror(errno));
- } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) && commit_ref(lock)) {
- status |= error("couldn't set %s", lock->ref_name);
- }
- }
- free(log_file);
- unlock_ref(lock);
- return status;
-
- failure:
- rollback_lock_file(&reflog_lock);
- free(log_file);
- unlock_ref(lock);
- return -1;
-}
-
static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
{
struct collected_reflog *e;
static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
{
- struct cmd_reflog_expire_cb cb;
+ struct expire_reflog_policy_cb cb;
unsigned long now = time(NULL);
int i, status, do_all;
int explicit_expiry = 0;
do_all = status = 0;
memset(&cb, 0, sizeof(cb));
- cb.expire_total = default_reflog_expire;
- cb.expire_unreachable = default_reflog_expire_unreachable;
+ cb.cmd.expire_total = default_reflog_expire;
+ cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
flags |= EXPIRE_REFLOGS_DRY_RUN;
else if (starts_with(arg, "--expire=")) {
- if (parse_expiry_date(arg + 9, &cb.expire_total))
+ if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
die(_("'%s' is not a valid timestamp"), arg);
explicit_expiry |= EXPIRE_TOTAL;
}
else if (starts_with(arg, "--expire-unreachable=")) {
- if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
+ if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
die(_("'%s' is not a valid timestamp"), arg);
explicit_expiry |= EXPIRE_UNREACH;
}
else if (!strcmp(arg, "--stale-fix"))
- cb.stalefix = 1;
+ cb.cmd.stalefix = 1;
else if (!strcmp(arg, "--rewrite"))
flags |= EXPIRE_REFLOGS_REWRITE;
else if (!strcmp(arg, "--updateref"))
* even in older repository. We cannot trust what's reachable
* from reflog if the repository was pruned with older git.
*/
- if (cb.stalefix) {
- init_revisions(&cb.revs, prefix);
+ if (cb.cmd.stalefix) {
+ init_revisions(&cb.cmd.revs, prefix);
if (flags & EXPIRE_REFLOGS_VERBOSE)
printf("Marking reachable objects...");
- mark_reachable_objects(&cb.revs, 0, 0, NULL);
+ mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
if (flags & EXPIRE_REFLOGS_VERBOSE)
putchar('\n');
}
for_each_reflog(collect_reflog, &collected);
for (i = 0; i < collected.nr; i++) {
struct collected_reflog *e = collected.e[i];
- set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
- status |= expire_reflog(e->reflog, e->sha1, flags, &cb);
+ set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
+ status |= reflog_expire(e->reflog, e->sha1, flags,
+ reflog_expiry_prepare,
+ should_expire_reflog_ent,
+ reflog_expiry_cleanup,
+ &cb);
free(e);
}
free(collected.e);
status |= error("%s points nowhere!", argv[i]);
continue;
}
- set_reflog_expiry_param(&cb, explicit_expiry, ref);
- status |= expire_reflog(ref, sha1, flags, &cb);
+ set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
+ status |= reflog_expire(ref, sha1, flags,
+ reflog_expiry_prepare,
+ should_expire_reflog_ent,
+ reflog_expiry_cleanup,
+ &cb);
}
return status;
}
const char *email, unsigned long timestamp, int tz,
const char *message, void *cb_data)
{
- struct cmd_reflog_expire_cb *cb = cb_data;
- if (!cb->expire_total || timestamp < cb->expire_total)
- cb->recno++;
+ struct expire_reflog_policy_cb *cb = cb_data;
+ if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
+ cb->cmd.recno++;
return 0;
}
static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
{
- struct cmd_reflog_expire_cb cb;
+ struct expire_reflog_policy_cb cb;
int i, status = 0;
unsigned int flags = 0;
recno = strtoul(spec + 2, &ep, 10);
if (*ep == '}') {
- cb.recno = -recno;
+ cb.cmd.recno = -recno;
for_each_reflog_ent(ref, count_reflog_ent, &cb);
} else {
- cb.expire_total = approxidate(spec + 2);
+ cb.cmd.expire_total = approxidate(spec + 2);
for_each_reflog_ent(ref, count_reflog_ent, &cb);
- cb.expire_total = 0;
+ cb.cmd.expire_total = 0;
}
- status |= expire_reflog(ref, sha1, flags, &cb);
+ status |= reflog_expire(ref, sha1, flags,
+ reflog_expiry_prepare,
+ should_expire_reflog_ent,
+ reflog_expiry_cleanup,
+ &cb);
free(ref);
}
return status;