expire_reflog(): treat the policy callback data as opaque

Now that expire_reflog() doesn't actually look in the expire_reflog_policy_cb data structure, we can make it opaque: * Change the callers of expire_reflog() to pass it a pointer to an entire "struct expire_reflog_policy_cb" rather than a pointer to a "struct cmd_reflog_expire_cb". * Change expire_reflog() to accept the argument as a "void *" and simply pass it through to the policy functions. * Change the policy functions, reflog_expiry_prepare(), reflog_expiry_cleanup(), and should_expire_reflog_ent(), to accept "void *cb_data" arguments and cast them back to "struct expire_reflog_policy_cb" internally. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Dec 12, 2014 at 09:56 UTC b729effbdbc9ae2d087a83b9e2a165fb8cc04641
1 file changed +36 -36
builtin/reflog.c
+36 -36
@@ -43,7 +43,7 @@ struct expire_reflog_policy_cb {
43 } unreachable_expire_kind;
44 struct commit_list *mark_list;
45 unsigned long mark_limit;
46 - struct cmd_reflog_expire_cb *cmd;
46 + struct cmd_reflog_expire_cb cmd;
47 struct commit *tip_commit;
48 struct commit_list *tips;
49 };
@@ -309,22 +309,22 @@ static int should_expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
309 struct expire_reflog_policy_cb *cb = cb_data;
310 struct commit *old, *new;
311
312 - if (timestamp < cb->cmd->expire_total)
312 + if (timestamp < cb->cmd.expire_total)
313 return 1;
314
315 old = new = NULL;
316 - if (cb->cmd->stalefix &&
316 + if (cb->cmd.stalefix &&
317 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
318 return 1;
319
320 - if (timestamp < cb->cmd->expire_unreachable) {
320 + if (timestamp < cb->cmd.expire_unreachable) {
321 if (cb->unreachable_expire_kind == UE_ALWAYS)
322 return 1;
323 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
324 return 1;
325 }
326
327 - if (cb->cmd->recno && --(cb->cmd->recno) == 0)
327 + if (cb->cmd.recno && --(cb->cmd.recno) == 0)
328 return 1;
329
330 return 0;
@@ -378,9 +378,11 @@ static int push_tip_to_list(const char *refname, const unsigned char *sha1,
378
379 static void reflog_expiry_prepare(const char *refname,
380 const unsigned char *sha1,
381 - struct expire_reflog_policy_cb *cb)
381 + void *cb_data)
382 {
383 - if (!cb->cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
383 + struct expire_reflog_policy_cb *cb = cb_data;
384 +
385 + if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
386 cb->tip_commit = NULL;
387 cb->unreachable_expire_kind = UE_HEAD;
388 } else {
@@ -391,7 +393,7 @@ static void reflog_expiry_prepare(const char *refname,
393 cb->unreachable_expire_kind = UE_NORMAL;
394 }
395
394 - if (cb->cmd->expire_unreachable <= cb->cmd->expire_total)
396 + if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
397 cb->unreachable_expire_kind = UE_ALWAYS;
398
399 cb->mark_list = NULL;
@@ -405,13 +407,15 @@ static void reflog_expiry_prepare(const char *refname,
407 } else {
408 commit_list_insert(cb->tip_commit, &cb->mark_list);
409 }
408 - cb->mark_limit = cb->cmd->expire_total;
410 + cb->mark_limit = cb->cmd.expire_total;
411 mark_reachable(cb);
412 }
413 }
414
413 -static void reflog_expiry_cleanup(struct expire_reflog_policy_cb *cb)
415 +static void reflog_expiry_cleanup(void *cb_data)
416 {
417 + struct expire_reflog_policy_cb *cb = cb_data;
418 +
419 if (cb->unreachable_expire_kind != UE_ALWAYS) {
420 if (cb->unreachable_expire_kind == UE_HEAD) {
421 struct commit_list *elem;
@@ -425,19 +429,17 @@ static void reflog_expiry_cleanup(struct expire_reflog_policy_cb *cb)
429 }
430
431 static int expire_reflog(const char *refname, const unsigned char *sha1,
428 - unsigned int flags, struct cmd_reflog_expire_cb *cmd)
432 + unsigned int flags, void *policy_cb_data)
433 {
434 static struct lock_file reflog_lock;
435 struct expire_reflog_cb cb;
432 - struct expire_reflog_policy_cb policy_cb;
436 struct ref_lock *lock;
437 char *log_file;
438 int status = 0;
439
440 memset(&cb, 0, sizeof(cb));
438 - memset(&policy_cb, 0, sizeof(policy_cb));
441 cb.flags = flags;
440 - cb.policy_cb = &policy_cb;
442 + cb.policy_cb = policy_cb_data;
443
444 /*
445 * The reflog file is locked by holding the lock on the
@@ -476,11 +478,9 @@ static int expire_reflog(const char *refname, const unsigned char *sha1,
478 }
479 }
480
479 - policy_cb.cmd = cmd;
480 -
481 - reflog_expiry_prepare(refname, sha1, &policy_cb);
481 + reflog_expiry_prepare(refname, sha1, cb.policy_cb);
482 for_each_reflog_ent(refname, expire_reflog_ent, &cb);
483 - reflog_expiry_cleanup(&policy_cb);
483 + reflog_expiry_cleanup(cb.policy_cb);
484
485 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
486 if (close_lock_file(&reflog_lock)) {
@@ -653,7 +653,7 @@ static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, c
653
654 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
655 {
656 - struct cmd_reflog_expire_cb cb;
656 + struct expire_reflog_policy_cb cb;
657 unsigned long now = time(NULL);
658 int i, status, do_all;
659 int explicit_expiry = 0;
@@ -667,25 +667,25 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
667 do_all = status = 0;
668 memset(&cb, 0, sizeof(cb));
669
670 - cb.expire_total = default_reflog_expire;
671 - cb.expire_unreachable = default_reflog_expire_unreachable;
670 + cb.cmd.expire_total = default_reflog_expire;
671 + cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
672
673 for (i = 1; i < argc; i++) {
674 const char *arg = argv[i];
675 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
676 flags |= EXPIRE_REFLOGS_DRY_RUN;
677 else if (starts_with(arg, "--expire=")) {
678 - if (parse_expiry_date(arg + 9, &cb.expire_total))
678 + if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
679 die(_("'%s' is not a valid timestamp"), arg);
680 explicit_expiry |= EXPIRE_TOTAL;
681 }
682 else if (starts_with(arg, "--expire-unreachable=")) {
683 - if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
683 + if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
684 die(_("'%s' is not a valid timestamp"), arg);
685 explicit_expiry |= EXPIRE_UNREACH;
686 }
687 else if (!strcmp(arg, "--stale-fix"))
688 - cb.stalefix = 1;
688 + cb.cmd.stalefix = 1;
689 else if (!strcmp(arg, "--rewrite"))
690 flags |= EXPIRE_REFLOGS_REWRITE;
691 else if (!strcmp(arg, "--updateref"))
@@ -709,11 +709,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
709 * even in older repository. We cannot trust what's reachable
710 * from reflog if the repository was pruned with older git.
711 */
712 - if (cb.stalefix) {
713 - init_revisions(&cb.revs, prefix);
712 + if (cb.cmd.stalefix) {
713 + init_revisions(&cb.cmd.revs, prefix);
714 if (flags & EXPIRE_REFLOGS_VERBOSE)
715 printf("Marking reachable objects...");
716 - mark_reachable_objects(&cb.revs, 0, 0, NULL);
716 + mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
717 if (flags & EXPIRE_REFLOGS_VERBOSE)
718 putchar('\n');
719 }
@@ -726,7 +726,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
726 for_each_reflog(collect_reflog, &collected);
727 for (i = 0; i < collected.nr; i++) {
728 struct collected_reflog *e = collected.e[i];
729 - set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
729 + set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
730 status |= expire_reflog(e->reflog, e->sha1, flags, &cb);
731 free(e);
732 }
@@ -740,7 +740,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
740 status |= error("%s points nowhere!", argv[i]);
741 continue;
742 }
743 - set_reflog_expiry_param(&cb, explicit_expiry, ref);
743 + set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
744 status |= expire_reflog(ref, sha1, flags, &cb);
745 }
746 return status;
@@ -750,15 +750,15 @@ static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
750 const char *email, unsigned long timestamp, int tz,
751 const char *message, void *cb_data)
752 {
753 - struct cmd_reflog_expire_cb *cb = cb_data;
754 - if (!cb->expire_total || timestamp < cb->expire_total)
755 - cb->recno++;
753 + struct expire_reflog_policy_cb *cb = cb_data;
754 + if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
755 + cb->cmd.recno++;
756 return 0;
757 }
758
759 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
760 {
761 - struct cmd_reflog_expire_cb cb;
761 + struct expire_reflog_policy_cb cb;
762 int i, status = 0;
763 unsigned int flags = 0;
764
@@ -805,12 +805,12 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
805
806 recno = strtoul(spec + 2, &ep, 10);
807 if (*ep == '}') {
808 - cb.recno = -recno;
808 + cb.cmd.recno = -recno;
809 for_each_reflog_ent(ref, count_reflog_ent, &cb);
810 } else {
811 - cb.expire_total = approxidate(spec + 2);
811 + cb.cmd.expire_total = approxidate(spec + 2);
812 for_each_reflog_ent(ref, count_reflog_ent, &cb);
813 - cb.expire_total = 0;
813 + cb.cmd.expire_total = 0;
814 }
815
816 status |= expire_reflog(ref, sha1, flags, &cb);