tag: fix formatting
[gitweb.git] / builtin / index-pack.c
index a2cd29d8f49586d9683f6ebb83421b45d3fa824d..31046c7a0a3776fe61e81c30e83f4139e980e1c8 100644 (file)
@@ -42,9 +42,7 @@ struct base_data {
 };
 
 struct thread_local {
-#ifndef NO_PTHREADS
        pthread_t thread;
-#endif
        struct base_data *base_cache;
        size_t base_cache_used;
        int pack_fd;
@@ -98,8 +96,6 @@ static uint32_t input_crc32;
 static int input_fd, output_fd;
 static const char *curr_pack;
 
-#ifndef NO_PTHREADS
-
 static struct thread_local *thread_data;
 static int nr_dispatched;
 static int threads_active;
@@ -179,26 +175,6 @@ static void cleanup_thread(void)
        free(thread_data);
 }
 
-#else
-
-#define read_lock()
-#define read_unlock()
-
-#define counter_lock()
-#define counter_unlock()
-
-#define work_lock()
-#define work_unlock()
-
-#define deepest_delta_lock()
-#define deepest_delta_unlock()
-
-#define type_cas_lock()
-#define type_cas_unlock()
-
-#endif
-
-
 static int mark_link(struct object *obj, int type, void *data, struct fsck_options *options)
 {
        if (!obj)
@@ -223,7 +199,7 @@ static unsigned check_object(struct object *obj)
 
        if (!(obj->flags & FLAG_CHECKED)) {
                unsigned long size;
-               int type = oid_object_info(&obj->oid, &size);
+               int type = oid_object_info(the_repository, &obj->oid, &size);
                if (type <= 0)
                        die(_("did not receive expected object %s"),
                              oid_to_hex(&obj->oid));
@@ -364,22 +340,20 @@ static NORETURN void bad_object(off_t offset, const char *format, ...)
 
 static inline struct thread_local *get_thread_data(void)
 {
-#ifndef NO_PTHREADS
-       if (threads_active)
-               return pthread_getspecific(key);
-       assert(!threads_active &&
-              "This should only be reached when all threads are gone");
-#endif
+       if (HAVE_THREADS) {
+               if (threads_active)
+                       return pthread_getspecific(key);
+               assert(!threads_active &&
+                      "This should only be reached when all threads are gone");
+       }
        return &nothread_data;
 }
 
-#ifndef NO_PTHREADS
 static void set_thread_data(struct thread_local *data)
 {
        if (threads_active)
                pthread_setspecific(key, data);
 }
-#endif
 
 static struct base_data *alloc_base_data(void)
 {
@@ -450,7 +424,8 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
        int hdrlen;
 
        if (!is_delta_type(type)) {
-               hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), size) + 1;
+               hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX,
+                                  type_name(type),(uintmax_t)size) + 1;
                the_hash_algo->init_fn(&c);
                the_hash_algo->update_fn(&c, hdr, hdrlen);
        } else
@@ -719,9 +694,9 @@ static void find_ref_delta_children(const struct object_id *oid,
                *last_index = -1;
                return;
        }
-       while (first > 0 && !oidcmp(&ref_deltas[first - 1].oid, oid))
+       while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid))
                --first;
-       while (last < end && !oidcmp(&ref_deltas[last + 1].oid, oid))
+       while (last < end && oideq(&ref_deltas[last + 1].oid, oid))
                ++last;
        *first_index = first;
        *last_index = last;
@@ -797,7 +772,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
        if (startup_info->have_repository) {
                read_lock();
                collision_test_needed =
-                       has_sha1_file_with_flags(oid->hash, OBJECT_INFO_QUICK);
+                       has_object_file_with_flags(oid, OBJECT_INFO_QUICK);
                read_unlock();
        }
 
@@ -812,7 +787,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
                enum object_type has_type;
                unsigned long has_size;
                read_lock();
-               has_type = oid_object_info(oid, &has_size);
+               has_type = oid_object_info(the_repository, oid, &has_size);
                if (has_type < 0)
                        die(_("cannot read existing object info %s"), oid_to_hex(oid));
                if (has_type != type || has_size != size)
