line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright (C) the libgit2 contributors. All rights reserved. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* This file is part of libgit2, distributed under the GNU GPL v2 with |
5
|
|
|
|
|
|
|
* a Linking Exception. For full terms see the included COPYING file. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "pack-objects.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "zstream.h" |
11
|
|
|
|
|
|
|
#include "delta.h" |
12
|
|
|
|
|
|
|
#include "iterator.h" |
13
|
|
|
|
|
|
|
#include "netops.h" |
14
|
|
|
|
|
|
|
#include "pack.h" |
15
|
|
|
|
|
|
|
#include "thread-utils.h" |
16
|
|
|
|
|
|
|
#include "tree.h" |
17
|
|
|
|
|
|
|
#include "util.h" |
18
|
|
|
|
|
|
|
#include "revwalk.h" |
19
|
|
|
|
|
|
|
#include "commit_list.h" |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#include "git2/pack.h" |
22
|
|
|
|
|
|
|
#include "git2/commit.h" |
23
|
|
|
|
|
|
|
#include "git2/tag.h" |
24
|
|
|
|
|
|
|
#include "git2/indexer.h" |
25
|
|
|
|
|
|
|
#include "git2/config.h" |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
struct unpacked { |
28
|
|
|
|
|
|
|
git_pobject *object; |
29
|
|
|
|
|
|
|
void *data; |
30
|
|
|
|
|
|
|
struct git_delta_index *index; |
31
|
|
|
|
|
|
|
size_t depth; |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
struct tree_walk_context { |
35
|
|
|
|
|
|
|
git_packbuilder *pb; |
36
|
|
|
|
|
|
|
git_buf buf; |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
struct pack_write_context { |
40
|
|
|
|
|
|
|
git_indexer *indexer; |
41
|
|
|
|
|
|
|
git_indexer_progress *stats; |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
struct walk_object { |
45
|
|
|
|
|
|
|
git_oid id; |
46
|
|
|
|
|
|
|
unsigned int uninteresting:1, |
47
|
|
|
|
|
|
|
seen:1; |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#ifdef GIT_THREADS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
#define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \ |
53
|
|
|
|
|
|
|
int result = git_mutex_##op(&(pb)->mtx); \ |
54
|
|
|
|
|
|
|
assert(!result); \ |
55
|
|
|
|
|
|
|
GIT_UNUSED(result); \ |
56
|
|
|
|
|
|
|
} while (0) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#else |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#define GIT_PACKBUILDER__MUTEX_OP(pb,mtx,op) GIT_UNUSED(pb) |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#endif /* GIT_THREADS */ |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
#define git_packbuilder__cache_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, lock) |
65
|
|
|
|
|
|
|
#define git_packbuilder__cache_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, unlock) |
66
|
|
|
|
|
|
|
#define git_packbuilder__progress_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, lock) |
67
|
|
|
|
|
|
|
#define git_packbuilder__progress_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, unlock) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
/* The minimal interval between progress updates (in seconds). */ |
70
|
|
|
|
|
|
|
#define MIN_PROGRESS_UPDATE_INTERVAL 0.5 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
/* Size of the buffer to feed to zlib */ |
73
|
|
|
|
|
|
|
#define COMPRESS_BUFLEN (1024 * 1024) |
74
|
|
|
|
|
|
|
|
75
|
53
|
|
|
|
|
|
static unsigned name_hash(const char *name) |
76
|
|
|
|
|
|
|
{ |
77
|
53
|
|
|
|
|
|
unsigned c, hash = 0; |
78
|
|
|
|
|
|
|
|
79
|
53
|
100
|
|
|
|
|
if (!name) |
80
|
34
|
|
|
|
|
|
return 0; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
/* |
83
|
|
|
|
|
|
|
* This effectively just creates a sortable number from the |
84
|
|
|
|
|
|
|
* last sixteen non-whitespace characters. Last characters |
85
|
|
|
|
|
|
|
* count "most", so things that end in ".c" sort together. |
86
|
|
|
|
|
|
|
*/ |
87
|
127
|
100
|
|
|
|
|
while ((c = *name++) != 0) { |
88
|
108
|
50
|
|
|
|
|
if (git__isspace(c)) |
89
|
0
|
|
|
|
|
|
continue; |
90
|
108
|
|
|
|
|
|
hash = (hash >> 2) + (c << 24); |
91
|
|
|
|
|
|
|
} |
92
|
19
|
|
|
|
|
|
return hash; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
7
|
|
|
|
|
|
static int packbuilder_config(git_packbuilder *pb) |
96
|
|
|
|
|
|
|
{ |
97
|
|
|
|
|
|
|
git_config *config; |
98
|
7
|
|
|
|
|
|
int ret = 0; |
99
|
|
|
|
|
|
|
int64_t val; |
100
|
|
|
|
|
|
|
|
101
|
7
|
50
|
|
|
|
|
if ((ret = git_repository_config_snapshot(&config, pb->repo)) < 0) |
102
|
0
|
|
|
|
|
|
return ret; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#define config_get(KEY,DST,DFLT) do { \ |
105
|
|
|
|
|
|
|
ret = git_config_get_int64(&val, config, KEY); \ |
106
|
|
|
|
|
|
|
if (!ret) { \ |
107
|
|
|
|
|
|
|
if (!git__is_sizet(val)) { \ |
108
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_CONFIG, \ |
109
|
|
|
|
|
|
|
"configuration value '%s' is too large", KEY); \ |
110
|
|
|
|
|
|
|
ret = -1; \ |
111
|
|
|
|
|
|
|
goto out; \ |
112
|
|
|
|
|
|
|
} \ |
113
|
|
|
|
|
|
|
(DST) = (size_t)val; \ |
114
|
|
|
|
|
|
|
} else if (ret == GIT_ENOTFOUND) { \ |
115
|
|
|
|
|
|
|
(DST) = (DFLT); \ |
116
|
|
|
|
|
|
|
ret = 0; \ |
117
|
|
|
|
|
|
|
} else if (ret < 0) goto out; } while (0) |
118
|
|
|
|
|
|
|
|
119
|
7
|
50
|
|
|
|
|
config_get("pack.deltaCacheSize", pb->max_delta_cache_size, |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
120
|
|
|
|
|
|
|
GIT_PACK_DELTA_CACHE_SIZE); |
121
|
7
|
50
|
|
|
|
|
config_get("pack.deltaCacheLimit", pb->cache_max_small_delta_size, |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
122
|
|
|
|
|
|
|
GIT_PACK_DELTA_CACHE_LIMIT); |
123
|
7
|
50
|
|
|
|
|
config_get("pack.deltaCacheSize", pb->big_file_threshold, |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
124
|
|
|
|
|
|
|
GIT_PACK_BIG_FILE_THRESHOLD); |
125
|
7
|
50
|
|
|
|
|
config_get("pack.windowMemory", pb->window_memory_limit, 0); |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
#undef config_get |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
out: |
130
|
7
|
|
|
|
|
|
git_config_free(config); |
131
|
|
|
|
|
|
|
|
132
|
7
|
|
|
|
|
|
return ret; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
7
|
|
|
|
|
|
int git_packbuilder_new(git_packbuilder **out, git_repository *repo) |
136
|
|
|
|
|
|
|
{ |
137
|
|
|
|
|
|
|
git_packbuilder *pb; |
138
|
|
|
|
|
|
|
|
139
|
7
|
|
|
|
|
|
*out = NULL; |
140
|
|
|
|
|
|
|
|
141
|
7
|
|
|
|
|
|
pb = git__calloc(1, sizeof(*pb)); |
142
|
7
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(pb); |
143
|
|
|
|
|
|
|
|
144
|
14
|
|
|
|
|
|
if (git_oidmap_new(&pb->object_ix) < 0 || |
145
|
14
|
50
|
|
|
|
|
git_oidmap_new(&pb->walk_objects) < 0 || |
146
|
7
|
|
|
|
|
|
git_pool_init(&pb->object_pool, sizeof(struct walk_object)) < 0) |
147
|
|
|
|
|
|
|
goto on_error; |
148
|
|
|
|
|
|
|
|
149
|
7
|
|
|
|
|
|
pb->repo = repo; |
150
|
7
|
|
|
|
|
|
pb->nr_threads = 1; /* do not spawn any thread by default */ |
151
|
|
|
|
|
|
|
|
152
|
14
|
|
|
|
|
|
if (git_hash_ctx_init(&pb->ctx) < 0 || |
153
|
14
|
50
|
|
|
|
|
git_zstream_init(&pb->zstream, GIT_ZSTREAM_DEFLATE) < 0 || |
154
|
14
|
50
|
|
|
|
|
git_repository_odb(&pb->odb, repo) < 0 || |
155
|
7
|
|
|
|
|
|
packbuilder_config(pb) < 0) |
156
|
|
|
|
|
|
|
goto on_error; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
#ifdef GIT_THREADS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
if (git_mutex_init(&pb->cache_mutex) || |
161
|
|
|
|
|
|
|
git_mutex_init(&pb->progress_mutex) || |
162
|
|
|
|
|
|
|
git_cond_init(&pb->progress_cond)) |
163
|
|
|
|
|
|
|
{ |
164
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to initialize packbuilder mutex"); |
165
|
|
|
|
|
|
|
goto on_error; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
#endif |
169
|
|
|
|
|
|
|
|
170
|
7
|
|
|
|
|
|
*out = pb; |
171
|
7
|
|
|
|
|
|
return 0; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
on_error: |
174
|
0
|
|
|
|
|
|
git_packbuilder_free(pb); |
175
|
0
|
|
|
|
|
|
return -1; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
6
|
|
|
|
|
|
unsigned int git_packbuilder_set_threads(git_packbuilder *pb, unsigned int n) |
179
|
|
|
|
|
|
|
{ |
180
|
6
|
50
|
|
|
|
|
assert(pb); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
#ifdef GIT_THREADS |
183
|
|
|
|
|
|
|
pb->nr_threads = n; |
184
|
|
|
|
|
|
|
#else |
185
|
|
|
|
|
|
|
GIT_UNUSED(n); |
186
|
6
|
50
|
|
|
|
|
assert(1 == pb->nr_threads); |
187
|
|
|
|
|
|
|
#endif |
188
|
|
|
|
|
|
|
|
189
|
6
|
|
|
|
|
|
return pb->nr_threads; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
6
|
|
|
|
|
|
static int rehash(git_packbuilder *pb) |
193
|
|
|
|
|
|
|
{ |
194
|
|
|
|
|
|
|
git_pobject *po; |
195
|
|
|
|
|
|
|
size_t i; |
196
|
|
|
|
|
|
|
|
197
|
6
|
|
|
|
|
|
git_oidmap_clear(pb->object_ix); |
198
|
|
|
|
|
|
|
|
199
|
6
|
50
|
|
|
|
|
for (i = 0, po = pb->object_list; i < pb->nr_objects; i++, po++) { |
200
|
0
|
0
|
|
|
|
|
if (git_oidmap_set(pb->object_ix, &po->id, po) < 0) |
201
|
0
|
|
|
|
|
|
return -1; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
6
|
|
|
|
|
|
return 0; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
64
|
|
|
|
|
|
int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid, |
208
|
|
|
|
|
|
|
const char *name) |
209
|
|
|
|
|
|
|
{ |
210
|
|
|
|
|
|
|
git_pobject *po; |
211
|
|
|
|
|
|
|
size_t newsize; |
212
|
|
|
|
|
|
|
int ret; |
213
|
|
|
|
|
|
|
|
214
|
64
|
50
|
|
|
|
|
assert(pb && oid); |
|
|
50
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
/* If the object already exists in the hash table, then we don't |
217
|
|
|
|
|
|
|
* have any work to do */ |
218
|
64
|
100
|
|
|
|
|
if (git_oidmap_exists(pb->object_ix, oid)) |
219
|
11
|
|
|
|
|
|
return 0; |
220
|
|
|
|
|
|
|
|
221
|
53
|
100
|
|
|
|
|
if (pb->nr_objects >= pb->nr_alloc) { |
222
|
6
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&newsize, pb->nr_alloc, 1024); |
|
|
50
|
|
|
|
|
|
223
|
6
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newsize, newsize / 2, 3); |
|
|
50
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
225
|
6
|
50
|
|
|
|
|
if (!git__is_uint32(newsize)) { |
226
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_NOMEMORY, "packfile too large to fit in memory."); |
227
|
0
|
|
|
|
|
|
return -1; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
6
|
|
|
|
|
|
pb->nr_alloc = newsize; |
231
|
|
|
|
|
|
|
|
232
|
6
|
|
|
|
|
|
pb->object_list = git__reallocarray(pb->object_list, |
233
|
|
|
|
|
|
|
pb->nr_alloc, sizeof(*po)); |
234
|
6
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(pb->object_list); |
235
|
|
|
|
|
|
|
|
236
|
6
|
50
|
|
|
|
|
if (rehash(pb) < 0) |
237
|
0
|
|
|
|
|
|
return -1; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
53
|
|
|
|
|
|
po = pb->object_list + pb->nr_objects; |
241
|
53
|
|
|
|
|
|
memset(po, 0x0, sizeof(*po)); |
242
|
|
|
|
|
|
|
|
243
|
53
|
50
|
|
|
|
|
if ((ret = git_odb_read_header(&po->size, &po->type, pb->odb, oid)) < 0) |
244
|
0
|
|
|
|
|
|
return ret; |
245
|
|
|
|
|
|
|
|
246
|
53
|
|
|
|
|
|
pb->nr_objects++; |
247
|
53
|
|
|
|
|
|
git_oid_cpy(&po->id, oid); |
248
|
53
|
|
|
|
|
|
po->hash = name_hash(name); |
249
|
|
|
|
|
|
|
|
250
|
53
|
50
|
|
|
|
|
if (git_oidmap_set(pb->object_ix, &po->id, po) < 0) { |
251
|
0
|
|
|
|
|
|
git_error_set_oom(); |
252
|
0
|
|
|
|
|
|
return -1; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
53
|
|
|
|
|
|
pb->done = false; |
256
|
|
|
|
|
|
|
|
257
|
53
|
100
|
|
|
|
|
if (pb->progress_cb) { |
258
|
32
|
|
|
|
|
|
double current_time = git__timer(); |
259
|
32
|
|
|
|
|
|
double elapsed = current_time - pb->last_progress_report_time; |
260
|
|
|
|
|
|
|
|
261
|
32
|
100
|
|
|
|
|
if (elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) { |
262
|
2
|
|
|
|
|
|
pb->last_progress_report_time = current_time; |
263
|
|
|
|
|
|
|
|
264
|
2
|
|
|
|
|
|
ret = pb->progress_cb( |
265
|
|
|
|
|
|
|
GIT_PACKBUILDER_ADDING_OBJECTS, |
266
|
|
|
|
|
|
|
pb->nr_objects, 0, pb->progress_cb_payload); |
267
|
|
|
|
|
|
|
|
268
|
2
|
50
|
|
|
|
|
if (ret) |
269
|
0
|
|
|
|
|
|
return git_error_set_after_callback(ret); |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
64
|
|
|
|
|
|
return 0; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
0
|
|
|
|
|
|
static int get_delta(void **out, git_odb *odb, git_pobject *po) |
277
|
|
|
|
|
|
|
{ |
278
|
0
|
|
|
|
|
|
git_odb_object *src = NULL, *trg = NULL; |
279
|
|
|
|
|
|
|
size_t delta_size; |
280
|
|
|
|
|
|
|
void *delta_buf; |
281
|
|
|
|
|
|
|
int error; |
282
|
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
*out = NULL; |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
if (git_odb_read(&src, odb, &po->delta->id) < 0 || |
286
|
0
|
|
|
|
|
|
git_odb_read(&trg, odb, &po->id) < 0) |
287
|
|
|
|
|
|
|
goto on_error; |
288
|
|
|
|
|
|
|
|
289
|
0
|
|
|
|
|
|
error = git_delta(&delta_buf, &delta_size, |
290
|
|
|
|
|
|
|
git_odb_object_data(src), git_odb_object_size(src), |
291
|
|
|
|
|
|
|
git_odb_object_data(trg), git_odb_object_size(trg), |
292
|
|
|
|
|
|
|
0); |
293
|
|
|
|
|
|
|
|
294
|
0
|
0
|
|
|
|
|
if (error < 0 && error != GIT_EBUFS) |
|
|
0
|
|
|
|
|
|
295
|
0
|
|
|
|
|
|
goto on_error; |
296
|
|
|
|
|
|
|
|
297
|
0
|
0
|
|
|
|
|
if (error == GIT_EBUFS || delta_size != po->delta_size) { |
|
|
0
|
|
|
|
|
|
298
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "delta size changed"); |
299
|
0
|
|
|
|
|
|
goto on_error; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
0
|
|
|
|
|
|
*out = delta_buf; |
303
|
|
|
|
|
|
|
|
304
|
0
|
|
|
|
|
|
git_odb_object_free(src); |
305
|
0
|
|
|
|
|
|
git_odb_object_free(trg); |
306
|
0
|
|
|
|
|
|
return 0; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
on_error: |
309
|
0
|
|
|
|
|
|
git_odb_object_free(src); |
310
|
0
|
|
|
|
|
|
git_odb_object_free(trg); |
311
|
0
|
|
|
|
|
|
return -1; |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
|
314
|
53
|
|
|
|
|
|
static int write_object( |
315
|
|
|
|
|
|
|
git_packbuilder *pb, |
316
|
|
|
|
|
|
|
git_pobject *po, |
317
|
|
|
|
|
|
|
int (*write_cb)(void *buf, size_t size, void *cb_data), |
318
|
|
|
|
|
|
|
void *cb_data) |
319
|
|
|
|
|
|
|
{ |
320
|
53
|
|
|
|
|
|
git_odb_object *obj = NULL; |
321
|
|
|
|
|
|
|
git_object_t type; |
322
|
53
|
|
|
|
|
|
unsigned char hdr[10], *zbuf = NULL; |
323
|
53
|
|
|
|
|
|
void *data = NULL; |
324
|
53
|
|
|
|
|
|
size_t hdr_len, zbuf_len = COMPRESS_BUFLEN, data_len; |
325
|
|
|
|
|
|
|
int error; |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
/* |
328
|
|
|
|
|
|
|
* If we have a delta base, let's use the delta to save space. |
329
|
|
|
|
|
|
|
* Otherwise load the whole object. 'data' ends up pointing to |
330
|
|
|
|
|
|
|
* whatever data we want to put into the packfile. |
331
|
|
|
|
|
|
|
*/ |
332
|
53
|
100
|
|
|
|
|
if (po->delta) { |
333
|
3
|
50
|
|
|
|
|
if (po->delta_data) |
334
|
3
|
|
|
|
|
|
data = po->delta_data; |
335
|
0
|
0
|
|
|
|
|
else if ((error = get_delta(&data, pb->odb, po)) < 0) |
336
|
0
|
|
|
|
|
|
goto done; |
337
|
|
|
|
|
|
|
|
338
|
3
|
|
|
|
|
|
data_len = po->delta_size; |
339
|
3
|
|
|
|
|
|
type = GIT_OBJECT_REF_DELTA; |
340
|
|
|
|
|
|
|
} else { |
341
|
50
|
50
|
|
|
|
|
if ((error = git_odb_read(&obj, pb->odb, &po->id)) < 0) |
342
|
0
|
|
|
|
|
|
goto done; |
343
|
|
|
|
|
|
|
|
344
|
50
|
|
|
|
|
|
data = (void *)git_odb_object_data(obj); |
345
|
50
|
|
|
|
|
|
data_len = git_odb_object_size(obj); |
346
|
50
|
|
|
|
|
|
type = git_odb_object_type(obj); |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
/* Write header */ |
350
|
53
|
|
|
|
|
|
hdr_len = git_packfile__object_header(hdr, data_len, type); |
351
|
|
|
|
|
|
|
|
352
|
53
|
50
|
|
|
|
|
if ((error = write_cb(hdr, hdr_len, cb_data)) < 0 || |
|
|
50
|
|
|
|
|
|
353
|
53
|
|
|
|
|
|
(error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0) |
354
|
|
|
|
|
|
|
goto done; |
355
|
|
|
|
|
|
|
|
356
|
53
|
100
|
|
|
|
|
if (type == GIT_OBJECT_REF_DELTA) { |
357
|
3
|
50
|
|
|
|
|
if ((error = write_cb(po->delta->id.id, GIT_OID_RAWSZ, cb_data)) < 0 || |
|
|
50
|
|
|
|
|
|
358
|
3
|
|
|
|
|
|
(error = git_hash_update(&pb->ctx, po->delta->id.id, GIT_OID_RAWSZ)) < 0) |
359
|
|
|
|
|
|
|
goto done; |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
/* Write data */ |
363
|
53
|
100
|
|
|
|
|
if (po->z_delta_size) { |
364
|
3
|
|
|
|
|
|
data_len = po->z_delta_size; |
365
|
|
|
|
|
|
|
|
366
|
3
|
50
|
|
|
|
|
if ((error = write_cb(data, data_len, cb_data)) < 0 || |
|
|
50
|
|
|
|
|
|
367
|
3
|
|
|
|
|
|
(error = git_hash_update(&pb->ctx, data, data_len)) < 0) |
368
|
|
|
|
|
|
|
goto done; |
369
|
|
|
|
|
|
|
} else { |
370
|
50
|
|
|
|
|
|
zbuf = git__malloc(zbuf_len); |
371
|
50
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(zbuf); |
372
|
|
|
|
|
|
|
|
373
|
50
|
|
|
|
|
|
git_zstream_reset(&pb->zstream); |
374
|
|
|
|
|
|
|
|
375
|
50
|
50
|
|
|
|
|
if ((error = git_zstream_set_input(&pb->zstream, data, data_len)) < 0) |
376
|
0
|
|
|
|
|
|
goto done; |
377
|
|
|
|
|
|
|
|
378
|
100
|
100
|
|
|
|
|
while (!git_zstream_done(&pb->zstream)) { |
379
|
50
|
50
|
|
|
|
|
if ((error = git_zstream_get_output(zbuf, &zbuf_len, &pb->zstream)) < 0 || |
|
|
50
|
|
|
|
|
|
380
|
50
|
50
|
|
|
|
|
(error = write_cb(zbuf, zbuf_len, cb_data)) < 0 || |
381
|
50
|
|
|
|
|
|
(error = git_hash_update(&pb->ctx, zbuf, zbuf_len)) < 0) |
382
|
|
|
|
|
|
|
goto done; |
383
|
|
|
|
|
|
|
|
384
|
50
|
|
|
|
|
|
zbuf_len = COMPRESS_BUFLEN; /* reuse buffer */ |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
} |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
/* |
389
|
|
|
|
|
|
|
* If po->delta is true, data is a delta and it is our |
390
|
|
|
|
|
|
|
* responsibility to free it (otherwise it's a git_object's |
391
|
|
|
|
|
|
|
* data). We set po->delta_data to NULL in case we got the |
392
|
|
|
|
|
|
|
* data from there instead of get_delta(). If we didn't, |
393
|
|
|
|
|
|
|
* there's no harm. |
394
|
|
|
|
|
|
|
*/ |
395
|
53
|
100
|
|
|
|
|
if (po->delta) { |
396
|
3
|
|
|
|
|
|
git__free(data); |
397
|
3
|
|
|
|
|
|
po->delta_data = NULL; |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
|
400
|
53
|
|
|
|
|
|
pb->nr_written++; |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
done: |
403
|
53
|
|
|
|
|
|
git__free(zbuf); |
404
|
53
|
|
|
|
|
|
git_odb_object_free(obj); |
405
|
53
|
|
|
|
|
|
return error; |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
enum write_one_status { |
409
|
|
|
|
|
|
|
WRITE_ONE_SKIP = -1, /* already written */ |
410
|
|
|
|
|
|
|
WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ |
411
|
|
|
|
|
|
|
WRITE_ONE_WRITTEN = 1, /* normal */ |
412
|
|
|
|
|
|
|
WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ |
413
|
|
|
|
|
|
|
}; |
414
|
|
|
|
|
|
|
|
415
|
56
|
|
|
|
|
|
static int write_one( |
416
|
|
|
|
|
|
|
enum write_one_status *status, |
417
|
|
|
|
|
|
|
git_packbuilder *pb, |
418
|
|
|
|
|
|
|
git_pobject *po, |
419
|
|
|
|
|
|
|
int (*write_cb)(void *buf, size_t size, void *cb_data), |
420
|
|
|
|
|
|
|
void *cb_data) |
421
|
|
|
|
|
|
|
{ |
422
|
|
|
|
|
|
|
int error; |
423
|
|
|
|
|
|
|
|
424
|
56
|
50
|
|
|
|
|
if (po->recursing) { |
425
|
0
|
|
|
|
|
|
*status = WRITE_ONE_RECURSIVE; |
426
|
0
|
|
|
|
|
|
return 0; |
427
|
56
|
100
|
|
|
|
|
} else if (po->written) { |
428
|
3
|
|
|
|
|
|
*status = WRITE_ONE_SKIP; |
429
|
3
|
|
|
|
|
|
return 0; |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
|
432
|
53
|
100
|
|
|
|
|
if (po->delta) { |
433
|
3
|
|
|
|
|
|
po->recursing = 1; |
434
|
|
|
|
|
|
|
|
435
|
3
|
50
|
|
|
|
|
if ((error = write_one(status, pb, po->delta, write_cb, cb_data)) < 0) |
436
|
0
|
|
|
|
|
|
return error; |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
/* we cannot depend on this one */ |
439
|
3
|
50
|
|
|
|
|
if (*status == WRITE_ONE_RECURSIVE) |
440
|
0
|
|
|
|
|
|
po->delta = NULL; |
441
|
|
|
|
|
|
|
} |
442
|
|
|
|
|
|
|
|
443
|
53
|
|
|
|
|
|
*status = WRITE_ONE_WRITTEN; |
444
|
53
|
|
|
|
|
|
po->written = 1; |
445
|
53
|
|
|
|
|
|
po->recursing = 0; |
446
|
|
|
|
|
|
|
|
447
|
53
|
|
|
|
|
|
return write_object(pb, po, write_cb, cb_data); |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
53
|
|
|
|
|
|
GIT_INLINE(void) add_to_write_order(git_pobject **wo, size_t *endp, |
451
|
|
|
|
|
|
|
git_pobject *po) |
452
|
|
|
|
|
|
|
{ |
453
|
53
|
50
|
|
|
|
|
if (po->filled) |
454
|
0
|
|
|
|
|
|
return; |
455
|
53
|
|
|
|
|
|
wo[(*endp)++] = po; |
456
|
53
|
|
|
|
|
|
po->filled = 1; |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
|
459
|
0
|
|
|
|
|
|
static void add_descendants_to_write_order(git_pobject **wo, size_t *endp, |
460
|
|
|
|
|
|
|
git_pobject *po) |
461
|
|
|
|
|
|
|
{ |
462
|
0
|
|
|
|
|
|
int add_to_order = 1; |
463
|
0
|
0
|
|
|
|
|
while (po) { |
464
|
0
|
0
|
|
|
|
|
if (add_to_order) { |
465
|
|
|
|
|
|
|
git_pobject *s; |
466
|
|
|
|
|
|
|
/* add this node... */ |
467
|
0
|
|
|
|
|
|
add_to_write_order(wo, endp, po); |
468
|
|
|
|
|
|
|
/* all its siblings... */ |
469
|
0
|
0
|
|
|
|
|
for (s = po->delta_sibling; s; s = s->delta_sibling) { |
470
|
0
|
|
|
|
|
|
add_to_write_order(wo, endp, s); |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
|
/* drop down a level to add left subtree nodes if possible */ |
474
|
0
|
0
|
|
|
|
|
if (po->delta_child) { |
475
|
0
|
|
|
|
|
|
add_to_order = 1; |
476
|
0
|
|
|
|
|
|
po = po->delta_child; |
477
|
|
|
|
|
|
|
} else { |
478
|
0
|
|
|
|
|
|
add_to_order = 0; |
479
|
|
|
|
|
|
|
/* our sibling might have some children, it is next */ |
480
|
0
|
0
|
|
|
|
|
if (po->delta_sibling) { |
481
|
0
|
|
|
|
|
|
po = po->delta_sibling; |
482
|
0
|
|
|
|
|
|
continue; |
483
|
|
|
|
|
|
|
} |
484
|
|
|
|
|
|
|
/* go back to our parent node */ |
485
|
0
|
|
|
|
|
|
po = po->delta; |
486
|
0
|
0
|
|
|
|
|
while (po && !po->delta_sibling) { |
|
|
0
|
|
|
|
|
|
487
|
|
|
|
|
|
|
/* we're on the right side of a subtree, keep |
488
|
|
|
|
|
|
|
* going up until we can go right again */ |
489
|
0
|
|
|
|
|
|
po = po->delta; |
490
|
|
|
|
|
|
|
} |
491
|
0
|
0
|
|
|
|
|
if (!po) { |
492
|
|
|
|
|
|
|
/* done- we hit our original root node */ |
493
|
0
|
|
|
|
|
|
return; |
494
|
|
|
|
|
|
|
} |
495
|
|
|
|
|
|
|
/* pass it off to sibling at this level */ |
496
|
0
|
|
|
|
|
|
po = po->delta_sibling; |
497
|
|
|
|
|
|
|
} |
498
|
|
|
|
|
|
|
}; |
499
|
|
|
|
|
|
|
} |
500
|
|
|
|
|
|
|
|
501
|
0
|
|
|
|
|
|
static void add_family_to_write_order(git_pobject **wo, size_t *endp, |
502
|
|
|
|
|
|
|
git_pobject *po) |
503
|
|
|
|
|
|
|
{ |
504
|
|
|
|
|
|
|
git_pobject *root; |
505
|
|
|
|
|
|
|
|
506
|
0
|
0
|
|
|
|
|
for (root = po; root->delta; root = root->delta) |
507
|
|
|
|
|
|
|
; /* nothing */ |
508
|
0
|
|
|
|
|
|
add_descendants_to_write_order(wo, endp, root); |
509
|
0
|
|
|
|
|
|
} |
510
|
|
|
|
|
|
|
|
511
|
0
|
|
|
|
|
|
static int cb_tag_foreach(const char *name, git_oid *oid, void *data) |
512
|
|
|
|
|
|
|
{ |
513
|
0
|
|
|
|
|
|
git_packbuilder *pb = data; |
514
|
|
|
|
|
|
|
git_pobject *po; |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
GIT_UNUSED(name); |
517
|
|
|
|
|
|
|
|
518
|
0
|
0
|
|
|
|
|
if ((po = git_oidmap_get(pb->object_ix, oid)) == NULL) |
519
|
0
|
|
|
|
|
|
return 0; |
520
|
|
|
|
|
|
|
|
521
|
0
|
|
|
|
|
|
po->tagged = 1; |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
/* TODO: peel objects */ |
524
|
|
|
|
|
|
|
|
525
|
0
|
|
|
|
|
|
return 0; |
526
|
|
|
|
|
|
|
} |
527
|
|
|
|
|
|
|
|
528
|
7
|
|
|
|
|
|
static git_pobject **compute_write_order(git_packbuilder *pb) |
529
|
|
|
|
|
|
|
{ |
530
|
|
|
|
|
|
|
size_t i, wo_end, last_untagged; |
531
|
|
|
|
|
|
|
git_pobject **wo; |
532
|
|
|
|
|
|
|
|
533
|
7
|
50
|
|
|
|
|
if ((wo = git__mallocarray(pb->nr_objects, sizeof(*wo))) == NULL) |
534
|
0
|
|
|
|
|
|
return NULL; |
535
|
|
|
|
|
|
|
|
536
|
60
|
100
|
|
|
|
|
for (i = 0; i < pb->nr_objects; i++) { |
537
|
53
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
538
|
53
|
|
|
|
|
|
po->tagged = 0; |
539
|
53
|
|
|
|
|
|
po->filled = 0; |
540
|
53
|
|
|
|
|
|
po->delta_child = NULL; |
541
|
53
|
|
|
|
|
|
po->delta_sibling = NULL; |
542
|
|
|
|
|
|
|
} |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
/* |
545
|
|
|
|
|
|
|
* Fully connect delta_child/delta_sibling network. |
546
|
|
|
|
|
|
|
* Make sure delta_sibling is sorted in the original |
547
|
|
|
|
|
|
|
* recency order. |
548
|
|
|
|
|
|
|
*/ |
549
|
60
|
100
|
|
|
|
|
for (i = pb->nr_objects; i > 0;) { |
550
|
53
|
|
|
|
|
|
git_pobject *po = &pb->object_list[--i]; |
551
|
53
|
100
|
|
|
|
|
if (!po->delta) |
552
|
50
|
|
|
|
|
|
continue; |
553
|
|
|
|
|
|
|
/* Mark me as the first child */ |
554
|
3
|
|
|
|
|
|
po->delta_sibling = po->delta->delta_child; |
555
|
3
|
|
|
|
|
|
po->delta->delta_child = po; |
556
|
|
|
|
|
|
|
} |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
/* |
559
|
|
|
|
|
|
|
* Mark objects that are at the tip of tags. |
560
|
|
|
|
|
|
|
*/ |
561
|
7
|
50
|
|
|
|
|
if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) { |
562
|
0
|
|
|
|
|
|
git__free(wo); |
563
|
0
|
|
|
|
|
|
return NULL; |
564
|
|
|
|
|
|
|
} |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
/* |
567
|
|
|
|
|
|
|
* Give the objects in the original recency order until |
568
|
|
|
|
|
|
|
* we see a tagged tip. |
569
|
|
|
|
|
|
|
*/ |
570
|
60
|
100
|
|
|
|
|
for (i = wo_end = 0; i < pb->nr_objects; i++) { |
571
|
53
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
572
|
53
|
50
|
|
|
|
|
if (po->tagged) |
573
|
0
|
|
|
|
|
|
break; |
574
|
53
|
|
|
|
|
|
add_to_write_order(wo, &wo_end, po); |
575
|
|
|
|
|
|
|
} |
576
|
7
|
|
|
|
|
|
last_untagged = i; |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
/* |
579
|
|
|
|
|
|
|
* Then fill all the tagged tips. |
580
|
|
|
|
|
|
|
*/ |
581
|
7
|
50
|
|
|
|
|
for (; i < pb->nr_objects; i++) { |
582
|
0
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
583
|
0
|
0
|
|
|
|
|
if (po->tagged) |
584
|
0
|
|
|
|
|
|
add_to_write_order(wo, &wo_end, po); |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
/* |
588
|
|
|
|
|
|
|
* And then all remaining commits and tags. |
589
|
|
|
|
|
|
|
*/ |
590
|
7
|
50
|
|
|
|
|
for (i = last_untagged; i < pb->nr_objects; i++) { |
591
|
0
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
592
|
0
|
0
|
|
|
|
|
if (po->type != GIT_OBJECT_COMMIT && |
|
|
0
|
|
|
|
|
|
593
|
0
|
|
|
|
|
|
po->type != GIT_OBJECT_TAG) |
594
|
0
|
|
|
|
|
|
continue; |
595
|
0
|
|
|
|
|
|
add_to_write_order(wo, &wo_end, po); |
596
|
|
|
|
|
|
|
} |
597
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
/* |
599
|
|
|
|
|
|
|
* And then all the trees. |
600
|
|
|
|
|
|
|
*/ |
601
|
7
|
50
|
|
|
|
|
for (i = last_untagged; i < pb->nr_objects; i++) { |
602
|
0
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
603
|
0
|
0
|
|
|
|
|
if (po->type != GIT_OBJECT_TREE) |
604
|
0
|
|
|
|
|
|
continue; |
605
|
0
|
|
|
|
|
|
add_to_write_order(wo, &wo_end, po); |
606
|
|
|
|
|
|
|
} |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
/* |
609
|
|
|
|
|
|
|
* Finally all the rest in really tight order |
610
|
|
|
|
|
|
|
*/ |
611
|
7
|
50
|
|
|
|
|
for (i = last_untagged; i < pb->nr_objects; i++) { |
612
|
0
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
613
|
0
|
0
|
|
|
|
|
if (!po->filled) |
614
|
0
|
|
|
|
|
|
add_family_to_write_order(wo, &wo_end, po); |
615
|
|
|
|
|
|
|
} |
616
|
|
|
|
|
|
|
|
617
|
7
|
50
|
|
|
|
|
if (wo_end != pb->nr_objects) { |
618
|
0
|
|
|
|
|
|
git__free(wo); |
619
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "invalid write order"); |
620
|
0
|
|
|
|
|
|
return NULL; |
621
|
|
|
|
|
|
|
} |
622
|
|
|
|
|
|
|
|
623
|
7
|
|
|
|
|
|
return wo; |
624
|
|
|
|
|
|
|
} |
625
|
|
|
|
|
|
|
|
626
|
7
|
|
|
|
|
|
static int write_pack(git_packbuilder *pb, |
627
|
|
|
|
|
|
|
int (*write_cb)(void *buf, size_t size, void *cb_data), |
628
|
|
|
|
|
|
|
void *cb_data) |
629
|
|
|
|
|
|
|
{ |
630
|
|
|
|
|
|
|
git_pobject **write_order; |
631
|
|
|
|
|
|
|
git_pobject *po; |
632
|
|
|
|
|
|
|
enum write_one_status status; |
633
|
|
|
|
|
|
|
struct git_pack_header ph; |
634
|
|
|
|
|
|
|
git_oid entry_oid; |
635
|
7
|
|
|
|
|
|
size_t i = 0; |
636
|
7
|
|
|
|
|
|
int error = 0; |
637
|
|
|
|
|
|
|
|
638
|
7
|
|
|
|
|
|
write_order = compute_write_order(pb); |
639
|
7
|
50
|
|
|
|
|
if (write_order == NULL) |
640
|
0
|
|
|
|
|
|
return -1; |
641
|
|
|
|
|
|
|
|
642
|
7
|
50
|
|
|
|
|
if (!git__is_uint32(pb->nr_objects)) { |
643
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "too many objects"); |
644
|
0
|
|
|
|
|
|
return -1; |
645
|
|
|
|
|
|
|
} |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
/* Write pack header */ |
648
|
7
|
|
|
|
|
|
ph.hdr_signature = htonl(PACK_SIGNATURE); |
649
|
7
|
|
|
|
|
|
ph.hdr_version = htonl(PACK_VERSION); |
650
|
7
|
|
|
|
|
|
ph.hdr_entries = htonl(pb->nr_objects); |
651
|
|
|
|
|
|
|
|
652
|
7
|
50
|
|
|
|
|
if ((error = write_cb(&ph, sizeof(ph), cb_data)) < 0 || |
|
|
50
|
|
|
|
|
|
653
|
7
|
|
|
|
|
|
(error = git_hash_update(&pb->ctx, &ph, sizeof(ph))) < 0) |
654
|
|
|
|
|
|
|
goto done; |
655
|
|
|
|
|
|
|
|
656
|
7
|
|
|
|
|
|
pb->nr_remaining = pb->nr_objects; |
657
|
|
|
|
|
|
|
do { |
658
|
7
|
|
|
|
|
|
pb->nr_written = 0; |
659
|
60
|
100
|
|
|
|
|
for ( ; i < pb->nr_objects; ++i) { |
660
|
53
|
|
|
|
|
|
po = write_order[i]; |
661
|
|
|
|
|
|
|
|
662
|
53
|
50
|
|
|
|
|
if ((error = write_one(&status, pb, po, write_cb, cb_data)) < 0) |
663
|
0
|
|
|
|
|
|
goto done; |
664
|
|
|
|
|
|
|
} |
665
|
|
|
|
|
|
|
|
666
|
7
|
|
|
|
|
|
pb->nr_remaining -= pb->nr_written; |
667
|
7
|
50
|
|
|
|
|
} while (pb->nr_remaining && i < pb->nr_objects); |
|
|
0
|
|
|
|
|
|
668
|
|
|
|
|
|
|
|
669
|
7
|
50
|
|
|
|
|
if ((error = git_hash_final(&entry_oid, &pb->ctx)) < 0) |
670
|
0
|
|
|
|
|
|
goto done; |
671
|
|
|
|
|
|
|
|
672
|
7
|
|
|
|
|
|
error = write_cb(entry_oid.id, GIT_OID_RAWSZ, cb_data); |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
done: |
675
|
|
|
|
|
|
|
/* if callback cancelled writing, we must still free delta_data */ |
676
|
7
|
50
|
|
|
|
|
for ( ; i < pb->nr_objects; ++i) { |
677
|
0
|
|
|
|
|
|
po = write_order[i]; |
678
|
0
|
0
|
|
|
|
|
if (po->delta_data) { |
679
|
0
|
|
|
|
|
|
git__free(po->delta_data); |
680
|
0
|
|
|
|
|
|
po->delta_data = NULL; |
681
|
|
|
|
|
|
|
} |
682
|
|
|
|
|
|
|
} |
683
|
|
|
|
|
|
|
|
684
|
7
|
|
|
|
|
|
git__free(write_order); |
685
|
7
|
|
|
|
|
|
return error; |
686
|
|
|
|
|
|
|
} |
687
|
|
|
|
|
|
|
|
688
|
33
|
|
|
|
|
|
static int write_pack_buf(void *buf, size_t size, void *data) |
689
|
|
|
|
|
|
|
{ |
690
|
33
|
|
|
|
|
|
git_buf *b = (git_buf *)data; |
691
|
33
|
|
|
|
|
|
return git_buf_put(b, buf, size); |
692
|
|
|
|
|
|
|
} |
693
|
|
|
|
|
|
|
|
694
|
43
|
|
|
|
|
|
static int type_size_sort(const void *_a, const void *_b) |
695
|
|
|
|
|
|
|
{ |
696
|
43
|
|
|
|
|
|
const git_pobject *a = (git_pobject *)_a; |
697
|
43
|
|
|
|
|
|
const git_pobject *b = (git_pobject *)_b; |
698
|
|
|
|
|
|
|
|
699
|
43
|
100
|
|
|
|
|
if (a->type > b->type) |
700
|
9
|
|
|
|
|
|
return -1; |
701
|
34
|
100
|
|
|
|
|
if (a->type < b->type) |
702
|
17
|
|
|
|
|
|
return 1; |
703
|
17
|
50
|
|
|
|
|
if (a->hash > b->hash) |
704
|
0
|
|
|
|
|
|
return -1; |
705
|
17
|
50
|
|
|
|
|
if (a->hash < b->hash) |
706
|
0
|
|
|
|
|
|
return 1; |
707
|
|
|
|
|
|
|
/* |
708
|
|
|
|
|
|
|
* TODO |
709
|
|
|
|
|
|
|
* |
710
|
|
|
|
|
|
|
if (a->preferred_base > b->preferred_base) |
711
|
|
|
|
|
|
|
return -1; |
712
|
|
|
|
|
|
|
if (a->preferred_base < b->preferred_base) |
713
|
|
|
|
|
|
|
return 1; |
714
|
|
|
|
|
|
|
*/ |
715
|
17
|
100
|
|
|
|
|
if (a->size > b->size) |
716
|
6
|
|
|
|
|
|
return -1; |
717
|
11
|
100
|
|
|
|
|
if (a->size < b->size) |
718
|
6
|
|
|
|
|
|
return 1; |
719
|
5
|
100
|
|
|
|
|
return a < b ? -1 : (a > b); /* newest first */ |
720
|
|
|
|
|
|
|
} |
721
|
|
|
|
|
|
|
|
722
|
3
|
|
|
|
|
|
static int delta_cacheable( |
723
|
|
|
|
|
|
|
git_packbuilder *pb, |
724
|
|
|
|
|
|
|
size_t src_size, |
725
|
|
|
|
|
|
|
size_t trg_size, |
726
|
|
|
|
|
|
|
size_t delta_size) |
727
|
|
|
|
|
|
|
{ |
728
|
|
|
|
|
|
|
size_t new_size; |
729
|
|
|
|
|
|
|
|
730
|
3
|
50
|
|
|
|
|
if (git__add_sizet_overflow(&new_size, pb->delta_cache_size, delta_size)) |
731
|
0
|
|
|
|
|
|
return 0; |
732
|
|
|
|
|
|
|
|
733
|
3
|
50
|
|
|
|
|
if (pb->max_delta_cache_size && new_size > pb->max_delta_cache_size) |
|
|
50
|
|
|
|
|
|
734
|
0
|
|
|
|
|
|
return 0; |
735
|
|
|
|
|
|
|
|
736
|
3
|
50
|
|
|
|
|
if (delta_size < pb->cache_max_small_delta_size) |
737
|
3
|
|
|
|
|
|
return 1; |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
/* cache delta, if objects are large enough compared to delta size */ |
740
|
0
|
0
|
|
|
|
|
if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) |
741
|
0
|
|
|
|
|
|
return 1; |
742
|
|
|
|
|
|
|
|
743
|
3
|
|
|
|
|
|
return 0; |
744
|
|
|
|
|
|
|
} |
745
|
|
|
|
|
|
|
|
746
|
33
|
|
|
|
|
|
static int try_delta(git_packbuilder *pb, struct unpacked *trg, |
747
|
|
|
|
|
|
|
struct unpacked *src, size_t max_depth, |
748
|
|
|
|
|
|
|
size_t *mem_usage, int *ret) |
749
|
|
|
|
|
|
|
{ |
750
|
33
|
|
|
|
|
|
git_pobject *trg_object = trg->object; |
751
|
33
|
|
|
|
|
|
git_pobject *src_object = src->object; |
752
|
|
|
|
|
|
|
git_odb_object *obj; |
753
|
|
|
|
|
|
|
size_t trg_size, src_size, delta_size, sizediff, max_size, sz; |
754
|
|
|
|
|
|
|
size_t ref_depth; |
755
|
|
|
|
|
|
|
void *delta_buf; |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
/* Don't bother doing diffs between different types */ |
758
|
33
|
100
|
|
|
|
|
if (trg_object->type != src_object->type) { |
759
|
13
|
|
|
|
|
|
*ret = -1; |
760
|
13
|
|
|
|
|
|
return 0; |
761
|
|
|
|
|
|
|
} |
762
|
|
|
|
|
|
|
|
763
|
20
|
|
|
|
|
|
*ret = 0; |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
/* TODO: support reuse-delta */ |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
/* Let's not bust the allowed depth. */ |
768
|
20
|
50
|
|
|
|
|
if (src->depth >= max_depth) |
769
|
0
|
|
|
|
|
|
return 0; |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
/* Now some size filtering heuristics. */ |
772
|
20
|
|
|
|
|
|
trg_size = trg_object->size; |
773
|
20
|
50
|
|
|
|
|
if (!trg_object->delta) { |
774
|
20
|
|
|
|
|
|
max_size = trg_size/2 - 20; |
775
|
20
|
|
|
|
|
|
ref_depth = 1; |
776
|
|
|
|
|
|
|
} else { |
777
|
0
|
|
|
|
|
|
max_size = trg_object->delta_size; |
778
|
0
|
|
|
|
|
|
ref_depth = trg->depth; |
779
|
|
|
|
|
|
|
} |
780
|
|
|
|
|
|
|
|
781
|
40
|
|
|
|
|
|
max_size = (uint64_t)max_size * (max_depth - src->depth) / |
782
|
20
|
|
|
|
|
|
(max_depth - ref_depth + 1); |
783
|
20
|
50
|
|
|
|
|
if (max_size == 0) |
784
|
0
|
|
|
|
|
|
return 0; |
785
|
|
|
|
|
|
|
|
786
|
20
|
|
|
|
|
|
src_size = src_object->size; |
787
|
20
|
50
|
|
|
|
|
sizediff = src_size < trg_size ? trg_size - src_size : 0; |
788
|
20
|
50
|
|
|
|
|
if (sizediff >= max_size) |
789
|
0
|
|
|
|
|
|
return 0; |
790
|
20
|
50
|
|
|
|
|
if (trg_size < src_size / 32) |
791
|
0
|
|
|
|
|
|
return 0; |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
/* Load data if not already done */ |
794
|
20
|
100
|
|
|
|
|
if (!trg->data) { |
795
|
12
|
50
|
|
|
|
|
if (git_odb_read(&obj, pb->odb, &trg_object->id) < 0) |
796
|
0
|
|
|
|
|
|
return -1; |
797
|
|
|
|
|
|
|
|
798
|
12
|
|
|
|
|
|
sz = git_odb_object_size(obj); |
799
|
12
|
|
|
|
|
|
trg->data = git__malloc(sz); |
800
|
12
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(trg->data); |
801
|
12
|
|
|
|
|
|
memcpy(trg->data, git_odb_object_data(obj), sz); |
802
|
|
|
|
|
|
|
|
803
|
12
|
|
|
|
|
|
git_odb_object_free(obj); |
804
|
|
|
|
|
|
|
|
805
|
12
|
50
|
|
|
|
|
if (sz != trg_size) { |
806
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, |
807
|
|
|
|
|
|
|
"inconsistent target object length"); |
808
|
0
|
|
|
|
|
|
return -1; |
809
|
|
|
|
|
|
|
} |
810
|
|
|
|
|
|
|
|
811
|
12
|
|
|
|
|
|
*mem_usage += sz; |
812
|
|
|
|
|
|
|
} |
813
|
20
|
100
|
|
|
|
|
if (!src->data) { |
814
|
|
|
|
|
|
|
size_t obj_sz; |
815
|
|
|
|
|
|
|
|
816
|
12
|
50
|
|
|
|
|
if (git_odb_read(&obj, pb->odb, &src_object->id) < 0 || |
|
|
50
|
|
|
|
|
|
817
|
6
|
|
|
|
|
|
!git__is_ulong(obj_sz = git_odb_object_size(obj))) |
818
|
0
|
|
|
|
|
|
return -1; |
819
|
|
|
|
|
|
|
|
820
|
6
|
|
|
|
|
|
sz = obj_sz; |
821
|
6
|
|
|
|
|
|
src->data = git__malloc(sz); |
822
|
6
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(src->data); |
823
|
6
|
|
|
|
|
|
memcpy(src->data, git_odb_object_data(obj), sz); |
824
|
|
|
|
|
|
|
|
825
|
6
|
|
|
|
|
|
git_odb_object_free(obj); |
826
|
|
|
|
|
|
|
|
827
|
6
|
50
|
|
|
|
|
if (sz != src_size) { |
828
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, |
829
|
|
|
|
|
|
|
"inconsistent source object length"); |
830
|
0
|
|
|
|
|
|
return -1; |
831
|
|
|
|
|
|
|
} |
832
|
|
|
|
|
|
|
|
833
|
6
|
|
|
|
|
|
*mem_usage += sz; |
834
|
|
|
|
|
|
|
} |
835
|
20
|
100
|
|
|
|
|
if (!src->index) { |
836
|
12
|
50
|
|
|
|
|
if (git_delta_index_init(&src->index, src->data, src_size) < 0) |
837
|
0
|
|
|
|
|
|
return 0; /* suboptimal pack - out of memory */ |
838
|
|
|
|
|
|
|
|
839
|
12
|
|
|
|
|
|
*mem_usage += git_delta_index_size(src->index); |
840
|
|
|
|
|
|
|
} |
841
|
|
|
|
|
|
|
|
842
|
20
|
100
|
|
|
|
|
if (git_delta_create_from_index(&delta_buf, &delta_size, src->index, trg->data, trg_size, |
843
|
|
|
|
|
|
|
max_size) < 0) |
844
|
17
|
|
|
|
|
|
return 0; |
845
|
|
|
|
|
|
|
|
846
|
3
|
50
|
|
|
|
|
if (trg_object->delta) { |
847
|
|
|
|
|
|
|
/* Prefer only shallower same-sized deltas. */ |
848
|
0
|
0
|
|
|
|
|
if (delta_size == trg_object->delta_size && |
|
|
0
|
|
|
|
|
|
849
|
0
|
|
|
|
|
|
src->depth + 1 >= trg->depth) { |
850
|
0
|
|
|
|
|
|
git__free(delta_buf); |
851
|
0
|
|
|
|
|
|
return 0; |
852
|
|
|
|
|
|
|
} |
853
|
|
|
|
|
|
|
} |
854
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
git_packbuilder__cache_lock(pb); |
856
|
3
|
50
|
|
|
|
|
if (trg_object->delta_data) { |
857
|
0
|
|
|
|
|
|
git__free(trg_object->delta_data); |
858
|
0
|
0
|
|
|
|
|
assert(pb->delta_cache_size >= trg_object->delta_size); |
859
|
0
|
|
|
|
|
|
pb->delta_cache_size -= trg_object->delta_size; |
860
|
0
|
|
|
|
|
|
trg_object->delta_data = NULL; |
861
|
|
|
|
|
|
|
} |
862
|
3
|
50
|
|
|
|
|
if (delta_cacheable(pb, src_size, trg_size, delta_size)) { |
863
|
3
|
|
|
|
|
|
bool overflow = git__add_sizet_overflow( |
864
|
|
|
|
|
|
|
&pb->delta_cache_size, pb->delta_cache_size, delta_size); |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
git_packbuilder__cache_unlock(pb); |
867
|
|
|
|
|
|
|
|
868
|
3
|
50
|
|
|
|
|
if (overflow) { |
869
|
0
|
|
|
|
|
|
git__free(delta_buf); |
870
|
0
|
|
|
|
|
|
return -1; |
871
|
|
|
|
|
|
|
} |
872
|
|
|
|
|
|
|
|
873
|
3
|
|
|
|
|
|
trg_object->delta_data = git__realloc(delta_buf, delta_size); |
874
|
3
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(trg_object->delta_data); |
875
|
|
|
|
|
|
|
} else { |
876
|
|
|
|
|
|
|
/* create delta when writing the pack */ |
877
|
|
|
|
|
|
|
git_packbuilder__cache_unlock(pb); |
878
|
0
|
|
|
|
|
|
git__free(delta_buf); |
879
|
|
|
|
|
|
|
} |
880
|
|
|
|
|
|
|
|
881
|
3
|
|
|
|
|
|
trg_object->delta = src_object; |
882
|
3
|
|
|
|
|
|
trg_object->delta_size = delta_size; |
883
|
3
|
|
|
|
|
|
trg->depth = src->depth + 1; |
884
|
|
|
|
|
|
|
|
885
|
3
|
|
|
|
|
|
*ret = 1; |
886
|
33
|
|
|
|
|
|
return 0; |
887
|
|
|
|
|
|
|
} |
888
|
|
|
|
|
|
|
|
889
|
0
|
|
|
|
|
|
static size_t check_delta_limit(git_pobject *me, size_t n) |
890
|
|
|
|
|
|
|
{ |
891
|
0
|
|
|
|
|
|
git_pobject *child = me->delta_child; |
892
|
0
|
|
|
|
|
|
size_t m = n; |
893
|
|
|
|
|
|
|
|
894
|
0
|
0
|
|
|
|
|
while (child) { |
895
|
0
|
|
|
|
|
|
size_t c = check_delta_limit(child, n + 1); |
896
|
0
|
0
|
|
|
|
|
if (m < c) |
897
|
0
|
|
|
|
|
|
m = c; |
898
|
0
|
|
|
|
|
|
child = child->delta_sibling; |
899
|
|
|
|
|
|
|
} |
900
|
0
|
|
|
|
|
|
return m; |
901
|
|
|
|
|
|
|
} |
902
|
|
|
|
|
|
|
|
903
|
24
|
|
|
|
|
|
static size_t free_unpacked(struct unpacked *n) |
904
|
|
|
|
|
|
|
{ |
905
|
24
|
|
|
|
|
|
size_t freed_mem = 0; |
906
|
|
|
|
|
|
|
|
907
|
24
|
50
|
|
|
|
|
if (n->index) { |
908
|
0
|
|
|
|
|
|
freed_mem += git_delta_index_size(n->index); |
909
|
0
|
|
|
|
|
|
git_delta_index_free(n->index); |
910
|
|
|
|
|
|
|
} |
911
|
24
|
|
|
|
|
|
n->index = NULL; |
912
|
|
|
|
|
|
|
|
913
|
24
|
50
|
|
|
|
|
if (n->data) { |
914
|
0
|
|
|
|
|
|
freed_mem += n->object->size; |
915
|
0
|
|
|
|
|
|
git__free(n->data); |
916
|
0
|
|
|
|
|
|
n->data = NULL; |
917
|
|
|
|
|
|
|
} |
918
|
24
|
|
|
|
|
|
n->object = NULL; |
919
|
24
|
|
|
|
|
|
n->depth = 0; |
920
|
24
|
|
|
|
|
|
return freed_mem; |
921
|
|
|
|
|
|
|
} |
922
|
|
|
|
|
|
|
|
923
|
30
|
|
|
|
|
|
static int report_delta_progress( |
924
|
|
|
|
|
|
|
git_packbuilder *pb, uint32_t count, bool force) |
925
|
|
|
|
|
|
|
{ |
926
|
|
|
|
|
|
|
int ret; |
927
|
|
|
|
|
|
|
|
928
|
30
|
100
|
|
|
|
|
if (pb->progress_cb) { |
929
|
19
|
|
|
|
|
|
double current_time = git__timer(); |
930
|
19
|
|
|
|
|
|
double elapsed = current_time - pb->last_progress_report_time; |
931
|
|
|
|
|
|
|
|
932
|
19
|
100
|
|
|
|
|
if (force || elapsed >= MIN_PROGRESS_UPDATE_INTERVAL) { |
|
|
100
|
|
|
|
|
|
933
|
4
|
|
|
|
|
|
pb->last_progress_report_time = current_time; |
934
|
|
|
|
|
|
|
|
935
|
4
|
|
|
|
|
|
ret = pb->progress_cb( |
936
|
|
|
|
|
|
|
GIT_PACKBUILDER_DELTAFICATION, |
937
|
|
|
|
|
|
|
count, pb->nr_objects, pb->progress_cb_payload); |
938
|
|
|
|
|
|
|
|
939
|
4
|
50
|
|
|
|
|
if (ret) |
940
|
0
|
|
|
|
|
|
return git_error_set_after_callback(ret); |
941
|
|
|
|
|
|
|
} |
942
|
|
|
|
|
|
|
} |
943
|
|
|
|
|
|
|
|
944
|
30
|
|
|
|
|
|
return 0; |
945
|
|
|
|
|
|
|
} |
946
|
|
|
|
|
|
|
|
947
|
6
|
|
|
|
|
|
static int find_deltas(git_packbuilder *pb, git_pobject **list, |
948
|
|
|
|
|
|
|
size_t *list_size, size_t window, size_t depth) |
949
|
|
|
|
|
|
|
{ |
950
|
|
|
|
|
|
|
git_pobject *po; |
951
|
6
|
|
|
|
|
|
git_buf zbuf = GIT_BUF_INIT; |
952
|
|
|
|
|
|
|
struct unpacked *array; |
953
|
6
|
|
|
|
|
|
size_t idx = 0, count = 0; |
954
|
6
|
|
|
|
|
|
size_t mem_usage = 0; |
955
|
|
|
|
|
|
|
size_t i; |
956
|
6
|
|
|
|
|
|
int error = -1; |
957
|
|
|
|
|
|
|
|
958
|
6
|
|
|
|
|
|
array = git__calloc(window, sizeof(struct unpacked)); |
959
|
6
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(array); |
960
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
for (;;) { |
962
|
30
|
|
|
|
|
|
struct unpacked *n = array + idx; |
963
|
30
|
|
|
|
|
|
size_t max_depth, j, best_base = SIZE_MAX; |
964
|
|
|
|
|
|
|
|
965
|
|
|
|
|
|
|
git_packbuilder__progress_lock(pb); |
966
|
30
|
100
|
|
|
|
|
if (!*list_size) { |
967
|
|
|
|
|
|
|
git_packbuilder__progress_unlock(pb); |
968
|
6
|
|
|
|
|
|
break; |
969
|
|
|
|
|
|
|
} |
970
|
|
|
|
|
|
|
|
971
|
24
|
|
|
|
|
|
pb->nr_deltified += 1; |
972
|
24
|
|
|
|
|
|
report_delta_progress(pb, pb->nr_deltified, false); |
973
|
|
|
|
|
|
|
|
974
|
24
|
|
|
|
|
|
po = *list++; |
975
|
24
|
|
|
|
|
|
(*list_size)--; |
976
|
|
|
|
|
|
|
git_packbuilder__progress_unlock(pb); |
977
|
|
|
|
|
|
|
|
978
|
24
|
|
|
|
|
|
mem_usage -= free_unpacked(n); |
979
|
24
|
|
|
|
|
|
n->object = po; |
980
|
|
|
|
|
|
|
|
981
|
24
|
50
|
|
|
|
|
while (pb->window_memory_limit && |
|
|
0
|
|
|
|
|
|
982
|
0
|
0
|
|
|
|
|
mem_usage > pb->window_memory_limit && |
983
|
|
|
|
|
|
|
count > 1) { |
984
|
0
|
|
|
|
|
|
size_t tail = (idx + window - count) % window; |
985
|
0
|
|
|
|
|
|
mem_usage -= free_unpacked(array + tail); |
986
|
0
|
|
|
|
|
|
count--; |
987
|
|
|
|
|
|
|
} |
988
|
|
|
|
|
|
|
|
989
|
|
|
|
|
|
|
/* |
990
|
|
|
|
|
|
|
* If the current object is at pack edge, take the depth the |
991
|
|
|
|
|
|
|
* objects that depend on the current object into account |
992
|
|
|
|
|
|
|
* otherwise they would become too deep. |
993
|
|
|
|
|
|
|
*/ |
994
|
24
|
|
|
|
|
|
max_depth = depth; |
995
|
24
|
50
|
|
|
|
|
if (po->delta_child) { |
996
|
0
|
|
|
|
|
|
size_t delta_limit = check_delta_limit(po, 0); |
997
|
|
|
|
|
|
|
|
998
|
0
|
0
|
|
|
|
|
if (delta_limit > max_depth) |
999
|
0
|
|
|
|
|
|
goto next; |
1000
|
|
|
|
|
|
|
|
1001
|
0
|
|
|
|
|
|
max_depth -= delta_limit; |
1002
|
|
|
|
|
|
|
} |
1003
|
|
|
|
|
|
|
|
1004
|
24
|
|
|
|
|
|
j = window; |
1005
|
44
|
50
|
|
|
|
|
while (--j > 0) { |
1006
|
|
|
|
|
|
|
int ret; |
1007
|
44
|
|
|
|
|
|
size_t other_idx = idx + j; |
1008
|
|
|
|
|
|
|
struct unpacked *m; |
1009
|
|
|
|
|
|
|
|
1010
|
44
|
100
|
|
|
|
|
if (other_idx >= window) |
1011
|
33
|
|
|
|
|
|
other_idx -= window; |
1012
|
|
|
|
|
|
|
|
1013
|
44
|
|
|
|
|
|
m = array + other_idx; |
1014
|
44
|
100
|
|
|
|
|
if (!m->object) |
1015
|
24
|
|
|
|
|
|
break; |
1016
|
|
|
|
|
|
|
|
1017
|
33
|
50
|
|
|
|
|
if (try_delta(pb, n, m, max_depth, &mem_usage, &ret) < 0) |
1018
|
0
|
|
|
|
|
|
goto on_error; |
1019
|
33
|
100
|
|
|
|
|
if (ret < 0) |
1020
|
13
|
|
|
|
|
|
break; |
1021
|
20
|
100
|
|
|
|
|
else if (ret > 0) |
1022
|
20
|
|
|
|
|
|
best_base = other_idx; |
1023
|
|
|
|
|
|
|
} |
1024
|
|
|
|
|
|
|
|
1025
|
|
|
|
|
|
|
/* |
1026
|
|
|
|
|
|
|
* If we decided to cache the delta data, then it is best |
1027
|
|
|
|
|
|
|
* to compress it right away. First because we have to do |
1028
|
|
|
|
|
|
|
* it anyway, and doing it here while we're threaded will |
1029
|
|
|
|
|
|
|
* save a lot of time in the non threaded write phase, |
1030
|
|
|
|
|
|
|
* as well as allow for caching more deltas within |
1031
|
|
|
|
|
|
|
* the same cache size limit. |
1032
|
|
|
|
|
|
|
* ... |
1033
|
|
|
|
|
|
|
* But only if not writing to stdout, since in that case |
1034
|
|
|
|
|
|
|
* the network is most likely throttling writes anyway, |
1035
|
|
|
|
|
|
|
* and therefore it is best to go to the write phase ASAP |
1036
|
|
|
|
|
|
|
* instead, as we can afford spending more time compressing |
1037
|
|
|
|
|
|
|
* between writes at that moment. |
1038
|
|
|
|
|
|
|
*/ |
1039
|
24
|
100
|
|
|
|
|
if (po->delta_data) { |
1040
|
3
|
50
|
|
|
|
|
if (git_zstream_deflatebuf(&zbuf, po->delta_data, po->delta_size) < 0) |
1041
|
0
|
|
|
|
|
|
goto on_error; |
1042
|
|
|
|
|
|
|
|
1043
|
3
|
|
|
|
|
|
git__free(po->delta_data); |
1044
|
3
|
|
|
|
|
|
po->delta_data = git__malloc(zbuf.size); |
1045
|
3
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(po->delta_data); |
1046
|
|
|
|
|
|
|
|
1047
|
3
|
|
|
|
|
|
memcpy(po->delta_data, zbuf.ptr, zbuf.size); |
1048
|
3
|
|
|
|
|
|
po->z_delta_size = zbuf.size; |
1049
|
3
|
|
|
|
|
|
git_buf_clear(&zbuf); |
1050
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
git_packbuilder__cache_lock(pb); |
1052
|
3
|
|
|
|
|
|
pb->delta_cache_size -= po->delta_size; |
1053
|
3
|
|
|
|
|
|
pb->delta_cache_size += po->z_delta_size; |
1054
|
|
|
|
|
|
|
git_packbuilder__cache_unlock(pb); |
1055
|
|
|
|
|
|
|
} |
1056
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
/* |
1058
|
|
|
|
|
|
|
* If we made n a delta, and if n is already at max |
1059
|
|
|
|
|
|
|
* depth, leaving it in the window is pointless. we |
1060
|
|
|
|
|
|
|
* should evict it first. |
1061
|
|
|
|
|
|
|
*/ |
1062
|
24
|
100
|
|
|
|
|
if (po->delta && max_depth <= n->depth) |
|
|
50
|
|
|
|
|
|
1063
|
0
|
|
|
|
|
|
continue; |
1064
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
/* |
1066
|
|
|
|
|
|
|
* Move the best delta base up in the window, after the |
1067
|
|
|
|
|
|
|
* currently deltified object, to keep it longer. It will |
1068
|
|
|
|
|
|
|
* be the first base object to be attempted next. |
1069
|
|
|
|
|
|
|
*/ |
1070
|
24
|
100
|
|
|
|
|
if (po->delta) { |
1071
|
3
|
|
|
|
|
|
struct unpacked swap = array[best_base]; |
1072
|
3
|
|
|
|
|
|
size_t dist = (window + idx - best_base) % window; |
1073
|
3
|
|
|
|
|
|
size_t dst = best_base; |
1074
|
8
|
100
|
|
|
|
|
while (dist--) { |
1075
|
5
|
|
|
|
|
|
size_t src = (dst + 1) % window; |
1076
|
5
|
|
|
|
|
|
array[dst] = array[src]; |
1077
|
5
|
|
|
|
|
|
dst = src; |
1078
|
|
|
|
|
|
|
} |
1079
|
3
|
|
|
|
|
|
array[dst] = swap; |
1080
|
|
|
|
|
|
|
} |
1081
|
|
|
|
|
|
|
|
1082
|
|
|
|
|
|
|
next: |
1083
|
24
|
|
|
|
|
|
idx++; |
1084
|
24
|
50
|
|
|
|
|
if (count + 1 < window) |
1085
|
24
|
|
|
|
|
|
count++; |
1086
|
24
|
50
|
|
|
|
|
if (idx >= window) |
1087
|
0
|
|
|
|
|
|
idx = 0; |
1088
|
24
|
|
|
|
|
|
} |
1089
|
6
|
|
|
|
|
|
error = 0; |
1090
|
|
|
|
|
|
|
|
1091
|
|
|
|
|
|
|
on_error: |
1092
|
72
|
100
|
|
|
|
|
for (i = 0; i < window; ++i) { |
1093
|
66
|
|
|
|
|
|
git__free(array[i].index); |
1094
|
66
|
|
|
|
|
|
git__free(array[i].data); |
1095
|
|
|
|
|
|
|
} |
1096
|
6
|
|
|
|
|
|
git__free(array); |
1097
|
6
|
|
|
|
|
|
git_buf_dispose(&zbuf); |
1098
|
|
|
|
|
|
|
|
1099
|
6
|
|
|
|
|
|
return error; |
1100
|
|
|
|
|
|
|
} |
1101
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
#ifdef GIT_THREADS |
1103
|
|
|
|
|
|
|
|
1104
|
|
|
|
|
|
|
struct thread_params { |
1105
|
|
|
|
|
|
|
git_thread thread; |
1106
|
|
|
|
|
|
|
git_packbuilder *pb; |
1107
|
|
|
|
|
|
|
|
1108
|
|
|
|
|
|
|
git_pobject **list; |
1109
|
|
|
|
|
|
|
|
1110
|
|
|
|
|
|
|
git_cond cond; |
1111
|
|
|
|
|
|
|
git_mutex mutex; |
1112
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
size_t list_size; |
1114
|
|
|
|
|
|
|
size_t remaining; |
1115
|
|
|
|
|
|
|
|
1116
|
|
|
|
|
|
|
size_t window; |
1117
|
|
|
|
|
|
|
size_t depth; |
1118
|
|
|
|
|
|
|
size_t working; |
1119
|
|
|
|
|
|
|
size_t data_ready; |
1120
|
|
|
|
|
|
|
}; |
1121
|
|
|
|
|
|
|
|
1122
|
|
|
|
|
|
|
static void *threaded_find_deltas(void *arg) |
1123
|
|
|
|
|
|
|
{ |
1124
|
|
|
|
|
|
|
struct thread_params *me = arg; |
1125
|
|
|
|
|
|
|
|
1126
|
|
|
|
|
|
|
while (me->remaining) { |
1127
|
|
|
|
|
|
|
if (find_deltas(me->pb, me->list, &me->remaining, |
1128
|
|
|
|
|
|
|
me->window, me->depth) < 0) { |
1129
|
|
|
|
|
|
|
; /* TODO */ |
1130
|
|
|
|
|
|
|
} |
1131
|
|
|
|
|
|
|
|
1132
|
|
|
|
|
|
|
git_packbuilder__progress_lock(me->pb); |
1133
|
|
|
|
|
|
|
me->working = 0; |
1134
|
|
|
|
|
|
|
git_cond_signal(&me->pb->progress_cond); |
1135
|
|
|
|
|
|
|
git_packbuilder__progress_unlock(me->pb); |
1136
|
|
|
|
|
|
|
|
1137
|
|
|
|
|
|
|
if (git_mutex_lock(&me->mutex)) { |
1138
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_THREAD, "unable to lock packfile condition mutex"); |
1139
|
|
|
|
|
|
|
return NULL; |
1140
|
|
|
|
|
|
|
} |
1141
|
|
|
|
|
|
|
|
1142
|
|
|
|
|
|
|
while (!me->data_ready) |
1143
|
|
|
|
|
|
|
git_cond_wait(&me->cond, &me->mutex); |
1144
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
/* |
1146
|
|
|
|
|
|
|
* We must not set ->data_ready before we wait on the |
1147
|
|
|
|
|
|
|
* condition because the main thread may have set it to 1 |
1148
|
|
|
|
|
|
|
* before we get here. In order to be sure that new |
1149
|
|
|
|
|
|
|
* work is available if we see 1 in ->data_ready, it |
1150
|
|
|
|
|
|
|
* was initialized to 0 before this thread was spawned |
1151
|
|
|
|
|
|
|
* and we reset it to 0 right away. |
1152
|
|
|
|
|
|
|
*/ |
1153
|
|
|
|
|
|
|
me->data_ready = 0; |
1154
|
|
|
|
|
|
|
git_mutex_unlock(&me->mutex); |
1155
|
|
|
|
|
|
|
} |
1156
|
|
|
|
|
|
|
/* leave ->working 1 so that this doesn't get more work assigned */ |
1157
|
|
|
|
|
|
|
return NULL; |
1158
|
|
|
|
|
|
|
} |
1159
|
|
|
|
|
|
|
|
1160
|
|
|
|
|
|
|
static int ll_find_deltas(git_packbuilder *pb, git_pobject **list, |
1161
|
|
|
|
|
|
|
size_t list_size, size_t window, size_t depth) |
1162
|
|
|
|
|
|
|
{ |
1163
|
|
|
|
|
|
|
struct thread_params *p; |
1164
|
|
|
|
|
|
|
size_t i; |
1165
|
|
|
|
|
|
|
int ret, active_threads = 0; |
1166
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
if (!pb->nr_threads) |
1168
|
|
|
|
|
|
|
pb->nr_threads = git_online_cpus(); |
1169
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
if (pb->nr_threads <= 1) { |
1171
|
|
|
|
|
|
|
find_deltas(pb, list, &list_size, window, depth); |
1172
|
|
|
|
|
|
|
return 0; |
1173
|
|
|
|
|
|
|
} |
1174
|
|
|
|
|
|
|
|
1175
|
|
|
|
|
|
|
p = git__mallocarray(pb->nr_threads, sizeof(*p)); |
1176
|
|
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(p); |
1177
|
|
|
|
|
|
|
|
1178
|
|
|
|
|
|
|
/* Partition the work among the threads */ |
1179
|
|
|
|
|
|
|
for (i = 0; i < pb->nr_threads; ++i) { |
1180
|
|
|
|
|
|
|
size_t sub_size = list_size / (pb->nr_threads - i); |
1181
|
|
|
|
|
|
|
|
1182
|
|
|
|
|
|
|
/* don't use too small segments or no deltas will be found */ |
1183
|
|
|
|
|
|
|
if (sub_size < 2*window && i+1 < pb->nr_threads) |
1184
|
|
|
|
|
|
|
sub_size = 0; |
1185
|
|
|
|
|
|
|
|
1186
|
|
|
|
|
|
|
p[i].pb = pb; |
1187
|
|
|
|
|
|
|
p[i].window = window; |
1188
|
|
|
|
|
|
|
p[i].depth = depth; |
1189
|
|
|
|
|
|
|
p[i].working = 1; |
1190
|
|
|
|
|
|
|
p[i].data_ready = 0; |
1191
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
/* try to split chunks on "path" boundaries */ |
1193
|
|
|
|
|
|
|
while (sub_size && sub_size < list_size && |
1194
|
|
|
|
|
|
|
list[sub_size]->hash && |
1195
|
|
|
|
|
|
|
list[sub_size]->hash == list[sub_size-1]->hash) |
1196
|
|
|
|
|
|
|
sub_size++; |
1197
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
p[i].list = list; |
1199
|
|
|
|
|
|
|
p[i].list_size = sub_size; |
1200
|
|
|
|
|
|
|
p[i].remaining = sub_size; |
1201
|
|
|
|
|
|
|
|
1202
|
|
|
|
|
|
|
list += sub_size; |
1203
|
|
|
|
|
|
|
list_size -= sub_size; |
1204
|
|
|
|
|
|
|
} |
1205
|
|
|
|
|
|
|
|
1206
|
|
|
|
|
|
|
/* Start work threads */ |
1207
|
|
|
|
|
|
|
for (i = 0; i < pb->nr_threads; ++i) { |
1208
|
|
|
|
|
|
|
if (!p[i].list_size) |
1209
|
|
|
|
|
|
|
continue; |
1210
|
|
|
|
|
|
|
|
1211
|
|
|
|
|
|
|
git_mutex_init(&p[i].mutex); |
1212
|
|
|
|
|
|
|
git_cond_init(&p[i].cond); |
1213
|
|
|
|
|
|
|
|
1214
|
|
|
|
|
|
|
ret = git_thread_create(&p[i].thread, |
1215
|
|
|
|
|
|
|
threaded_find_deltas, &p[i]); |
1216
|
|
|
|
|
|
|
if (ret) { |
1217
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_THREAD, "unable to create thread"); |
1218
|
|
|
|
|
|
|
return -1; |
1219
|
|
|
|
|
|
|
} |
1220
|
|
|
|
|
|
|
active_threads++; |
1221
|
|
|
|
|
|
|
} |
1222
|
|
|
|
|
|
|
|
1223
|
|
|
|
|
|
|
/* |
1224
|
|
|
|
|
|
|
* Now let's wait for work completion. Each time a thread is done |
1225
|
|
|
|
|
|
|
* with its work, we steal half of the remaining work from the |
1226
|
|
|
|
|
|
|
* thread with the largest number of unprocessed objects and give |
1227
|
|
|
|
|
|
|
* it to that newly idle thread. This ensure good load balancing |
1228
|
|
|
|
|
|
|
* until the remaining object list segments are simply too short |
1229
|
|
|
|
|
|
|
* to be worth splitting anymore. |
1230
|
|
|
|
|
|
|
*/ |
1231
|
|
|
|
|
|
|
while (active_threads) { |
1232
|
|
|
|
|
|
|
struct thread_params *target = NULL; |
1233
|
|
|
|
|
|
|
struct thread_params *victim = NULL; |
1234
|
|
|
|
|
|
|
size_t sub_size = 0; |
1235
|
|
|
|
|
|
|
|
1236
|
|
|
|
|
|
|
/* Start by locating a thread that has transitioned its |
1237
|
|
|
|
|
|
|
* 'working' flag from 1 -> 0. This indicates that it is |
1238
|
|
|
|
|
|
|
* ready to receive more work using our work-stealing |
1239
|
|
|
|
|
|
|
* algorithm. */ |
1240
|
|
|
|
|
|
|
git_packbuilder__progress_lock(pb); |
1241
|
|
|
|
|
|
|
for (;;) { |
1242
|
|
|
|
|
|
|
for (i = 0; !target && i < pb->nr_threads; i++) |
1243
|
|
|
|
|
|
|
if (!p[i].working) |
1244
|
|
|
|
|
|
|
target = &p[i]; |
1245
|
|
|
|
|
|
|
if (target) |
1246
|
|
|
|
|
|
|
break; |
1247
|
|
|
|
|
|
|
git_cond_wait(&pb->progress_cond, &pb->progress_mutex); |
1248
|
|
|
|
|
|
|
} |
1249
|
|
|
|
|
|
|
|
1250
|
|
|
|
|
|
|
/* At this point we hold the progress lock and have located |
1251
|
|
|
|
|
|
|
* a thread to receive more work. We still need to locate a |
1252
|
|
|
|
|
|
|
* thread from which to steal work (the victim). */ |
1253
|
|
|
|
|
|
|
for (i = 0; i < pb->nr_threads; i++) |
1254
|
|
|
|
|
|
|
if (p[i].remaining > 2*window && |
1255
|
|
|
|
|
|
|
(!victim || victim->remaining < p[i].remaining)) |
1256
|
|
|
|
|
|
|
victim = &p[i]; |
1257
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
if (victim) { |
1259
|
|
|
|
|
|
|
sub_size = victim->remaining / 2; |
1260
|
|
|
|
|
|
|
list = victim->list + victim->list_size - sub_size; |
1261
|
|
|
|
|
|
|
while (sub_size && list[0]->hash && |
1262
|
|
|
|
|
|
|
list[0]->hash == list[-1]->hash) { |
1263
|
|
|
|
|
|
|
list++; |
1264
|
|
|
|
|
|
|
sub_size--; |
1265
|
|
|
|
|
|
|
} |
1266
|
|
|
|
|
|
|
if (!sub_size) { |
1267
|
|
|
|
|
|
|
/* |
1268
|
|
|
|
|
|
|
* It is possible for some "paths" to have |
1269
|
|
|
|
|
|
|
* so many objects that no hash boundary |
1270
|
|
|
|
|
|
|
* might be found. Let's just steal the |
1271
|
|
|
|
|
|
|
* exact half in that case. |
1272
|
|
|
|
|
|
|
*/ |
1273
|
|
|
|
|
|
|
sub_size = victim->remaining / 2; |
1274
|
|
|
|
|
|
|
list -= sub_size; |
1275
|
|
|
|
|
|
|
} |
1276
|
|
|
|
|
|
|
target->list = list; |
1277
|
|
|
|
|
|
|
victim->list_size -= sub_size; |
1278
|
|
|
|
|
|
|
victim->remaining -= sub_size; |
1279
|
|
|
|
|
|
|
} |
1280
|
|
|
|
|
|
|
target->list_size = sub_size; |
1281
|
|
|
|
|
|
|
target->remaining = sub_size; |
1282
|
|
|
|
|
|
|
target->working = 1; |
1283
|
|
|
|
|
|
|
git_packbuilder__progress_unlock(pb); |
1284
|
|
|
|
|
|
|
|
1285
|
|
|
|
|
|
|
if (git_mutex_lock(&target->mutex)) { |
1286
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_THREAD, "unable to lock packfile condition mutex"); |
1287
|
|
|
|
|
|
|
git__free(p); |
1288
|
|
|
|
|
|
|
return -1; |
1289
|
|
|
|
|
|
|
} |
1290
|
|
|
|
|
|
|
|
1291
|
|
|
|
|
|
|
target->data_ready = 1; |
1292
|
|
|
|
|
|
|
git_cond_signal(&target->cond); |
1293
|
|
|
|
|
|
|
git_mutex_unlock(&target->mutex); |
1294
|
|
|
|
|
|
|
|
1295
|
|
|
|
|
|
|
if (!sub_size) { |
1296
|
|
|
|
|
|
|
git_thread_join(&target->thread, NULL); |
1297
|
|
|
|
|
|
|
git_cond_free(&target->cond); |
1298
|
|
|
|
|
|
|
git_mutex_free(&target->mutex); |
1299
|
|
|
|
|
|
|
active_threads--; |
1300
|
|
|
|
|
|
|
} |
1301
|
|
|
|
|
|
|
} |
1302
|
|
|
|
|
|
|
|
1303
|
|
|
|
|
|
|
git__free(p); |
1304
|
|
|
|
|
|
|
return 0; |
1305
|
|
|
|
|
|
|
} |
1306
|
|
|
|
|
|
|
|
1307
|
|
|
|
|
|
|
#else |
1308
|
|
|
|
|
|
|
#define ll_find_deltas(pb, l, ls, w, d) find_deltas(pb, l, &ls, w, d) |
1309
|
|
|
|
|
|
|
#endif |
1310
|
|
|
|
|
|
|
|
1311
|
11
|
|
|
|
|
|
static int prepare_pack(git_packbuilder *pb) |
1312
|
|
|
|
|
|
|
{ |
1313
|
|
|
|
|
|
|
git_pobject **delta_list; |
1314
|
11
|
|
|
|
|
|
size_t i, n = 0; |
1315
|
|
|
|
|
|
|
|
1316
|
11
|
100
|
|
|
|
|
if (pb->nr_objects == 0 || pb->done) |
|
|
100
|
|
|
|
|
|
1317
|
5
|
|
|
|
|
|
return 0; /* nothing to do */ |
1318
|
|
|
|
|
|
|
|
1319
|
|
|
|
|
|
|
/* |
1320
|
|
|
|
|
|
|
* Although we do not report progress during deltafication, we |
1321
|
|
|
|
|
|
|
* at least report that we are in the deltafication stage |
1322
|
|
|
|
|
|
|
*/ |
1323
|
6
|
100
|
|
|
|
|
if (pb->progress_cb) |
1324
|
3
|
|
|
|
|
|
pb->progress_cb(GIT_PACKBUILDER_DELTAFICATION, 0, pb->nr_objects, pb->progress_cb_payload); |
1325
|
|
|
|
|
|
|
|
1326
|
6
|
|
|
|
|
|
delta_list = git__mallocarray(pb->nr_objects, sizeof(*delta_list)); |
1327
|
6
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(delta_list); |
1328
|
|
|
|
|
|
|
|
1329
|
59
|
100
|
|
|
|
|
for (i = 0; i < pb->nr_objects; ++i) { |
1330
|
53
|
|
|
|
|
|
git_pobject *po = pb->object_list + i; |
1331
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
/* Make sure the item is within our size limits */ |
1333
|
53
|
100
|
|
|
|
|
if (po->size < 50 || po->size > pb->big_file_threshold) |
|
|
50
|
|
|
|
|
|
1334
|
29
|
|
|
|
|
|
continue; |
1335
|
|
|
|
|
|
|
|
1336
|
24
|
|
|
|
|
|
delta_list[n++] = po; |
1337
|
|
|
|
|
|
|
} |
1338
|
|
|
|
|
|
|
|
1339
|
6
|
50
|
|
|
|
|
if (n > 1) { |
1340
|
6
|
|
|
|
|
|
git__tsort((void **)delta_list, n, type_size_sort); |
1341
|
6
|
50
|
|
|
|
|
if (ll_find_deltas(pb, delta_list, n, |
1342
|
|
|
|
|
|
|
GIT_PACK_WINDOW + 1, |
1343
|
|
|
|
|
|
|
GIT_PACK_DEPTH) < 0) { |
1344
|
0
|
|
|
|
|
|
git__free(delta_list); |
1345
|
0
|
|
|
|
|
|
return -1; |
1346
|
|
|
|
|
|
|
} |
1347
|
|
|
|
|
|
|
} |
1348
|
|
|
|
|
|
|
|
1349
|
6
|
|
|
|
|
|
report_delta_progress(pb, pb->nr_objects, true); |
1350
|
|
|
|
|
|
|
|
1351
|
6
|
|
|
|
|
|
pb->done = true; |
1352
|
6
|
|
|
|
|
|
git__free(delta_list); |
1353
|
11
|
|
|
|
|
|
return 0; |
1354
|
|
|
|
|
|
|
} |
1355
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
#define PREPARE_PACK if (prepare_pack(pb) < 0) { return -1; } |
1357
|
|
|
|
|
|
|
|
1358
|
4
|
|
|
|
|
|
int git_packbuilder_foreach(git_packbuilder *pb, int (*cb)(void *buf, size_t size, void *payload), void *payload) |
1359
|
|
|
|
|
|
|
{ |
1360
|
4
|
50
|
|
|
|
|
PREPARE_PACK; |
1361
|
4
|
|
|
|
|
|
return write_pack(pb, cb, payload); |
1362
|
|
|
|
|
|
|
} |
1363
|
|
|
|
|
|
|
|
1364
|
3
|
|
|
|
|
|
int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb) |
1365
|
|
|
|
|
|
|
{ |
1366
|
3
|
50
|
|
|
|
|
PREPARE_PACK; |
1367
|
3
|
|
|
|
|
|
git_buf_sanitize(buf); |
1368
|
3
|
|
|
|
|
|
return write_pack(pb, &write_pack_buf, buf); |
1369
|
|
|
|
|
|
|
} |
1370
|
|
|
|
|
|
|
|
1371
|
90
|
|
|
|
|
|
static int write_cb(void *buf, size_t len, void *payload) |
1372
|
|
|
|
|
|
|
{ |
1373
|
90
|
|
|
|
|
|
struct pack_write_context *ctx = payload; |
1374
|
90
|
|
|
|
|
|
return git_indexer_append(ctx->indexer, buf, len, ctx->stats); |
1375
|
|
|
|
|
|
|
} |
1376
|
|
|
|
|
|
|
|
1377
|
4
|
|
|
|
|
|
int git_packbuilder_write( |
1378
|
|
|
|
|
|
|
git_packbuilder *pb, |
1379
|
|
|
|
|
|
|
const char *path, |
1380
|
|
|
|
|
|
|
unsigned int mode, |
1381
|
|
|
|
|
|
|
git_indexer_progress_cb progress_cb, |
1382
|
|
|
|
|
|
|
void *progress_cb_payload) |
1383
|
|
|
|
|
|
|
{ |
1384
|
4
|
|
|
|
|
|
int error = -1; |
1385
|
4
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
1386
|
4
|
|
|
|
|
|
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; |
1387
|
4
|
|
|
|
|
|
git_indexer *indexer = NULL; |
1388
|
|
|
|
|
|
|
git_indexer_progress stats; |
1389
|
|
|
|
|
|
|
struct pack_write_context ctx; |
1390
|
|
|
|
|
|
|
int t; |
1391
|
|
|
|
|
|
|
|
1392
|
4
|
50
|
|
|
|
|
PREPARE_PACK; |
1393
|
|
|
|
|
|
|
|
1394
|
4
|
50
|
|
|
|
|
if (path == NULL) { |
1395
|
0
|
0
|
|
|
|
|
if ((error = git_repository_item_path(&object_path, pb->repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0) |
1396
|
0
|
|
|
|
|
|
goto cleanup; |
1397
|
0
|
0
|
|
|
|
|
if ((error = git_buf_joinpath(&object_path, git_buf_cstr(&object_path), "pack")) < 0) |
1398
|
0
|
|
|
|
|
|
goto cleanup; |
1399
|
0
|
|
|
|
|
|
path = git_buf_cstr(&object_path); |
1400
|
|
|
|
|
|
|
} |
1401
|
|
|
|
|
|
|
|
1402
|
4
|
|
|
|
|
|
opts.progress_cb = progress_cb; |
1403
|
4
|
|
|
|
|
|
opts.progress_cb_payload = progress_cb_payload; |
1404
|
|
|
|
|
|
|
|
1405
|
4
|
50
|
|
|
|
|
if ((error = git_indexer_new(&indexer, path, mode, pb->odb, &opts)) < 0) |
1406
|
0
|
|
|
|
|
|
goto cleanup; |
1407
|
|
|
|
|
|
|
|
1408
|
4
|
50
|
|
|
|
|
if (!git_repository__configmap_lookup(&t, pb->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) |
|
|
50
|
|
|
|
|
|
1409
|
0
|
|
|
|
|
|
git_indexer__set_fsync(indexer, 1); |
1410
|
|
|
|
|
|
|
|
1411
|
4
|
|
|
|
|
|
ctx.indexer = indexer; |
1412
|
4
|
|
|
|
|
|
ctx.stats = &stats; |
1413
|
|
|
|
|
|
|
|
1414
|
4
|
50
|
|
|
|
|
if ((error = git_packbuilder_foreach(pb, write_cb, &ctx)) < 0) |
1415
|
0
|
|
|
|
|
|
goto cleanup; |
1416
|
|
|
|
|
|
|
|
1417
|
4
|
50
|
|
|
|
|
if ((error = git_indexer_commit(indexer, &stats)) < 0) |
1418
|
0
|
|
|
|
|
|
goto cleanup; |
1419
|
|
|
|
|
|
|
|
1420
|
4
|
|
|
|
|
|
git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer)); |
1421
|
|
|
|
|
|
|
|
1422
|
|
|
|
|
|
|
cleanup: |
1423
|
4
|
|
|
|
|
|
git_indexer_free(indexer); |
1424
|
4
|
|
|
|
|
|
git_buf_dispose(&object_path); |
1425
|
4
|
|
|
|
|
|
return error; |
1426
|
|
|
|
|
|
|
} |
1427
|
|
|
|
|
|
|
|
1428
|
|
|
|
|
|
|
#undef PREPARE_PACK |
1429
|
|
|
|
|
|
|
|
1430
|
2
|
|
|
|
|
|
const git_oid *git_packbuilder_hash(git_packbuilder *pb) |
1431
|
|
|
|
|
|
|
{ |
1432
|
2
|
|
|
|
|
|
return &pb->pack_oid; |
1433
|
|
|
|
|
|
|
} |
1434
|
|
|
|
|
|
|
|
1435
|
|
|
|
|
|
|
|
1436
|
11
|
|
|
|
|
|
static int cb_tree_walk( |
1437
|
|
|
|
|
|
|
const char *root, const git_tree_entry *entry, void *payload) |
1438
|
|
|
|
|
|
|
{ |
1439
|
|
|
|
|
|
|
int error; |
1440
|
11
|
|
|
|
|
|
struct tree_walk_context *ctx = payload; |
1441
|
|
|
|
|
|
|
|
1442
|
|
|
|
|
|
|
/* A commit inside a tree represents a submodule commit and should be skipped. */ |
1443
|
11
|
50
|
|
|
|
|
if (git_tree_entry_type(entry) == GIT_OBJECT_COMMIT) |
1444
|
0
|
|
|
|
|
|
return 0; |
1445
|
|
|
|
|
|
|
|
1446
|
22
|
50
|
|
|
|
|
if (!(error = git_buf_sets(&ctx->buf, root)) && |
|
|
50
|
|
|
|
|
|
1447
|
11
|
|
|
|
|
|
!(error = git_buf_puts(&ctx->buf, git_tree_entry_name(entry)))) |
1448
|
11
|
|
|
|
|
|
error = git_packbuilder_insert( |
1449
|
11
|
|
|
|
|
|
ctx->pb, git_tree_entry_id(entry), git_buf_cstr(&ctx->buf)); |
1450
|
|
|
|
|
|
|
|
1451
|
11
|
|
|
|
|
|
return error; |
1452
|
|
|
|
|
|
|
} |
1453
|
|
|
|
|
|
|
|
1454
|
4
|
|
|
|
|
|
int git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *oid) |
1455
|
|
|
|
|
|
|
{ |
1456
|
|
|
|
|
|
|
git_commit *commit; |
1457
|
|
|
|
|
|
|
|
1458
|
8
|
|
|
|
|
|
if (git_commit_lookup(&commit, pb->repo, oid) < 0 || |
1459
|
4
|
|
|
|
|
|
git_packbuilder_insert(pb, oid, NULL) < 0) |
1460
|
0
|
|
|
|
|
|
return -1; |
1461
|
|
|
|
|
|
|
|
1462
|
4
|
50
|
|
|
|
|
if (git_packbuilder_insert_tree(pb, git_commit_tree_id(commit)) < 0) |
1463
|
0
|
|
|
|
|
|
return -1; |
1464
|
|
|
|
|
|
|
|
1465
|
4
|
|
|
|
|
|
git_commit_free(commit); |
1466
|
4
|
|
|
|
|
|
return 0; |
1467
|
|
|
|
|
|
|
} |
1468
|
|
|
|
|
|
|
|
1469
|
4
|
|
|
|
|
|
int git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *oid) |
1470
|
|
|
|
|
|
|
{ |
1471
|
|
|
|
|
|
|
int error; |
1472
|
4
|
|
|
|
|
|
git_tree *tree = NULL; |
1473
|
4
|
|
|
|
|
|
struct tree_walk_context context = { pb, GIT_BUF_INIT }; |
1474
|
|
|
|
|
|
|
|
1475
|
4
|
50
|
|
|
|
|
if (!(error = git_tree_lookup(&tree, pb->repo, oid)) && |
|
|
50
|
|
|
|
|
|
1476
|
|
|
|
|
|
|
!(error = git_packbuilder_insert(pb, oid, NULL))) |
1477
|
4
|
|
|
|
|
|
error = git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context); |
1478
|
|
|
|
|
|
|
|
1479
|
4
|
|
|
|
|
|
git_tree_free(tree); |
1480
|
4
|
|
|
|
|
|
git_buf_dispose(&context.buf); |
1481
|
4
|
|
|
|
|
|
return error; |
1482
|
|
|
|
|
|
|
} |
1483
|
|
|
|
|
|
|
|
1484
|
1
|
|
|
|
|
|
int git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name) |
1485
|
|
|
|
|
|
|
{ |
1486
|
|
|
|
|
|
|
git_object *obj; |
1487
|
|
|
|
|
|
|
int error; |
1488
|
|
|
|
|
|
|
|
1489
|
1
|
50
|
|
|
|
|
assert(pb && id); |
|
|
50
|
|
|
|
|
|
1490
|
|
|
|
|
|
|
|
1491
|
1
|
50
|
|
|
|
|
if ((error = git_object_lookup(&obj, pb->repo, id, GIT_OBJECT_ANY)) < 0) |
1492
|
0
|
|
|
|
|
|
return error; |
1493
|
|
|
|
|
|
|
|
1494
|
1
|
|
|
|
|
|
switch (git_object_type(obj)) { |
1495
|
|
|
|
|
|
|
case GIT_OBJECT_BLOB: |
1496
|
0
|
|
|
|
|
|
error = git_packbuilder_insert(pb, id, name); |
1497
|
0
|
|
|
|
|
|
break; |
1498
|
|
|
|
|
|
|
case GIT_OBJECT_TREE: |
1499
|
0
|
|
|
|
|
|
error = git_packbuilder_insert_tree(pb, id); |
1500
|
0
|
|
|
|
|
|
break; |
1501
|
|
|
|
|
|
|
case GIT_OBJECT_COMMIT: |
1502
|
1
|
|
|
|
|
|
error = git_packbuilder_insert_commit(pb, id); |
1503
|
1
|
|
|
|
|
|
break; |
1504
|
|
|
|
|
|
|
case GIT_OBJECT_TAG: |
1505
|
0
|
0
|
|
|
|
|
if ((error = git_packbuilder_insert(pb, id, name)) < 0) |
1506
|
0
|
|
|
|
|
|
goto cleanup; |
1507
|
0
|
|
|
|
|
|
error = git_packbuilder_insert_recur(pb, git_tag_target_id((git_tag *) obj), NULL); |
1508
|
0
|
|
|
|
|
|
break; |
1509
|
|
|
|
|
|
|
|
1510
|
|
|
|
|
|
|
default: |
1511
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "unknown object type"); |
1512
|
0
|
|
|
|
|
|
error = -1; |
1513
|
|
|
|
|
|
|
} |
1514
|
|
|
|
|
|
|
|
1515
|
|
|
|
|
|
|
cleanup: |
1516
|
1
|
|
|
|
|
|
git_object_free(obj); |
1517
|
1
|
|
|
|
|
|
return error; |
1518
|
|
|
|
|
|
|
} |
1519
|
|
|
|
|
|
|
|
1520
|
5
|
|
|
|
|
|
size_t git_packbuilder_object_count(git_packbuilder *pb) |
1521
|
|
|
|
|
|
|
{ |
1522
|
5
|
|
|
|
|
|
return pb->nr_objects; |
1523
|
|
|
|
|
|
|
} |
1524
|
|
|
|
|
|
|
|
1525
|
4
|
|
|
|
|
|
size_t git_packbuilder_written(git_packbuilder *pb) |
1526
|
|
|
|
|
|
|
{ |
1527
|
4
|
|
|
|
|
|
return pb->nr_written; |
1528
|
|
|
|
|
|
|
} |
1529
|
|
|
|
|
|
|
|
1530
|
36
|
|
|
|
|
|
static int lookup_walk_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id) |
1531
|
|
|
|
|
|
|
{ |
1532
|
|
|
|
|
|
|
struct walk_object *obj; |
1533
|
|
|
|
|
|
|
|
1534
|
36
|
|
|
|
|
|
obj = git_pool_mallocz(&pb->object_pool, 1); |
1535
|
36
|
50
|
|
|
|
|
if (!obj) { |
1536
|
0
|
|
|
|
|
|
git_error_set_oom(); |
1537
|
0
|
|
|
|
|
|
return -1; |
1538
|
|
|
|
|
|
|
} |
1539
|
|
|
|
|
|
|
|
1540
|
36
|
|
|
|
|
|
git_oid_cpy(&obj->id, id); |
1541
|
|
|
|
|
|
|
|
1542
|
36
|
|
|
|
|
|
*out = obj; |
1543
|
36
|
|
|
|
|
|
return 0; |
1544
|
|
|
|
|
|
|
} |
1545
|
|
|
|
|
|
|
|
1546
|
44
|
|
|
|
|
|
static int retrieve_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id) |
1547
|
|
|
|
|
|
|
{ |
1548
|
|
|
|
|
|
|
struct walk_object *obj; |
1549
|
|
|
|
|
|
|
int error; |
1550
|
|
|
|
|
|
|
|
1551
|
44
|
100
|
|
|
|
|
if ((obj = git_oidmap_get(pb->walk_objects, id)) == NULL) { |
1552
|
36
|
50
|
|
|
|
|
if ((error = lookup_walk_object(&obj, pb, id)) < 0) |
1553
|
0
|
|
|
|
|
|
return error; |
1554
|
|
|
|
|
|
|
|
1555
|
36
|
50
|
|
|
|
|
if ((error = git_oidmap_set(pb->walk_objects, &obj->id, obj)) < 0) |
1556
|
0
|
|
|
|
|
|
return error; |
1557
|
|
|
|
|
|
|
} |
1558
|
|
|
|
|
|
|
|
1559
|
44
|
|
|
|
|
|
*out = obj; |
1560
|
44
|
|
|
|
|
|
return 0; |
1561
|
|
|
|
|
|
|
} |
1562
|
|
|
|
|
|
|
|
1563
|
0
|
|
|
|
|
|
static int mark_blob_uninteresting(git_packbuilder *pb, const git_oid *id) |
1564
|
|
|
|
|
|
|
{ |
1565
|
|
|
|
|
|
|
int error; |
1566
|
|
|
|
|
|
|
struct walk_object *obj; |
1567
|
|
|
|
|
|
|
|
1568
|
0
|
0
|
|
|
|
|
if ((error = retrieve_object(&obj, pb, id)) < 0) |
1569
|
0
|
|
|
|
|
|
return error; |
1570
|
|
|
|
|
|
|
|
1571
|
0
|
|
|
|
|
|
obj->uninteresting = 1; |
1572
|
|
|
|
|
|
|
|
1573
|
0
|
|
|
|
|
|
return 0; |
1574
|
|
|
|
|
|
|
} |
1575
|
|
|
|
|
|
|
|
1576
|
0
|
|
|
|
|
|
static int mark_tree_uninteresting(git_packbuilder *pb, const git_oid *id) |
1577
|
|
|
|
|
|
|
{ |
1578
|
|
|
|
|
|
|
struct walk_object *obj; |
1579
|
|
|
|
|
|
|
git_tree *tree; |
1580
|
|
|
|
|
|
|
int error; |
1581
|
|
|
|
|
|
|
size_t i; |
1582
|
|
|
|
|
|
|
|
1583
|
0
|
0
|
|
|
|
|
if ((error = retrieve_object(&obj, pb, id)) < 0) |
1584
|
0
|
|
|
|
|
|
return error; |
1585
|
|
|
|
|
|
|
|
1586
|
0
|
0
|
|
|
|
|
if (obj->uninteresting) |
1587
|
0
|
|
|
|
|
|
return 0; |
1588
|
|
|
|
|
|
|
|
1589
|
0
|
|
|
|
|
|
obj->uninteresting = 1; |
1590
|
|
|
|
|
|
|
|
1591
|
0
|
0
|
|
|
|
|
if ((error = git_tree_lookup(&tree, pb->repo, id)) < 0) |
1592
|
0
|
|
|
|
|
|
return error; |
1593
|
|
|
|
|
|
|
|
1594
|
0
|
0
|
|
|
|
|
for (i = 0; i < git_tree_entrycount(tree); i++) { |
1595
|
0
|
|
|
|
|
|
const git_tree_entry *entry = git_tree_entry_byindex(tree, i); |
1596
|
0
|
|
|
|
|
|
const git_oid *entry_id = git_tree_entry_id(entry); |
1597
|
0
|
|
|
|
|
|
switch (git_tree_entry_type(entry)) { |
1598
|
|
|
|
|
|
|
case GIT_OBJECT_TREE: |
1599
|
0
|
0
|
|
|
|
|
if ((error = mark_tree_uninteresting(pb, entry_id)) < 0) |
1600
|
0
|
|
|
|
|
|
goto cleanup; |
1601
|
0
|
|
|
|
|
|
break; |
1602
|
|
|
|
|
|
|
case GIT_OBJECT_BLOB: |
1603
|
0
|
0
|
|
|
|
|
if ((error = mark_blob_uninteresting(pb, entry_id)) < 0) |
1604
|
0
|
|
|
|
|
|
goto cleanup; |
1605
|
0
|
|
|
|
|
|
break; |
1606
|
|
|
|
|
|
|
default: |
1607
|
|
|
|
|
|
|
/* it's a submodule or something unknown, we don't want it */ |
1608
|
|
|
|
|
|
|
; |
1609
|
|
|
|
|
|
|
} |
1610
|
|
|
|
|
|
|
} |
1611
|
|
|
|
|
|
|
|
1612
|
|
|
|
|
|
|
cleanup: |
1613
|
0
|
|
|
|
|
|
git_tree_free(tree); |
1614
|
0
|
|
|
|
|
|
return error; |
1615
|
|
|
|
|
|
|
} |
1616
|
|
|
|
|
|
|
|
1617
|
|
|
|
|
|
|
/* |
1618
|
|
|
|
|
|
|
* Mark the edges of the graph uninteresting. Since we start from a |
1619
|
|
|
|
|
|
|
* git_revwalk, the commits are already uninteresting, but we need to |
1620
|
|
|
|
|
|
|
* mark the trees and blobs. |
1621
|
|
|
|
|
|
|
*/ |
1622
|
3
|
|
|
|
|
|
static int mark_edges_uninteresting(git_packbuilder *pb, git_commit_list *commits) |
1623
|
|
|
|
|
|
|
{ |
1624
|
|
|
|
|
|
|
int error; |
1625
|
|
|
|
|
|
|
git_commit_list *list; |
1626
|
|
|
|
|
|
|
git_commit *commit; |
1627
|
|
|
|
|
|
|
|
1628
|
6
|
100
|
|
|
|
|
for (list = commits; list; list = list->next) { |
1629
|
3
|
50
|
|
|
|
|
if (!list->item->uninteresting) |
1630
|
3
|
|
|
|
|
|
continue; |
1631
|
|
|
|
|
|
|
|
1632
|
0
|
0
|
|
|
|
|
if ((error = git_commit_lookup(&commit, pb->repo, &list->item->oid)) < 0) |
1633
|
0
|
|
|
|
|
|
return error; |
1634
|
|
|
|
|
|
|
|
1635
|
0
|
|
|
|
|
|
error = mark_tree_uninteresting(pb, git_commit_tree_id(commit)); |
1636
|
0
|
|
|
|
|
|
git_commit_free(commit); |
1637
|
|
|
|
|
|
|
|
1638
|
0
|
0
|
|
|
|
|
if (error < 0) |
1639
|
0
|
|
|
|
|
|
return error; |
1640
|
|
|
|
|
|
|
} |
1641
|
|
|
|
|
|
|
|
1642
|
3
|
|
|
|
|
|
return 0; |
1643
|
|
|
|
|
|
|
} |
1644
|
|
|
|
|
|
|
|
1645
|
17
|
|
|
|
|
|
static int pack_objects_insert_tree(git_packbuilder *pb, git_tree *tree) |
1646
|
|
|
|
|
|
|
{ |
1647
|
|
|
|
|
|
|
size_t i; |
1648
|
|
|
|
|
|
|
int error; |
1649
|
|
|
|
|
|
|
git_tree *subtree; |
1650
|
|
|
|
|
|
|
struct walk_object *obj; |
1651
|
|
|
|
|
|
|
const char *name; |
1652
|
|
|
|
|
|
|
|
1653
|
17
|
50
|
|
|
|
|
if ((error = retrieve_object(&obj, pb, git_tree_id(tree))) < 0) |
1654
|
0
|
|
|
|
|
|
return error; |
1655
|
|
|
|
|
|
|
|
1656
|
17
|
50
|
|
|
|
|
if (obj->seen || obj->uninteresting) |
|
|
50
|
|
|
|
|
|
1657
|
0
|
|
|
|
|
|
return 0; |
1658
|
|
|
|
|
|
|
|
1659
|
17
|
|
|
|
|
|
obj->seen = 1; |
1660
|
|
|
|
|
|
|
|
1661
|
17
|
50
|
|
|
|
|
if ((error = git_packbuilder_insert(pb, &obj->id, NULL))) |
1662
|
0
|
|
|
|
|
|
return error; |
1663
|
|
|
|
|
|
|
|
1664
|
43
|
100
|
|
|
|
|
for (i = 0; i < git_tree_entrycount(tree); i++) { |
1665
|
26
|
|
|
|
|
|
const git_tree_entry *entry = git_tree_entry_byindex(tree, i); |
1666
|
26
|
|
|
|
|
|
const git_oid *entry_id = git_tree_entry_id(entry); |
1667
|
26
|
|
|
|
|
|
switch (git_tree_entry_type(entry)) { |
1668
|
|
|
|
|
|
|
case GIT_OBJECT_TREE: |
1669
|
8
|
50
|
|
|
|
|
if ((error = git_tree_lookup(&subtree, pb->repo, entry_id)) < 0) |
1670
|
0
|
|
|
|
|
|
return error; |
1671
|
|
|
|
|
|
|
|
1672
|
8
|
|
|
|
|
|
error = pack_objects_insert_tree(pb, subtree); |
1673
|
8
|
|
|
|
|
|
git_tree_free(subtree); |
1674
|
|
|
|
|
|
|
|
1675
|
8
|
50
|
|
|
|
|
if (error < 0) |
1676
|
0
|
|
|
|
|
|
return error; |
1677
|
|
|
|
|
|
|
|
1678
|
8
|
|
|
|
|
|
break; |
1679
|
|
|
|
|
|
|
case GIT_OBJECT_BLOB: |
1680
|
18
|
50
|
|
|
|
|
if ((error = retrieve_object(&obj, pb, entry_id)) < 0) |
1681
|
0
|
|
|
|
|
|
return error; |
1682
|
18
|
50
|
|
|
|
|
if (obj->uninteresting) |
1683
|
0
|
|
|
|
|
|
continue; |
1684
|
18
|
|
|
|
|
|
name = git_tree_entry_name(entry); |
1685
|
18
|
50
|
|
|
|
|
if ((error = git_packbuilder_insert(pb, entry_id, name)) < 0) |
1686
|
0
|
|
|
|
|
|
return error; |
1687
|
18
|
|
|
|
|
|
break; |
1688
|
|
|
|
|
|
|
default: |
1689
|
|
|
|
|
|
|
/* it's a submodule or something unknown, we don't want it */ |
1690
|
|
|
|
|
|
|
; |
1691
|
|
|
|
|
|
|
} |
1692
|
|
|
|
|
|
|
} |
1693
|
|
|
|
|
|
|
|
1694
|
|
|
|
|
|
|
|
1695
|
17
|
|
|
|
|
|
return error; |
1696
|
|
|
|
|
|
|
} |
1697
|
|
|
|
|
|
|
|
1698
|
9
|
|
|
|
|
|
static int pack_objects_insert_commit(git_packbuilder *pb, struct walk_object *obj) |
1699
|
|
|
|
|
|
|
{ |
1700
|
|
|
|
|
|
|
int error; |
1701
|
9
|
|
|
|
|
|
git_commit *commit = NULL; |
1702
|
9
|
|
|
|
|
|
git_tree *tree = NULL; |
1703
|
|
|
|
|
|
|
|
1704
|
9
|
|
|
|
|
|
obj->seen = 1; |
1705
|
|
|
|
|
|
|
|
1706
|
9
|
50
|
|
|
|
|
if ((error = git_packbuilder_insert(pb, &obj->id, NULL)) < 0) |
1707
|
0
|
|
|
|
|
|
return error; |
1708
|
|
|
|
|
|
|
|
1709
|
9
|
50
|
|
|
|
|
if ((error = git_commit_lookup(&commit, pb->repo, &obj->id)) < 0) |
1710
|
0
|
|
|
|
|
|
return error; |
1711
|
|
|
|
|
|
|
|
1712
|
9
|
50
|
|
|
|
|
if ((error = git_tree_lookup(&tree, pb->repo, git_commit_tree_id(commit))) < 0) |
1713
|
0
|
|
|
|
|
|
goto cleanup; |
1714
|
|
|
|
|
|
|
|
1715
|
9
|
50
|
|
|
|
|
if ((error = pack_objects_insert_tree(pb, tree)) < 0) |
1716
|
0
|
|
|
|
|
|
goto cleanup; |
1717
|
|
|
|
|
|
|
|
1718
|
|
|
|
|
|
|
cleanup: |
1719
|
9
|
|
|
|
|
|
git_commit_free(commit); |
1720
|
9
|
|
|
|
|
|
git_tree_free(tree); |
1721
|
9
|
|
|
|
|
|
return error; |
1722
|
|
|
|
|
|
|
} |
1723
|
|
|
|
|
|
|
|
1724
|
3
|
|
|
|
|
|
int git_packbuilder_insert_walk(git_packbuilder *pb, git_revwalk *walk) |
1725
|
|
|
|
|
|
|
{ |
1726
|
|
|
|
|
|
|
int error; |
1727
|
|
|
|
|
|
|
git_oid id; |
1728
|
|
|
|
|
|
|
struct walk_object *obj; |
1729
|
|
|
|
|
|
|
|
1730
|
3
|
50
|
|
|
|
|
assert(pb && walk); |
|
|
50
|
|
|
|
|
|
1731
|
|
|
|
|
|
|
|
1732
|
3
|
50
|
|
|
|
|
if ((error = mark_edges_uninteresting(pb, walk->user_input)) < 0) |
1733
|
0
|
|
|
|
|
|
return error; |
1734
|
|
|
|
|
|
|
|
1735
|
|
|
|
|
|
|
/* |
1736
|
|
|
|
|
|
|
* TODO: git marks the parents of the edges |
1737
|
|
|
|
|
|
|
* uninteresting. This may provide a speed advantage, but does |
1738
|
|
|
|
|
|
|
* seem to assume the remote does not have a single-commit |
1739
|
|
|
|
|
|
|
* history on the other end. |
1740
|
|
|
|
|
|
|
*/ |
1741
|
|
|
|
|
|
|
|
1742
|
|
|
|
|
|
|
/* walk down each tree up to the blobs and insert them, stopping when uninteresting */ |
1743
|
12
|
100
|
|
|
|
|
while ((error = git_revwalk_next(&id, walk)) == 0) { |
1744
|
9
|
50
|
|
|
|
|
if ((error = retrieve_object(&obj, pb, &id)) < 0) |
1745
|
0
|
|
|
|
|
|
return error; |
1746
|
|
|
|
|
|
|
|
1747
|
9
|
50
|
|
|
|
|
if (obj->seen || obj->uninteresting) |
|
|
50
|
|
|
|
|
|
1748
|
0
|
|
|
|
|
|
continue; |
1749
|
|
|
|
|
|
|
|
1750
|
9
|
50
|
|
|
|
|
if ((error = pack_objects_insert_commit(pb, obj)) < 0) |
1751
|
0
|
|
|
|
|
|
return error; |
1752
|
|
|
|
|
|
|
} |
1753
|
|
|
|
|
|
|
|
1754
|
3
|
50
|
|
|
|
|
if (error == GIT_ITEROVER) |
1755
|
3
|
|
|
|
|
|
error = 0; |
1756
|
|
|
|
|
|
|
|
1757
|
3
|
|
|
|
|
|
return error; |
1758
|
|
|
|
|
|
|
} |
1759
|
|
|
|
|
|
|
|
1760
|
3
|
|
|
|
|
|
int git_packbuilder_set_callbacks(git_packbuilder *pb, git_packbuilder_progress progress_cb, void *progress_cb_payload) |
1761
|
|
|
|
|
|
|
{ |
1762
|
3
|
50
|
|
|
|
|
if (!pb) |
1763
|
0
|
|
|
|
|
|
return -1; |
1764
|
|
|
|
|
|
|
|
1765
|
3
|
|
|
|
|
|
pb->progress_cb = progress_cb; |
1766
|
3
|
|
|
|
|
|
pb->progress_cb_payload = progress_cb_payload; |
1767
|
|
|
|
|
|
|
|
1768
|
3
|
|
|
|
|
|
return 0; |
1769
|
|
|
|
|
|
|
} |
1770
|
|
|
|
|
|
|
|
1771
|
7
|
|
|
|
|
|
void git_packbuilder_free(git_packbuilder *pb) |
1772
|
|
|
|
|
|
|
{ |
1773
|
7
|
50
|
|
|
|
|
if (pb == NULL) |
1774
|
0
|
|
|
|
|
|
return; |
1775
|
|
|
|
|
|
|
|
1776
|
|
|
|
|
|
|
#ifdef GIT_THREADS |
1777
|
|
|
|
|
|
|
|
1778
|
|
|
|
|
|
|
git_mutex_free(&pb->cache_mutex); |
1779
|
|
|
|
|
|
|
git_mutex_free(&pb->progress_mutex); |
1780
|
|
|
|
|
|
|
git_cond_free(&pb->progress_cond); |
1781
|
|
|
|
|
|
|
|
1782
|
|
|
|
|
|
|
#endif |
1783
|
|
|
|
|
|
|
|
1784
|
7
|
50
|
|
|
|
|
if (pb->odb) |
1785
|
7
|
|
|
|
|
|
git_odb_free(pb->odb); |
1786
|
|
|
|
|
|
|
|
1787
|
7
|
50
|
|
|
|
|
if (pb->object_ix) |
1788
|
7
|
|
|
|
|
|
git_oidmap_free(pb->object_ix); |
1789
|
|
|
|
|
|
|
|
1790
|
7
|
100
|
|
|
|
|
if (pb->object_list) |
1791
|
6
|
|
|
|
|
|
git__free(pb->object_list); |
1792
|
|
|
|
|
|
|
|
1793
|
7
|
|
|
|
|
|
git_oidmap_free(pb->walk_objects); |
1794
|
7
|
|
|
|
|
|
git_pool_clear(&pb->object_pool); |
1795
|
|
|
|
|
|
|
|
1796
|
7
|
|
|
|
|
|
git_hash_ctx_cleanup(&pb->ctx); |
1797
|
7
|
|
|
|
|
|
git_zstream_free(&pb->zstream); |
1798
|
|
|
|
|
|
|
|
1799
|
7
|
|
|
|
|
|
git__free(pb); |
1800
|
|
|
|
|
|
|
} |