Extract the I/O code from the "business logic" in repack_ref_fn(). Later there will be another caller for this function. Signed-off-by: Michael Haggerty <mhagger@xxxxxxxxxxxx> --- refs.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/refs.c b/refs.c index 140360f..9e3eff2 100644 --- a/refs.c +++ b/refs.c @@ -1956,29 +1956,43 @@ struct ref_lock *lock_any_ref_for_update(const char *refname, return lock_ref_sha1_basic(refname, old_sha1, flags, NULL); } -static int repack_ref_fn(struct ref_entry *entry, void *cb_data) +/* + * Write an entry to the packed-refs file for the specified refname. + * If peeled is non-NULL, write it as the entry's peeled value. + */ +static void write_packed_entry(int fd, char *refname, unsigned char *sha1, + unsigned char *peeled) { - int *fd = cb_data; char line[PATH_MAX + 100]; int len; - /* Silently skip broken refs: */ - if (!ref_resolves_to_object(entry, 0)) - return 0; len = snprintf(line, sizeof(line), "%s %s\n", - sha1_to_hex(entry->u.value.sha1), entry->name); + sha1_to_hex(sha1), refname); /* this should not happen but just being defensive */ if (len > sizeof(line)) - die("too long a refname '%s'", entry->name); - write_or_die(*fd, line, len); - if (!peel_entry(entry)) { - /* This reference could be peeled; write the peeled value: */ + die("too long a refname '%s'", refname); + write_or_die(fd, line, len); + + if (peeled) { if (snprintf(line, sizeof(line), "^%s\n", - sha1_to_hex(entry->u.value.peeled)) != - PEELED_LINE_LENGTH) + sha1_to_hex(peeled)) != PEELED_LINE_LENGTH) die("internal error"); - write_or_die(*fd, line, PEELED_LINE_LENGTH); + write_or_die(fd, line, PEELED_LINE_LENGTH); } +} + +static int repack_ref_fn(struct ref_entry *entry, void *cb_data) +{ + int *fd = cb_data; + enum peel_status peel_status; + + /* Silently skip broken refs: */ + if (!ref_resolves_to_object(entry, 0)) + return 0; + peel_status = peel_entry(entry); + write_packed_entry(*fd, entry->name, entry->u.value.sha1, + peel_status == PEEL_PEELED ? + entry->u.value.peeled : NULL); return 0; } -- 1.8.2.1 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html