@@ -832,11 +807,14 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
        if (strict || do_fsck_object) {
                read_lock();
                if (type == OBJ_BLOB) {
-                       struct blob *blob = lookup_blob(oid);
+                       struct blob *blob = lookup_blob(the_repository, oid);
                        if (blob)
                                blob->object.flags |= FLAG_CHECKED;
                        else
                                die(_("invalid blob object %s"), oid_to_hex(oid));
+                       if (do_fsck_object &&
+                           fsck_object(&blob->object, (void *)data, size, &fsck_options))
+                               die(_("fsck error in packed object"));
                } else {
                        struct object *obj;
                        int eaten;
@@ -848,13 +826,14 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
                         * we do not need to free the memory here, as the
                         * buf is deleted by the caller.
                         */
-                       obj = parse_object_buffer(oid, type, size, buf,
+                       obj = parse_object_buffer(the_repository, oid, type,
+                                                 size, buf,
                                                  &eaten);
                        if (!obj)
                                die(_("invalid %s"), type_name(type));
                        if (do_fsck_object &&
                            fsck_object(obj, buf, size, &fsck_options))
-                               die(_("Error in object"));
+                               die(_("fsck error in packed object"));
                        if (strict && fsck_walk(obj, NULL, &fsck_options))
                                die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
 
@@ -866,7 +845,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
                        if (obj->type == OBJ_COMMIT) {
                                struct commit *commit = (struct commit *) obj;
                                if (detach_commit_buffer(commit, NULL) != data)
-                                       die("BUG: parse_object_buffer transmogrified our buffer");
+                                       BUG("parse_object_buffer transmogrified our buffer");
                        }
                        obj->flags |= FLAG_CHECKED;
                }
@@ -1015,7 +994,7 @@ static struct base_data *find_unresolved_deltas_1(struct base_data *base,
 
                if (!compare_and_swap_type(&child->real_type, OBJ_REF_DELTA,
                                           base->obj->real_type))
-                       die("BUG: child->real_type != OBJ_REF_DELTA");
+                       BUG("child->real_type != OBJ_REF_DELTA");
 
                resolve_delta(child, base, result);
                if (base->ref_first == base->ref_last && base->ofs_last == -1)
@@ -1088,7 +1067,6 @@ static void resolve_base(struct object_entry *obj)
        find_unresolved_deltas(base_obj);
 }
 
-#ifndef NO_PTHREADS
 static void *threaded_second_pass(void *data)
 {
        set_thread_data(data);
@@ -1112,7 +1090,6 @@ static void *threaded_second_pass(void *data)
        }
        return NULL;
 }
-#endif
 
 /*
  * First pass:
@@ -1162,7 +1139,7 @@ static void parse_pack_objects(unsigned char *hash)
        /* Check pack integrity */
        flush();
        the_hash_algo->final_fn(hash, &input_ctx);
-       if (hashcmp(fill(the_hash_algo->rawsz), hash))
+       if (!hasheq(fill(the_hash_algo->rawsz), hash))
                die(_("pack is corrupted (SHA1 mismatch)"));
        use(the_hash_algo->rawsz);
 
@@ -1209,7 +1186,6 @@ static void resolve_deltas(void)
                progress = start_progress(_("Resolving deltas"),
                                          nr_ref_deltas + nr_ofs_deltas);
 
-#ifndef NO_PTHREADS
        nr_dispatched = 0;
        if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
                init_thread();
@@ -1225,7 +1201,6 @@ static void resolve_deltas(void)
                cleanup_thread();
                return;
        }
-#endif
 
        for (i = 0; i < nr_objects; i++) {
                struct object_entry *obj = &objects[i];
@@ -1276,7 +1251,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
                fixup_pack_header_footer(output_fd, pack_hash,
                                         curr_pack, nr_objects,
                                         read_hash, consumed_bytes-the_hash_algo->rawsz);
-               if (hashcmp(read_hash, tail_hash) != 0)
+               if (!hasheq(read_hash, tail_hash))
                        die(_("Unexpected tail checksum for %s "
                              "(disk corruption?)"), curr_pack);
        }
