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 <mhag...@alum.mit.edu>
---
 refs.c | 33 ++++++++-------------------------
 1 file changed, 8 insertions(+), 25 deletions(-)

diff --git a/refs.c b/refs.c
index f2d83f3..df670cb 100644
--- a/refs.c
+++ b/refs.c
@@ -2000,7 +2000,7 @@ struct ref_to_prune {
 struct pack_refs_cb_data {
        unsigned int flags;
        struct ref_to_prune *ref_to_prune;
-       FILE *refs_file;
+       int fd;
 };
 
 static int pack_one_ref(struct ref_entry *entry, void *cb_data)
@@ -2019,15 +2019,13 @@ static int pack_one_ref(struct ref_entry *entry, void 
*cb_data)
            !(entry->flag & REF_ISPACKED))
                return 0;
 
-       fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(entry->u.value.sha1),
-               entry->name);
-
        peel_status = peel_entry(entry, 1);
-       if (peel_status == PEEL_PEELED)
-               fprintf(cb->refs_file, "^%s\n", 
sha1_to_hex(entry->u.value.peeled));
-       else if (peel_status != PEEL_NON_TAG)
+       if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
                die("internal error peeling reference %s (%s)",
                    entry->name, sha1_to_hex(entry->u.value.sha1));
+       write_packed_entry(cb->fd, entry->name, entry->u.value.sha1,
+                          peel_status == PEEL_PEELED ?
+                          entry->u.value.peeled : NULL);
 
        /* If the ref was already packed, there is no need to prune it. */
        if ((cb->flags & PACK_REFS_PRUNE) && !(entry->flag & REF_ISPACKED)) {
@@ -2096,32 +2094,17 @@ static struct lock_file packlock;
 
 int pack_refs(unsigned int flags)
 {
-       int fd;
        struct pack_refs_cb_data cbdata;
 
        memset(&cbdata, 0, sizeof(cbdata));
        cbdata.flags = flags;
 
-       fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"),
-                                      LOCK_DIE_ON_ERROR);
-       cbdata.refs_file = fdopen(fd, "w");
-       if (!cbdata.refs_file)
-               die_errno("unable to create ref-pack file structure");
+       cbdata.fd = hold_lock_file_for_update(&packlock, 
git_path("packed-refs"),
+                                             LOCK_DIE_ON_ERROR);
 
-       /* perhaps other traits later as well */
-       fprintf(cbdata.refs_file, "# pack-refs with: peeled fully-peeled \n");
+       write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
 
        do_for_each_entry(NULL, "", pack_one_ref, &cbdata);
-       if (ferror(cbdata.refs_file))
-               die("failed to write ref-pack file");
-       if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
-               die_errno("failed to write ref-pack file");
-       /*
-        * Since the lock file was fdopen()'ed and then fclose()'ed above,
-        * assign -1 to the lock file descriptor so that commit_lock_file()
-        * won't try to close() it.
-        */
-       packlock.fd = -1;
        if (commit_lock_file(&packlock) < 0)
                die_errno("unable to overwrite old ref-pack file");
        prune_refs(cbdata.ref_to_prune);
-- 
1.8.2.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to