pack_one_ref(): use write_packed_entry() to do the writing

Change pack_refs() to work with a file descriptor instead of a FILE* (making the file-locking code less awkward) and use write_packed_entry() to do the writing. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 22, 2013 at 21:52 UTC 0f29920f1e0ce00aaf867fdd9ad2174011179f47
1 file changed +8 -25
refs.c
+8 -25
@@ -2001,7 +2001,7 @@ struct ref_to_prune {
2001 struct pack_refs_cb_data {
2002 unsigned int flags;
2003 struct ref_to_prune *ref_to_prune;
2004 - FILE *refs_file;
2004 + int fd;
2005 };
2006
2007 static int pack_one_ref(struct ref_entry *entry, void *cb_data)
@@ -2020,15 +2020,13 @@ static int pack_one_ref(struct ref_entry *entry, void *cb_data)
2020 !(entry->flag & REF_ISPACKED))
2021 return 0;
2022
2023 - fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(entry->u.value.sha1),
2024 - entry->name);
2025 -
2023 peel_status = peel_entry(entry, 1);
2027 - if (peel_status == PEEL_PEELED)
2028 - fprintf(cb->refs_file, "^%s\n", sha1_to_hex(entry->u.value.peeled));
2029 - else if (peel_status != PEEL_NON_TAG)
2024 + if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
2025 die("internal error peeling reference %s (%s)",
2026 entry->name, sha1_to_hex(entry->u.value.sha1));
2027 + write_packed_entry(cb->fd, entry->name, entry->u.value.sha1,
2028 + peel_status == PEEL_PEELED ?
2029 + entry->u.value.peeled : NULL);
2030
2031 /* If the ref was already packed, there is no need to prune it. */
2032 if ((cb->flags & PACK_REFS_PRUNE) && !(entry->flag & REF_ISPACKED)) {
@@ -2097,32 +2095,17 @@ static struct lock_file packlock;
2095
2096 int pack_refs(unsigned int flags)
2097 {
2100 - int fd;
2098 struct pack_refs_cb_data cbdata;
2099
2100 memset(&cbdata, 0, sizeof(cbdata));
2101 cbdata.flags = flags;
2102
2106 - fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"),
2107 - LOCK_DIE_ON_ERROR);
2108 - cbdata.refs_file = fdopen(fd, "w");
2109 - if (!cbdata.refs_file)
2110 - die_errno("unable to create ref-pack file structure");
2103 + cbdata.fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"),
2104 + LOCK_DIE_ON_ERROR);
2105
2112 - /* perhaps other traits later as well */
2113 - fprintf(cbdata.refs_file, "# pack-refs with: peeled fully-peeled \n");
2106 + write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
2107
2108 do_for_each_entry(NULL, "", pack_one_ref, &cbdata);
2116 - if (ferror(cbdata.refs_file))
2117 - die("failed to write ref-pack file");
2118 - if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
2119 - die_errno("failed to write ref-pack file");
2120 - /*
2121 - * Since the lock file was fdopen()'ed and then fclose()'ed above,
2122 - * assign -1 to the lock file descriptor so that commit_lock_file()
2123 - * won't try to close() it.
2124 - */
2125 - packlock.fd = -1;
2109 if (commit_lock_file(&packlock) < 0)
2110 die_errno("unable to overwrite old ref-pack file");
2111 prune_refs(cbdata.ref_to_prune);