blob: a3a37bd215d844806814d677d2c7dfa71c8f31c6 [file] [log] [blame]
Ramsay Jones2ad9d4c2011-04-07 19:23:40 +01001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07003#include "delta.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00004#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00005#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00006#include "hex.h"
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07007#include "pack.h"
8#include "csum-file.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02009#include "blob.h"
10#include "commit.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020011#include "tree.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040012#include "progress.h"
Martin Koegler0153be02008-02-25 22:46:12 +010013#include "fsck.h"
Elijah Newrena034e912023-05-16 06:34:06 +000014#include "strbuf.h"
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +070015#include "streaming.h"
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +070016#include "thread-utils.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070017#include "packfile.h"
Elijah Newren75f273d2023-04-11 03:00:41 +000018#include "pack-revindex.h"
Elijah Newren87bed172023-04-11 00:41:53 -070019#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000020#include "object-store-ll.h"
Elijah Newren6f2d7432023-04-11 03:00:42 +000021#include "oid-array.h"
Elijah Newrencbeab742023-02-24 00:09:33 +000022#include "replace-object.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020023#include "promisor-remote.h"
Elijah Newrene38da482023-03-21 06:26:05 +000024#include "setup.h"
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070025
26static const char index_pack_usage[] =
John Cai0f8edf72024-02-01 01:38:02 +000027"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict[=<msg-id>=<severity>...]] [--fsck-objects[=<msg-id>=<severity>...]] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070028
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050029struct object_entry {
Geert Boschaa7e44b2007-06-01 15:18:05 -040030 struct pack_idx_entry idx;
Nicolas Pitre2d477052006-10-20 14:45:21 -040031 unsigned long size;
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +070032 unsigned char hdr_size;
33 signed char type;
34 signed char real_type;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070035};
36
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +070037struct object_stat {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070038 unsigned delta_depth;
39 int base_object_no;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040040};
41
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -040042struct base_data {
Jonathan Tanb4718ca2020-08-24 12:16:36 -070043 /* Initialized by make_base(). */
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -040044 struct base_data *base;
Shawn O. Pearce03993e12008-07-13 22:07:46 -040045 struct object_entry *obj;
Nguyễn Thái Ngọc Duy2baad222012-01-14 19:19:54 +070046 int ref_first, ref_last;
47 int ofs_first, ofs_last;
Jonathan Tanf08cbf62020-09-08 12:48:35 -070048 /*
49 * Threads should increment retain_data if they are about to call
50 * patch_delta() using this struct's data as a base, and decrement this
51 * when they are done. While retain_data is nonzero, this struct's data
52 * will not be freed even if the delta base cache limit is exceeded.
53 */
54 int retain_data;
55 /*
56 * The number of direct children that have not been fully processed
57 * (entered work_head, entered done_head, left done_head). When this
58 * number reaches zero, this struct base_data can be freed.
59 */
60 int children_remaining;
Jonathan Tanb4718ca2020-08-24 12:16:36 -070061
62 /* Not initialized by make_base(). */
Jonathan Tanf08cbf62020-09-08 12:48:35 -070063 struct list_head list;
Jonathan Tanb4718ca2020-08-24 12:16:36 -070064 void *data;
65 unsigned long size;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -040066};
67
Jonathan Tanf08cbf62020-09-08 12:48:35 -070068/*
69 * Stack of struct base_data that have unprocessed children.
70 * threaded_second_pass() uses this as a source of work (the other being the
71 * objects array).
72 *
73 * Guarded by work_mutex.
74 */
75static LIST_HEAD(work_head);
76
77/*
78 * Stack of struct base_data that have children, all of whom have been
79 * processed or are being processed, and at least one child is being processed.
80 * These struct base_data must be kept around until the last child is
81 * processed.
82 *
83 * Guarded by work_mutex.
84 */
85static LIST_HEAD(done_head);
86
87/*
88 * All threads share one delta base cache.
89 *
90 * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only
91 * in a thread.
92 */
93static size_t base_cache_used;
94static size_t base_cache_limit;
95
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +070096struct thread_local {
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +070097 pthread_t thread;
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +070098 int pack_fd;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +070099};
100
Nguyễn Thái Ngọc Duy95308d62018-03-06 17:16:14 +0700101/* Remember to update object flag allocation in object.h */
Martin Koegler0153be02008-02-25 22:46:12 +0100102#define FLAG_LINK (1u<<20)
103#define FLAG_CHECKED (1u<<21)
104
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700105struct ofs_delta_entry {
106 off_t offset;
107 int obj_no;
108};
109
110struct ref_delta_entry {
brian m. carlsonaf8caf32018-03-12 02:27:37 +0000111 struct object_id oid;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400112 int obj_no;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700113};
114
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700115static struct object_entry *objects;
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +0700116static struct object_stat *obj_stat;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700117static struct ofs_delta_entry *ofs_deltas;
118static struct ref_delta_entry *ref_deltas;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700119static struct thread_local nothread_data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700120static int nr_objects;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700121static int nr_ofs_deltas;
122static int nr_ref_deltas;
123static int ref_deltas_alloc;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400124static int nr_resolved_deltas;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700125static int nr_threads;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700126
Nicolas Pitree42797f2006-10-23 14:50:18 -0400127static int from_stdin;
Martin Koegler0153be02008-02-25 22:46:12 +0100128static int strict;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700129static int do_fsck_object;
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +0200130static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400131static int verbose;
Ævar Arnfjörð Bjarmasonf46c46e2021-09-05 09:34:44 +0200132static const char *progress_title;
Jeff Kinge376f172016-07-15 06:34:22 -0400133static int show_resolving_progress;
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700134static int show_stat;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700135static int check_self_contained_and_connected;
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400136
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400137static struct progress *progress;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400138
Nicolas Pitre2d477052006-10-20 14:45:21 -0400139/* We always read in 4kB chunks. */
140static unsigned char input_buffer[4096];
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400141static unsigned int input_offset, input_len;
142static off_t consumed_bytes;
Jeff King411481b2016-08-24 20:41:55 +0200143static off_t max_input_size;
Junio C Hamanod1a0ed12011-06-03 15:32:16 -0700144static unsigned deepest_delta;
brian m. carlson454253f2018-02-01 02:18:39 +0000145static git_hash_ctx input_ctx;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400146static uint32_t input_crc32;
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700147static int input_fd, output_fd;
148static const char *curr_pack;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400149
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700150static struct thread_local *thread_data;
151static int nr_dispatched;
152static int threads_active;
153
154static pthread_mutex_t read_mutex;
155#define read_lock() lock_mutex(&read_mutex)
156#define read_unlock() unlock_mutex(&read_mutex)
157
158static pthread_mutex_t counter_mutex;
159#define counter_lock() lock_mutex(&counter_mutex)
160#define counter_unlock() unlock_mutex(&counter_mutex)
161
162static pthread_mutex_t work_mutex;
163#define work_lock() lock_mutex(&work_mutex)
164#define work_unlock() unlock_mutex(&work_mutex)
165
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700166static pthread_mutex_t deepest_delta_mutex;
167#define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
168#define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
169
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700170static pthread_key_t key;
171
172static inline void lock_mutex(pthread_mutex_t *mutex)
173{
174 if (threads_active)
175 pthread_mutex_lock(mutex);
176}
177
178static inline void unlock_mutex(pthread_mutex_t *mutex)
179{
180 if (threads_active)
181 pthread_mutex_unlock(mutex);
182}
183
184/*
185 * Mutex and conditional variable can't be statically-initialized on Windows.
186 */
187static void init_thread(void)
188{
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700189 int i;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700190 init_recursive_mutex(&read_mutex);
191 pthread_mutex_init(&counter_mutex, NULL);
192 pthread_mutex_init(&work_mutex, NULL);
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700193 if (show_stat)
194 pthread_mutex_init(&deepest_delta_mutex, NULL);
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700195 pthread_key_create(&key, NULL);
René Scharfeca56dad2021-03-13 17:17:22 +0100196 CALLOC_ARRAY(thread_data, nr_threads);
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700197 for (i = 0; i < nr_threads; i++) {
René Scharfe6346f702021-09-10 22:25:50 +0200198 thread_data[i].pack_fd = xopen(curr_pack, O_RDONLY);
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700199 }
200
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700201 threads_active = 1;
202}
203
204static void cleanup_thread(void)
205{
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700206 int i;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700207 if (!threads_active)
208 return;
209 threads_active = 0;
210 pthread_mutex_destroy(&read_mutex);
211 pthread_mutex_destroy(&counter_mutex);
212 pthread_mutex_destroy(&work_mutex);
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700213 if (show_stat)
214 pthread_mutex_destroy(&deepest_delta_mutex);
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700215 for (i = 0; i < nr_threads; i++)
216 close(thread_data[i].pack_fd);
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700217 pthread_key_delete(key);
218 free(thread_data);
219}
220
Ævar Arnfjörð Bjarmasona1aad712021-03-28 15:15:35 +0200221static int mark_link(struct object *obj, enum object_type type,
Jeff King0b4e9012023-07-03 02:44:18 -0400222 void *data UNUSED,
223 struct fsck_options *options UNUSED)
Martin Koegler0153be02008-02-25 22:46:12 +0100224{
225 if (!obj)
226 return -1;
227
228 if (type != OBJ_ANY && obj->type != type)
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000229 die(_("object type mismatch at %s"), oid_to_hex(&obj->oid));
Martin Koegler0153be02008-02-25 22:46:12 +0100230
231 obj->flags |= FLAG_LINK;
232 return 0;
233}
234
235/* The content of each linked object must have been checked
236 or it must be already present in the object database */
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700237static unsigned check_object(struct object *obj)
Martin Koegler0153be02008-02-25 22:46:12 +0100238{
239 if (!obj)
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700240 return 0;
Martin Koegler0153be02008-02-25 22:46:12 +0100241
242 if (!(obj->flags & FLAG_LINK))
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700243 return 0;
Martin Koegler0153be02008-02-25 22:46:12 +0100244
245 if (!(obj->flags & FLAG_CHECKED)) {
246 unsigned long size;
Stefan Beller0df8e962018-04-25 11:20:59 -0700247 int type = oid_object_info(the_repository, &obj->oid, &size);
Jeff King77583e72014-05-12 00:38:39 -0400248 if (type <= 0)
249 die(_("did not receive expected object %s"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000250 oid_to_hex(&obj->oid));
Jeff King77583e72014-05-12 00:38:39 -0400251 if (type != obj->type)
252 die(_("object %s: expected type %s, found %s"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000253 oid_to_hex(&obj->oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800254 type_name(obj->type), type_name(type));
Martin Koegler0153be02008-02-25 22:46:12 +0100255 obj->flags |= FLAG_CHECKED;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700256 return 1;
Martin Koegler0153be02008-02-25 22:46:12 +0100257 }
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700258
259 return 0;
Martin Koegler0153be02008-02-25 22:46:12 +0100260}
261
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700262static unsigned check_objects(void)
Martin Koegler0153be02008-02-25 22:46:12 +0100263{
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700264 unsigned i, max, foreign_nr = 0;
Martin Koegler0153be02008-02-25 22:46:12 +0100265
266 max = get_max_object_index();
SZEDER Gábor79e3aa62019-04-01 01:12:35 +0200267
268 if (verbose)
269 progress = start_delayed_progress(_("Checking objects"), max);
270
271 for (i = 0; i < max; i++) {
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700272 foreign_nr += check_object(get_indexed_object(i));
SZEDER Gábor79e3aa62019-04-01 01:12:35 +0200273 display_progress(progress, i + 1);
274 }
275
276 stop_progress(&progress);
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700277 return foreign_nr;
Martin Koegler0153be02008-02-25 22:46:12 +0100278}
279
280
Nicolas Pitre636171c2006-10-25 23:28:17 -0400281/* Discard current buffer used content. */
Rene Scharfea6e8a762006-11-18 13:07:06 +0100282static void flush(void)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400283{
284 if (input_offset) {
285 if (output_fd >= 0)
286 write_or_die(output_fd, input_buffer, input_offset);
brian m. carlson454253f2018-02-01 02:18:39 +0000287 the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
Jim Meyering554a2632006-12-11 19:06:34 +0100288 memmove(input_buffer, input_buffer + input_offset, input_len);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400289 input_offset = 0;
290 }
291}
292
Nicolas Pitre2d477052006-10-20 14:45:21 -0400293/*
294 * Make sure at least "min" bytes are available in the buffer, and
295 * return the pointer to the buffer.
296 */
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400297static void *fill(int min)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400298{
299 if (min <= input_len)
300 return input_buffer + input_offset;
301 if (min > sizeof(input_buffer))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700302 die(Q_("cannot fill %d byte",
303 "cannot fill %d bytes",
304 min),
305 min);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400306 flush();
Nicolas Pitre2d477052006-10-20 14:45:21 -0400307 do {
Johan Herland8a912bc2007-05-15 14:49:22 +0200308 ssize_t ret = xread(input_fd, input_buffer + input_len,
Nicolas Pitre2d477052006-10-20 14:45:21 -0400309 sizeof(input_buffer) - input_len);
310 if (ret <= 0) {
311 if (!ret)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700312 die(_("early EOF"));
313 die_errno(_("read error on input"));
Nicolas Pitre2d477052006-10-20 14:45:21 -0400314 }
315 input_len += ret;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500316 if (from_stdin)
317 display_throughput(progress, consumed_bytes + input_len);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400318 } while (input_len < min);
319 return input_buffer;
320}
321
322static void use(int bytes)
323{
324 if (bytes > input_len)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700325 die(_("used more bytes than were available"));
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400326 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400327 input_len -= bytes;
328 input_offset += bytes;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400329
330 /* make sure off_t is sufficiently large not to wrap */
Erik Faye-Lundc03c8312010-10-05 09:24:10 +0200331 if (signed_add_overflows(consumed_bytes, bytes))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700332 die(_("pack too large for current definition of off_t"));
Nicolas Pitre2d477052006-10-20 14:45:21 -0400333 consumed_bytes += bytes;
Matt Cooper0cf5fbc2022-02-24 00:07:20 +0000334 if (max_input_size && consumed_bytes > max_input_size) {
335 struct strbuf size_limit = STRBUF_INIT;
336 strbuf_humanise_bytes(&size_limit, max_input_size);
337 die(_("pack exceeds maximum allowed size (%s)"),
338 size_limit.buf);
339 }
Nicolas Pitre2d477052006-10-20 14:45:21 -0400340}
341
Linus Torvalds3bb72562010-01-22 07:55:19 -0800342static const char *open_pack_file(const char *pack_name)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700343{
Nicolas Pitree42797f2006-10-23 14:50:18 -0400344 if (from_stdin) {
345 input_fd = 0;
346 if (!pack_name) {
Jeff King594fa992017-03-28 15:45:43 -0400347 struct strbuf tmp_file = STRBUF_INIT;
348 output_fd = odb_mkstemp(&tmp_file,
Junio C Hamano6e180cd2009-02-24 23:11:29 -0800349 "pack/tmp_pack_XXXXXX");
Jeff King594fa992017-03-28 15:45:43 -0400350 pack_name = strbuf_detach(&tmp_file, NULL);
Jeff King892e7232017-03-28 15:45:25 -0400351 } else {
René Scharfe66e905b2021-08-25 22:16:46 +0200352 output_fd = xopen(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
Jeff King892e7232017-03-28 15:45:25 -0400353 }
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700354 nothread_data.pack_fd = output_fd;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400355 } else {
René Scharfe66e905b2021-08-25 22:16:46 +0200356 input_fd = xopen(pack_name, O_RDONLY);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400357 output_fd = -1;
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +0700358 nothread_data.pack_fd = input_fd;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400359 }
brian m. carlson454253f2018-02-01 02:18:39 +0000360 the_hash_algo->init_fn(&input_ctx);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400361 return pack_name;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700362}
363
364static void parse_pack_header(void)
365{
Nicolas Pitre2d477052006-10-20 14:45:21 -0400366 struct pack_header *hdr = fill(sizeof(struct pack_header));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700367
368 /* Header consistency check */
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700369 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700370 die(_("pack signature mismatch"));
Nicolas Pitred60fc1c2006-02-09 17:50:04 -0500371 if (!pack_version_ok(hdr->hdr_version))
Nguyễn Thái Ngọc Duyf350df42012-08-31 19:13:04 +0700372 die(_("pack version %"PRIu32" unsupported"),
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100373 ntohl(hdr->hdr_version));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700374
375 nr_objects = ntohl(hdr->hdr_entries);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400376 use(sizeof(struct pack_header));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700377}
378
Ævar Arnfjörð Bjarmason103e02c2021-07-10 10:47:27 +0200379__attribute__((format (printf, 2, 3)))
Nguyễn Thái Ngọc Duyfd3e6742016-07-13 17:44:01 +0200380static NORETURN void bad_object(off_t offset, const char *format, ...)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700381{
382 va_list params;
383 char buf[1024];
384
385 va_start(params, format);
386 vsnprintf(buf, sizeof(buf), format, params);
387 va_end(params);
Nguyễn Thái Ngọc Duyfd3e6742016-07-13 17:44:01 +0200388 die(_("pack has bad object at offset %"PRIuMAX": %s"),
389 (uintmax_t)offset, buf);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700390}
391
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700392static inline struct thread_local *get_thread_data(void)
393{
Nguyễn Thái Ngọc Duy2094c5e2018-11-03 09:48:40 +0100394 if (HAVE_THREADS) {
395 if (threads_active)
396 return pthread_getspecific(key);
397 assert(!threads_active &&
398 "This should only be reached when all threads are gone");
399 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700400 return &nothread_data;
401}
402
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700403static void set_thread_data(struct thread_local *data)
404{
405 if (threads_active)
406 pthread_setspecific(key, data);
407}
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700408
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400409static void free_base_data(struct base_data *c)
410{
411 if (c->data) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000412 FREE_AND_NULL(c->data);
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700413 base_cache_used -= c->size;
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400414 }
415}
416
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000417static void prune_base_data(struct base_data *retain)
418{
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700419 struct list_head *pos;
Jonathan Tana7f7e842020-08-24 12:16:35 -0700420
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700421 if (base_cache_used <= base_cache_limit)
Jonathan Tana7f7e842020-08-24 12:16:35 -0700422 return;
423
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700424 list_for_each_prev(pos, &done_head) {
425 struct base_data *b = list_entry(pos, struct base_data, list);
426 if (b->retain_data || b == retain)
427 continue;
428 if (b->data) {
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400429 free_base_data(b);
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700430 if (base_cache_used <= base_cache_limit)
431 return;
432 }
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000433 }
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000434
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700435 list_for_each_prev(pos, &work_head) {
436 struct base_data *b = list_entry(pos, struct base_data, list);
437 if (b->retain_data || b == retain)
438 continue;
439 if (b->data) {
440 free_base_data(b);
441 if (base_cache_used <= base_cache_limit)
442 return;
443 }
Jonathan Tana7f7e842020-08-24 12:16:35 -0700444 }
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400445}
446
Nguyễn Thái Ngọc Duy681b07d2012-05-23 21:09:46 +0700447static int is_delta_type(enum object_type type)
448{
449 return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
450}
451
Nguyễn Thái Ngọc Duyda49a7d2016-07-13 17:44:02 +0200452static void *unpack_entry_data(off_t offset, unsigned long size,
brian m. carlson454253f2018-02-01 02:18:39 +0000453 enum object_type type, struct object_id *oid)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700454{
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700455 static char fixed_buf[8192];
Nicolas Pitre7ce47212010-04-12 12:12:06 -0400456 int status;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700457 git_zstream stream;
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700458 void *buf;
brian m. carlson454253f2018-02-01 02:18:39 +0000459 git_hash_ctx c;
Nguyễn Thái Ngọc Duy681b07d2012-05-23 21:09:46 +0700460 char hdr[32];
461 int hdrlen;
462
463 if (!is_delta_type(type)) {
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +0100464 hdrlen = format_object_header(hdr, sizeof(hdr), type, size);
brian m. carlson454253f2018-02-01 02:18:39 +0000465 the_hash_algo->init_fn(&c);
466 the_hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy681b07d2012-05-23 21:09:46 +0700467 } else
brian m. carlson454253f2018-02-01 02:18:39 +0000468 oid = NULL;
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700469 if (type == OBJ_BLOB && size > big_file_threshold)
470 buf = fixed_buf;
471 else
Duy Nguyena1e920a2014-12-08 15:17:55 +0100472 buf = xmallocz(size);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700473
474 memset(&stream, 0, sizeof(stream));
Nicolas Pitre7ce47212010-04-12 12:12:06 -0400475 git_inflate_init(&stream);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700476 stream.next_out = buf;
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700477 stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700478
Nicolas Pitre7ce47212010-04-12 12:12:06 -0400479 do {
Nguyễn Thái Ngọc Duy681b07d2012-05-23 21:09:46 +0700480 unsigned char *last_out = stream.next_out;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400481 stream.next_in = fill(1);
482 stream.avail_in = input_len;
Nicolas Pitre7ce47212010-04-12 12:12:06 -0400483 status = git_inflate(&stream, 0);
484 use(input_len - stream.avail_in);
brian m. carlson454253f2018-02-01 02:18:39 +0000485 if (oid)
486 the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700487 if (buf == fixed_buf) {
488 stream.next_out = buf;
489 stream.avail_out = sizeof(fixed_buf);
490 }
Nicolas Pitre7ce47212010-04-12 12:12:06 -0400491 } while (status == Z_OK);
492 if (stream.total_out != size || status != Z_STREAM_END)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700493 bad_object(offset, _("inflate returned %d"), status);
Linus Torvalds39c68542009-01-07 19:54:47 -0800494 git_inflate_end(&stream);
brian m. carlson454253f2018-02-01 02:18:39 +0000495 if (oid)
brian m. carlson5951bf42021-04-26 01:02:53 +0000496 the_hash_algo->final_oid_fn(oid, &c);
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700497 return buf == fixed_buf ? NULL : buf;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700498}
499
Nguyễn Thái Ngọc Duy681b07d2012-05-23 21:09:46 +0700500static void *unpack_raw_entry(struct object_entry *obj,
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700501 off_t *ofs_offset,
brian m. carlson454253f2018-02-01 02:18:39 +0000502 struct object_id *ref_oid,
503 struct object_id *oid)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700504{
Linus Torvalds48fb7de2009-06-17 17:22:27 -0700505 unsigned char *p;
506 unsigned long size, c;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400507 off_t base_offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700508 unsigned shift;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400509 void *data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700510
Geert Boschaa7e44b2007-06-01 15:18:05 -0400511 obj->idx.offset = consumed_bytes;
Stephen Boyd1e4cd682011-04-03 00:06:54 -0700512 input_crc32 = crc32(0, NULL, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400513
514 p = fill(1);
515 c = *p;
516 use(1);
517 obj->type = (c >> 4) & 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700518 size = (c & 15);
519 shift = 4;
520 while (c & 0x80) {
Nicolas Pitre2d477052006-10-20 14:45:21 -0400521 p = fill(1);
522 c = *p;
523 use(1);
Linus Torvalds48fb7de2009-06-17 17:22:27 -0700524 size += (c & 0x7f) << shift;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700525 shift += 7;
526 }
Nicolas Pitre2d477052006-10-20 14:45:21 -0400527 obj->size = size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700528
Nicolas Pitre2d477052006-10-20 14:45:21 -0400529 switch (obj->type) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400530 case OBJ_REF_DELTA:
brian m. carlson92e2cab2021-04-26 01:02:50 +0000531 oidread(ref_oid, fill(the_hash_algo->rawsz));
brian m. carlson454253f2018-02-01 02:18:39 +0000532 use(the_hash_algo->rawsz);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400533 break;
534 case OBJ_OFS_DELTA:
Nicolas Pitre2d477052006-10-20 14:45:21 -0400535 p = fill(1);
536 c = *p;
537 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400538 base_offset = c & 127;
539 while (c & 128) {
540 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400541 if (!base_offset || MSB(base_offset, 7))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700542 bad_object(obj->idx.offset, _("offset value overflow for delta base object"));
Nicolas Pitre2d477052006-10-20 14:45:21 -0400543 p = fill(1);
544 c = *p;
545 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400546 base_offset = (base_offset << 7) + (c & 127);
547 }
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700548 *ofs_offset = obj->idx.offset - base_offset;
549 if (*ofs_offset <= 0 || *ofs_offset >= obj->idx.offset)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700550 bad_object(obj->idx.offset, _("delta base offset is out of bound"));
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400551 break;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700552 case OBJ_COMMIT:
553 case OBJ_TREE:
554 case OBJ_BLOB:
555 case OBJ_TAG:
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700556 break;
557 default:
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700558 bad_object(obj->idx.offset, _("unknown object type %d"), obj->type);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700559 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400560 obj->hdr_size = consumed_bytes - obj->idx.offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700561
brian m. carlson454253f2018-02-01 02:18:39 +0000562 data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid);
Geert Boschaa7e44b2007-06-01 15:18:05 -0400563 obj->idx.crc32 = input_crc32;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400564 return data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400565}
566
Nguyễn Thái Ngọc Duy8a2e1632012-05-23 21:09:48 +0700567static void *unpack_data(struct object_entry *obj,
568 int (*consume)(const unsigned char *, unsigned long, void *),
569 void *cb_data)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400570{
Nicolas Pitrea91ef6e2007-11-10 23:29:10 -0500571 off_t from = obj[0].idx.offset + obj[0].hdr_size;
Nguyễn Thái Ngọc Duy7171a0b2016-07-13 17:44:00 +0200572 off_t len = obj[1].idx.offset - from;
Nicolas Pitre776ea372010-04-12 12:11:07 -0400573 unsigned char *data, *inbuf;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700574 git_zstream stream;
Nicolas Pitre776ea372010-04-12 12:11:07 -0400575 int status;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400576
Duy Nguyena1e920a2014-12-08 15:17:55 +0100577 data = xmallocz(consume ? 64*1024 : obj->size);
Nguyễn Thái Ngọc Duy7171a0b2016-07-13 17:44:00 +0200578 inbuf = xmalloc((len < 64*1024) ? (int)len : 64*1024);
Nicolas Pitre776ea372010-04-12 12:11:07 -0400579
580 memset(&stream, 0, sizeof(stream));
581 git_inflate_init(&stream);
582 stream.next_out = data;
Nguyễn Thái Ngọc Duy8a2e1632012-05-23 21:09:48 +0700583 stream.avail_out = consume ? 64*1024 : obj->size;
Nicolas Pitre776ea372010-04-12 12:11:07 -0400584
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500585 do {
Nguyễn Thái Ngọc Duy7171a0b2016-07-13 17:44:00 +0200586 ssize_t n = (len < 64*1024) ? (ssize_t)len : 64*1024;
Junio C Hamano53f52cd2014-06-03 12:06:42 -0700587 n = xpread(get_thread_data()->pack_fd, inbuf, n, from);
Samuel Tardieufb742432008-10-06 19:28:41 +0200588 if (n < 0)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700589 die_errno(_("cannot pread pack file"));
Samuel Tardieufb742432008-10-06 19:28:41 +0200590 if (!n)
Nguyễn Thái Ngọc Duy7171a0b2016-07-13 17:44:00 +0200591 die(Q_("premature end of pack file, %"PRIuMAX" byte missing",
592 "premature end of pack file, %"PRIuMAX" bytes missing",
Ævar Arnfjörð Bjarmason6f693252022-03-07 16:27:07 +0100593 len),
Nguyễn Thái Ngọc Duy7171a0b2016-07-13 17:44:00 +0200594 (uintmax_t)len);
Nicolas Pitre776ea372010-04-12 12:11:07 -0400595 from += n;
596 len -= n;
597 stream.next_in = inbuf;
598 stream.avail_in = n;
Jeff Kingf8b09032012-07-04 03:12:14 -0400599 if (!consume)
600 status = git_inflate(&stream, 0);
601 else {
602 do {
603 status = git_inflate(&stream, 0);
604 if (consume(data, stream.next_out - data, cb_data)) {
605 free(inbuf);
606 free(data);
607 return NULL;
608 }
609 stream.next_out = data;
610 stream.avail_out = 64*1024;
611 } while (status == Z_OK && stream.avail_in);
Nguyễn Thái Ngọc Duy8a2e1632012-05-23 21:09:48 +0700612 }
Nicolas Pitre776ea372010-04-12 12:11:07 -0400613 } while (len && status == Z_OK && !stream.avail_in);
614
615 /* This has been inflated OK when first encountered, so... */
616 if (status != Z_STREAM_END || stream.total_out != obj->size)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700617 die(_("serious inflate inconsistency"));
Nicolas Pitre776ea372010-04-12 12:11:07 -0400618
619 git_inflate_end(&stream);
620 free(inbuf);
Nguyễn Thái Ngọc Duy8a2e1632012-05-23 21:09:48 +0700621 if (consume) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000622 FREE_AND_NULL(data);
Nguyễn Thái Ngọc Duy8a2e1632012-05-23 21:09:48 +0700623 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700624 return data;
625}
626
Nguyễn Thái Ngọc Duy8a2e1632012-05-23 21:09:48 +0700627static void *get_data_from_pack(struct object_entry *obj)
628{
629 return unpack_data(obj, NULL, NULL);
630}
631
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700632static int compare_ofs_delta_bases(off_t offset1, off_t offset2,
633 enum object_type type1,
634 enum object_type type2)
Junio C Hamano7218a212011-02-02 10:06:51 -0800635{
636 int cmp = type1 - type2;
637 if (cmp)
638 return cmp;
Jeff Kingf0e7f112015-06-04 08:35:42 -0400639 return offset1 < offset2 ? -1 :
640 offset1 > offset2 ? 1 :
641 0;
Junio C Hamano7218a212011-02-02 10:06:51 -0800642}
643
Jonathan Tanfc968e22020-08-24 12:16:33 -0700644static int find_ofs_delta(const off_t offset)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700645{
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700646 int first = 0, last = nr_ofs_deltas;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700647
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700648 while (first < last) {
Derrick Stolee19716b22017-10-08 14:29:37 -0400649 int next = first + (last - first) / 2;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700650 struct ofs_delta_entry *delta = &ofs_deltas[next];
651 int cmp;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700652
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700653 cmp = compare_ofs_delta_bases(offset, delta->offset,
Jonathan Tanfc968e22020-08-24 12:16:33 -0700654 OBJ_OFS_DELTA,
655 objects[delta->obj_no].type);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700656 if (!cmp)
657 return next;
658 if (cmp < 0) {
659 last = next;
660 continue;
661 }
662 first = next+1;
663 }
664 return -first-1;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700665}
666
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700667static void find_ofs_delta_children(off_t offset,
Jonathan Tanfc968e22020-08-24 12:16:33 -0700668 int *first_index, int *last_index)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700669{
Jonathan Tanfc968e22020-08-24 12:16:33 -0700670 int first = find_ofs_delta(offset);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700671 int last = first;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700672 int end = nr_ofs_deltas - 1;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700673
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400674 if (first < 0) {
675 *first_index = 0;
676 *last_index = -1;
677 return;
678 }
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700679 while (first > 0 && ofs_deltas[first - 1].offset == offset)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700680 --first;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700681 while (last < end && ofs_deltas[last + 1].offset == offset)
682 ++last;
683 *first_index = first;
684 *last_index = last;
685}
686
brian m. carlsonaf8caf32018-03-12 02:27:37 +0000687static int compare_ref_delta_bases(const struct object_id *oid1,
688 const struct object_id *oid2,
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700689 enum object_type type1,
690 enum object_type type2)
691{
692 int cmp = type1 - type2;
693 if (cmp)
694 return cmp;
brian m. carlsonaf8caf32018-03-12 02:27:37 +0000695 return oidcmp(oid1, oid2);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700696}
697
Jonathan Tanfc968e22020-08-24 12:16:33 -0700698static int find_ref_delta(const struct object_id *oid)
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700699{
700 int first = 0, last = nr_ref_deltas;
701
702 while (first < last) {
Derrick Stolee19716b22017-10-08 14:29:37 -0400703 int next = first + (last - first) / 2;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700704 struct ref_delta_entry *delta = &ref_deltas[next];
705 int cmp;
706
brian m. carlsonaf8caf32018-03-12 02:27:37 +0000707 cmp = compare_ref_delta_bases(oid, &delta->oid,
Jonathan Tanfc968e22020-08-24 12:16:33 -0700708 OBJ_REF_DELTA,
709 objects[delta->obj_no].type);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700710 if (!cmp)
711 return next;
712 if (cmp < 0) {
713 last = next;
714 continue;
715 }
716 first = next+1;
717 }
718 return -first-1;
719}
720
brian m. carlsonaf8caf32018-03-12 02:27:37 +0000721static void find_ref_delta_children(const struct object_id *oid,
Jonathan Tanfc968e22020-08-24 12:16:33 -0700722 int *first_index, int *last_index)
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700723{
Jonathan Tanfc968e22020-08-24 12:16:33 -0700724 int first = find_ref_delta(oid);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700725 int last = first;
726 int end = nr_ref_deltas - 1;
727
728 if (first < 0) {
729 *first_index = 0;
730 *last_index = -1;
731 return;
732 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400733 while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid))
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +0700734 --first;
Jeff King4a7e27e2018-08-28 17:22:40 -0400735 while (last < end && oideq(&ref_deltas[last + 1].oid, oid))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700736 ++last;
737 *first_index = first;
738 *last_index = last;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700739}
740
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700741struct compare_data {
742 struct object_entry *entry;
743 struct git_istream *st;
744 unsigned char *buf;
745 unsigned long buf_size;
746};
747
748static int compare_objects(const unsigned char *buf, unsigned long size,
749 void *cb_data)
750{
751 struct compare_data *data = cb_data;
752
753 if (data->buf_size < size) {
754 free(data->buf);
755 data->buf = xmalloc(size);
756 data->buf_size = size;
757 }
758
759 while (size) {
760 ssize_t len = read_istream(data->st, data->buf, size);
761 if (len == 0)
762 die(_("SHA1 COLLISION FOUND WITH %s !"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000763 oid_to_hex(&data->entry->idx.oid));
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700764 if (len < 0)
765 die(_("unable to read %s"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000766 oid_to_hex(&data->entry->idx.oid));
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700767 if (memcmp(buf, data->buf, len))
768 die(_("SHA1 COLLISION FOUND WITH %s !"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000769 oid_to_hex(&data->entry->idx.oid));
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700770 size -= len;
771 buf += len;
772 }
773 return 0;
774}
775
776static int check_collison(struct object_entry *entry)
777{
778 struct compare_data data;
779 enum object_type type;
780 unsigned long size;
781
782 if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB)
783 return -1;
784
785 memset(&data, 0, sizeof(data));
786 data.entry = entry;
Matheus Tavaresc8123e72020-01-30 17:32:20 -0300787 data.st = open_istream(the_repository, &entry->idx.oid, &type, &size,
788 NULL);
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700789 if (!data.st)
790 return -1;
791 if (size != entry->size || type != entry->type)
792 die(_("SHA1 COLLISION FOUND WITH %s !"),
brian m. carlsone6a492b2017-05-06 22:10:11 +0000793 oid_to_hex(&entry->idx.oid));
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700794 unpack_data(entry, compare_objects, &data);
795 close_istream(data.st);
796 free(data.buf);
797 return 0;
798}
799
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700800static void sha1_object(const void *data, struct object_entry *obj_entry,
801 unsigned long size, enum object_type type,
brian m. carlson3e930982017-05-06 22:10:13 +0000802 const struct object_id *oid)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700803{
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700804 void *new_data = NULL;
Jeff King29401e12016-12-16 16:43:22 -0500805 int collision_test_needed = 0;
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700806
807 assert(data || obj_entry);
808
Jeff King29401e12016-12-16 16:43:22 -0500809 if (startup_info->have_repository) {
810 read_lock();
Jonathan Tane83e71c2017-06-21 17:40:24 -0700811 collision_test_needed =
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200812 repo_has_object_file_with_flags(the_repository, oid,
813 OBJECT_INFO_QUICK);
Jeff King29401e12016-12-16 16:43:22 -0500814 read_unlock();
815 }
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700816
817 if (collision_test_needed && !data) {
818 read_lock();
819 if (!check_collison(obj_entry))
820 collision_test_needed = 0;
821 read_unlock();
822 }
823 if (collision_test_needed) {
Nicolas Pitre8685da42007-03-20 15:32:35 -0400824 void *has_data;
825 enum object_type has_type;
826 unsigned long has_size;
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700827 read_lock();
Stefan Beller0df8e962018-04-25 11:20:59 -0700828 has_type = oid_object_info(the_repository, oid, &has_size);
Jeff King51054172017-04-01 04:09:32 -0400829 if (has_type < 0)
brian m. carlson3e930982017-05-06 22:10:13 +0000830 die(_("cannot read existing object info %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700831 if (has_type != type || has_size != size)
brian m. carlson3e930982017-05-06 22:10:13 +0000832 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200833 has_data = repo_read_object_file(the_repository, oid,
834 &has_type, &has_size);
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700835 read_unlock();
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700836 if (!data)
837 data = new_data = get_data_from_pack(obj_entry);
Nicolas Pitre8685da42007-03-20 15:32:35 -0400838 if (!has_data)
brian m. carlson3e930982017-05-06 22:10:13 +0000839 die(_("cannot read existing object %s"), oid_to_hex(oid));
Nicolas Pitre8685da42007-03-20 15:32:35 -0400840 if (size != has_size || type != has_type ||
841 memcmp(data, has_data, size) != 0)
brian m. carlson3e930982017-05-06 22:10:13 +0000842 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
Nicolas Pitrebbf4b412007-04-03 12:33:46 -0400843 free(has_data);
Nguyễn Thái Ngọc Duy46140432012-05-24 20:55:44 +0700844 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700845
Jonathan Tanffb2c0f2018-03-14 11:42:40 -0700846 if (strict || do_fsck_object) {
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700847 read_lock();
Martin Koegler0153be02008-02-25 22:46:12 +0100848 if (type == OBJ_BLOB) {
Stefan Bellerda14a7f2018-06-28 18:21:55 -0700849 struct blob *blob = lookup_blob(the_repository, oid);
Martin Koegler0153be02008-02-25 22:46:12 +0100850 if (blob)
851 blob->object.flags |= FLAG_CHECKED;
852 else
brian m. carlson3e930982017-05-06 22:10:13 +0000853 die(_("invalid blob object %s"), oid_to_hex(oid));
Jeff King73c3f0f2018-05-04 19:45:01 -0400854 if (do_fsck_object &&
855 fsck_object(&blob->object, (void *)data, size, &fsck_options))
856 die(_("fsck error in packed object"));
Martin Koegler0153be02008-02-25 22:46:12 +0100857 } else {
858 struct object *obj;
859 int eaten;
860 void *buf = (void *) data;
861
Nguyễn Thái Ngọc Duy920734b2013-05-26 08:16:16 +0700862 assert(data && "data can only be NULL for large _blobs_");
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700863
Martin Koegler0153be02008-02-25 22:46:12 +0100864 /*
865 * we do not need to free the memory here, as the
866 * buf is deleted by the caller.
867 */
Stefan Beller1ec5bfd2018-06-28 18:21:53 -0700868 obj = parse_object_buffer(the_repository, oid, type,
869 size, buf,
brian m. carlsonc251c832017-05-06 22:10:38 +0000870 &eaten);
Martin Koegler0153be02008-02-25 22:46:12 +0100871 if (!obj)
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800872 die(_("invalid %s"), type_name(type));
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700873 if (do_fsck_object &&
Johannes Schindelin22410542015-06-22 17:25:00 +0200874 fsck_object(obj, buf, size, &fsck_options))
Jeff Kingdb5a58c2018-05-02 16:37:09 -0400875 die(_("fsck error in packed object"));
Jonathan Tanffb2c0f2018-03-14 11:42:40 -0700876 if (strict && fsck_walk(obj, NULL, &fsck_options))
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000877 die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
Martin Koegler0153be02008-02-25 22:46:12 +0100878
879 if (obj->type == OBJ_TREE) {
880 struct tree *item = (struct tree *) obj;
881 item->buffer = NULL;
Jeff King6e454b92013-06-05 18:37:39 -0400882 obj->parsed = 0;
Martin Koegler0153be02008-02-25 22:46:12 +0100883 }
884 if (obj->type == OBJ_COMMIT) {
885 struct commit *commit = (struct commit *) obj;
Jeff King8597ea32014-06-10 17:44:13 -0400886 if (detach_commit_buffer(commit, NULL) != data)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200887 BUG("parse_object_buffer transmogrified our buffer");
Martin Koegler0153be02008-02-25 22:46:12 +0100888 }
889 obj->flags |= FLAG_CHECKED;
890 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700891 read_unlock();
Martin Koegler0153be02008-02-25 22:46:12 +0100892 }
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +0700893
894 free(new_data);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700895}
896
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700897/*
Jonathan Tanec6a8f92020-10-07 13:16:58 -0700898 * Ensure that this node has been reconstructed and return its contents.
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700899 *
Jonathan Tanec6a8f92020-10-07 13:16:58 -0700900 * In the typical and best case, this node would already be reconstructed
901 * (through the invocation to resolve_delta() in threaded_second_pass()) and it
902 * would not be pruned. However, if pruning of this node was necessary due to
903 * reaching delta_base_cache_limit, this function will find the closest
904 * ancestor with reconstructed data that has not been pruned (or if there is
905 * none, the ultimate base object), and reconstruct each node in the delta
906 * chain in order to generate the reconstructed data for this node.
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700907 */
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000908static void *get_base_data(struct base_data *c)
909{
910 if (!c->data) {
911 struct object_entry *obj = c->obj;
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700912 struct base_data **delta = NULL;
913 int delta_nr = 0, delta_alloc = 0;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000914
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700915 while (is_delta_type(c->obj->type) && !c->data) {
916 ALLOC_GROW(delta, delta_nr + 1, delta_alloc);
917 delta[delta_nr++] = c;
918 c = c->base;
919 }
920 if (!delta_nr) {
921 c->data = get_data_from_pack(obj);
922 c->size = obj->size;
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700923 base_cache_used += c->size;
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700924 prune_base_data(c);
925 }
926 for (; delta_nr > 0; delta_nr--) {
927 void *base, *raw;
928 c = delta[delta_nr - 1];
929 obj = c->obj;
930 base = get_base_data(c->base);
931 raw = get_data_from_pack(obj);
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000932 c->data = patch_delta(
933 base, c->base->size,
934 raw, obj->size,
935 &c->size);
936 free(raw);
937 if (!c->data)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700938 bad_object(obj->idx.offset, _("failed to apply delta"));
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700939 base_cache_used += c->size;
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700940 prune_base_data(c);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400941 }
Nguyễn Thái Ngọc Duy20e95d02012-01-14 19:19:55 +0700942 free(delta);
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000943 }
944 return c->data;
945}
946
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700947static struct base_data *make_base(struct object_entry *obj,
948 struct base_data *parent)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700949{
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700950 struct base_data *base = xcalloc(1, sizeof(struct base_data));
951 base->base = parent;
952 base->obj = obj;
953 find_ref_delta_children(&obj->idx.oid,
954 &base->ref_first, &base->ref_last);
955 find_ofs_delta_children(obj->idx.offset,
956 &base->ofs_first, &base->ofs_last);
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700957 base->children_remaining = base->ref_last - base->ref_first +
958 base->ofs_last - base->ofs_first + 2;
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700959 return base;
960}
961
962static struct base_data *resolve_delta(struct object_entry *delta_obj,
963 struct base_data *base)
964{
Jonathan Tanee6f0582020-08-24 12:16:37 -0700965 void *delta_data, *result_data;
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700966 struct base_data *result;
967 unsigned long result_size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700968
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700969 if (show_stat) {
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +0700970 int i = delta_obj - objects;
971 int j = base->obj - objects;
972 obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1;
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700973 deepest_delta_lock();
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +0700974 if (deepest_delta < obj_stat[i].delta_depth)
975 deepest_delta = obj_stat[i].delta_depth;
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700976 deepest_delta_unlock();
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +0700977 obj_stat[i].base_object_no = j;
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +0700978 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400979 delta_data = get_data_from_pack(delta_obj);
Jonathan Tanee6f0582020-08-24 12:16:37 -0700980 assert(base->data);
981 result_data = patch_delta(base->data, base->size,
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700982 delta_data, delta_obj->size, &result_size);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700983 free(delta_data);
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700984 if (!result_data)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +0700985 bad_object(delta_obj->idx.offset, _("failed to apply delta"));
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700986 hash_object_file(the_hash_algo, result_data, result_size,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100987 delta_obj->real_type, &delta_obj->idx.oid);
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700988 sha1_object(result_data, NULL, result_size, delta_obj->real_type,
brian m. carlson3e930982017-05-06 22:10:13 +0000989 &delta_obj->idx.oid);
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700990
991 result = make_base(delta_obj, base);
Jonathan Tanf08cbf62020-09-08 12:48:35 -0700992 result->data = result_data;
993 result->size = result_size;
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700994
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700995 counter_lock();
Nicolas Pitre636171c2006-10-25 23:28:17 -0400996 nr_resolved_deltas++;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +0700997 counter_unlock();
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400998
Jonathan Tanb4718ca2020-08-24 12:16:36 -0700999 return result;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001000}
1001
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001002static int compare_ofs_delta_entry(const void *a, const void *b)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001003{
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001004 const struct ofs_delta_entry *delta_a = a;
1005 const struct ofs_delta_entry *delta_b = b;
Junio C Hamano7218a212011-02-02 10:06:51 -08001006
Jeff Kingf0e7f112015-06-04 08:35:42 -04001007 return delta_a->offset < delta_b->offset ? -1 :
1008 delta_a->offset > delta_b->offset ? 1 :
1009 0;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001010}
1011
1012static int compare_ref_delta_entry(const void *a, const void *b)
1013{
1014 const struct ref_delta_entry *delta_a = a;
1015 const struct ref_delta_entry *delta_b = b;
1016
brian m. carlsonaf8caf32018-03-12 02:27:37 +00001017 return oidcmp(&delta_a->oid, &delta_b->oid);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001018}
1019
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001020static void *threaded_second_pass(void *data)
1021{
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001022 if (data)
1023 set_thread_data(data);
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001024 for (;;) {
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001025 struct base_data *parent = NULL;
1026 struct object_entry *child_obj;
1027 struct base_data *child;
1028
Jeff Kingcea69152020-10-07 14:19:23 -04001029 counter_lock();
1030 display_progress(progress, nr_resolved_deltas);
1031 counter_unlock();
1032
Thomas Rast8f82aad2013-03-19 15:16:41 +01001033 work_lock();
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001034 if (list_empty(&work_head)) {
1035 /*
1036 * Take an object from the object array.
1037 */
1038 while (nr_dispatched < nr_objects &&
1039 is_delta_type(objects[nr_dispatched].type))
1040 nr_dispatched++;
1041 if (nr_dispatched >= nr_objects) {
1042 work_unlock();
1043 break;
1044 }
1045 child_obj = &objects[nr_dispatched++];
1046 } else {
1047 /*
1048 * Peek at the top of the stack, and take a child from
1049 * it.
1050 */
1051 parent = list_first_entry(&work_head, struct base_data,
1052 list);
1053
1054 if (parent->ref_first <= parent->ref_last) {
1055 int offset = ref_deltas[parent->ref_first++].obj_no;
1056 child_obj = objects + offset;
1057 if (child_obj->real_type != OBJ_REF_DELTA)
1058 die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
1059 (uintmax_t) child_obj->idx.offset,
1060 oid_to_hex(&parent->obj->idx.oid));
1061 child_obj->real_type = parent->obj->real_type;
1062 } else {
1063 child_obj = objects +
1064 ofs_deltas[parent->ofs_first++].obj_no;
1065 assert(child_obj->real_type == OBJ_OFS_DELTA);
1066 child_obj->real_type = parent->obj->real_type;
1067 }
1068
1069 if (parent->ref_first > parent->ref_last &&
1070 parent->ofs_first > parent->ofs_last) {
1071 /*
1072 * This parent has run out of children, so move
1073 * it to done_head.
1074 */
1075 list_del(&parent->list);
1076 list_add(&parent->list, &done_head);
1077 }
1078
1079 /*
1080 * Ensure that the parent has data, since we will need
1081 * it later.
1082 *
1083 * NEEDSWORK: If parent data needs to be reloaded, this
1084 * prolongs the time that the current thread spends in
1085 * the mutex. A mitigating factor is that parent data
1086 * needs to be reloaded only if the delta base cache
1087 * limit is exceeded, so in the typical case, this does
1088 * not happen.
1089 */
1090 get_base_data(parent);
1091 parent->retain_data++;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001092 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001093 work_unlock();
1094
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001095 if (parent) {
1096 child = resolve_delta(child_obj, parent);
1097 if (!child->children_remaining)
1098 FREE_AND_NULL(child->data);
1099 } else {
1100 child = make_base(child_obj, NULL);
1101 if (child->children_remaining) {
1102 /*
1103 * Since this child has its own delta children,
1104 * we will need this data in the future.
1105 * Inflate now so that future iterations will
1106 * have access to this object's data while
1107 * outside the work mutex.
1108 */
1109 child->data = get_data_from_pack(child_obj);
1110 child->size = child_obj->size;
1111 }
1112 }
1113
1114 work_lock();
1115 if (parent)
1116 parent->retain_data--;
1117 if (child->data) {
1118 /*
1119 * This child has its own children, so add it to
1120 * work_head.
1121 */
1122 list_add(&child->list, &work_head);
1123 base_cache_used += child->size;
1124 prune_base_data(NULL);
Ævar Arnfjörð Bjarmasonf2bcc692022-03-04 19:32:04 +01001125 free_base_data(child);
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001126 } else {
1127 /*
1128 * This child does not have its own children. It may be
1129 * the last descendant of its ancestors; free those
1130 * that we can.
1131 */
1132 struct base_data *p = parent;
1133
1134 while (p) {
1135 struct base_data *next_p;
1136
1137 p->children_remaining--;
1138 if (p->children_remaining)
1139 break;
1140
1141 next_p = p->base;
1142 free_base_data(p);
1143 list_del(&p->list);
1144 free(p);
1145
1146 p = next_p;
1147 }
Ævar Arnfjörð Bjarmasonf2bcc692022-03-04 19:32:04 +01001148 FREE_AND_NULL(child);
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001149 }
1150 work_unlock();
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001151 }
1152 return NULL;
1153}
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001154
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001155/*
1156 * First pass:
1157 * - find locations of all objects;
1158 * - calculate SHA1 of all non-delta objects;
1159 * - remember base (SHA1 or offset) for all deltas.
1160 */
brian m. carlson454253f2018-02-01 02:18:39 +00001161static void parse_pack_objects(unsigned char *hash)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001162{
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +07001163 int i, nr_delays = 0;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001164 struct ofs_delta_entry *ofs_delta = ofs_deltas;
brian m. carlson454253f2018-02-01 02:18:39 +00001165 struct object_id ref_delta_oid;
Nicolas Pitre2d477052006-10-20 14:45:21 -04001166 struct stat st;
Eric Wonge0b8c842023-09-01 02:09:28 +00001167 git_hash_ctx tmp_ctx;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001168
Nicolas Pitre13aaf142007-04-20 14:10:07 -04001169 if (verbose)
Nicolas Pitre29e63ed2007-10-30 14:57:35 -04001170 progress = start_progress(
Ævar Arnfjörð Bjarmasonf46c46e2021-09-05 09:34:44 +02001171 progress_title ? progress_title :
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001172 from_stdin ? _("Receiving objects") : _("Indexing objects"),
Nicolas Pitre29e63ed2007-10-30 14:57:35 -04001173 nr_objects);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001174 for (i = 0; i < nr_objects; i++) {
1175 struct object_entry *obj = &objects[i];
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001176 void *data = unpack_raw_entry(obj, &ofs_delta->offset,
brian m. carlson454253f2018-02-01 02:18:39 +00001177 &ref_delta_oid,
1178 &obj->idx.oid);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001179 obj->real_type = obj->type;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001180 if (obj->type == OBJ_OFS_DELTA) {
1181 nr_ofs_deltas++;
1182 ofs_delta->obj_no = i;
1183 ofs_delta++;
1184 } else if (obj->type == OBJ_REF_DELTA) {
1185 ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
brian m. carlsonaf8caf32018-03-12 02:27:37 +00001186 oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001187 ref_deltas[nr_ref_deltas].obj_no = i;
1188 nr_ref_deltas++;
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +07001189 } else if (!data) {
1190 /* large blobs, check later */
1191 obj->real_type = OBJ_BAD;
1192 nr_delays++;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001193 } else
brian m. carlsone6a492b2017-05-06 22:10:11 +00001194 sha1_object(data, NULL, obj->size, obj->type,
brian m. carlson3e930982017-05-06 22:10:13 +00001195 &obj->idx.oid);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001196 free(data);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04001197 display_progress(progress, i+1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001198 }
Geert Boschaa7e44b2007-06-01 15:18:05 -04001199 objects[i].idx.offset = consumed_bytes;
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04001200 stop_progress(&progress);
Nicolas Pitre2d477052006-10-20 14:45:21 -04001201
1202 /* Check pack integrity */
Nicolas Pitre636171c2006-10-25 23:28:17 -04001203 flush();
Eric Wonge0b8c842023-09-01 02:09:28 +00001204 the_hash_algo->init_fn(&tmp_ctx);
1205 the_hash_algo->clone_fn(&tmp_ctx, &input_ctx);
1206 the_hash_algo->final_fn(hash, &tmp_ctx);
Jeff King67947c32018-08-28 17:22:52 -04001207 if (!hasheq(fill(the_hash_algo->rawsz), hash))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001208 die(_("pack is corrupted (SHA1 mismatch)"));
brian m. carlson454253f2018-02-01 02:18:39 +00001209 use(the_hash_algo->rawsz);
Nicolas Pitre2d477052006-10-20 14:45:21 -04001210
1211 /* If input_fd is a file, we should have reached its end now. */
1212 if (fstat(input_fd, &st))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001213 die_errno(_("cannot fstat packfile"));
Johannes Schindelinfa257b02007-02-22 19:14:14 +01001214 if (S_ISREG(st.st_mode) &&
1215 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001216 die(_("pack has junk at the end"));
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +07001217
1218 for (i = 0; i < nr_objects; i++) {
1219 struct object_entry *obj = &objects[i];
1220 if (obj->real_type != OBJ_BAD)
1221 continue;
1222 obj->real_type = obj->type;
brian m. carlsone6a492b2017-05-06 22:10:11 +00001223 sha1_object(NULL, obj, obj->size, obj->type,
brian m. carlson3e930982017-05-06 22:10:13 +00001224 &obj->idx.oid);
Nguyễn Thái Ngọc Duy9ec2dde2012-05-23 21:09:47 +07001225 nr_delays--;
1226 }
1227 if (nr_delays)
1228 die(_("confusion beyond insanity in parse_pack_objects()"));
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001229}
1230
1231/*
1232 * Second pass:
1233 * - for all non-delta objects, look if it is used as a base for
1234 * deltas;
1235 * - if used as a base, uncompress the object and apply all deltas,
1236 * recursively checking if the resulting object is used as a base
1237 * for some more deltas.
1238 */
1239static void resolve_deltas(void)
1240{
1241 int i;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001242
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001243 if (!nr_ofs_deltas && !nr_ref_deltas)
Nicolas Pitre3c9af362006-10-25 23:32:59 -04001244 return;
1245
Nicolas Pitre53dda6f2006-09-21 00:08:33 -04001246 /* Sort deltas by base SHA1/offset for fast searching */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02001247 QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry);
1248 QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001249
Jeff Kinge376f172016-07-15 06:34:22 -04001250 if (verbose || show_resolving_progress)
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001251 progress = start_progress(_("Resolving deltas"),
1252 nr_ref_deltas + nr_ofs_deltas);
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001253
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001254 nr_dispatched = 0;
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001255 base_cache_limit = delta_base_cache_limit * nr_threads;
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001256 if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
1257 init_thread();
Jeff King993d38a2024-01-05 03:50:34 -05001258 work_lock();
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001259 for (i = 0; i < nr_threads; i++) {
1260 int ret = pthread_create(&thread_data[i].thread, NULL,
1261 threaded_second_pass, thread_data + i);
1262 if (ret)
Nguyễn Thái Ngọc Duyf350df42012-08-31 19:13:04 +07001263 die(_("unable to create thread: %s"),
1264 strerror(ret));
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001265 }
Jeff King993d38a2024-01-05 03:50:34 -05001266 work_unlock();
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001267 for (i = 0; i < nr_threads; i++)
1268 pthread_join(thread_data[i].thread, NULL);
1269 cleanup_thread();
1270 return;
1271 }
Jonathan Tan46e6fb12020-08-24 12:16:34 -07001272 threaded_second_pass(&nothread_data);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001273}
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001274
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001275/*
1276 * Third pass:
1277 * - append objects to convert thin pack to full pack if required
brian m. carlson454253f2018-02-01 02:18:39 +00001278 * - write the final pack hash
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001279 */
brian m. carlson98a3bea2018-02-01 02:18:46 +00001280static void fix_unresolved_deltas(struct hashfile *f);
brian m. carlson454253f2018-02-01 02:18:39 +00001281static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001282{
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001283 if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001284 stop_progress(&progress);
brian m. carlson454253f2018-02-01 02:18:39 +00001285 /* Flush remaining pack final hash. */
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001286 flush();
1287 return;
1288 }
1289
1290 if (fix_thin_pack) {
brian m. carlson98a3bea2018-02-01 02:18:46 +00001291 struct hashfile *f;
brian m. carlson454253f2018-02-01 02:18:39 +00001292 unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
Nguyễn Thái Ngọc Duy5c3459f2013-03-16 08:25:18 +07001293 struct strbuf msg = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001294 int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001295 int nr_objects_initial = nr_objects;
1296 if (nr_unresolved <= 0)
Junio C Hamanocc134312012-05-14 11:50:40 -07001297 die(_("confusion beyond insanity"));
René Scharfe2756ca42014-09-16 20:56:57 +02001298 REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
Jeff King57165db2013-03-19 12:17:22 -04001299 memset(objects + nr_objects + 1, 0,
1300 nr_unresolved * sizeof(*objects));
brian m. carlson98a3bea2018-02-01 02:18:46 +00001301 f = hashfd(output_fd, curr_pack);
Junio C Hamano781d9302015-07-03 09:51:57 -07001302 fix_unresolved_deltas(f);
Vasco Almeida71d99b82016-04-08 20:02:39 +00001303 strbuf_addf(&msg, Q_("completed with %d local object",
1304 "completed with %d local objects",
1305 nr_objects - nr_objects_initial),
Nguyễn Thái Ngọc Duy5c3459f2013-03-16 08:25:18 +07001306 nr_objects - nr_objects_initial);
1307 stop_progress_msg(&progress, msg.buf);
1308 strbuf_release(&msg);
Neeraj Singh020406e2022-03-10 22:43:21 +00001309 finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
brian m. carlson454253f2018-02-01 02:18:39 +00001310 hashcpy(read_hash, pack_hash);
1311 fixup_pack_header_footer(output_fd, pack_hash,
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001312 curr_pack, nr_objects,
brian m. carlson454253f2018-02-01 02:18:39 +00001313 read_hash, consumed_bytes-the_hash_algo->rawsz);
Jeff King67947c32018-08-28 17:22:52 -04001314 if (!hasheq(read_hash, tail_hash))
Nguyễn Thái Ngọc Duyf350df42012-08-31 19:13:04 +07001315 die(_("Unexpected tail checksum for %s "
1316 "(disk corruption?)"), curr_pack);
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001317 }
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001318 if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas)
Junio C Hamanocc134312012-05-14 11:50:40 -07001319 die(Q_("pack has %d unresolved delta",
1320 "pack has %d unresolved deltas",
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001321 nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas),
1322 nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001323}
1324
brian m. carlson98a3bea2018-02-01 02:18:46 +00001325static int write_compressed(struct hashfile *f, void *in, unsigned int size)
Nicolas Pitre636171c2006-10-25 23:28:17 -04001326{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001327 git_zstream stream;
Nicolas Pitre7734d7f2010-04-12 16:50:35 -04001328 int status;
1329 unsigned char outbuf[4096];
Nicolas Pitre636171c2006-10-25 23:28:17 -04001330
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001331 git_deflate_init(&stream, zlib_compression_level);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001332 stream.next_in = in;
1333 stream.avail_in = size;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001334
Nicolas Pitre7734d7f2010-04-12 16:50:35 -04001335 do {
1336 stream.next_out = outbuf;
1337 stream.avail_out = sizeof(outbuf);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001338 status = git_deflate(&stream, Z_FINISH);
brian m. carlson98a3bea2018-02-01 02:18:46 +00001339 hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out);
Nicolas Pitre7734d7f2010-04-12 16:50:35 -04001340 } while (status == Z_OK);
1341
1342 if (status != Z_STREAM_END)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001343 die(_("unable to deflate appended object (%d)"), status);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001344 size = stream.total_out;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001345 git_deflate_end(&stream);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001346 return size;
1347}
1348
brian m. carlson98a3bea2018-02-01 02:18:46 +00001349static struct object_entry *append_obj_to_pack(struct hashfile *f,
Shawn O. Pearce03993e12008-07-13 22:07:46 -04001350 const unsigned char *sha1, void *buf,
Nicolas Pitre636171c2006-10-25 23:28:17 -04001351 unsigned long size, enum object_type type)
1352{
1353 struct object_entry *obj = &objects[nr_objects++];
1354 unsigned char header[10];
1355 unsigned long s = size;
1356 int n = 0;
1357 unsigned char c = (type << 4) | (s & 15);
1358 s >>= 4;
1359 while (s) {
1360 header[n++] = c | 0x80;
1361 c = s & 0x7f;
1362 s >>= 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001363 }
Nicolas Pitre636171c2006-10-25 23:28:17 -04001364 header[n++] = c;
Nicolas Pitre85221482008-08-29 16:08:01 -04001365 crc32_begin(f);
brian m. carlson98a3bea2018-02-01 02:18:46 +00001366 hashwrite(f, header, n);
Björn Steinbrink72de2882008-07-24 18:32:00 +01001367 obj[0].size = size;
1368 obj[0].hdr_size = n;
1369 obj[0].type = type;
1370 obj[0].real_type = type;
Geert Boschaa7e44b2007-06-01 15:18:05 -04001371 obj[1].idx.offset = obj[0].idx.offset + n;
Nicolas Pitre85221482008-08-29 16:08:01 -04001372 obj[1].idx.offset += write_compressed(f, buf, size);
1373 obj[0].idx.crc32 = crc32_end(f);
brian m. carlson98a3bea2018-02-01 02:18:46 +00001374 hashflush(f);
brian m. carlson92e2cab2021-04-26 01:02:50 +00001375 oidread(&obj->idx.oid, sha1);
Shawn O. Pearce03993e12008-07-13 22:07:46 -04001376 return obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001377}
1378
1379static int delta_pos_compare(const void *_a, const void *_b)
1380{
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001381 struct ref_delta_entry *a = *(struct ref_delta_entry **)_a;
1382 struct ref_delta_entry *b = *(struct ref_delta_entry **)_b;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001383 return a->obj_no - b->obj_no;
1384}
1385
brian m. carlson98a3bea2018-02-01 02:18:46 +00001386static void fix_unresolved_deltas(struct hashfile *f)
Nicolas Pitre636171c2006-10-25 23:28:17 -04001387{
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001388 struct ref_delta_entry **sorted_by_pos;
Junio C Hamano781d9302015-07-03 09:51:57 -07001389 int i;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001390
1391 /*
1392 * Since many unresolved deltas may well be themselves base objects
1393 * for more unresolved deltas, we really want to include the
1394 * smallest number of base objects that would cover as much delta
1395 * as possible by picking the
1396 * trunc deltas first, allowing for other deltas to resolve without
1397 * additional base objects. Since most base objects are to be found
1398 * before deltas depending on them, a good heuristic is to start
1399 * resolving deltas in the same order as their position in the pack.
1400 */
Jeff Kingb32fa952016-02-22 17:44:25 -05001401 ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001402 for (i = 0; i < nr_ref_deltas; i++)
Junio C Hamano781d9302015-07-03 09:51:57 -07001403 sorted_by_pos[i] = &ref_deltas[i];
René Scharfe9ed0d8d2016-09-29 17:27:31 +02001404 QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001405
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +02001406 if (repo_has_promisor_remote(the_repository)) {
Jonathan Tan8a30a1e2019-05-14 14:10:55 -07001407 /*
1408 * Prefetch the delta bases.
1409 */
1410 struct oid_array to_fetch = OID_ARRAY_INIT;
1411 for (i = 0; i < nr_ref_deltas; i++) {
1412 struct ref_delta_entry *d = sorted_by_pos[i];
1413 if (!oid_object_info_extended(the_repository, &d->oid,
1414 NULL,
1415 OBJECT_INFO_FOR_PREFETCH))
1416 continue;
1417 oid_array_append(&to_fetch, &d->oid);
1418 }
Jonathan Tandb7ed742020-04-02 12:19:16 -07001419 promisor_remote_get_direct(the_repository,
1420 to_fetch.oid, to_fetch.nr);
Jonathan Tan8a30a1e2019-05-14 14:10:55 -07001421 oid_array_clear(&to_fetch);
1422 }
1423
Junio C Hamano781d9302015-07-03 09:51:57 -07001424 for (i = 0; i < nr_ref_deltas; i++) {
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001425 struct ref_delta_entry *d = sorted_by_pos[i];
Nicolas Pitre21666f12007-02-26 14:55:59 -05001426 enum object_type type;
Jonathan Tanb4718ca2020-08-24 12:16:36 -07001427 void *data;
1428 unsigned long size;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001429
1430 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
1431 continue;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02001432 data = repo_read_object_file(the_repository, &d->oid, &type,
1433 &size);
Jonathan Tanb4718ca2020-08-24 12:16:36 -07001434 if (!data)
Nicolas Pitre636171c2006-10-25 23:28:17 -04001435 continue;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001436
Ævar Arnfjörð Bjarmasonee213de2022-02-05 00:48:29 +01001437 if (check_object_signature(the_repository, &d->oid, data, size,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01001438 type) < 0)
brian m. carlsonaf8caf32018-03-12 02:27:37 +00001439 die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001440
1441 /*
1442 * Add this as an object to the objects array and call
1443 * threaded_second_pass() (which will pick up the added
1444 * object).
1445 */
1446 append_obj_to_pack(f, d->oid.hash, data, size, type);
Ævar Arnfjörð Bjarmasonf2bcc692022-03-04 19:32:04 +01001447 free(data);
Jonathan Tanf08cbf62020-09-08 12:48:35 -07001448 threaded_second_pass(NULL);
1449
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -04001450 display_progress(progress, nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001451 }
1452 free(sorted_by_pos);
1453}
1454
Taylor Blau84d54492021-01-25 18:37:22 -05001455static const char *derive_filename(const char *pack_name, const char *strip,
1456 const char *suffix, struct strbuf *buf)
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001457{
1458 size_t len;
Taylor Blau84d54492021-01-25 18:37:22 -05001459 if (!strip_suffix(pack_name, strip, &len) || !len ||
1460 pack_name[len - 1] != '.')
1461 die(_("packfile name '%s' does not end with '.%s'"),
1462 pack_name, strip);
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001463 strbuf_add(buf, pack_name, len);
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001464 strbuf_addstr(buf, suffix);
1465 return buf->buf;
1466}
1467
1468static void write_special_file(const char *suffix, const char *msg,
Junio C Hamano0fd90da2018-02-15 14:55:47 -08001469 const char *pack_name, const unsigned char *hash,
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001470 const char **report)
1471{
1472 struct strbuf name_buf = STRBUF_INIT;
1473 const char *filename;
1474 int fd;
1475 int msg_len = strlen(msg);
1476
1477 if (pack_name)
Taylor Blau84d54492021-01-25 18:37:22 -05001478 filename = derive_filename(pack_name, "pack", suffix, &name_buf);
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001479 else
Junio C Hamano0fd90da2018-02-15 14:55:47 -08001480 filename = odb_pack_name(&name_buf, hash, suffix);
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001481
1482 fd = odb_pack_keep(filename);
1483 if (fd < 0) {
1484 if (errno != EEXIST)
1485 die_errno(_("cannot write %s file '%s'"),
1486 suffix, filename);
1487 } else {
1488 if (msg_len > 0) {
1489 write_or_die(fd, msg, msg_len);
1490 write_or_die(fd, "\n", 1);
1491 }
1492 if (close(fd) != 0)
1493 die_errno(_("cannot close written %s file '%s'"),
1494 suffix, filename);
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001495 if (report)
1496 *report = suffix;
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001497 }
1498 strbuf_release(&name_buf);
1499}
1500
Ævar Arnfjörð Bjarmason8737dab2021-09-09 19:24:49 -04001501static void rename_tmp_packfile(const char **final_name,
1502 const char *curr_name,
1503 struct strbuf *name, unsigned char *hash,
1504 const char *ext, int make_read_only_if_same)
1505{
1506 if (*final_name != curr_name) {
1507 if (!*final_name)
1508 *final_name = odb_pack_name(name, hash, ext);
1509 if (finalize_object_file(curr_name, *final_name))
Jiang Xinf7337192021-11-01 10:14:17 +08001510 die(_("unable to rename temporary '*.%s' file to '%s'"),
Ævar Arnfjörð Bjarmason8737dab2021-09-09 19:24:49 -04001511 ext, *final_name);
1512 } else if (make_read_only_if_same) {
1513 chmod(*final_name, 0444);
1514 }
1515}
1516
Nicolas Pitree42797f2006-10-23 14:50:18 -04001517static void final(const char *final_pack_name, const char *curr_pack_name,
1518 const char *final_index_name, const char *curr_index_name,
Taylor Blaue37d0b82021-01-25 18:37:26 -05001519 const char *final_rev_index_name, const char *curr_rev_index_name,
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001520 const char *keep_msg, const char *promisor_msg,
brian m. carlson454253f2018-02-01 02:18:39 +00001521 unsigned char *hash)
Nicolas Pitree42797f2006-10-23 14:50:18 -04001522{
Shawn O. Pearce3a556022007-03-06 20:44:17 -05001523 const char *report = "pack";
Jeff Kingf2075482017-03-16 10:27:20 -04001524 struct strbuf pack_name = STRBUF_INIT;
1525 struct strbuf index_name = STRBUF_INIT;
Taylor Blaue37d0b82021-01-25 18:37:26 -05001526 struct strbuf rev_index_name = STRBUF_INIT;
Nicolas Pitree42797f2006-10-23 14:50:18 -04001527 int err;
1528
1529 if (!from_stdin) {
1530 close(input_fd);
1531 } else {
Neeraj Singh020406e2022-03-10 22:43:21 +00001532 fsync_component_or_die(FSYNC_COMPONENT_PACK, output_fd, curr_pack_name);
Nicolas Pitree42797f2006-10-23 14:50:18 -04001533 err = close(output_fd);
1534 if (err)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001535 die_errno(_("error while closing pack file"));
Nicolas Pitree42797f2006-10-23 14:50:18 -04001536 }
1537
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001538 if (keep_msg)
Junio C Hamano0fd90da2018-02-15 14:55:47 -08001539 write_special_file("keep", keep_msg, final_pack_name, hash,
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001540 &report);
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001541 if (promisor_msg)
1542 write_special_file("promisor", promisor_msg, final_pack_name,
Junio C Hamano0fd90da2018-02-15 14:55:47 -08001543 hash, NULL);
Shawn Pearceb8077702006-10-29 04:41:59 -05001544
Ævar Arnfjörð Bjarmason8737dab2021-09-09 19:24:49 -04001545 rename_tmp_packfile(&final_pack_name, curr_pack_name, &pack_name,
1546 hash, "pack", from_stdin);
Ævar Arnfjörð Bjarmason8737dab2021-09-09 19:24:49 -04001547 if (curr_rev_index_name)
1548 rename_tmp_packfile(&final_rev_index_name, curr_rev_index_name,
1549 &rev_index_name, hash, "rev", 1);
Taylor Blau522a5c22021-09-09 19:24:53 -04001550 rename_tmp_packfile(&final_index_name, curr_index_name, &index_name,
1551 hash, "idx", 1);
Taylor Blaue37d0b82021-01-25 18:37:26 -05001552
Jeff King368b4e52018-05-31 18:45:31 -04001553 if (do_fsck_object) {
1554 struct packed_git *p;
1555 p = add_packed_git(final_index_name, strlen(final_index_name), 0);
1556 if (p)
Junio C Hamano549ca8a2018-06-13 12:50:46 -07001557 install_packed_git(the_repository, p);
Jeff King368b4e52018-05-31 18:45:31 -04001558 }
Jeff King73c3f0f2018-05-04 19:45:01 -04001559
Nicolas Pitre576162a2006-11-01 17:06:25 -05001560 if (!from_stdin) {
brian m. carlson69fa3372019-08-18 20:04:23 +00001561 printf("%s\n", hash_to_hex(hash));
Nicolas Pitre576162a2006-11-01 17:06:25 -05001562 } else {
Jeff King5b1ef2c2017-03-28 15:46:50 -04001563 struct strbuf buf = STRBUF_INIT;
1564
brian m. carlson69fa3372019-08-18 20:04:23 +00001565 strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash));
Jeff King5b1ef2c2017-03-28 15:46:50 -04001566 write_or_die(1, buf.buf, buf.len);
1567 strbuf_release(&buf);
Nicolas Pitre576162a2006-11-01 17:06:25 -05001568
1569 /*
1570 * Let's just mimic git-unpack-objects here and write
1571 * the last part of the input buffer to stdout.
1572 */
1573 while (input_len) {
1574 err = xwrite(1, input_buffer + input_offset, input_len);
1575 if (err <= 0)
1576 break;
1577 input_len -= err;
1578 input_offset += err;
1579 }
1580 }
Jeff Kingba47a302017-03-16 10:27:15 -04001581
Taylor Blaue37d0b82021-01-25 18:37:26 -05001582 strbuf_release(&rev_index_name);
Jeff Kingf2075482017-03-16 10:27:20 -04001583 strbuf_release(&index_name);
1584 strbuf_release(&pack_name);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001585}
1586
Glen Chooa4e7e312023-06-28 19:26:22 +00001587static int git_index_pack_config(const char *k, const char *v,
1588 const struct config_context *ctx, void *cb)
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04001589{
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001590 struct pack_idx_option *opts = cb;
1591
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04001592 if (!strcmp(k, "pack.indexversion")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001593 opts->version = git_config_int(k, v, ctx->kvi);
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001594 if (opts->version > 2)
Jiang Xinb4eda052022-06-17 18:03:09 +08001595 die(_("bad pack.indexVersion=%"PRIu32), opts->version);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04001596 return 0;
1597 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001598 if (!strcmp(k, "pack.threads")) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001599 nr_threads = git_config_int(k, v, ctx->kvi);
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001600 if (nr_threads < 0)
Nguyễn Thái Ngọc Duyf350df42012-08-31 19:13:04 +07001601 die(_("invalid number of threads specified (%d)"),
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001602 nr_threads);
Nguyễn Thái Ngọc Duy2094c5e2018-11-03 09:48:40 +01001603 if (!HAVE_THREADS && nr_threads != 1) {
Nguyễn Thái Ngọc Duyf350df42012-08-31 19:13:04 +07001604 warning(_("no threads support, ignoring %s"), k);
Nguyễn Thái Ngọc Duy2094c5e2018-11-03 09:48:40 +01001605 nr_threads = 1;
1606 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001607 return 0;
1608 }
Taylor Blaue37d0b82021-01-25 18:37:26 -05001609 if (!strcmp(k, "pack.writereverseindex")) {
1610 if (git_config_bool(k, v))
1611 opts->flags |= WRITE_REV;
1612 else
1613 opts->flags &= ~WRITE_REV;
1614 }
Glen Chooa4e7e312023-06-28 19:26:22 +00001615 return git_default_config(k, v, ctx, cb);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04001616}
1617
Junio C Hamano3c9fc072011-02-25 16:55:26 -08001618static int cmp_uint32(const void *a_, const void *b_)
1619{
1620 uint32_t a = *((uint32_t *)a_);
1621 uint32_t b = *((uint32_t *)b_);
1622
1623 return (a < b) ? -1 : (a != b);
1624}
1625
1626static void read_v2_anomalous_offsets(struct packed_git *p,
1627 struct pack_idx_option *opts)
1628{
1629 const uint32_t *idx1, *idx2;
1630 uint32_t i;
1631
1632 /* The address of the 4-byte offset table */
brian m. carlson629dffc2020-05-25 19:59:10 +00001633 idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset))
Jeff Kingf86f7692020-11-13 00:06:48 -05001634 + (size_t)p->num_objects /* CRC32 table */
Junio C Hamano3c9fc072011-02-25 16:55:26 -08001635 );
1636
1637 /* The address of the 8-byte offset table */
1638 idx2 = idx1 + p->num_objects;
1639
1640 for (i = 0; i < p->num_objects; i++) {
1641 uint32_t off = ntohl(idx1[i]);
1642 if (!(off & 0x80000000))
1643 continue;
1644 off = off & 0x7fffffff;
Jeff King47fe3f62016-02-25 09:22:52 -05001645 check_pack_index_ptr(p, &idx2[off * 2]);
Junio C Hamano3c9fc072011-02-25 16:55:26 -08001646 if (idx2[off * 2])
1647 continue;
1648 /*
1649 * The real offset is ntohl(idx2[off * 2]) in high 4
1650 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1651 * octets. But idx2[off * 2] is Zero!!!
1652 */
1653 ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
1654 opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
1655 }
1656
René Scharfe1b5294d2016-09-30 01:40:14 +02001657 QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32);
Junio C Hamano3c9fc072011-02-25 16:55:26 -08001658}
1659
Junio C Hamanoe337a042011-02-02 17:29:01 -08001660static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
1661{
1662 struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
1663
1664 if (!p)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001665 die(_("Cannot open existing pack file '%s'"), pack_name);
Junio C Hamanoe337a042011-02-02 17:29:01 -08001666 if (open_pack_index(p))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001667 die(_("Cannot open existing pack idx file for '%s'"), pack_name);
Junio C Hamanoe337a042011-02-02 17:29:01 -08001668
1669 /* Read the attributes from the existing idx file */
1670 opts->version = p->index_version;
1671
Junio C Hamano3c9fc072011-02-25 16:55:26 -08001672 if (opts->version == 2)
1673 read_v2_anomalous_offsets(p, opts);
1674
Junio C Hamanoe337a042011-02-02 17:29:01 -08001675 /*
1676 * Get rid of the idx file as we do not need it anymore.
1677 * NEEDSWORK: extract this bit from free_pack_by_name() in
Martin Ågrene5afd442020-12-31 12:56:21 +01001678 * object-file.c, perhaps? It shouldn't matter very much as we
Junio C Hamanoe337a042011-02-02 17:29:01 -08001679 * know we haven't installed this pack (hence we never have
1680 * read anything from it).
1681 */
1682 close_pack_index(p);
1683 free(p);
1684}
1685
Junio C Hamano38a45562011-06-03 15:32:15 -07001686static void show_pack_info(int stat_only)
1687{
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001688 int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas;
Junio C Hamanod1a0ed12011-06-03 15:32:16 -07001689 unsigned long *chain_histogram = NULL;
1690
1691 if (deepest_delta)
René Scharfeca56dad2021-03-13 17:17:22 +01001692 CALLOC_ARRAY(chain_histogram, deepest_delta);
Junio C Hamanod1a0ed12011-06-03 15:32:16 -07001693
Junio C Hamano38a45562011-06-03 15:32:15 -07001694 for (i = 0; i < nr_objects; i++) {
1695 struct object_entry *obj = &objects[i];
1696
Junio C Hamanod1a0ed12011-06-03 15:32:16 -07001697 if (is_delta_type(obj->type))
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +07001698 chain_histogram[obj_stat[i].delta_depth - 1]++;
Junio C Hamano38a45562011-06-03 15:32:15 -07001699 if (stat_only)
1700 continue;
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01001701 printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
brian m. carlsone6a492b2017-05-06 22:10:11 +00001702 oid_to_hex(&obj->idx.oid),
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01001703 type_name(obj->real_type), (uintmax_t)obj->size,
1704 (uintmax_t)(obj[1].idx.offset - obj->idx.offset),
Junio C Hamano38a45562011-06-03 15:32:15 -07001705 (uintmax_t)obj->idx.offset);
1706 if (is_delta_type(obj->type)) {
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +07001707 struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
brian m. carlsone6a492b2017-05-06 22:10:11 +00001708 printf(" %u %s", obj_stat[i].delta_depth,
1709 oid_to_hex(&bobj->idx.oid));
Junio C Hamano38a45562011-06-03 15:32:15 -07001710 }
1711 putchar('\n');
1712 }
Junio C Hamanod1a0ed12011-06-03 15:32:16 -07001713
1714 if (baseobjects)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001715 printf_ln(Q_("non delta: %d object",
1716 "non delta: %d objects",
1717 baseobjects),
1718 baseobjects);
Junio C Hamanod1a0ed12011-06-03 15:32:16 -07001719 for (i = 0; i < deepest_delta; i++) {
1720 if (!chain_histogram[i])
1721 continue;
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001722 printf_ln(Q_("chain length = %d: %lu object",
1723 "chain length = %d: %lu objects",
1724 chain_histogram[i]),
1725 i + 1,
1726 chain_histogram[i]);
Junio C Hamanod1a0ed12011-06-03 15:32:16 -07001727 }
Ævar Arnfjörð Bjarmasonf2bcc692022-03-04 19:32:04 +01001728 free(chain_histogram);
Junio C Hamano38a45562011-06-03 15:32:15 -07001729}
1730
Linus Torvalds3bb72562010-01-22 07:55:19 -08001731int cmd_index_pack(int argc, const char **argv, const char *prefix)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001732{
Taylor Blaue37d0b82021-01-25 18:37:26 -05001733 int i, fix_thin_pack = 0, verify = 0, stat_only = 0, rev_index;
Nguyễn Thái Ngọc Duy39539492014-03-25 20:41:41 +07001734 const char *curr_index;
Taylor Blaue37d0b82021-01-25 18:37:26 -05001735 const char *curr_rev_index = NULL;
1736 const char *index_name = NULL, *pack_name = NULL, *rev_index_name = NULL;
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001737 const char *keep_msg = NULL;
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001738 const char *promisor_msg = NULL;
Jonathan Tan8e29c7c2017-12-05 16:58:48 +00001739 struct strbuf index_name_buf = STRBUF_INIT;
Taylor Blaue37d0b82021-01-25 18:37:26 -05001740 struct strbuf rev_index_name_buf = STRBUF_INIT;
Geert Boschaa7e44b2007-06-01 15:18:05 -04001741 struct pack_idx_entry **idx_objects;
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001742 struct pack_idx_option opts;
brian m. carlson454253f2018-02-01 02:18:39 +00001743 unsigned char pack_hash[GIT_MAX_RAWSZ];
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001744 unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
Jeff King83558682016-07-15 06:43:47 -04001745 int report_end_of_input = 0;
brian m. carlson586740a2020-06-19 17:55:52 +00001746 int hash_algo = 0;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001747
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001748 /*
Jonathan Tan8a30a1e2019-05-14 14:10:55 -07001749 * index-pack never needs to fetch missing objects except when
1750 * REF_DELTA bases are missing (which are explicitly handled). It only
1751 * accesses the repo to do hash collision checks and to check which
1752 * REF_DELTA bases need to be fetched.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001753 */
1754 fetch_if_missing = 0;
1755
Jonathan Nieder99caeed2009-11-09 09:05:01 -06001756 if (argc == 2 && !strcmp(argv[1], "-h"))
1757 usage(index_pack_usage);
1758
Derrick Stoleed24eda42023-06-06 13:24:35 +00001759 disable_replace_refs();
Johannes Schindelin22410542015-06-22 17:25:00 +02001760 fsck_options.walk = mark_link;
Nelson Elhage6e2a09d2010-08-12 10:18:12 -04001761
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001762 reset_pack_idx_option(&opts);
Taylor Blaua8dd7e02023-04-12 18:20:33 -04001763 opts.flags |= WRITE_REV;
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001764 git_config(git_index_pack_config, &opts);
Nguyễn Thái Ngọc Duy3f8099f2010-07-24 06:30:49 -05001765 if (prefix && chdir(prefix))
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001766 die(_("Cannot come back to cwd"));
Nicolas Pitre4d00bda2007-11-01 23:26:04 -04001767
Taylor Blau9f7f10a2023-04-12 18:20:36 -04001768 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
1769 rev_index = 0;
Taylor Blaue8c58f82021-01-25 18:37:42 -05001770 else
1771 rev_index = !!(opts.flags & (WRITE_REV_VERIFY | WRITE_REV));
Taylor Blaue37d0b82021-01-25 18:37:26 -05001772
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001773 for (i = 1; i < argc; i++) {
Linus Torvalds3bb72562010-01-22 07:55:19 -08001774 const char *arg = argv[i];
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001775
1776 if (*arg == '-') {
Nicolas Pitree42797f2006-10-23 14:50:18 -04001777 if (!strcmp(arg, "--stdin")) {
1778 from_stdin = 1;
Nicolas Pitre636171c2006-10-25 23:28:17 -04001779 } else if (!strcmp(arg, "--fix-thin")) {
1780 fix_thin_pack = 1;
Christian Couder72885a62017-12-09 21:40:08 +01001781 } else if (skip_to_optional_arg(arg, "--strict", &arg)) {
Johannes Schindelin5d477a32015-06-22 17:25:31 +02001782 strict = 1;
1783 do_fsck_object = 1;
1784 fsck_set_msg_types(&fsck_options, arg);
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001785 } else if (!strcmp(arg, "--check-self-contained-and-connected")) {
1786 strict = 1;
1787 check_self_contained_and_connected = 1;
John Cai0f8edf72024-02-01 01:38:02 +00001788 } else if (skip_to_optional_arg(arg, "--fsck-objects", &arg)) {
Jonathan Tanffb2c0f2018-03-14 11:42:40 -07001789 do_fsck_object = 1;
John Cai0f8edf72024-02-01 01:38:02 +00001790 fsck_set_msg_types(&fsck_options, arg);
Junio C Hamanoe337a042011-02-02 17:29:01 -08001791 } else if (!strcmp(arg, "--verify")) {
1792 verify = 1;
Junio C Hamano38a45562011-06-03 15:32:15 -07001793 } else if (!strcmp(arg, "--verify-stat")) {
1794 verify = 1;
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +07001795 show_stat = 1;
Junio C Hamano38a45562011-06-03 15:32:15 -07001796 } else if (!strcmp(arg, "--verify-stat-only")) {
1797 verify = 1;
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +07001798 show_stat = 1;
Junio C Hamano38a45562011-06-03 15:32:15 -07001799 stat_only = 1;
Christian Couder72885a62017-12-09 21:40:08 +01001800 } else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) {
1801 ; /* nothing to do */
Junio C Hamanof3d618d2018-02-13 13:39:03 -08001802 } else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) {
1803 ; /* already parsed */
Christian Couder59556542013-11-30 21:55:40 +01001804 } else if (starts_with(arg, "--threads=")) {
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001805 char *end;
1806 nr_threads = strtoul(arg+10, &end, 0);
1807 if (!arg[10] || *end || nr_threads < 0)
1808 usage(index_pack_usage);
Nguyễn Thái Ngọc Duy2094c5e2018-11-03 09:48:40 +01001809 if (!HAVE_THREADS && nr_threads != 1) {
1810 warning(_("no threads support, ignoring %s"), arg);
1811 nr_threads = 1;
1812 }
Christian Couder59556542013-11-30 21:55:40 +01001813 } else if (starts_with(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -05001814 struct pack_header *hdr;
1815 char *c;
1816
1817 hdr = (struct pack_header *)input_buffer;
1818 hdr->hdr_signature = htonl(PACK_SIGNATURE);
1819 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
1820 if (*c != ',')
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001821 die(_("bad %s"), arg);
Nicolas Pitrebed006f2006-11-01 17:06:20 -05001822 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
1823 if (*c)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001824 die(_("bad %s"), arg);
Nicolas Pitrebed006f2006-11-01 17:06:20 -05001825 input_len = sizeof(*hdr);
Nicolas Pitre3c9af362006-10-25 23:32:59 -04001826 } else if (!strcmp(arg, "-v")) {
1827 verbose = 1;
Ævar Arnfjörð Bjarmasonf46c46e2021-09-05 09:34:44 +02001828 } else if (!strcmp(arg, "--progress-title")) {
1829 if (progress_title || (i+1) >= argc)
1830 usage(index_pack_usage);
1831 progress_title = argv[++i];
Jeff Kinge376f172016-07-15 06:34:22 -04001832 } else if (!strcmp(arg, "--show-resolving-progress")) {
1833 show_resolving_progress = 1;
Jeff King83558682016-07-15 06:43:47 -04001834 } else if (!strcmp(arg, "--report-end-of-input")) {
1835 report_end_of_input = 1;
Nicolas Pitree42797f2006-10-23 14:50:18 -04001836 } else if (!strcmp(arg, "-o")) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001837 if (index_name || (i+1) >= argc)
1838 usage(index_pack_usage);
1839 index_name = argv[++i];
Christian Couder59556542013-11-30 21:55:40 +01001840 } else if (starts_with(arg, "--index-version=")) {
Nicolas Pitre4ba7d712007-04-09 17:32:03 -04001841 char *c;
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001842 opts.version = strtoul(arg + 16, &c, 10);
1843 if (opts.version > 2)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001844 die(_("bad %s"), arg);
Nicolas Pitre4ba7d712007-04-09 17:32:03 -04001845 if (*c == ',')
Junio C Hamanoebcfb372011-02-25 15:43:25 -08001846 opts.off32_limit = strtoul(c+1, &c, 0);
1847 if (*c || opts.off32_limit & 0x80000000)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001848 die(_("bad %s"), arg);
Jeff King411481b2016-08-24 20:41:55 +02001849 } else if (skip_prefix(arg, "--max-input-size=", &arg)) {
1850 max_input_size = strtoumax(arg, NULL, 10);
brian m. carlson586740a2020-06-19 17:55:52 +00001851 } else if (skip_prefix(arg, "--object-format=", &arg)) {
1852 hash_algo = hash_algo_by_name(arg);
1853 if (hash_algo == GIT_HASH_UNKNOWN)
1854 die(_("unknown hash algorithm '%s'"), arg);
1855 repo_set_hash_algo(the_repository, hash_algo);
Taylor Blaue37d0b82021-01-25 18:37:26 -05001856 } else if (!strcmp(arg, "--rev-index")) {
1857 rev_index = 1;
1858 } else if (!strcmp(arg, "--no-rev-index")) {
1859 rev_index = 0;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001860 } else
1861 usage(index_pack_usage);
1862 continue;
1863 }
1864
1865 if (pack_name)
1866 usage(index_pack_usage);
1867 pack_name = arg;
1868 }
1869
Nicolas Pitree42797f2006-10-23 14:50:18 -04001870 if (!pack_name && !from_stdin)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001871 usage(index_pack_usage);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001872 if (fix_thin_pack && !from_stdin)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +00001873 die(_("the option '%s' requires '%s'"), "--fix-thin", "--stdin");
Jeff King7176a312016-12-15 21:30:59 -05001874 if (from_stdin && !startup_info->have_repository)
1875 die(_("--stdin requires a git repository"));
brian m. carlson586740a2020-06-19 17:55:52 +00001876 if (from_stdin && hash_algo)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001877 die(_("options '%s' and '%s' cannot be used together"), "--object-format", "--stdin");
Junio C Hamanobfee6142016-03-03 11:29:09 -08001878 if (!index_name && pack_name)
Taylor Blau84d54492021-01-25 18:37:22 -05001879 index_name = derive_filename(pack_name, "pack", "idx", &index_name_buf);
Junio C Hamanobfee6142016-03-03 11:29:09 -08001880
Taylor Blaue37d0b82021-01-25 18:37:26 -05001881 opts.flags &= ~(WRITE_REV | WRITE_REV_VERIFY);
1882 if (rev_index) {
1883 opts.flags |= verify ? WRITE_REV_VERIFY : WRITE_REV;
1884 if (index_name)
1885 rev_index_name = derive_filename(index_name,
1886 "idx", "rev",
1887 &rev_index_name_buf);
1888 }
1889
Junio C Hamanoe337a042011-02-02 17:29:01 -08001890 if (verify) {
1891 if (!index_name)
Nguyễn Thái Ngọc Duyc2b97ec2012-04-23 19:30:29 +07001892 die(_("--verify with no packfile name given"));
Junio C Hamanoe337a042011-02-02 17:29:01 -08001893 read_idx_option(&opts, index_name);
Junio C Hamano68be2fe2011-11-16 22:04:13 -08001894 opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
Junio C Hamanoe337a042011-02-02 17:29:01 -08001895 }
Junio C Hamano68be2fe2011-11-16 22:04:13 -08001896 if (strict)
1897 opts.flags |= WRITE_IDX_STRICT;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001898
Nguyễn Thái Ngọc Duy2094c5e2018-11-03 09:48:40 +01001899 if (HAVE_THREADS && !nr_threads) {
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001900 nr_threads = online_cpus();
Jeff Kingfbff95b2020-08-21 13:58:00 -04001901 /*
1902 * Experiments show that going above 20 threads doesn't help,
1903 * no matter how many cores you have. Below that, we tend to
1904 * max at half the number of online_cpus(), presumably because
1905 * half of those are hyperthreads rather than full cores. We'll
1906 * never reduce the level below "3", though, to match a
1907 * historical value that nobody complained about.
1908 */
1909 if (nr_threads < 4)
1910 ; /* too few cores to consider capping */
1911 else if (nr_threads < 6)
1912 nr_threads = 3; /* historic cap */
1913 else if (nr_threads < 40)
1914 nr_threads /= 2;
1915 else
1916 nr_threads = 20; /* hard cap */
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001917 }
Nguyễn Thái Ngọc Duyb8a24862012-05-06 19:31:55 +07001918
Nicolas Pitree42797f2006-10-23 14:50:18 -04001919 curr_pack = open_pack_file(pack_name);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001920 parse_pack_header();
René Scharfeca56dad2021-03-13 17:17:22 +01001921 CALLOC_ARRAY(objects, st_add(nr_objects, 1));
Nguyễn Thái Ngọc Duy41730572015-02-26 17:52:07 +07001922 if (show_stat)
René Scharfeca56dad2021-03-13 17:17:22 +01001923 CALLOC_ARRAY(obj_stat, st_add(nr_objects, 1));
1924 CALLOC_ARRAY(ofs_deltas, nr_objects);
brian m. carlson454253f2018-02-01 02:18:39 +00001925 parse_pack_objects(pack_hash);
Jeff King83558682016-07-15 06:43:47 -04001926 if (report_end_of_input)
1927 write_in_full(2, "\0", 1);
Nguyễn Thái Ngọc Duy5272f752012-05-06 19:31:54 +07001928 resolve_deltas();
brian m. carlson454253f2018-02-01 02:18:39 +00001929 conclude_pack(fix_thin_pack, curr_pack, pack_hash);
Nguyễn Thái Ngọc Duyc6458e62015-04-18 17:47:05 +07001930 free(ofs_deltas);
1931 free(ref_deltas);
Martin Koegler0153be02008-02-25 22:46:12 +01001932 if (strict)
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001933 foreign_nr = check_objects();
Geert Boschaa7e44b2007-06-01 15:18:05 -04001934
Nguyễn Thái Ngọc Duy3aba2fd2013-03-19 20:01:15 +07001935 if (show_stat)
Junio C Hamano38a45562011-06-03 15:32:15 -07001936 show_pack_info(stat_only);
1937
Jeff Kingb32fa952016-02-22 17:44:25 -05001938 ALLOC_ARRAY(idx_objects, nr_objects);
Geert Boschaa7e44b2007-06-01 15:18:05 -04001939 for (i = 0; i < nr_objects; i++)
1940 idx_objects[i] = &objects[i].idx;
brian m. carlson454253f2018-02-01 02:18:39 +00001941 curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
Taylor Blaue37d0b82021-01-25 18:37:26 -05001942 if (rev_index)
1943 curr_rev_index = write_rev_file(rev_index_name, idx_objects,
1944 nr_objects, pack_hash,
1945 opts.flags);
Geert Boschaa7e44b2007-06-01 15:18:05 -04001946 free(idx_objects);
1947
Junio C Hamanoe337a042011-02-02 17:29:01 -08001948 if (!verify)
1949 final(pack_name, curr_pack,
1950 index_name, curr_index,
Taylor Blaue37d0b82021-01-25 18:37:26 -05001951 rev_index_name, curr_rev_index,
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001952 keep_msg, promisor_msg,
brian m. carlson454253f2018-02-01 02:18:39 +00001953 pack_hash);
Junio C Hamanoe337a042011-02-02 17:29:01 -08001954 else
1955 close(input_fd);
Jeff King73c3f0f2018-05-04 19:45:01 -04001956
Ævar Arnfjörð Bjarmason462f5ca2021-03-28 15:15:49 +02001957 if (do_fsck_object && fsck_finish(&fsck_options))
1958 die(_("fsck error in pack objects"));
Jeff King73c3f0f2018-05-04 19:45:01 -04001959
Ævar Arnfjörð Bjarmasonf2bcc692022-03-04 19:32:04 +01001960 free(opts.anomaly);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001961 free(objects);
Jeff King592ce202014-06-30 12:59:10 -04001962 strbuf_release(&index_name_buf);
Taylor Blaue37d0b82021-01-25 18:37:26 -05001963 strbuf_release(&rev_index_name_buf);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001964 if (!pack_name)
Linus Torvalds3bb72562010-01-22 07:55:19 -08001965 free((void *) curr_pack);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001966 if (!index_name)
Linus Torvalds3bb72562010-01-22 07:55:19 -08001967 free((void *) curr_index);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001968 if (!rev_index_name)
Taylor Blaue37d0b82021-01-25 18:37:26 -05001969 free((void *) curr_rev_index);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001970
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001971 /*
1972 * Let the caller know this pack is not self contained
1973 */
1974 if (check_self_contained_and_connected && foreign_nr)
1975 return 1;
1976
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001977 return 0;
1978}