@@ -1479,6 +1454,13 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
        } else
                chmod(final_index_name, 0444);
 
+       if (do_fsck_object) {
+               struct packed_git *p;
+               p = add_packed_git(final_index_name, strlen(final_index_name), 0);
+               if (p)
+                       install_packed_git(the_repository, p);
+       }
+
        if (!from_stdin) {
                printf("%s\n", sha1_to_hex(hash));
        } else {
@@ -1520,11 +1502,10 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
                if (nr_threads < 0)
                        die(_("invalid number of threads specified (%d)"),
                            nr_threads);
-#ifdef NO_PTHREADS
-               if (nr_threads != 1)
+               if (!HAVE_THREADS && nr_threads != 1) {
                        warning(_("no threads support, ignoring %s"), k);
-               nr_threads = 1;
-#endif
+                       nr_threads = 1;
+               }
                return 0;
        }
        return git_default_config(k, v, cb);
@@ -1543,12 +1524,13 @@ static void read_v2_anomalous_offsets(struct packed_git *p,
 {
        const uint32_t *idx1, *idx2;
        uint32_t i;
+       const uint32_t hashwords = the_hash_algo->rawsz / sizeof(uint32_t);
 
        /* The address of the 4-byte offset table */
        idx1 = (((const uint32_t *)p->index_data)
                + 2 /* 8-byte header */
                + 256 /* fan out */
-               + 5 * p->num_objects /* 20-byte SHA-1 table */
+               + hashwords * p->num_objects /* object ID table */
                + p->num_objects /* CRC32 table */
                );
 
@@ -1616,10 +1598,10 @@ static void show_pack_info(int stat_only)
                        chain_histogram[obj_stat[i].delta_depth - 1]++;
                if (stat_only)
                        continue;
-               printf("%s %-6s %lu %lu %"PRIuMAX,
+               printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
                       oid_to_hex(&obj->idx.oid),
-                      type_name(obj->real_type), obj->size,
-                      (unsigned long)(obj[1].idx.offset - obj->idx.offset),
+                      type_name(obj->real_type), (uintmax_t)obj->size,
+                      (uintmax_t)(obj[1].idx.offset - obj->idx.offset),
                       (uintmax_t)obj->idx.offset);
                if (is_delta_type(obj->type)) {
                        struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
@@ -1668,7 +1650,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage(index_pack_usage);
 
-       check_replace_refs = 0;
+       read_replace_refs = 0;
        fsck_options.walk = mark_link;
 
        reset_pack_idx_option(&opts);
@@ -1711,12 +1693,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
                                nr_threads = strtoul(arg+10, &end, 0);
                                if (!arg[10] || *end || nr_threads < 0)
                                        usage(index_pack_usage);
-#ifdef NO_PTHREADS
-                               if (nr_threads != 1)
-                                       warning(_("no threads support, "
-                                                 "ignoring %s"), arg);
-                               nr_threads = 1;
-#endif
+                               if (!HAVE_THREADS && nr_threads != 1) {
+                                       warning(_("no threads support, ignoring %s"), arg);
+                                       nr_threads = 1;
+                               }
                        } else if (starts_with(arg, "--pack_header=")) {
                                struct pack_header *hdr;
                                char *c;
@@ -1779,14 +1759,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
        if (strict)
                opts.flags |= WRITE_IDX_STRICT;
 
-#ifndef NO_PTHREADS
-       if (!nr_threads) {
+       if (HAVE_THREADS && !nr_threads) {
                nr_threads = online_cpus();
                /* An experiment showed that more threads does not mean faster */
                if (nr_threads > 3)
                        nr_threads = 3;
        }
-#endif
 
        curr_pack = open_pack_file(pack_name);
        parse_pack_header();
@@ -1820,6 +1798,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
                      pack_hash);
        else
                close(input_fd);
+
+       if (do_fsck_object && fsck_finish(&fsck_options))
+               die(_("fsck error in pack objects"));
+
        free(objects);
        strbuf_release(&index_name_buf);
        if (pack_name == NULL)