Skip to content

Commit ddd64c5

Browse files
mhaggergitster
authored andcommitted
struct expire_reflog_cb: a new callback data type
Add a new data type, "struct expire_reflog_cb", for holding the data that expire_reflog() passes to expire_reflog_ent() via for_each_reflog_ent(). For now it only holds a pointer to a "struct expire_reflog_policy_cb", which still contains all of the actual data. In future commits we will move some fields from the latter to the former. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea7b4f6 commit ddd64c5

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

builtin/reflog.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ struct expire_reflog_policy_cb {
5050
struct commit_list *tips;
5151
};
5252

53+
struct expire_reflog_cb {
54+
void *policy_cb;
55+
};
56+
5357
struct collected_reflog {
5458
unsigned char sha1[20];
5559
char reflog[FLEX_ARRAY];
5660
};
61+
5762
struct collect_reflog_cb {
5863
struct collected_reflog **e;
5964
int alloc;
@@ -328,28 +333,29 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
328333
const char *email, unsigned long timestamp, int tz,
329334
const char *message, void *cb_data)
330335
{
331-
struct expire_reflog_policy_cb *cb = cb_data;
336+
struct expire_reflog_cb *cb = cb_data;
337+
struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
332338

333-
if (cb->cmd->rewrite)
334-
osha1 = cb->last_kept_sha1;
339+
if (policy_cb->cmd->rewrite)
340+
osha1 = policy_cb->last_kept_sha1;
335341

336342
if (should_expire_reflog_ent(osha1, nsha1, email, timestamp, tz,
337-
message, cb_data)) {
338-
if (!cb->newlog)
343+
message, policy_cb)) {
344+
if (!policy_cb->newlog)
339345
printf("would prune %s", message);
340-
else if (cb->cmd->verbose)
346+
else if (policy_cb->cmd->verbose)
341347
printf("prune %s", message);
342348
} else {
343-
if (cb->newlog) {
349+
if (policy_cb->newlog) {
344350
char sign = (tz < 0) ? '-' : '+';
345351
int zone = (tz < 0) ? (-tz) : tz;
346-
fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
352+
fprintf(policy_cb->newlog, "%s %s %s %lu %c%04d\t%s",
347353
sha1_to_hex(osha1), sha1_to_hex(nsha1),
348354
email, timestamp, sign, zone,
349355
message);
350-
hashcpy(cb->last_kept_sha1, nsha1);
356+
hashcpy(policy_cb->last_kept_sha1, nsha1);
351357
}
352-
if (cb->cmd->verbose)
358+
if (policy_cb->cmd->verbose)
353359
printf("keep %s", message);
354360
}
355361
return 0;
@@ -421,12 +427,15 @@ static int expire_reflog(const char *refname, const unsigned char *sha1,
421427
unsigned int flags, struct cmd_reflog_expire_cb *cmd)
422428
{
423429
static struct lock_file reflog_lock;
424-
struct expire_reflog_policy_cb cb;
430+
struct expire_reflog_cb cb;
431+
struct expire_reflog_policy_cb policy_cb;
425432
struct ref_lock *lock;
426433
char *log_file;
427434
int status = 0;
428435

429436
memset(&cb, 0, sizeof(cb));
437+
memset(&policy_cb, 0, sizeof(policy_cb));
438+
cb.policy_cb = &policy_cb;
430439

431440
/*
432441
* The reflog file is locked by holding the lock on the
@@ -457,27 +466,27 @@ static int expire_reflog(const char *refname, const unsigned char *sha1,
457466
strbuf_release(&err);
458467
goto failure;
459468
}
460-
cb.newlog = fdopen_lock_file(&reflog_lock, "w");
461-
if (!cb.newlog) {
469+
policy_cb.newlog = fdopen_lock_file(&reflog_lock, "w");
470+
if (!policy_cb.newlog) {
462471
error("cannot fdopen %s (%s)",
463472
reflog_lock.filename.buf, strerror(errno));
464473
goto failure;
465474
}
466475
}
467476

468-
cb.cmd = cmd;
477+
policy_cb.cmd = cmd;
469478

470-
reflog_expiry_prepare(refname, sha1, &cb);
479+
reflog_expiry_prepare(refname, sha1, &policy_cb);
471480
for_each_reflog_ent(refname, expire_reflog_ent, &cb);
472-
reflog_expiry_cleanup(&cb);
481+
reflog_expiry_cleanup(&policy_cb);
473482

474483
if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
475484
if (close_lock_file(&reflog_lock)) {
476485
status |= error("couldn't write %s: %s", log_file,
477486
strerror(errno));
478487
} else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
479488
(write_in_full(lock->lock_fd,
480-
sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
489+
sha1_to_hex(policy_cb.last_kept_sha1), 40) != 40 ||
481490
write_str_in_full(lock->lock_fd, "\n") != 1 ||
482491
close_ref(lock) < 0)) {
483492
status |= error("couldn't write %s",

0 commit comments

Comments
 (0)