Code

Make builtin-reset.c use parse_options.
[git.git] / fast-import.c
index 4cf50929765f56f339cd3479312c80c747a78eba..7f197d5e36ae977e21251dbb45fa72973ea7830f 100644 (file)
@@ -275,6 +275,8 @@ struct recent_command
 static unsigned long max_depth = 10;
 static off_t max_packsize = (1LL << 32) - 1;
 static int force_update;
+static int pack_compression_level = Z_DEFAULT_COMPRESSION;
+static int pack_compression_seen;
 
 /* Stats and misc. counters */
 static uintmax_t alloc_count;
@@ -370,6 +372,8 @@ static void write_branch_report(FILE *rpt, struct branch *b)
        fputc('\n', rpt);
 }
 
+static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
+
 static void write_crash_report(const char *err)
 {
        char *loc = git_path("fast_import_crash_%d", getpid());
@@ -428,12 +432,37 @@ static void write_crash_report(const char *err)
                        write_branch_report(rpt, b);
        }
 
+       if (first_tag) {
+               struct tag *tg;
+               fputc('\n', rpt);
+               fputs("Annotated Tags\n", rpt);
+               fputs("--------------\n", rpt);
+               for (tg = first_tag; tg; tg = tg->next_tag) {
+                       fputs(sha1_to_hex(tg->sha1), rpt);
+                       fputc(' ', rpt);
+                       fputs(tg->name, rpt);
+                       fputc('\n', rpt);
+               }
+       }
+
+       fputc('\n', rpt);
+       fputs("Marks\n", rpt);
+       fputs("-----\n", rpt);
+       if (mark_file)
+               fprintf(rpt, "  exported to %s\n", mark_file);
+       else
+               dump_marks_helper(rpt, 0, marks);
+
        fputc('\n', rpt);
        fputs("-------------------\n", rpt);
        fputs("END OF CRASH REPORT\n", rpt);
        fclose(rpt);
 }
 
+static void end_packfile(void);
+static void unkeep_all_packs(void);
+static void dump_marks(void);
+
 static NORETURN void die_nicely(const char *err, va_list params)
 {
        static int zombie;
@@ -447,6 +476,9 @@ static NORETURN void die_nicely(const char *err, va_list params)
        if (!zombie) {
                zombie = 1;
                write_crash_report(message);
+               end_packfile();
+               unkeep_all_packs();
+               dump_marks();
        }
        exit(128);
 }
@@ -917,6 +949,7 @@ static void end_packfile(void)
                struct branch *b;
                struct tag *t;
 
+               close_pack_windows(pack_data);
                fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
                                    pack_data->pack_name, object_count);
                close(pack_data->pack_fd);
@@ -926,7 +959,6 @@ static void end_packfile(void)
                new_p = add_packed_git(idx_name, strlen(idx_name), 1);
                if (!new_p)
                        die("core git rejected index %s", idx_name);
-               new_p->windows = old_p->windows;
                all_packs[pack_id] = new_p;
                install_packed_git(new_p);
 
