Ramsay Jones | 2ad9d4c | 2011-04-07 19:23:40 +0100 | [diff] [blame] | 1 | #include "builtin.h" |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 2 | #include "delta.h" |
| 3 | #include "pack.h" |
| 4 | #include "csum-file.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 5 | #include "blob.h" |
| 6 | #include "commit.h" |
| 7 | #include "tag.h" |
| 8 | #include "tree.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 9 | #include "progress.h" |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 10 | #include "fsck.h" |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 11 | #include "exec_cmd.h" |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 12 | |
| 13 | static const char index_pack_usage[] = |
Štěpán Němec | 0adda93 | 2010-10-08 19:31:17 +0200 | [diff] [blame] | 14 | "git index-pack [-v] [-o <index-file>] [ --keep | --keep=<msg> ] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])"; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 15 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 16 | struct object_entry { |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 17 | struct pack_idx_entry idx; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 18 | unsigned long size; |
| 19 | unsigned int hdr_size; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 20 | enum object_type type; |
| 21 | enum object_type real_type; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 24 | union delta_base { |
| 25 | unsigned char sha1[20]; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 26 | off_t offset; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 27 | }; |
| 28 | |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 29 | struct base_data { |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 30 | struct base_data *base; |
| 31 | struct base_data *child; |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 32 | struct object_entry *obj; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 33 | void *data; |
| 34 | unsigned long size; |
| 35 | }; |
| 36 | |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 37 | /* |
| 38 | * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want |
| 39 | * to memcmp() only the first 20 bytes. |
| 40 | */ |
| 41 | #define UNION_BASE_SZ 20 |
| 42 | |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 43 | #define FLAG_LINK (1u<<20) |
| 44 | #define FLAG_CHECKED (1u<<21) |
| 45 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 46 | struct delta_entry { |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 47 | union delta_base base; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 48 | int obj_no; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 51 | static struct object_entry *objects; |
| 52 | static struct delta_entry *deltas; |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 53 | static struct base_data *base_cache; |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 54 | static size_t base_cache_used; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 55 | static int nr_objects; |
| 56 | static int nr_deltas; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 57 | static int nr_resolved_deltas; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 58 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 59 | static int from_stdin; |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 60 | static int strict; |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 61 | static int verbose; |
| 62 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 63 | static struct progress *progress; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 64 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 65 | /* We always read in 4kB chunks. */ |
| 66 | static unsigned char input_buffer[4096]; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 67 | static unsigned int input_offset, input_len; |
| 68 | static off_t consumed_bytes; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 69 | static git_SHA_CTX input_ctx; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 70 | static uint32_t input_crc32; |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 71 | static int input_fd, output_fd, pack_fd; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 72 | |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 73 | static int mark_link(struct object *obj, int type, void *data) |
| 74 | { |
| 75 | if (!obj) |
| 76 | return -1; |
| 77 | |
| 78 | if (type != OBJ_ANY && obj->type != type) |
| 79 | die("object type mismatch at %s", sha1_to_hex(obj->sha1)); |
| 80 | |
| 81 | obj->flags |= FLAG_LINK; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* The content of each linked object must have been checked |
| 86 | or it must be already present in the object database */ |
| 87 | static void check_object(struct object *obj) |
| 88 | { |
| 89 | if (!obj) |
| 90 | return; |
| 91 | |
| 92 | if (!(obj->flags & FLAG_LINK)) |
| 93 | return; |
| 94 | |
| 95 | if (!(obj->flags & FLAG_CHECKED)) { |
| 96 | unsigned long size; |
| 97 | int type = sha1_object_info(obj->sha1, &size); |
| 98 | if (type != obj->type || type <= 0) |
| 99 | die("object of unexpected type"); |
| 100 | obj->flags |= FLAG_CHECKED; |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void check_objects(void) |
| 106 | { |
| 107 | unsigned i, max; |
| 108 | |
| 109 | max = get_max_object_index(); |
| 110 | for (i = 0; i < max; i++) |
| 111 | check_object(get_indexed_object(i)); |
| 112 | } |
| 113 | |
| 114 | |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 115 | /* Discard current buffer used content. */ |
Rene Scharfe | a6e8a76 | 2006-11-18 13:07:06 +0100 | [diff] [blame] | 116 | static void flush(void) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 117 | { |
| 118 | if (input_offset) { |
| 119 | if (output_fd >= 0) |
| 120 | write_or_die(output_fd, input_buffer, input_offset); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 121 | git_SHA1_Update(&input_ctx, input_buffer, input_offset); |
Jim Meyering | 554a263 | 2006-12-11 19:06:34 +0100 | [diff] [blame] | 122 | memmove(input_buffer, input_buffer + input_offset, input_len); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 123 | input_offset = 0; |
| 124 | } |
| 125 | } |
| 126 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 127 | /* |
| 128 | * Make sure at least "min" bytes are available in the buffer, and |
| 129 | * return the pointer to the buffer. |
| 130 | */ |
Nicolas Pitre | b89c4e9 | 2006-10-27 16:14:23 -0400 | [diff] [blame] | 131 | static void *fill(int min) |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 132 | { |
| 133 | if (min <= input_len) |
| 134 | return input_buffer + input_offset; |
| 135 | if (min > sizeof(input_buffer)) |
| 136 | die("cannot fill %d bytes", min); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 137 | flush(); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 138 | do { |
Johan Herland | 8a912bc | 2007-05-15 14:49:22 +0200 | [diff] [blame] | 139 | ssize_t ret = xread(input_fd, input_buffer + input_len, |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 140 | sizeof(input_buffer) - input_len); |
| 141 | if (ret <= 0) { |
| 142 | if (!ret) |
| 143 | die("early EOF"); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 144 | die_errno("read error on input"); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 145 | } |
| 146 | input_len += ret; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 147 | if (from_stdin) |
| 148 | display_throughput(progress, consumed_bytes + input_len); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 149 | } while (input_len < min); |
| 150 | return input_buffer; |
| 151 | } |
| 152 | |
| 153 | static void use(int bytes) |
| 154 | { |
| 155 | if (bytes > input_len) |
| 156 | die("used more bytes than were available"); |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 157 | input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 158 | input_len -= bytes; |
| 159 | input_offset += bytes; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 160 | |
| 161 | /* make sure off_t is sufficiently large not to wrap */ |
Erik Faye-Lund | c03c831 | 2010-10-05 09:24:10 +0200 | [diff] [blame] | 162 | if (signed_add_overflows(consumed_bytes, bytes)) |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 163 | die("pack too large for current definition of off_t"); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 164 | consumed_bytes += bytes; |
| 165 | } |
| 166 | |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 167 | static const char *open_pack_file(const char *pack_name) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 168 | { |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 169 | if (from_stdin) { |
| 170 | input_fd = 0; |
| 171 | if (!pack_name) { |
| 172 | static char tmpfile[PATH_MAX]; |
Junio C Hamano | 6e180cd | 2009-02-24 23:11:29 -0800 | [diff] [blame] | 173 | output_fd = odb_mkstemp(tmpfile, sizeof(tmpfile), |
| 174 | "pack/tmp_pack_XXXXXX"); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 175 | pack_name = xstrdup(tmpfile); |
| 176 | } else |
| 177 | output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); |
| 178 | if (output_fd < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 179 | die_errno("unable to create '%s'", pack_name); |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 180 | pack_fd = output_fd; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 181 | } else { |
| 182 | input_fd = open(pack_name, O_RDONLY); |
| 183 | if (input_fd < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 184 | die_errno("cannot open packfile '%s'", pack_name); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 185 | output_fd = -1; |
Nicolas Pitre | 6d2fa7f | 2006-12-19 10:53:08 -0500 | [diff] [blame] | 186 | pack_fd = input_fd; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 187 | } |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 188 | git_SHA1_Init(&input_ctx); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 189 | return pack_name; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void parse_pack_header(void) |
| 193 | { |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 194 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 195 | |
| 196 | /* Header consistency check */ |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 197 | if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 198 | die("pack signature mismatch"); |
Nicolas Pitre | d60fc1c | 2006-02-09 17:50:04 -0500 | [diff] [blame] | 199 | if (!pack_version_ok(hdr->hdr_version)) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 200 | die("pack version %"PRIu32" unsupported", |
| 201 | ntohl(hdr->hdr_version)); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 202 | |
| 203 | nr_objects = ntohl(hdr->hdr_entries); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 204 | use(sizeof(struct pack_header)); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Erik Faye-Lund | a4f3131 | 2009-09-30 18:05:49 +0000 | [diff] [blame] | 207 | static NORETURN void bad_object(unsigned long offset, const char *format, |
| 208 | ...) __attribute__((format (printf, 2, 3))); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 209 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 210 | static NORETURN void bad_object(unsigned long offset, const char *format, ...) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 211 | { |
| 212 | va_list params; |
| 213 | char buf[1024]; |
| 214 | |
| 215 | va_start(params, format); |
| 216 | vsnprintf(buf, sizeof(buf), format, params); |
| 217 | va_end(params); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 218 | die("pack has bad object at offset %lu: %s", offset, buf); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 221 | static void free_base_data(struct base_data *c) |
| 222 | { |
| 223 | if (c->data) { |
| 224 | free(c->data); |
| 225 | c->data = NULL; |
| 226 | base_cache_used -= c->size; |
| 227 | } |
| 228 | } |
| 229 | |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 230 | static void prune_base_data(struct base_data *retain) |
| 231 | { |
Benjamin Kramer | 8e24cba | 2009-03-15 22:01:20 +0100 | [diff] [blame] | 232 | struct base_data *b; |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 233 | for (b = base_cache; |
| 234 | base_cache_used > delta_base_cache_limit && b; |
| 235 | b = b->child) { |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 236 | if (b->data && b != retain) |
| 237 | free_base_data(b); |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 241 | static void link_base_data(struct base_data *base, struct base_data *c) |
| 242 | { |
| 243 | if (base) |
| 244 | base->child = c; |
| 245 | else |
| 246 | base_cache = c; |
| 247 | |
| 248 | c->base = base; |
| 249 | c->child = NULL; |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 250 | if (c->data) |
| 251 | base_cache_used += c->size; |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 252 | prune_base_data(c); |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void unlink_base_data(struct base_data *c) |
| 256 | { |
| 257 | struct base_data *base = c->base; |
| 258 | if (base) |
| 259 | base->child = NULL; |
| 260 | else |
| 261 | base_cache = NULL; |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 262 | free_base_data(c); |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 265 | static void *unpack_entry_data(unsigned long offset, unsigned long size) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 266 | { |
Nicolas Pitre | 7ce4721 | 2010-04-12 12:12:06 -0400 | [diff] [blame] | 267 | int status; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 268 | z_stream stream; |
| 269 | void *buf = xmalloc(size); |
| 270 | |
| 271 | memset(&stream, 0, sizeof(stream)); |
Nicolas Pitre | 7ce4721 | 2010-04-12 12:12:06 -0400 | [diff] [blame] | 272 | git_inflate_init(&stream); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 273 | stream.next_out = buf; |
| 274 | stream.avail_out = size; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 275 | |
Nicolas Pitre | 7ce4721 | 2010-04-12 12:12:06 -0400 | [diff] [blame] | 276 | do { |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 277 | stream.next_in = fill(1); |
| 278 | stream.avail_in = input_len; |
Nicolas Pitre | 7ce4721 | 2010-04-12 12:12:06 -0400 | [diff] [blame] | 279 | status = git_inflate(&stream, 0); |
| 280 | use(input_len - stream.avail_in); |
| 281 | } while (status == Z_OK); |
| 282 | if (stream.total_out != size || status != Z_STREAM_END) |
| 283 | bad_object(offset, "inflate returned %d", status); |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 284 | git_inflate_end(&stream); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 285 | return buf; |
| 286 | } |
| 287 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 288 | static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 289 | { |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 290 | unsigned char *p; |
| 291 | unsigned long size, c; |
Nicolas Pitre | d7dd022 | 2007-04-09 01:06:30 -0400 | [diff] [blame] | 292 | off_t base_offset; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 293 | unsigned shift; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 294 | void *data; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 295 | |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 296 | obj->idx.offset = consumed_bytes; |
Stephen Boyd | 1e4cd68 | 2011-04-03 00:06:54 -0700 | [diff] [blame] | 297 | input_crc32 = crc32(0, NULL, 0); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 298 | |
| 299 | p = fill(1); |
| 300 | c = *p; |
| 301 | use(1); |
| 302 | obj->type = (c >> 4) & 7; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 303 | size = (c & 15); |
| 304 | shift = 4; |
| 305 | while (c & 0x80) { |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 306 | p = fill(1); |
| 307 | c = *p; |
| 308 | use(1); |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 309 | size += (c & 0x7f) << shift; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 310 | shift += 7; |
| 311 | } |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 312 | obj->size = size; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 313 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 314 | switch (obj->type) { |
Nicolas Pitre | eb32d23 | 2006-09-21 00:06:49 -0400 | [diff] [blame] | 315 | case OBJ_REF_DELTA: |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 316 | hashcpy(delta_base->sha1, fill(20)); |
| 317 | use(20); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 318 | break; |
| 319 | case OBJ_OFS_DELTA: |
| 320 | memset(delta_base, 0, sizeof(*delta_base)); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 321 | p = fill(1); |
| 322 | c = *p; |
| 323 | use(1); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 324 | base_offset = c & 127; |
| 325 | while (c & 128) { |
| 326 | base_offset += 1; |
Nicolas Pitre | 8723f21 | 2007-04-09 01:06:29 -0400 | [diff] [blame] | 327 | if (!base_offset || MSB(base_offset, 7)) |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 328 | bad_object(obj->idx.offset, "offset value overflow for delta base object"); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 329 | p = fill(1); |
| 330 | c = *p; |
| 331 | use(1); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 332 | base_offset = (base_offset << 7) + (c & 127); |
| 333 | } |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 334 | delta_base->offset = obj->idx.offset - base_offset; |
Nicolas Pitre | d8f3255 | 2008-10-29 19:02:45 -0400 | [diff] [blame] | 335 | if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset) |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 336 | bad_object(obj->idx.offset, "delta base offset is out of bound"); |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 337 | break; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 338 | case OBJ_COMMIT: |
| 339 | case OBJ_TREE: |
| 340 | case OBJ_BLOB: |
| 341 | case OBJ_TAG: |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 342 | break; |
| 343 | default: |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 344 | bad_object(obj->idx.offset, "unknown object type %d", obj->type); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 345 | } |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 346 | obj->hdr_size = consumed_bytes - obj->idx.offset; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 347 | |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 348 | data = unpack_entry_data(obj->idx.offset, obj->size); |
| 349 | obj->idx.crc32 = input_crc32; |
Nicolas Pitre | ee5743c | 2007-04-09 01:06:32 -0400 | [diff] [blame] | 350 | return data; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Nicolas Pitre | b89c4e9 | 2006-10-27 16:14:23 -0400 | [diff] [blame] | 353 | static void *get_data_from_pack(struct object_entry *obj) |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 354 | { |
Nicolas Pitre | a91ef6e | 2007-11-10 23:29:10 -0500 | [diff] [blame] | 355 | off_t from = obj[0].idx.offset + obj[0].hdr_size; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 356 | unsigned long len = obj[1].idx.offset - from; |
Nicolas Pitre | 776ea37 | 2010-04-12 12:11:07 -0400 | [diff] [blame] | 357 | unsigned char *data, *inbuf; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 358 | z_stream stream; |
Nicolas Pitre | 776ea37 | 2010-04-12 12:11:07 -0400 | [diff] [blame] | 359 | int status; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 360 | |
Nicolas Pitre | 776ea37 | 2010-04-12 12:11:07 -0400 | [diff] [blame] | 361 | data = xmalloc(obj->size); |
| 362 | inbuf = xmalloc((len < 64*1024) ? len : 64*1024); |
| 363 | |
| 364 | memset(&stream, 0, sizeof(stream)); |
| 365 | git_inflate_init(&stream); |
| 366 | stream.next_out = data; |
| 367 | stream.avail_out = obj->size; |
| 368 | |
Shawn O. Pearce | a91d49c | 2007-02-27 23:47:19 -0500 | [diff] [blame] | 369 | do { |
Nicolas Pitre | 776ea37 | 2010-04-12 12:11:07 -0400 | [diff] [blame] | 370 | ssize_t n = (len < 64*1024) ? len : 64*1024; |
| 371 | n = pread(pack_fd, inbuf, n, from); |
Samuel Tardieu | fb74243 | 2008-10-06 19:28:41 +0200 | [diff] [blame] | 372 | if (n < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 373 | die_errno("cannot pread pack file"); |
Samuel Tardieu | fb74243 | 2008-10-06 19:28:41 +0200 | [diff] [blame] | 374 | if (!n) |
Nicolas Pitre | 776ea37 | 2010-04-12 12:11:07 -0400 | [diff] [blame] | 375 | die("premature end of pack file, %lu bytes missing", len); |
| 376 | from += n; |
| 377 | len -= n; |
| 378 | stream.next_in = inbuf; |
| 379 | stream.avail_in = n; |
| 380 | status = git_inflate(&stream, 0); |
| 381 | } while (len && status == Z_OK && !stream.avail_in); |
| 382 | |
| 383 | /* This has been inflated OK when first encountered, so... */ |
| 384 | if (status != Z_STREAM_END || stream.total_out != obj->size) |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 385 | die("serious inflate inconsistency"); |
Nicolas Pitre | 776ea37 | 2010-04-12 12:11:07 -0400 | [diff] [blame] | 386 | |
| 387 | git_inflate_end(&stream); |
| 388 | free(inbuf); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 389 | return data; |
| 390 | } |
| 391 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 392 | static int find_delta(const union delta_base *base) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 393 | { |
| 394 | int first = 0, last = nr_deltas; |
| 395 | |
| 396 | while (first < last) { |
| 397 | int next = (first + last) / 2; |
| 398 | struct delta_entry *delta = &deltas[next]; |
| 399 | int cmp; |
| 400 | |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 401 | cmp = memcmp(base, &delta->base, UNION_BASE_SZ); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 402 | if (!cmp) |
| 403 | return next; |
| 404 | if (cmp < 0) { |
| 405 | last = next; |
| 406 | continue; |
| 407 | } |
| 408 | first = next+1; |
| 409 | } |
| 410 | return -first-1; |
| 411 | } |
| 412 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 413 | static void find_delta_children(const union delta_base *base, |
| 414 | int *first_index, int *last_index) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 415 | { |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 416 | int first = find_delta(base); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 417 | int last = first; |
| 418 | int end = nr_deltas - 1; |
| 419 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 420 | if (first < 0) { |
| 421 | *first_index = 0; |
| 422 | *last_index = -1; |
| 423 | return; |
| 424 | } |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 425 | while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ)) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 426 | --first; |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 427 | while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ)) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 428 | ++last; |
| 429 | *first_index = first; |
| 430 | *last_index = last; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void sha1_object(const void *data, unsigned long size, |
Nicolas Pitre | 9096c66 | 2007-03-20 17:07:48 -0400 | [diff] [blame] | 434 | enum object_type type, unsigned char *sha1) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 435 | { |
Nicolas Pitre | ce9fbf1 | 2007-03-20 16:02:09 -0400 | [diff] [blame] | 436 | hash_sha1_file(data, size, typename(type), sha1); |
Nicolas Pitre | 9096c66 | 2007-03-20 17:07:48 -0400 | [diff] [blame] | 437 | if (has_sha1_file(sha1)) { |
Nicolas Pitre | 8685da4 | 2007-03-20 15:32:35 -0400 | [diff] [blame] | 438 | void *has_data; |
| 439 | enum object_type has_type; |
| 440 | unsigned long has_size; |
| 441 | has_data = read_sha1_file(sha1, &has_type, &has_size); |
| 442 | if (!has_data) |
| 443 | die("cannot read existing object %s", sha1_to_hex(sha1)); |
| 444 | if (size != has_size || type != has_type || |
| 445 | memcmp(data, has_data, size) != 0) |
| 446 | die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1)); |
Nicolas Pitre | bbf4b41 | 2007-04-03 12:33:46 -0400 | [diff] [blame] | 447 | free(has_data); |
Nicolas Pitre | 8685da4 | 2007-03-20 15:32:35 -0400 | [diff] [blame] | 448 | } |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 449 | if (strict) { |
| 450 | if (type == OBJ_BLOB) { |
| 451 | struct blob *blob = lookup_blob(sha1); |
| 452 | if (blob) |
| 453 | blob->object.flags |= FLAG_CHECKED; |
| 454 | else |
| 455 | die("invalid blob object %s", sha1_to_hex(sha1)); |
| 456 | } else { |
| 457 | struct object *obj; |
| 458 | int eaten; |
| 459 | void *buf = (void *) data; |
| 460 | |
| 461 | /* |
| 462 | * we do not need to free the memory here, as the |
| 463 | * buf is deleted by the caller. |
| 464 | */ |
| 465 | obj = parse_object_buffer(sha1, type, size, buf, &eaten); |
| 466 | if (!obj) |
| 467 | die("invalid %s", typename(type)); |
| 468 | if (fsck_object(obj, 1, fsck_error_function)) |
| 469 | die("Error in object"); |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 470 | if (fsck_walk(obj, mark_link, NULL)) |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 471 | die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1)); |
| 472 | |
| 473 | if (obj->type == OBJ_TREE) { |
| 474 | struct tree *item = (struct tree *) obj; |
| 475 | item->buffer = NULL; |
| 476 | } |
| 477 | if (obj->type == OBJ_COMMIT) { |
| 478 | struct commit *commit = (struct commit *) obj; |
| 479 | commit->buffer = NULL; |
| 480 | } |
| 481 | obj->flags |= FLAG_CHECKED; |
| 482 | } |
| 483 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 486 | static void *get_base_data(struct base_data *c) |
| 487 | { |
| 488 | if (!c->data) { |
| 489 | struct object_entry *obj = c->obj; |
| 490 | |
| 491 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) { |
| 492 | void *base = get_base_data(c->base); |
| 493 | void *raw = get_data_from_pack(obj); |
| 494 | c->data = patch_delta( |
| 495 | base, c->base->size, |
| 496 | raw, obj->size, |
| 497 | &c->size); |
| 498 | free(raw); |
| 499 | if (!c->data) |
| 500 | bad_object(obj->idx.offset, "failed to apply delta"); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 501 | } else { |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 502 | c->data = get_data_from_pack(obj); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 503 | c->size = obj->size; |
| 504 | } |
Shawn O. Pearce | 92392b4 | 2008-07-15 04:45:34 +0000 | [diff] [blame] | 505 | |
| 506 | base_cache_used += c->size; |
| 507 | prune_base_data(c); |
| 508 | } |
| 509 | return c->data; |
| 510 | } |
| 511 | |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 512 | static void resolve_delta(struct object_entry *delta_obj, |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 513 | struct base_data *base, struct base_data *result) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 514 | { |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 515 | void *base_data, *delta_data; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 516 | |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 517 | delta_obj->real_type = base->obj->real_type; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 518 | delta_data = get_data_from_pack(delta_obj); |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 519 | base_data = get_base_data(base); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 520 | result->obj = delta_obj; |
Nicolas Pitre | ce3f6dc | 2008-10-20 16:46:19 -0400 | [diff] [blame] | 521 | result->data = patch_delta(base_data, base->size, |
| 522 | delta_data, delta_obj->size, &result->size); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 523 | free(delta_data); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 524 | if (!result->data) |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 525 | bad_object(delta_obj->idx.offset, "failed to apply delta"); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 526 | sha1_object(result->data, result->size, delta_obj->real_type, |
| 527 | delta_obj->idx.sha1); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 528 | nr_resolved_deltas++; |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 529 | } |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 530 | |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 531 | static void find_unresolved_deltas(struct base_data *base, |
| 532 | struct base_data *prev_base) |
| 533 | { |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 534 | int i, ref_first, ref_last, ofs_first, ofs_last; |
Shawn O. Pearce | 4a438ca | 2008-07-13 22:07:45 -0400 | [diff] [blame] | 535 | |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 536 | /* |
| 537 | * This is a recursive function. Those brackets should help reducing |
| 538 | * stack usage by limiting the scope of the delta_base union. |
| 539 | */ |
| 540 | { |
| 541 | union delta_base base_spec; |
| 542 | |
| 543 | hashcpy(base_spec.sha1, base->obj->idx.sha1); |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 544 | find_delta_children(&base_spec, &ref_first, &ref_last); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 545 | |
| 546 | memset(&base_spec, 0, sizeof(base_spec)); |
| 547 | base_spec.offset = base->obj->idx.offset; |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 548 | find_delta_children(&base_spec, &ofs_first, &ofs_last); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Nicolas Pitre | 9ed1961 | 2008-10-23 15:05:59 -0400 | [diff] [blame] | 551 | if (ref_last == -1 && ofs_last == -1) { |
| 552 | free(base->data); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 553 | return; |
Nicolas Pitre | 9ed1961 | 2008-10-23 15:05:59 -0400 | [diff] [blame] | 554 | } |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 555 | |
| 556 | link_base_data(prev_base, base); |
| 557 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 558 | for (i = ref_first; i <= ref_last; i++) { |
| 559 | struct object_entry *child = objects + deltas[i].obj_no; |
| 560 | if (child->real_type == OBJ_REF_DELTA) { |
| 561 | struct base_data result; |
| 562 | resolve_delta(child, base, &result); |
| 563 | if (i == ref_last && ofs_last == -1) |
| 564 | free_base_data(base); |
| 565 | find_unresolved_deltas(&result, base); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 566 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 567 | } |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 568 | |
Nicolas Pitre | 6a87ed9 | 2008-10-17 15:57:58 -0400 | [diff] [blame] | 569 | for (i = ofs_first; i <= ofs_last; i++) { |
| 570 | struct object_entry *child = objects + deltas[i].obj_no; |
| 571 | if (child->real_type == OBJ_OFS_DELTA) { |
| 572 | struct base_data result; |
| 573 | resolve_delta(child, base, &result); |
| 574 | if (i == ofs_last) |
| 575 | free_base_data(base); |
| 576 | find_unresolved_deltas(&result, base); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 577 | } |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 580 | unlink_base_data(base); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static int compare_delta_entry(const void *a, const void *b) |
| 584 | { |
| 585 | const struct delta_entry *delta_a = a; |
| 586 | const struct delta_entry *delta_b = b; |
Nicolas Pitre | 3c55287 | 2006-10-17 16:23:26 -0400 | [diff] [blame] | 587 | return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 590 | /* Parse all objects and return the pack content SHA1 hash */ |
| 591 | static void parse_pack_objects(unsigned char *sha1) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 592 | { |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 593 | int i; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 594 | struct delta_entry *delta = deltas; |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 595 | struct stat st; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * First pass: |
| 599 | * - find locations of all objects; |
| 600 | * - calculate SHA1 of all non-delta objects; |
Nicolas Pitre | b89c4e9 | 2006-10-27 16:14:23 -0400 | [diff] [blame] | 601 | * - remember base (SHA1 or offset) for all deltas. |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 602 | */ |
Nicolas Pitre | 13aaf14 | 2007-04-20 14:10:07 -0400 | [diff] [blame] | 603 | if (verbose) |
Nicolas Pitre | 29e63ed | 2007-10-30 14:57:35 -0400 | [diff] [blame] | 604 | progress = start_progress( |
| 605 | from_stdin ? "Receiving objects" : "Indexing objects", |
| 606 | nr_objects); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 607 | for (i = 0; i < nr_objects; i++) { |
| 608 | struct object_entry *obj = &objects[i]; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 609 | void *data = unpack_raw_entry(obj, &delta->base); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 610 | obj->real_type = obj->type; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 611 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) { |
| 612 | nr_deltas++; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 613 | delta->obj_no = i; |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 614 | delta++; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 615 | } else |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 616 | sha1_object(data, obj->size, obj->type, obj->idx.sha1); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 617 | free(data); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 618 | display_progress(progress, i+1); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 619 | } |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 620 | objects[i].idx.offset = consumed_bytes; |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 621 | stop_progress(&progress); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 622 | |
| 623 | /* Check pack integrity */ |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 624 | flush(); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 625 | git_SHA1_Final(sha1, &input_ctx); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 626 | if (hashcmp(fill(20), sha1)) |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 627 | die("pack is corrupted (SHA1 mismatch)"); |
Nicolas Pitre | 9bee247 | 2006-10-25 23:31:53 -0400 | [diff] [blame] | 628 | use(20); |
Nicolas Pitre | 2d47705 | 2006-10-20 14:45:21 -0400 | [diff] [blame] | 629 | |
| 630 | /* If input_fd is a file, we should have reached its end now. */ |
| 631 | if (fstat(input_fd, &st)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 632 | die_errno("cannot fstat packfile"); |
Johannes Schindelin | fa257b0 | 2007-02-22 19:14:14 +0100 | [diff] [blame] | 633 | if (S_ISREG(st.st_mode) && |
| 634 | lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 635 | die("pack has junk at the end"); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 636 | |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 637 | if (!nr_deltas) |
| 638 | return; |
| 639 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 640 | /* Sort deltas by base SHA1/offset for fast searching */ |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 641 | qsort(deltas, nr_deltas, sizeof(struct delta_entry), |
| 642 | compare_delta_entry); |
| 643 | |
| 644 | /* |
| 645 | * Second pass: |
| 646 | * - for all non-delta objects, look if it is used as a base for |
| 647 | * deltas; |
| 648 | * - if used as a base, uncompress the object and apply all deltas, |
| 649 | * recursively checking if the resulting object is used as a base |
| 650 | * for some more deltas. |
| 651 | */ |
Nicolas Pitre | 13aaf14 | 2007-04-20 14:10:07 -0400 | [diff] [blame] | 652 | if (verbose) |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 653 | progress = start_progress("Resolving deltas", nr_deltas); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 654 | for (i = 0; i < nr_objects; i++) { |
| 655 | struct object_entry *obj = &objects[i]; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 656 | struct base_data base_obj; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 657 | |
Nicolas Pitre | 53dda6f | 2006-09-21 00:08:33 -0400 | [diff] [blame] | 658 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 659 | continue; |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 660 | base_obj.obj = obj; |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 661 | base_obj.data = NULL; |
| 662 | find_unresolved_deltas(&base_obj, NULL); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 663 | display_progress(progress, nr_resolved_deltas); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 664 | } |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 665 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 666 | |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 667 | static int write_compressed(struct sha1file *f, void *in, unsigned int size) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 668 | { |
| 669 | z_stream stream; |
Nicolas Pitre | 7734d7f | 2010-04-12 16:50:35 -0400 | [diff] [blame] | 670 | int status; |
| 671 | unsigned char outbuf[4096]; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 672 | |
| 673 | memset(&stream, 0, sizeof(stream)); |
| 674 | deflateInit(&stream, zlib_compression_level); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 675 | stream.next_in = in; |
| 676 | stream.avail_in = size; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 677 | |
Nicolas Pitre | 7734d7f | 2010-04-12 16:50:35 -0400 | [diff] [blame] | 678 | do { |
| 679 | stream.next_out = outbuf; |
| 680 | stream.avail_out = sizeof(outbuf); |
| 681 | status = deflate(&stream, Z_FINISH); |
| 682 | sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out); |
| 683 | } while (status == Z_OK); |
| 684 | |
| 685 | if (status != Z_STREAM_END) |
| 686 | die("unable to deflate appended object (%d)", status); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 687 | size = stream.total_out; |
Nicolas Pitre | 7734d7f | 2010-04-12 16:50:35 -0400 | [diff] [blame] | 688 | deflateEnd(&stream); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 689 | return size; |
| 690 | } |
| 691 | |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 692 | static struct object_entry *append_obj_to_pack(struct sha1file *f, |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 693 | const unsigned char *sha1, void *buf, |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 694 | unsigned long size, enum object_type type) |
| 695 | { |
| 696 | struct object_entry *obj = &objects[nr_objects++]; |
| 697 | unsigned char header[10]; |
| 698 | unsigned long s = size; |
| 699 | int n = 0; |
| 700 | unsigned char c = (type << 4) | (s & 15); |
| 701 | s >>= 4; |
| 702 | while (s) { |
| 703 | header[n++] = c | 0x80; |
| 704 | c = s & 0x7f; |
| 705 | s >>= 7; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 706 | } |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 707 | header[n++] = c; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 708 | crc32_begin(f); |
| 709 | sha1write(f, header, n); |
Björn Steinbrink | 72de288 | 2008-07-24 18:32:00 +0100 | [diff] [blame] | 710 | obj[0].size = size; |
| 711 | obj[0].hdr_size = n; |
| 712 | obj[0].type = type; |
| 713 | obj[0].real_type = type; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 714 | obj[1].idx.offset = obj[0].idx.offset + n; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 715 | obj[1].idx.offset += write_compressed(f, buf, size); |
| 716 | obj[0].idx.crc32 = crc32_end(f); |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 717 | sha1flush(f); |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 718 | hashcpy(obj->idx.sha1, sha1); |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 719 | return obj; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static int delta_pos_compare(const void *_a, const void *_b) |
| 723 | { |
| 724 | struct delta_entry *a = *(struct delta_entry **)_a; |
| 725 | struct delta_entry *b = *(struct delta_entry **)_b; |
| 726 | return a->obj_no - b->obj_no; |
| 727 | } |
| 728 | |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 729 | static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 730 | { |
| 731 | struct delta_entry **sorted_by_pos; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 732 | int i, n = 0; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 733 | |
| 734 | /* |
| 735 | * Since many unresolved deltas may well be themselves base objects |
| 736 | * for more unresolved deltas, we really want to include the |
| 737 | * smallest number of base objects that would cover as much delta |
| 738 | * as possible by picking the |
| 739 | * trunc deltas first, allowing for other deltas to resolve without |
| 740 | * additional base objects. Since most base objects are to be found |
| 741 | * before deltas depending on them, a good heuristic is to start |
| 742 | * resolving deltas in the same order as their position in the pack. |
| 743 | */ |
| 744 | sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos)); |
| 745 | for (i = 0; i < nr_deltas; i++) { |
| 746 | if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA) |
| 747 | continue; |
| 748 | sorted_by_pos[n++] = &deltas[i]; |
| 749 | } |
| 750 | qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare); |
| 751 | |
| 752 | for (i = 0; i < n; i++) { |
| 753 | struct delta_entry *d = sorted_by_pos[i]; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 754 | enum object_type type; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 755 | struct base_data base_obj; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 756 | |
| 757 | if (objects[d->obj_no].real_type != OBJ_REF_DELTA) |
| 758 | continue; |
Shawn O. Pearce | f41aebd | 2008-07-13 22:07:44 -0400 | [diff] [blame] | 759 | base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size); |
| 760 | if (!base_obj.data) |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 761 | continue; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 762 | |
Shawn O. Pearce | 03993e1 | 2008-07-13 22:07:46 -0400 | [diff] [blame] | 763 | if (check_sha1_signature(d->base.sha1, base_obj.data, |
| 764 | base_obj.size, typename(type))) |
| 765 | die("local object %s is corrupt", sha1_to_hex(d->base.sha1)); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 766 | base_obj.obj = append_obj_to_pack(f, d->base.sha1, |
| 767 | base_obj.data, base_obj.size, type); |
Nicolas Pitre | 9441b61 | 2008-10-17 15:57:57 -0400 | [diff] [blame] | 768 | find_unresolved_deltas(&base_obj, NULL); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 769 | display_progress(progress, nr_resolved_deltas); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 770 | } |
| 771 | free(sorted_by_pos); |
| 772 | } |
| 773 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 774 | static void final(const char *final_pack_name, const char *curr_pack_name, |
| 775 | const char *final_index_name, const char *curr_index_name, |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 776 | const char *keep_name, const char *keep_msg, |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 777 | unsigned char *sha1) |
| 778 | { |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 779 | const char *report = "pack"; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 780 | char name[PATH_MAX]; |
| 781 | int err; |
| 782 | |
| 783 | if (!from_stdin) { |
| 784 | close(input_fd); |
| 785 | } else { |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 786 | fsync_or_die(output_fd, curr_pack_name); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 787 | err = close(output_fd); |
| 788 | if (err) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 789 | die_errno("error while closing pack file"); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 790 | } |
| 791 | |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 792 | if (keep_msg) { |
| 793 | int keep_fd, keep_msg_len = strlen(keep_msg); |
Junio C Hamano | 6e180cd | 2009-02-24 23:11:29 -0800 | [diff] [blame] | 794 | |
| 795 | if (!keep_name) |
| 796 | keep_fd = odb_pack_keep(name, sizeof(name), sha1); |
| 797 | else |
| 798 | keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 799 | |
Nicolas Pitre | 9ca4a20 | 2006-11-01 17:06:24 -0500 | [diff] [blame] | 800 | if (keep_fd < 0) { |
| 801 | if (errno != EEXIST) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 802 | die_errno("cannot write keep file '%s'", |
| 803 | keep_name); |
Nicolas Pitre | 9ca4a20 | 2006-11-01 17:06:24 -0500 | [diff] [blame] | 804 | } else { |
| 805 | if (keep_msg_len > 0) { |
| 806 | write_or_die(keep_fd, keep_msg, keep_msg_len); |
| 807 | write_or_die(keep_fd, "\n", 1); |
| 808 | } |
Jim Meyering | 91c8d59 | 2007-06-24 21:20:41 +0200 | [diff] [blame] | 809 | if (close(keep_fd) != 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 810 | die_errno("cannot close written keep file '%s'", |
| 811 | keep_name); |
Nicolas Pitre | 576162a | 2006-11-01 17:06:25 -0500 | [diff] [blame] | 812 | report = "keep"; |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 813 | } |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 814 | } |
| 815 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 816 | if (final_pack_name != curr_pack_name) { |
| 817 | if (!final_pack_name) { |
| 818 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", |
| 819 | get_object_directory(), sha1_to_hex(sha1)); |
| 820 | final_pack_name = name; |
| 821 | } |
| 822 | if (move_temp_to_file(curr_pack_name, final_pack_name)) |
| 823 | die("cannot store pack file"); |
Johan Herland | fb8b193 | 2009-03-26 16:16:47 +0100 | [diff] [blame] | 824 | } else if (from_stdin) |
Petr Baudis | 33b6503 | 2008-10-03 12:20:43 +0200 | [diff] [blame] | 825 | chmod(final_pack_name, 0444); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 826 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 827 | if (final_index_name != curr_index_name) { |
| 828 | if (!final_index_name) { |
| 829 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", |
| 830 | get_object_directory(), sha1_to_hex(sha1)); |
| 831 | final_index_name = name; |
| 832 | } |
| 833 | if (move_temp_to_file(curr_index_name, final_index_name)) |
| 834 | die("cannot store index file"); |
Johan Herland | fb8b193 | 2009-03-26 16:16:47 +0100 | [diff] [blame] | 835 | } else |
| 836 | chmod(final_index_name, 0444); |
Nicolas Pitre | 576162a | 2006-11-01 17:06:25 -0500 | [diff] [blame] | 837 | |
| 838 | if (!from_stdin) { |
| 839 | printf("%s\n", sha1_to_hex(sha1)); |
| 840 | } else { |
| 841 | char buf[48]; |
| 842 | int len = snprintf(buf, sizeof(buf), "%s\t%s\n", |
| 843 | report, sha1_to_hex(sha1)); |
Junio C Hamano | d1b2ddc | 2007-01-11 13:15:51 -0800 | [diff] [blame] | 844 | write_or_die(1, buf, len); |
Nicolas Pitre | 576162a | 2006-11-01 17:06:25 -0500 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * Let's just mimic git-unpack-objects here and write |
| 848 | * the last part of the input buffer to stdout. |
| 849 | */ |
| 850 | while (input_len) { |
| 851 | err = xwrite(1, input_buffer + input_offset, input_len); |
| 852 | if (err <= 0) |
| 853 | break; |
| 854 | input_len -= err; |
| 855 | input_offset += err; |
| 856 | } |
| 857 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 860 | static int git_index_pack_config(const char *k, const char *v, void *cb) |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 861 | { |
| 862 | if (!strcmp(k, "pack.indexversion")) { |
| 863 | pack_idx_default_version = git_config_int(k, v); |
| 864 | if (pack_idx_default_version > 2) |
Ramsay Jones | 6e1c234 | 2008-07-03 16:52:09 +0100 | [diff] [blame] | 865 | die("bad pack.indexversion=%"PRIu32, |
| 866 | pack_idx_default_version); |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 867 | return 0; |
| 868 | } |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 869 | return git_default_config(k, v, cb); |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 870 | } |
| 871 | |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 872 | int cmd_index_pack(int argc, const char **argv, const char *prefix) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 873 | { |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 874 | int i, fix_thin_pack = 0; |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 875 | const char *curr_pack, *curr_index; |
| 876 | const char *index_name = NULL, *pack_name = NULL; |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 877 | const char *keep_name = NULL, *keep_msg = NULL; |
| 878 | char *index_name_buf = NULL, *keep_name_buf = NULL; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 879 | struct pack_idx_entry **idx_objects; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 880 | unsigned char pack_sha1[20]; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 881 | |
Jonathan Nieder | 99caeed | 2009-11-09 09:05:01 -0600 | [diff] [blame] | 882 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 883 | usage(index_pack_usage); |
| 884 | |
Nelson Elhage | 6e2a09d | 2010-08-12 10:18:12 -0400 | [diff] [blame] | 885 | read_replace_refs = 0; |
| 886 | |
Nguyễn Thái Ngọc Duy | 3f8099f | 2010-07-24 06:30:49 -0500 | [diff] [blame] | 887 | git_config(git_index_pack_config, NULL); |
| 888 | if (prefix && chdir(prefix)) |
| 889 | die("Cannot come back to cwd"); |
Nicolas Pitre | 4d00bda | 2007-11-01 23:26:04 -0400 | [diff] [blame] | 890 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 891 | for (i = 1; i < argc; i++) { |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 892 | const char *arg = argv[i]; |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 893 | |
| 894 | if (*arg == '-') { |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 895 | if (!strcmp(arg, "--stdin")) { |
| 896 | from_stdin = 1; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 897 | } else if (!strcmp(arg, "--fix-thin")) { |
| 898 | fix_thin_pack = 1; |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 899 | } else if (!strcmp(arg, "--strict")) { |
| 900 | strict = 1; |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 901 | } else if (!strcmp(arg, "--keep")) { |
| 902 | keep_msg = ""; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 903 | } else if (!prefixcmp(arg, "--keep=")) { |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 904 | keep_msg = arg + 7; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 905 | } else if (!prefixcmp(arg, "--pack_header=")) { |
Nicolas Pitre | bed006f | 2006-11-01 17:06:20 -0500 | [diff] [blame] | 906 | struct pack_header *hdr; |
| 907 | char *c; |
| 908 | |
| 909 | hdr = (struct pack_header *)input_buffer; |
| 910 | hdr->hdr_signature = htonl(PACK_SIGNATURE); |
| 911 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); |
| 912 | if (*c != ',') |
| 913 | die("bad %s", arg); |
| 914 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); |
| 915 | if (*c) |
| 916 | die("bad %s", arg); |
| 917 | input_len = sizeof(*hdr); |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 918 | } else if (!strcmp(arg, "-v")) { |
| 919 | verbose = 1; |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 920 | } else if (!strcmp(arg, "-o")) { |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 921 | if (index_name || (i+1) >= argc) |
| 922 | usage(index_pack_usage); |
| 923 | index_name = argv[++i]; |
Nicolas Pitre | 4ba7d71 | 2007-04-09 17:32:03 -0400 | [diff] [blame] | 924 | } else if (!prefixcmp(arg, "--index-version=")) { |
| 925 | char *c; |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 926 | pack_idx_default_version = strtoul(arg + 16, &c, 10); |
| 927 | if (pack_idx_default_version > 2) |
Nicolas Pitre | 4ba7d71 | 2007-04-09 17:32:03 -0400 | [diff] [blame] | 928 | die("bad %s", arg); |
| 929 | if (*c == ',') |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 930 | pack_idx_off32_limit = strtoul(c+1, &c, 0); |
| 931 | if (*c || pack_idx_off32_limit & 0x80000000) |
Nicolas Pitre | 4ba7d71 | 2007-04-09 17:32:03 -0400 | [diff] [blame] | 932 | die("bad %s", arg); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 933 | } else |
| 934 | usage(index_pack_usage); |
| 935 | continue; |
| 936 | } |
| 937 | |
| 938 | if (pack_name) |
| 939 | usage(index_pack_usage); |
| 940 | pack_name = arg; |
| 941 | } |
| 942 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 943 | if (!pack_name && !from_stdin) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 944 | usage(index_pack_usage); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 945 | if (fix_thin_pack && !from_stdin) |
| 946 | die("--fix-thin cannot be used without --stdin"); |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 947 | if (!index_name && pack_name) { |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 948 | int len = strlen(pack_name); |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 949 | if (!has_extension(pack_name, ".pack")) |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 950 | die("packfile name '%s' does not end with '.pack'", |
| 951 | pack_name); |
Pavel Roskin | 6689f08 | 2005-12-21 15:35:48 -0500 | [diff] [blame] | 952 | index_name_buf = xmalloc(len); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 953 | memcpy(index_name_buf, pack_name, len - 5); |
| 954 | strcpy(index_name_buf + len - 5, ".idx"); |
| 955 | index_name = index_name_buf; |
| 956 | } |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 957 | if (keep_msg && !keep_name && pack_name) { |
| 958 | int len = strlen(pack_name); |
| 959 | if (!has_extension(pack_name, ".pack")) |
| 960 | die("packfile name '%s' does not end with '.pack'", |
| 961 | pack_name); |
| 962 | keep_name_buf = xmalloc(len); |
| 963 | memcpy(keep_name_buf, pack_name, len - 5); |
| 964 | strcpy(keep_name_buf + len - 5, ".keep"); |
| 965 | keep_name = keep_name_buf; |
| 966 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 967 | |
Nicolas Pitre | e42797f | 2006-10-23 14:50:18 -0400 | [diff] [blame] | 968 | curr_pack = open_pack_file(pack_name); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 969 | parse_pack_header(); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 970 | objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry)); |
| 971 | deltas = xmalloc(nr_objects * sizeof(struct delta_entry)); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 972 | parse_pack_objects(pack_sha1); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 973 | if (nr_deltas == nr_resolved_deltas) { |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 974 | stop_progress(&progress); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 975 | /* Flush remaining pack final 20-byte SHA1. */ |
| 976 | flush(); |
| 977 | } else { |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 978 | if (fix_thin_pack) { |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 979 | struct sha1file *f; |
| 980 | unsigned char read_sha1[20], tail_sha1[20]; |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 981 | char msg[48]; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 982 | int nr_unresolved = nr_deltas - nr_resolved_deltas; |
Nicolas Pitre | 3c9af36 | 2006-10-25 23:32:59 -0400 | [diff] [blame] | 983 | int nr_objects_initial = nr_objects; |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 984 | if (nr_unresolved <= 0) |
| 985 | die("confusion beyond insanity"); |
| 986 | objects = xrealloc(objects, |
| 987 | (nr_objects + nr_unresolved + 1) |
| 988 | * sizeof(*objects)); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 989 | f = sha1fd(output_fd, curr_pack); |
| 990 | fix_unresolved_deltas(f, nr_unresolved); |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 991 | sprintf(msg, "completed with %d local objects", |
| 992 | nr_objects - nr_objects_initial); |
| 993 | stop_progress_msg(&progress, msg); |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 994 | sha1close(f, tail_sha1, 0); |
| 995 | hashcpy(read_sha1, pack_sha1); |
| 996 | fixup_pack_header_footer(output_fd, pack_sha1, |
Nicolas Pitre | abeb40e | 2008-08-29 16:07:59 -0400 | [diff] [blame] | 997 | curr_pack, nr_objects, |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 998 | read_sha1, consumed_bytes-20); |
| 999 | if (hashcmp(read_sha1, tail_sha1) != 0) |
| 1000 | die("Unexpected tail checksum for %s " |
| 1001 | "(disk corruption?)", curr_pack); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 1002 | } |
| 1003 | if (nr_deltas != nr_resolved_deltas) |
| 1004 | die("pack has %d unresolved deltas", |
| 1005 | nr_deltas - nr_resolved_deltas); |
Nicolas Pitre | 636171c | 2006-10-25 23:28:17 -0400 | [diff] [blame] | 1006 | } |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1007 | free(deltas); |
Martin Koegler | 0153be0 | 2008-02-25 22:46:12 +0100 | [diff] [blame] | 1008 | if (strict) |
| 1009 | check_objects(); |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 1010 | |
| 1011 | idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *)); |
| 1012 | for (i = 0; i < nr_objects; i++) |
| 1013 | idx_objects[i] = &objects[i].idx; |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1014 | curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1); |
Geert Bosch | aa7e44b | 2007-06-01 15:18:05 -0400 | [diff] [blame] | 1015 | free(idx_objects); |
| 1016 | |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 1017 | final(pack_name, curr_pack, |
| 1018 | index_name, curr_index, |
| 1019 | keep_name, keep_msg, |
Nicolas Pitre | 8522148 | 2008-08-29 16:08:01 -0400 | [diff] [blame] | 1020 | pack_sha1); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1021 | free(objects); |
| 1022 | free(index_name_buf); |
Shawn Pearce | b807770 | 2006-10-29 04:41:59 -0500 | [diff] [blame] | 1023 | free(keep_name_buf); |
Nicolas Pitre | c85228e | 2007-10-16 21:55:50 -0400 | [diff] [blame] | 1024 | if (pack_name == NULL) |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 1025 | free((void *) curr_pack); |
Nicolas Pitre | c85228e | 2007-10-16 21:55:50 -0400 | [diff] [blame] | 1026 | if (index_name == NULL) |
Linus Torvalds | 3bb7256 | 2010-01-22 07:55:19 -0800 | [diff] [blame] | 1027 | free((void *) curr_index); |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1028 | |
Sergey Vlasov | 9cf6d33 | 2005-10-12 12:01:31 -0700 | [diff] [blame] | 1029 | return 0; |
| 1030 | } |