pack_one_ref(): use write_packed_entry() to do the writing
authorMichael Haggerty <mhagger@alum.mit.edu>
Mon, 22 Apr 2013 19:52:38 +0000 (21:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 May 2013 22:33:11 +0000 (15:33 -0700)
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>
refs.c
diff --git a/refs.c b/refs.c
index 7e4aca95bbb4d8ee0af47e75e578bb94c213b99e..00174e9498a7f6e904230e7056a0539bbc9c8e60 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2001,7 +2001,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)
@@ -2020,15 +2020,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)) {
@@ -2097,32 +2095,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);