pack_refs(): split creation of packed refs and entry writing

Split pack_refs() into multiple passes: * Iterate over loose refs. For each one that can be turned into a packed ref, create a corresponding entry in the packed refs cache. * Write the packed refs to the packed-refs file. This change isolates the mutation of the packed-refs file to a single place. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jun 20, 2013 at 10:37 UTC 267f9a8cc8192e120a6476fc55590e288e08b459
1 file changed +34 -14
refs.c
+34 -14
@@ -2023,35 +2023,50 @@ struct ref_to_prune {
2023
2024 struct pack_refs_cb_data {
2025 unsigned int flags;
2026 + struct ref_dir *packed_refs;
2027 struct ref_to_prune *ref_to_prune;
2027 - int fd;
2028 };
2029
2030 -static int pack_one_ref(struct ref_entry *entry, void *cb_data)
2030 +/*
2031 + * An each_ref_entry_fn that is run over loose references only. If
2032 + * the loose reference can be packed, add an entry in the packed ref
2033 + * cache. If the reference should be pruned, also add it to
2034 + * ref_to_prune in the pack_refs_cb_data.
2035 + */
2036 +static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
2037 {
2038 struct pack_refs_cb_data *cb = cb_data;
2039 enum peel_status peel_status;
2040 + struct ref_entry *packed_entry;
2041 int is_tag_ref = !prefixcmp(entry->name, "refs/tags/");
2042
2036 - /* ALWAYS pack refs that were already packed or are tags */
2037 - if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref &&
2038 - !(entry->flag & REF_ISPACKED))
2043 + /* ALWAYS pack tags */
2044 + if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
2045 return 0;
2046
2047 /* Do not pack symbolic or broken refs: */
2048 if ((entry->flag & REF_ISSYMREF) || !ref_resolves_to_object(entry))
2049 return 0;
2050
2051 + /* Add a packed ref cache entry equivalent to the loose entry. */
2052 peel_status = peel_entry(entry, 1);
2053 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
2054 die("internal error peeling reference %s (%s)",
2055 entry->name, sha1_to_hex(entry->u.value.sha1));
2049 - write_packed_entry(cb->fd, entry->name, entry->u.value.sha1,
2050 - peel_status == PEEL_PEELED ?
2051 - entry->u.value.peeled : NULL);
2056 + packed_entry = find_ref(cb->packed_refs, entry->name);
2057 + if (packed_entry) {
2058 + /* Overwrite existing packed entry with info from loose entry */
2059 + packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;
2060 + hashcpy(packed_entry->u.value.sha1, entry->u.value.sha1);
2061 + } else {
2062 + packed_entry = create_ref_entry(entry->name, entry->u.value.sha1,
2063 + REF_ISPACKED | REF_KNOWS_PEELED, 0);
2064 + add_ref(cb->packed_refs, packed_entry);
2065 + }
2066 + hashcpy(packed_entry->u.value.peeled, entry->u.value.peeled);
2067
2053 - /* If the ref was already packed, there is no need to prune it. */
2054 - if ((cb->flags & PACK_REFS_PRUNE) && !(entry->flag & REF_ISPACKED)) {
2068 + /* Schedule the loose reference for pruning if requested. */
2069 + if ((cb->flags & PACK_REFS_PRUNE)) {
2070 int namelen = strlen(entry->name) + 1;
2071 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
2072 hashcpy(n->sha1, entry->u.value.sha1);
@@ -2118,16 +2133,21 @@ static struct lock_file packlock;
2133 int pack_refs(unsigned int flags)
2134 {
2135 struct pack_refs_cb_data cbdata;
2136 + int fd;
2137
2138 memset(&cbdata, 0, sizeof(cbdata));
2139 cbdata.flags = flags;
2140
2125 - cbdata.fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"),
2126 - LOCK_DIE_ON_ERROR);
2141 + fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"),
2142 + LOCK_DIE_ON_ERROR);
2143 + cbdata.packed_refs = get_packed_refs(&ref_cache);
2144
2128 - write_or_die(cbdata.fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
2145 + do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0,
2146 + pack_if_possible_fn, &cbdata);
2147 +
2148 + write_or_die(fd, PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
2149 + do_for_each_entry_in_dir(cbdata.packed_refs, 0, write_packed_entry_fn, &fd);
2150
2130 - do_for_each_entry(&ref_cache, "", pack_one_ref, &cbdata);
2151 if (commit_lock_file(&packlock) < 0)
2152 die_errno("unable to overwrite old ref-pack file");
2153 prune_refs(cbdata.ref_to_prune);