expire_reflog(): extract two policy-related functions
Extract two functions, reflog_expiry_prepare() and reflog_expiry_cleanup(), from expire_reflog(). This is a further step towards separating the code for deciding on expiration policy from the code that manages the physical deletion of reflog entries. This change requires a couple of local variables from expire_reflog() to be turned into fields of "struct expire_reflog_cb". More reorganization of the callback data will follow in later commits. 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
c48a1635355118d490fef5af14d8296a8e6f92df
1 file changed
+52
-42
builtin/reflog.c
+52
-42
@@ -43,6 +43,8 @@ struct expire_reflog_cb {
43
unsigned long mark_limit;
44
struct cmd_reflog_expire_cb *cmd;
45
unsigned char last_kept_sha1[20];
46
+ struct commit *tip_commit;
47
+ struct commit_list *tips;
48
};
49
50
struct collected_reflog {
@@ -363,6 +365,54 @@ static int push_tip_to_list(const char *refname, const unsigned char *sha1, int
365
return 0;
366
}
367
368
+static void reflog_expiry_prepare(const char *refname,
369
+ const unsigned char *sha1,
370
+ struct expire_reflog_cb *cb)
371
+{
372
+ if (!cb->cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
373
+ cb->tip_commit = NULL;
374
+ cb->unreachable_expire_kind = UE_HEAD;
375
+ } else {
376
+ cb->tip_commit = lookup_commit_reference_gently(sha1, 1);
377
+ if (!cb->tip_commit)
378
+ cb->unreachable_expire_kind = UE_ALWAYS;
379
+ else
380
+ cb->unreachable_expire_kind = UE_NORMAL;
381
+ }
382
+
383
+ if (cb->cmd->expire_unreachable <= cb->cmd->expire_total)
384
+ cb->unreachable_expire_kind = UE_ALWAYS;
385
+
386
+ cb->mark_list = NULL;
387
+ cb->tips = NULL;
388
+ if (cb->unreachable_expire_kind != UE_ALWAYS) {
389
+ if (cb->unreachable_expire_kind == UE_HEAD) {
390
+ struct commit_list *elem;
391
+ for_each_ref(push_tip_to_list, &cb->tips);
392
+ for (elem = cb->tips; elem; elem = elem->next)
393
+ commit_list_insert(elem->item, &cb->mark_list);
394
+ } else {
395
+ commit_list_insert(cb->tip_commit, &cb->mark_list);
396
+ }
397
+ cb->mark_limit = cb->cmd->expire_total;
398
+ mark_reachable(cb);
399
+ }
400
+}
401
+
402
+static void reflog_expiry_cleanup(struct expire_reflog_cb *cb)
403
+{
404
+ if (cb->unreachable_expire_kind != UE_ALWAYS) {
405
+ if (cb->unreachable_expire_kind == UE_HEAD) {
406
+ struct commit_list *elem;
407
+ for (elem = cb->tips; elem; elem = elem->next)
408
+ clear_commit_marks(elem->item, REACHABLE);
409
+ free_commit_list(cb->tips);
410
+ } else {
411
+ clear_commit_marks(cb->tip_commit, REACHABLE);
412
+ }
413
+ }
414
+}
415
+
416
static int expire_reflog(const char *refname, const unsigned char *sha1,
417
struct cmd_reflog_expire_cb *cmd)
418
{
@@ -370,8 +420,6 @@ static int expire_reflog(const char *refname, const unsigned char *sha1,
420
struct expire_reflog_cb cb;
421
struct ref_lock *lock;
422
char *log_file;
373
- struct commit *tip_commit;
374
- struct commit_list *tips;
423
int status = 0;
424
425
memset(&cb, 0, sizeof(cb));
@@ -415,47 +463,9 @@ static int expire_reflog(const char *refname, const unsigned char *sha1,
463
464
cb.cmd = cmd;
465
418
- if (!cmd->expire_unreachable || !strcmp(refname, "HEAD")) {
419
- tip_commit = NULL;
420
- cb.unreachable_expire_kind = UE_HEAD;
421
- } else {
422
- tip_commit = lookup_commit_reference_gently(sha1, 1);
423
- if (!tip_commit)
424
- cb.unreachable_expire_kind = UE_ALWAYS;
425
- else
426
- cb.unreachable_expire_kind = UE_NORMAL;
427
- }
428
-
429
- if (cmd->expire_unreachable <= cmd->expire_total)
430
- cb.unreachable_expire_kind = UE_ALWAYS;
431
-
432
- cb.mark_list = NULL;
433
- tips = NULL;
434
- if (cb.unreachable_expire_kind != UE_ALWAYS) {
435
- if (cb.unreachable_expire_kind == UE_HEAD) {
436
- struct commit_list *elem;
437
- for_each_ref(push_tip_to_list, &tips);
438
- for (elem = tips; elem; elem = elem->next)
439
- commit_list_insert(elem->item, &cb.mark_list);
440
- } else {
441
- commit_list_insert(tip_commit, &cb.mark_list);
442
- }
443
- cb.mark_limit = cmd->expire_total;
444
- mark_reachable(&cb);
445
- }
446
-
466
+ reflog_expiry_prepare(refname, sha1, &cb);
467
for_each_reflog_ent(refname, expire_reflog_ent, &cb);
448
-
449
- if (cb.unreachable_expire_kind != UE_ALWAYS) {
450
- if (cb.unreachable_expire_kind == UE_HEAD) {
451
- struct commit_list *elem;
452
- for (elem = tips; elem; elem = elem->next)
453
- clear_commit_marks(elem->item, REACHABLE);
454
- free_commit_list(tips);
455
- } else {
456
- clear_commit_marks(tip_commit, REACHABLE);
457
- }
458
- }
468
+ reflog_expiry_cleanup(&cb);
469
470
if (cb.newlog) {
471
if (close_lock_file(&reflog_lock)) {