]>
Commit | Line | Data |
---|---|---|
30415d50 JS |
1 | #include "cache.h" |
2 | #include "bundle.h" | |
3 | #include "object.h" | |
4 | #include "commit.h" | |
5 | #include "diff.h" | |
6 | #include "revision.h" | |
7 | #include "list-objects.h" | |
8 | #include "run-command.h" | |
fa303836 | 9 | #include "refs.h" |
30415d50 JS |
10 | |
11 | static const char bundle_signature[] = "# v2 git bundle\n"; | |
12 | ||
13 | static void add_to_ref_list(const unsigned char *sha1, const char *name, | |
14 | struct ref_list *list) | |
15 | { | |
16 | if (list->nr + 1 >= list->alloc) { | |
17 | list->alloc = alloc_nr(list->nr + 1); | |
18 | list->list = xrealloc(list->list, | |
19 | list->alloc * sizeof(list->list[0])); | |
20 | } | |
21 | memcpy(list->list[list->nr].sha1, sha1, 20); | |
22 | list->list[list->nr].name = xstrdup(name); | |
23 | list->nr++; | |
24 | } | |
25 | ||
e9ee84cf JH |
26 | /* Eventually this should go to strbuf.[ch] */ |
27 | static int strbuf_readline_fd(struct strbuf *sb, int fd) | |
28 | { | |
29 | strbuf_reset(sb); | |
30 | ||
31 | while (1) { | |
32 | char ch; | |
33 | ssize_t len = xread(fd, &ch, 1); | |
34 | if (len < 0) | |
35 | return -1; | |
36 | strbuf_addch(sb, ch); | |
37 | if (ch == '\n') | |
38 | break; | |
39 | } | |
40 | return 0; | |
41 | } | |
42 | ||
f3fa1838 JH |
43 | int read_bundle_header(const char *path, struct bundle_header *header) |
44 | { | |
e9ee84cf JH |
45 | struct strbuf buf = STRBUF_INIT; |
46 | int fd = open(path, O_RDONLY); | |
47 | int status = 0; | |
30415d50 | 48 | |
e9ee84cf | 49 | if (fd < 0) |
30415d50 | 50 | return error("could not open '%s'", path); |
e9ee84cf JH |
51 | |
52 | /* The bundle header begins with the signature */ | |
53 | if (strbuf_readline_fd(&buf, fd) || | |
54 | strcmp(buf.buf, bundle_signature)) { | |
55 | error("'%s' does not look like a v2 bundle file", path); | |
56 | status = -1; | |
57 | goto abort; | |
30415d50 | 58 | } |
e9ee84cf JH |
59 | |
60 | /* The bundle header ends with an empty line */ | |
61 | while (!strbuf_readline_fd(&buf, fd) && | |
62 | buf.len && buf.buf[0] != '\n') { | |
30415d50 | 63 | unsigned char sha1[20]; |
e9ee84cf JH |
64 | int is_prereq = 0; |
65 | ||
66 | if (*buf.buf == '-') { | |
67 | is_prereq = 1; | |
68 | strbuf_remove(&buf, 0, 1); | |
69 | } | |
70 | strbuf_rtrim(&buf); | |
30415d50 | 71 | |
e9ee84cf JH |
72 | /* |
73 | * Tip lines have object name, SP, and refname. | |
74 | * Prerequisites have object name that is optionally | |
75 | * followed by SP and subject line. | |
76 | */ | |
77 | if (get_sha1_hex(buf.buf, sha1) || | |
78 | (40 <= buf.len && !isspace(buf.buf[40])) || | |
79 | (!is_prereq && buf.len <= 40)) { | |
80 | error("unrecognized header: %s%s (%d)", | |
81 | (is_prereq ? "-" : ""), buf.buf, (int)buf.len); | |
82 | status = -1; | |
83 | break; | |
84 | } else { | |
85 | if (is_prereq) | |
86 | add_to_ref_list(sha1, "", &header->prerequisites); | |
87 | else | |
88 | add_to_ref_list(sha1, buf.buf + 41, &header->references); | |
30415d50 | 89 | } |
30415d50 | 90 | } |
e9ee84cf JH |
91 | |
92 | abort: | |
93 | if (status) { | |
94 | close(fd); | |
95 | fd = -1; | |
96 | } | |
97 | strbuf_release(&buf); | |
30415d50 JS |
98 | return fd; |
99 | } | |
100 | ||
101 | static int list_refs(struct ref_list *r, int argc, const char **argv) | |
102 | { | |
103 | int i; | |
104 | ||
105 | for (i = 0; i < r->nr; i++) { | |
106 | if (argc > 1) { | |
107 | int j; | |
108 | for (j = 1; j < argc; j++) | |
109 | if (!strcmp(r->list[i].name, argv[j])) | |
110 | break; | |
111 | if (j == argc) | |
112 | continue; | |
113 | } | |
114 | printf("%s %s\n", sha1_to_hex(r->list[i].sha1), | |
115 | r->list[i].name); | |
116 | } | |
117 | return 0; | |
118 | } | |
119 | ||
120 | #define PREREQ_MARK (1u<<16) | |
121 | ||
122 | int verify_bundle(struct bundle_header *header, int verbose) | |
123 | { | |
124 | /* | |
125 | * Do fast check, then if any prereqs are missing then go line by line | |
126 | * to be verbose about the errors | |
127 | */ | |
128 | struct ref_list *p = &header->prerequisites; | |
129 | struct rev_info revs; | |
a80aad7b | 130 | const char *argv[] = {NULL, "--all", NULL}; |
30415d50 JS |
131 | struct object_array refs; |
132 | struct commit *commit; | |
133 | int i, ret = 0, req_nr; | |
134 | const char *message = "Repository lacks these prerequisite commits:"; | |
135 | ||
136 | init_revisions(&revs, NULL); | |
137 | for (i = 0; i < p->nr; i++) { | |
138 | struct ref_list_entry *e = p->list + i; | |
139 | struct object *o = parse_object(e->sha1); | |
140 | if (o) { | |
141 | o->flags |= PREREQ_MARK; | |
142 | add_pending_object(&revs, o, e->name); | |
143 | continue; | |
144 | } | |
145 | if (++ret == 1) | |
9db56f71 | 146 | error("%s", message); |
30415d50 JS |
147 | error("%s %s", sha1_to_hex(e->sha1), e->name); |
148 | } | |
149 | if (revs.pending.nr != p->nr) | |
150 | return ret; | |
151 | req_nr = revs.pending.nr; | |
152 | setup_revisions(2, argv, &revs, NULL); | |
153 | ||
154 | memset(&refs, 0, sizeof(struct object_array)); | |
155 | for (i = 0; i < revs.pending.nr; i++) { | |
156 | struct object_array_entry *e = revs.pending.objects + i; | |
157 | add_object_array(e->item, e->name, &refs); | |
158 | } | |
159 | ||
3d51e1b5 MK |
160 | if (prepare_revision_walk(&revs)) |
161 | die("revision walk setup failed"); | |
30415d50 JS |
162 | |
163 | i = req_nr; | |
164 | while (i && (commit = get_revision(&revs))) | |
165 | if (commit->object.flags & PREREQ_MARK) | |
166 | i--; | |
167 | ||
168 | for (i = 0; i < req_nr; i++) | |
169 | if (!(refs.objects[i].item->flags & SHOWN)) { | |
170 | if (++ret == 1) | |
9db56f71 | 171 | error("%s", message); |
30415d50 JS |
172 | error("%s %s", sha1_to_hex(refs.objects[i].item->sha1), |
173 | refs.objects[i].name); | |
174 | } | |
175 | ||
176 | for (i = 0; i < refs.nr; i++) | |
177 | clear_commit_marks((struct commit *)refs.objects[i].item, -1); | |
178 | ||
179 | if (verbose) { | |
180 | struct ref_list *r; | |
181 | ||
182 | r = &header->references; | |
183 | printf("The bundle contains %d ref%s\n", | |
184 | r->nr, (1 < r->nr) ? "s" : ""); | |
185 | list_refs(r, 0, NULL); | |
186 | r = &header->prerequisites; | |
187 | printf("The bundle requires these %d ref%s\n", | |
188 | r->nr, (1 < r->nr) ? "s" : ""); | |
189 | list_refs(r, 0, NULL); | |
190 | } | |
191 | return ret; | |
192 | } | |
193 | ||
194 | int list_bundle_refs(struct bundle_header *header, int argc, const char **argv) | |
195 | { | |
196 | return list_refs(&header->references, argc, argv); | |
197 | } | |
198 | ||
c9a42c4a JS |
199 | static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) |
200 | { | |
201 | unsigned long size; | |
202 | enum object_type type; | |
203 | char *buf, *line, *lineend; | |
204 | unsigned long date; | |
205 | ||
206 | if (revs->max_age == -1 && revs->min_age == -1) | |
207 | return 1; | |
208 | ||
209 | buf = read_sha1_file(tag->sha1, &type, &size); | |
210 | if (!buf) | |
211 | return 1; | |
212 | line = memmem(buf, size, "\ntagger ", 8); | |
213 | if (!line++) | |
214 | return 1; | |
215 | lineend = memchr(line, buf + size - line, '\n'); | |
216 | line = memchr(line, lineend ? lineend - line : buf + size - line, '>'); | |
217 | if (!line++) | |
218 | return 1; | |
219 | date = strtoul(line, NULL, 10); | |
220 | free(buf); | |
221 | return (revs->max_age == -1 || revs->max_age < date) && | |
222 | (revs->min_age == -1 || revs->min_age > date); | |
223 | } | |
224 | ||
30415d50 JS |
225 | int create_bundle(struct bundle_header *header, const char *path, |
226 | int argc, const char **argv) | |
227 | { | |
228 | static struct lock_file lock; | |
229 | int bundle_fd = -1; | |
230 | int bundle_to_stdout; | |
231 | const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *)); | |
787d2a78 | 232 | const char **argv_pack = xmalloc(6 * sizeof(const char *)); |
30415d50 JS |
233 | int i, ref_count = 0; |
234 | char buffer[1024]; | |
235 | struct rev_info revs; | |
236 | struct child_process rls; | |
237 | FILE *rls_fout; | |
238 | ||
239 | bundle_to_stdout = !strcmp(path, "-"); | |
240 | if (bundle_to_stdout) | |
241 | bundle_fd = 1; | |
242 | else | |
acd3b9ec JH |
243 | bundle_fd = hold_lock_file_for_update(&lock, path, |
244 | LOCK_DIE_ON_ERROR); | |
30415d50 JS |
245 | |
246 | /* write signature */ | |
247 | write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature)); | |
248 | ||
249 | /* init revs to list objects for pack-objects later */ | |
250 | save_commit_buffer = 0; | |
251 | init_revisions(&revs, NULL); | |
252 | ||
253 | /* write prerequisites */ | |
254 | memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *)); | |
255 | argv_boundary[0] = "rev-list"; | |
256 | argv_boundary[1] = "--boundary"; | |
257 | argv_boundary[2] = "--pretty=oneline"; | |
258 | argv_boundary[argc + 2] = NULL; | |
259 | memset(&rls, 0, sizeof(rls)); | |
260 | rls.argv = argv_boundary; | |
261 | rls.out = -1; | |
262 | rls.git_cmd = 1; | |
263 | if (start_command(&rls)) | |
264 | return -1; | |
41698375 | 265 | rls_fout = xfdopen(rls.out, "r"); |
30415d50 JS |
266 | while (fgets(buffer, sizeof(buffer), rls_fout)) { |
267 | unsigned char sha1[20]; | |
268 | if (buffer[0] == '-') { | |
269 | write_or_die(bundle_fd, buffer, strlen(buffer)); | |
270 | if (!get_sha1_hex(buffer + 1, sha1)) { | |
271 | struct object *object = parse_object(sha1); | |
272 | object->flags |= UNINTERESTING; | |
273 | add_pending_object(&revs, object, buffer); | |
274 | } | |
275 | } else if (!get_sha1_hex(buffer, sha1)) { | |
276 | struct object *object = parse_object(sha1); | |
277 | object->flags |= SHOWN; | |
278 | } | |
279 | } | |
280 | fclose(rls_fout); | |
281 | if (finish_command(&rls)) | |
282 | return error("rev-list died"); | |
283 | ||
284 | /* write references */ | |
285 | argc = setup_revisions(argc, argv, &revs, NULL); | |
22568f0a | 286 | |
8b3dce56 JH |
287 | if (argc > 1) |
288 | return error("unrecognized argument: %s'", argv[1]); | |
30415d50 | 289 | |
b2a6d1c6 JH |
290 | object_array_remove_duplicates(&revs.pending); |
291 | ||
30415d50 JS |
292 | for (i = 0; i < revs.pending.nr; i++) { |
293 | struct object_array_entry *e = revs.pending.objects + i; | |
294 | unsigned char sha1[20]; | |
295 | char *ref; | |
fa303836 JH |
296 | const char *display_ref; |
297 | int flag; | |
30415d50 JS |
298 | |
299 | if (e->item->flags & UNINTERESTING) | |
300 | continue; | |
301 | if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1) | |
302 | continue; | |
fa303836 JH |
303 | if (!resolve_ref(e->name, sha1, 1, &flag)) |
304 | flag = 0; | |
305 | display_ref = (flag & REF_ISSYMREF) ? e->name : ref; | |
306 | ||
c9a42c4a JS |
307 | if (e->item->type == OBJ_TAG && |
308 | !is_tag_in_date_range(e->item, &revs)) { | |
309 | e->item->flags |= UNINTERESTING; | |
310 | continue; | |
311 | } | |
312 | ||
30415d50 JS |
313 | /* |
314 | * Make sure the refs we wrote out is correct; --max-count and | |
315 | * other limiting options could have prevented all the tips | |
316 | * from getting output. | |
317 | * | |
318 | * Non commit objects such as tags and blobs do not have | |
319 | * this issue as they are not affected by those extra | |
320 | * constraints. | |
321 | */ | |
322 | if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) { | |
323 | warning("ref '%s' is excluded by the rev-list options", | |
324 | e->name); | |
325 | free(ref); | |
326 | continue; | |
327 | } | |
328 | /* | |
329 | * If you run "git bundle create bndl v1.0..v2.0", the | |
330 | * name of the positive ref is "v2.0" but that is the | |
331 | * commit that is referenced by the tag, and not the tag | |
332 | * itself. | |
333 | */ | |
334 | if (hashcmp(sha1, e->item->sha1)) { | |
335 | /* | |
336 | * Is this the positive end of a range expressed | |
337 | * in terms of a tag (e.g. v2.0 from the range | |
338 | * "v1.0..v2.0")? | |
339 | */ | |
340 | struct commit *one = lookup_commit_reference(sha1); | |
341 | struct object *obj; | |
342 | ||
343 | if (e->item == &(one->object)) { | |
344 | /* | |
345 | * Need to include e->name as an | |
346 | * independent ref to the pack-objects | |
347 | * input, so that the tag is included | |
348 | * in the output; otherwise we would | |
349 | * end up triggering "empty bundle" | |
350 | * error. | |
351 | */ | |
352 | obj = parse_object(sha1); | |
353 | obj->flags |= SHOWN; | |
354 | add_pending_object(&revs, obj, e->name); | |
355 | } | |
356 | free(ref); | |
357 | continue; | |
358 | } | |
359 | ||
360 | ref_count++; | |
361 | write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40); | |
362 | write_or_die(bundle_fd, " ", 1); | |
fa303836 | 363 | write_or_die(bundle_fd, display_ref, strlen(display_ref)); |
30415d50 JS |
364 | write_or_die(bundle_fd, "\n", 1); |
365 | free(ref); | |
366 | } | |
367 | if (!ref_count) | |
368 | die ("Refusing to create empty bundle."); | |
369 | ||
370 | /* end header */ | |
371 | write_or_die(bundle_fd, "\n", 1); | |
372 | ||
373 | /* write pack */ | |
374 | argv_pack[0] = "pack-objects"; | |
4f366275 | 375 | argv_pack[1] = "--all-progress-implied"; |
30415d50 JS |
376 | argv_pack[2] = "--stdout"; |
377 | argv_pack[3] = "--thin"; | |
787d2a78 SP |
378 | argv_pack[4] = "--delta-base-offset"; |
379 | argv_pack[5] = NULL; | |
30415d50 JS |
380 | memset(&rls, 0, sizeof(rls)); |
381 | rls.argv = argv_pack; | |
382 | rls.in = -1; | |
383 | rls.out = bundle_fd; | |
384 | rls.git_cmd = 1; | |
385 | if (start_command(&rls)) | |
386 | return error("Could not spawn pack-objects"); | |
4ed7cd3a BC |
387 | |
388 | /* | |
389 | * start_command closed bundle_fd if it was > 1 | |
390 | * so set the lock fd to -1 so commit_lock_file() | |
391 | * won't fail trying to close it. | |
392 | */ | |
393 | lock.fd = -1; | |
394 | ||
30415d50 JS |
395 | for (i = 0; i < revs.pending.nr; i++) { |
396 | struct object *object = revs.pending.objects[i].item; | |
397 | if (object->flags & UNINTERESTING) | |
95693d45 JM |
398 | write_or_die(rls.in, "^", 1); |
399 | write_or_die(rls.in, sha1_to_hex(object->sha1), 40); | |
400 | write_or_die(rls.in, "\n", 1); | |
30415d50 | 401 | } |
e72ae288 | 402 | close(rls.in); |
30415d50 JS |
403 | if (finish_command(&rls)) |
404 | return error ("pack-objects died"); | |
0f5cdf65 CH |
405 | if (!bundle_to_stdout) { |
406 | if (commit_lock_file(&lock)) | |
407 | die_errno("cannot create '%s'", path); | |
408 | } | |
c20181e3 | 409 | return 0; |
30415d50 JS |
410 | } |
411 | ||
412 | int unbundle(struct bundle_header *header, int bundle_fd) | |
413 | { | |
414 | const char *argv_index_pack[] = {"index-pack", | |
415 | "--fix-thin", "--stdin", NULL}; | |
416 | struct child_process ip; | |
417 | ||
418 | if (verify_bundle(header, 0)) | |
419 | return -1; | |
420 | memset(&ip, 0, sizeof(ip)); | |
421 | ip.argv = argv_index_pack; | |
422 | ip.in = bundle_fd; | |
423 | ip.no_stdout = 1; | |
424 | ip.git_cmd = 1; | |
425 | if (run_command(&ip)) | |
426 | return error("index-pack died"); | |
427 | return 0; | |
428 | } |