Code

Add an optional argument for --color options
[git.git] / builtin-pack-objects.c
index 9cc8a8451d21840b315d1355207293a1b0bcbf2c..e1d3adf405bb6ac842a3415e0461b4772396060d 100644 (file)
 #include "progress.h"
 #include "refs.h"
 
-#ifdef THREADED_DELTA_SEARCH
+#ifndef NO_PTHREADS
 #include "thread-utils.h"
 #include <pthread.h>
 #endif
 
-static const char pack_usage[] = "\
-git pack-objects [{ -q | --progress | --all-progress }] \n\
-       [--max-pack-size=N] [--local] [--incremental] \n\
-       [--window=N] [--window-memory=N] [--depth=N] \n\
-       [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
-       [--threads=N] [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
-       [--stdout | base-name] [--include-tag] \n\
-       [--keep-unreachable | --unpack-unreachable] \n\
-       [<ref-list | <object-list]";
+static const char pack_usage[] =
+  "git pack-objects [{ -q | --progress | --all-progress }]\n"
+  "        [--all-progress-implied]\n"
+  "        [--max-pack-size=N] [--local] [--incremental]\n"
+  "        [--window=N] [--window-memory=N] [--depth=N]\n"
+  "        [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset]\n"
+  "        [--threads=N] [--non-empty] [--revs [--unpacked | --all]*]\n"
+  "        [--reflog] [--stdout | base-name] [--include-tag]\n"
+  "        [--keep-unreachable | --unpack-unreachable \n"
+  "        [<ref-list | <object-list]";
 
 struct object_entry {
        struct pack_idx_entry idx;
@@ -76,7 +77,7 @@ static int allow_ofs_delta;
 static const char *base_name;
 static int progress = 1;
 static int window = 10;
-static uint32_t pack_size_limit, pack_size_limit_cfg;
+static unsigned long pack_size_limit, pack_size_limit_cfg;
 static int depth = 50;
 static int delta_search_threads;
 static int pack_to_stdout;
@@ -245,7 +246,7 @@ static unsigned long write_object(struct sha1file *f,
 
        type = entry->type;
 
-       /* write limit if limited packsize and not first object */
+       /* apply size limit if limited packsize and not first object */
        if (!pack_size_limit || !nr_written)
                limit = 0;
        else if (pack_size_limit <= write_offset)
@@ -442,7 +443,7 @@ static int write_one(struct sha1file *f,
 
        /* offset is non zero if object is written already. */
        if (e->idx.offset || e->preferred_base)
-               return 1;
+               return -1;
 
        /* if we are deltified, write out base object first. */
        if (e->delta && !write_one(f, e->delta, offset))
@@ -524,7 +525,8 @@ static void write_pack_file(void)
                if (!pack_to_stdout) {
                        mode_t mode = umask(0);
                        struct stat st;
-                       char *idx_tmp_name, tmpname[PATH_MAX];
+                       const char *idx_tmp_name;
+                       char tmpname[PATH_MAX];
 
                        umask(mode);
                        mode = 0444 & ~mode;
@@ -568,7 +570,7 @@ static void write_pack_file(void)
                        if (rename(idx_tmp_name, tmpname))
                                die_errno("unable to rename temporary index file");
 
-                       free(idx_tmp_name);
+                       free((void *) idx_tmp_name);
                        free(pack_tmp_name);
                        puts(sha1_to_hex(sha1));
                }
@@ -585,19 +587,6 @@ static void write_pack_file(void)
        if (written != nr_result)
                die("wrote %"PRIu32" objects while expecting %"PRIu32,
                        written, nr_result);
-       /*
-        * We have scanned through [0 ... i).  Since we have written
-        * the correct number of objects,  the remaining [i ... nr_objects)
-        * items must be either already written (due to out-of-order delta base)
-        * or a preferred base.  Count those which are neither and complain if any.
-        */
-       for (j = 0; i < nr_objects; i++) {
-               struct object_entry *e = objects + i;
-               j += !e->idx.offset && !e->preferred_base;
-       }
-       if (j)
-               die("wrote %"PRIu32" objects as expected but %"PRIu32
-                       " unwritten", written, j);
 }
 
 static int locate_object_entry_hash(const unsigned char *sha1)
@@ -672,7 +661,7 @@ static void setup_delta_attr_check(struct git_attr_check *check)
        static struct git_attr *attr_delta;
 
        if (!attr_delta)
-               attr_delta = git_attr("delta", 5);
+               attr_delta = git_attr("delta");
 
        check[0].attr = attr_delta;
 }
@@ -1008,6 +997,33 @@ static void add_preferred_base(unsigned char *sha1)
        it->pcache.tree_size = size;
 }
 
+static void cleanup_preferred_base(void)
+{
+       struct pbase_tree *it;
+       unsigned i;
+
+       it = pbase_tree;
+       pbase_tree = NULL;
+       while (it) {
+               struct pbase_tree *this = it;
+               it = this->next;
+               free(this->pcache.tree_data);
+               free(this);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
+               if (!pbase_tree_cache[i])
+                       continue;
+               free(pbase_tree_cache[i]->tree_data);
+               free(pbase_tree_cache[i]);
+               pbase_tree_cache[i] = NULL;
+       }
+
+       free(done_pbase_paths);
+       done_pbase_paths = NULL;
+       done_pbase_paths_num = done_pbase_paths_alloc = 0;
+}
+
 static void check_object(struct object_entry *entry)
 {
        if (entry->in_pack) {
@@ -1226,17 +1242,17 @@ static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
        return 0;
 }
 
-#ifdef THREADED_DELTA_SEARCH
+#ifndef NO_PTHREADS
 
-static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t read_mutex;
 #define read_lock()            pthread_mutex_lock(&read_mutex)
 #define read_unlock()          pthread_mutex_unlock(&read_mutex)
 
-static pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t cache_mutex;
 #define cache_lock()           pthread_mutex_lock(&cache_mutex)
 #define cache_unlock()         pthread_mutex_unlock(&cache_mutex)
 
-static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t progress_mutex;
 #define progress_lock()                pthread_mutex_lock(&progress_mutex)
 #define progress_unlock()      pthread_mutex_unlock(&progress_mutex)
 
@@ -1351,7 +1367,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
        /*
         * Handle memory allocation outside of the cache
         * accounting lock.  Compiler will optimize the strangeness
-        * away when THREADED_DELTA_SEARCH is not defined.
+        * away when NO_PTHREADS is defined.
         */
        free(trg_entry->delta_data);
        cache_lock();
@@ -1538,7 +1554,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
        free(array);
 }
 
-#ifdef THREADED_DELTA_SEARCH
+#ifndef NO_PTHREADS
 
 /*
  * The main thread waits on the condition that (at least) one of the workers
@@ -1563,7 +1579,26 @@ struct thread_params {
        unsigned *processed;
 };
 
-static pthread_cond_t progress_cond = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t progress_cond;
+
+/*
+ * Mutex and conditional variable can't be statically-initialized on Windows.
+ */
+static void init_threaded_search(void)
+{
+       pthread_mutex_init(&read_mutex, NULL);
+       pthread_mutex_init(&cache_mutex, NULL);
+       pthread_mutex_init(&progress_mutex, NULL);
+       pthread_cond_init(&progress_cond, NULL);
+}
+
+static void cleanup_threaded_search(void)
+{
+       pthread_cond_destroy(&progress_cond);
+       pthread_mutex_destroy(&read_mutex);
+       pthread_mutex_destroy(&cache_mutex);
+       pthread_mutex_destroy(&progress_mutex);
+}
 
 static void *threaded_find_deltas(void *arg)
 {
@@ -1599,16 +1634,22 @@ static void *threaded_find_deltas(void *arg)
 static void ll_find_deltas(struct object_entry **list, unsigned list_size,
                           int window, int depth, unsigned *processed)
 {
-       struct thread_params p[delta_search_threads];
+       struct thread_params *p;
        int i, ret, active_threads = 0;
 
+       init_threaded_search();
+
+       if (!delta_search_threads)      /* --threads=0 means autodetect */
+               delta_search_threads = online_cpus();
        if (delta_search_threads <= 1) {
                find_deltas(list, &list_size, window, depth, processed);
+               cleanup_threaded_search();
                return;
        }
        if (progress > pack_to_stdout)
                fprintf(stderr, "Delta compression using up to %d threads.\n",
                                delta_search_threads);
+       p = xcalloc(delta_search_threads, sizeof(*p));
 
        /* Partition the work amongst work threads. */
        for (i = 0; i < delta_search_threads; i++) {
@@ -1717,6 +1758,8 @@ static void ll_find_deltas(struct object_entry **list, unsigned list_size,
                        active_threads--;
                }
        }
+       cleanup_threaded_search();
+       free(p);
 }
 
 #else
@@ -1808,7 +1851,7 @@ static void prepare_pack(int window, int depth)
 
 static int git_pack_config(const char *k, const char *v, void *cb)
 {
-       if(!strcmp(k, "pack.window")) {
+       if (!strcmp(k, "pack.window")) {
                window = git_config_int(k, v);
                return 0;
        }
@@ -1843,7 +1886,7 @@ static int git_pack_config(const char *k, const char *v, void *cb)
                if (delta_search_threads < 0)
                        die("invalid number of threads specified (%d)",
                            delta_search_threads);
-#ifndef THREADED_DELTA_SEARCH
+#ifdef NO_PTHREADS
                if (delta_search_threads != 1)
                        warning("no threads support, ignoring %s", k);
 #endif
@@ -2093,11 +2136,14 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
 {
        int use_internal_rev_list = 0;
        int thin = 0;
+       int all_progress_implied = 0;
        uint32_t i;
        const char **rp_av;
        int rp_ac_alloc = 64;
        int rp_ac;
 
+       read_replace_refs = 0;
+
        rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
 
        rp_av[0] = "pack-objects";
@@ -2144,10 +2190,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
                        continue;
                }
                if (!prefixcmp(arg, "--max-pack-size=")) {
-                       char *end;
                        pack_size_limit_cfg = 0;
-                       pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
-                       if (!arg[16] || *end)
+                       if (!git_parse_ulong(arg+16, &pack_size_limit))
                                usage(pack_usage);
                        continue;
                }
@@ -2168,7 +2212,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
                        delta_search_threads = strtoul(arg+10, &end, 0);
                        if (!arg[10] || *end || delta_search_threads < 0)
                                usage(pack_usage);
-#ifndef THREADED_DELTA_SEARCH
+#ifdef NO_PTHREADS
                        if (delta_search_threads != 1)
                                warning("no threads support, "
                                        "ignoring %s", arg);
@@ -2190,6 +2234,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
                        progress = 2;
                        continue;
                }
+               if (!strcmp("--all-progress-implied", arg)) {
+                       all_progress_implied = 1;
+                       continue;
+               }
                if (!strcmp("-q", arg)) {
                        progress = 0;
                        continue;
@@ -2283,9 +2331,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
 
        if (!pack_to_stdout && !pack_size_limit)
                pack_size_limit = pack_size_limit_cfg;
-
        if (pack_to_stdout && pack_size_limit)
                die("--max-pack-size cannot be used to build a pack for transfer.");
+       if (pack_size_limit && pack_size_limit < 1024*1024) {
+               warning("minimum pack size limit is 1 MiB");
+               pack_size_limit = 1024*1024;
+       }
 
        if (!pack_to_stdout && thin)
                die("--thin cannot be used to build an indexable pack.");
@@ -2293,10 +2344,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
        if (keep_unreachable && unpack_unreachable)
                die("--keep-unreachable and --unpack-unreachable are incompatible.");
 
-#ifdef THREADED_DELTA_SEARCH
-       if (!delta_search_threads)      /* --threads=0 means autodetect */
-               delta_search_threads = online_cpus();
-#endif
+       if (progress && all_progress_implied)
+               progress = 2;
 
        prepare_packed_git();
 
@@ -2308,6 +2357,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
                rp_av[rp_ac] = NULL;
                get_object_list(rp_ac, rp_av);
        }
+       cleanup_preferred_base();
        if (include_tag && nr_result)
                for_each_ref(add_ref_tag, NULL);
        stop_progress(&progress_state);