@@ -1038,7 +1070,7 @@ static int store_object(
                delta = NULL;
 
        memset(&s, 0, sizeof(s));
-       deflateInit(&s, zlib_compression_level);
+       deflateInit(&s, pack_compression_level);
        if (delta) {
                s.next_in = delta;
                s.avail_in = deltalen;
@@ -1066,7 +1098,7 @@ static int store_object(
                        delta = NULL;
 
                        memset(&s, 0, sizeof(s));
-                       deflateInit(&s, zlib_compression_level);
+                       deflateInit(&s, pack_compression_level);
                        s.next_in = (void *)dat->buf;
                        s.avail_in = dat->len;
                        s.avail_out = deflateBound(&s, s.avail_in);
@@ -1123,14 +1155,49 @@ static int store_object(
        return 0;
 }
 
+/* All calls must be guarded by find_object() or find_mark() to
+ * ensure the 'struct object_entry' passed was written by this
+ * process instance.  We unpack the entry by the offset, avoiding
+ * the need for the corresponding .idx file.  This unpacking rule
+ * works because we only use OBJ_REF_DELTA within the packfiles
+ * created by fast-import.
+ *
+ * oe must not be NULL.  Such an oe usually comes from giving
+ * an unknown SHA-1 to find_object() or an undefined mark to
+ * find_mark().  Callers must test for this condition and use
+ * the standard read_sha1_file() when it happens.
+ *
+ * oe->pack_id must not be MAX_PACK_ID.  Such an oe is usually from
+ * find_mark(), where the mark was reloaded from an existing marks
+ * file and is referencing an object that this fast-import process
+ * instance did not write out to a packfile.  Callers must test for
+ * this condition and use read_sha1_file() instead.
+ */
 static void *gfi_unpack_entry(
        struct object_entry *oe,
        unsigned long *sizep)
 {
        enum object_type type;
        struct packed_git *p = all_packs[oe->pack_id];
-       if (p == pack_data)
+       if (p == pack_data && p->pack_size < (pack_size + 20)) {
+               /* The object is stored in the packfile we are writing to
+                * and we have modified it since the last time we scanned
+                * back to read a previously written object.  If an old
+                * window covered [p->pack_size, p->pack_size + 20) its
+                * data is stale and is not valid.  Closing all windows
+                * and updating the packfile length ensures we can read
+                * the newly written data.
+                */
+               close_pack_windows(p);
+
+               /* We have to offer 20 bytes additional on the end of
+                * the packfile as the core unpacker code assumes the
+                * footer is present at the file end and must promise
+                * at least 20 bytes within any window it maps.  But
+                * we don't actually create the footer here.
+                */
                p->pack_size = pack_size + 20;
+       }
        return unpack_entry(p, oe->offset, &type, sizep);
 }
 
@@ -1167,6 +1234,8 @@ static void load_tree(struct tree_entry *root)
                        die("Not a tree: %s", sha1_to_hex(sha1));
                t->delta_depth = myoe->depth;
                buf = gfi_unpack_entry(myoe, &size);
+               if (!buf)
+                       die("Can't load tree %s", sha1_to_hex(sha1));
        } else {
                enum object_type type;
                buf = read_sha1_file(sha1, &type, &size);
@@ -1539,9 +1608,10 @@ static void dump_marks(void)
 
        f = fdopen(mark_fd, "w");
        if (!f) {
+               int saved_errno = errno;
                rollback_lock_file(&mark_lock);
                failure |= error("Unable to write marks file %s: %s",
-                       mark_file, strerror(errno));
+                       mark_file, strerror(saved_errno));
                return;
        }
 
@@ -1554,16 +1624,18 @@ static void dump_marks(void)
 
        dump_marks_helper(f, 0, marks);
        if (ferror(f) || fclose(f)) {
+               int saved_errno = errno;
                rollback_lock_file(&mark_lock);
                failure |= error("Unable to write marks file %s: %s",
-                       mark_file, strerror(errno));
+                       mark_file, strerror(saved_errno));
                return;
        }
 
        if (commit_lock_file(&mark_lock)) {
+               int saved_errno = errno;
                rollback_lock_file(&mark_lock);
                failure |= error("Unable to commit marks file %s: %s",
-                       mark_file, strerror(errno));
+                       mark_file, strerror(saved_errno));
                return;
        }
 }
@@ -2277,6 +2349,27 @@ static void import_marks(const char *input_file)
        fclose(f);
 }
 
+static int git_pack_config(const char *k, const char *v)
+{
+       if (!strcmp(k, "pack.depth")) {
+               max_depth = git_config_int(k, v);
+               if (max_depth > MAX_DEPTH)
+                       max_depth = MAX_DEPTH;
+               return 0;
+       }
+       if (!strcmp(k, "pack.compression")) {
+               int level = git_config_int(k, v);
+               if (level == -1)
+                       level = Z_DEFAULT_COMPRESSION;
+               else if (level < 0 || level > Z_BEST_COMPRESSION)
+                       die("bad pack compression level %d", level);
+               pack_compression_level = level;
+               pack_compression_seen = 1;
+               return 0;
+       }
+       return git_default_config(k, v);
+}
+
 static const char fast_import_usage[] =
 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
 
@@ -2284,7 +2377,11 @@ int main(int argc, const char **argv)
 {
        unsigned int i, show_stats = 1;
 
-       git_config(git_default_config);
+       setup_git_directory();
+       git_config(git_pack_config);
+       if (!pack_compression_seen && core_compression_seen)
+               pack_compression_level = core_compression_level;
+
        alloc_objects(object_entry_alloc);
        strbuf_init(&command_buf, 0);
        atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));