Convert lookup_commit* to struct object_id
[git] / bundle.c
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "bundle.h"
4 #include "object.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "list-objects.h"
9 #include "run-command.h"
10 #include "refs.h"
11 #include "argv-array.h"
12
13 static const char bundle_signature[] = "# v2 git bundle\n";
14
15 static void add_to_ref_list(const struct object_id *oid, const char *name,
16                 struct ref_list *list)
17 {
18         ALLOC_GROW(list->list, list->nr + 1, list->alloc);
19         oidcpy(&list->list[list->nr].oid, oid);
20         list->list[list->nr].name = xstrdup(name);
21         list->nr++;
22 }
23
24 static int parse_bundle_header(int fd, struct bundle_header *header,
25                                const char *report_path)
26 {
27         struct strbuf buf = STRBUF_INIT;
28         int status = 0;
29
30         /* The bundle header begins with the signature */
31         if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
32             strcmp(buf.buf, bundle_signature)) {
33                 if (report_path)
34                         error(_("'%s' does not look like a v2 bundle file"),
35                               report_path);
36                 status = -1;
37                 goto abort;
38         }
39
40         /* The bundle header ends with an empty line */
41         while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
42                buf.len && buf.buf[0] != '\n') {
43                 struct object_id oid;
44                 int is_prereq = 0;
45                 const char *p;
46
47                 if (*buf.buf == '-') {
48                         is_prereq = 1;
49                         strbuf_remove(&buf, 0, 1);
50                 }
51                 strbuf_rtrim(&buf);
52
53                 /*
54                  * Tip lines have object name, SP, and refname.
55                  * Prerequisites have object name that is optionally
56                  * followed by SP and subject line.
57                  */
58                 if (parse_oid_hex(buf.buf, &oid, &p) ||
59                     (*p && !isspace(*p)) ||
60                     (!is_prereq && !*p)) {
61                         if (report_path)
62                                 error(_("unrecognized header: %s%s (%d)"),
63                                       (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
64                         status = -1;
65                         break;
66                 } else {
67                         if (is_prereq)
68                                 add_to_ref_list(&oid, "", &header->prerequisites);
69                         else
70                                 add_to_ref_list(&oid, p + 1, &header->references);
71                 }
72         }
73
74  abort:
75         if (status) {
76                 close(fd);
77                 fd = -1;
78         }
79         strbuf_release(&buf);
80         return fd;
81 }
82
83 int read_bundle_header(const char *path, struct bundle_header *header)
84 {
85         int fd = open(path, O_RDONLY);
86
87         if (fd < 0)
88                 return error(_("could not open '%s'"), path);
89         return parse_bundle_header(fd, header, path);
90 }
91
92 int is_bundle(const char *path, int quiet)
93 {
94         struct bundle_header header;
95         int fd = open(path, O_RDONLY);
96
97         if (fd < 0)
98                 return 0;
99         memset(&header, 0, sizeof(header));
100         fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
101         if (fd >= 0)
102                 close(fd);
103         return (fd >= 0);
104 }
105
106 static int list_refs(struct ref_list *r, int argc, const char **argv)
107 {
108         int i;
109
110         for (i = 0; i < r->nr; i++) {
111                 if (argc > 1) {
112                         int j;
113                         for (j = 1; j < argc; j++)
114                                 if (!strcmp(r->list[i].name, argv[j]))
115                                         break;
116                         if (j == argc)
117                                 continue;
118                 }
119                 printf("%s %s\n", oid_to_hex(&r->list[i].oid),
120                                 r->list[i].name);
121         }
122         return 0;
123 }
124
125 /* Remember to update object flag allocation in object.h */
126 #define PREREQ_MARK (1u<<16)
127
128 int verify_bundle(struct bundle_header *header, int verbose)
129 {
130         /*
131          * Do fast check, then if any prereqs are missing then go line by line
132          * to be verbose about the errors
133          */
134         struct ref_list *p = &header->prerequisites;
135         struct rev_info revs;
136         const char *argv[] = {NULL, "--all", NULL};
137         struct object_array refs;
138         struct commit *commit;
139         int i, ret = 0, req_nr;
140         const char *message = _("Repository lacks these prerequisite commits:");
141
142         init_revisions(&revs, NULL);
143         for (i = 0; i < p->nr; i++) {
144                 struct ref_list_entry *e = p->list + i;
145                 struct object *o = parse_object(e->oid.hash);
146                 if (o) {
147                         o->flags |= PREREQ_MARK;
148                         add_pending_object(&revs, o, e->name);
149                         continue;
150                 }
151                 if (++ret == 1)
152                         error("%s", message);
153                 error("%s %s", oid_to_hex(&e->oid), e->name);
154         }
155         if (revs.pending.nr != p->nr)
156                 return ret;
157         req_nr = revs.pending.nr;
158         setup_revisions(2, argv, &revs, NULL);
159
160         refs = revs.pending;
161         revs.leak_pending = 1;
162
163         if (prepare_revision_walk(&revs))
164                 die(_("revision walk setup failed"));
165
166         i = req_nr;
167         while (i && (commit = get_revision(&revs)))
168                 if (commit->object.flags & PREREQ_MARK)
169                         i--;
170
171         for (i = 0; i < req_nr; i++)
172                 if (!(refs.objects[i].item->flags & SHOWN)) {
173                         if (++ret == 1)
174                                 error("%s", message);
175                         error("%s %s", oid_to_hex(&refs.objects[i].item->oid),
176                                 refs.objects[i].name);
177                 }
178
179         clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
180         free(refs.objects);
181
182         if (verbose) {
183                 struct ref_list *r;
184
185                 r = &header->references;
186                 printf_ln(Q_("The bundle contains this ref:",
187                              "The bundle contains these %d refs:",
188                              r->nr),
189                           r->nr);
190                 list_refs(r, 0, NULL);
191                 r = &header->prerequisites;
192                 if (!r->nr) {
193                         printf_ln(_("The bundle records a complete history."));
194                 } else {
195                         printf_ln(Q_("The bundle requires this ref:",
196                                      "The bundle requires these %d refs:",
197                                      r->nr),
198                                   r->nr);
199                         list_refs(r, 0, NULL);
200                 }
201         }
202         return ret;
203 }
204
205 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
206 {
207         return list_refs(&header->references, argc, argv);
208 }
209
210 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
211 {
212         unsigned long size;
213         enum object_type type;
214         char *buf = NULL, *line, *lineend;
215         unsigned long date;
216         int result = 1;
217
218         if (revs->max_age == -1 && revs->min_age == -1)
219                 goto out;
220
221         buf = read_sha1_file(tag->oid.hash, &type, &size);
222         if (!buf)
223                 goto out;
224         line = memmem(buf, size, "\ntagger ", 8);
225         if (!line++)
226                 goto out;
227         lineend = memchr(line, '\n', buf + size - line);
228         line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
229         if (!line++)
230                 goto out;
231         date = strtoul(line, NULL, 10);
232         result = (revs->max_age == -1 || revs->max_age < date) &&
233                 (revs->min_age == -1 || revs->min_age > date);
234 out:
235         free(buf);
236         return result;
237 }
238
239
240 /* Write the pack data to bundle_fd, then close it if it is > 1. */
241 static int write_pack_data(int bundle_fd, struct rev_info *revs)
242 {
243         struct child_process pack_objects = CHILD_PROCESS_INIT;
244         int i;
245
246         argv_array_pushl(&pack_objects.args,
247                          "pack-objects", "--all-progress-implied",
248                          "--stdout", "--thin", "--delta-base-offset",
249                          NULL);
250         pack_objects.in = -1;
251         pack_objects.out = bundle_fd;
252         pack_objects.git_cmd = 1;
253         if (start_command(&pack_objects))
254                 return error(_("Could not spawn pack-objects"));
255
256         for (i = 0; i < revs->pending.nr; i++) {
257                 struct object *object = revs->pending.objects[i].item;
258                 if (object->flags & UNINTERESTING)
259                         write_or_die(pack_objects.in, "^", 1);
260                 write_or_die(pack_objects.in, oid_to_hex(&object->oid), GIT_SHA1_HEXSZ);
261                 write_or_die(pack_objects.in, "\n", 1);
262         }
263         close(pack_objects.in);
264         if (finish_command(&pack_objects))
265                 return error(_("pack-objects died"));
266         return 0;
267 }
268
269 static int compute_and_write_prerequisites(int bundle_fd,
270                                            struct rev_info *revs,
271                                            int argc, const char **argv)
272 {
273         struct child_process rls = CHILD_PROCESS_INIT;
274         struct strbuf buf = STRBUF_INIT;
275         FILE *rls_fout;
276         int i;
277
278         argv_array_pushl(&rls.args,
279                          "rev-list", "--boundary", "--pretty=oneline",
280                          NULL);
281         for (i = 1; i < argc; i++)
282                 argv_array_push(&rls.args, argv[i]);
283         rls.out = -1;
284         rls.git_cmd = 1;
285         if (start_command(&rls))
286                 return -1;
287         rls_fout = xfdopen(rls.out, "r");
288         while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
289                 struct object_id oid;
290                 if (buf.len > 0 && buf.buf[0] == '-') {
291                         write_or_die(bundle_fd, buf.buf, buf.len);
292                         if (!get_oid_hex(buf.buf + 1, &oid)) {
293                                 struct object *object = parse_object_or_die(oid.hash, buf.buf);
294                                 object->flags |= UNINTERESTING;
295                                 add_pending_object(revs, object, buf.buf);
296                         }
297                 } else if (!get_oid_hex(buf.buf, &oid)) {
298                         struct object *object = parse_object_or_die(oid.hash, buf.buf);
299                         object->flags |= SHOWN;
300                 }
301         }
302         strbuf_release(&buf);
303         fclose(rls_fout);
304         if (finish_command(&rls))
305                 return error(_("rev-list died"));
306         return 0;
307 }
308
309 /*
310  * Write out bundle refs based on the tips already
311  * parsed into revs.pending. As a side effect, may
312  * manipulate revs.pending to include additional
313  * necessary objects (like tags).
314  *
315  * Returns the number of refs written, or negative
316  * on error.
317  */
318 static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
319 {
320         int i;
321         int ref_count = 0;
322
323         for (i = 0; i < revs->pending.nr; i++) {
324                 struct object_array_entry *e = revs->pending.objects + i;
325                 struct object_id oid;
326                 char *ref;
327                 const char *display_ref;
328                 int flag;
329
330                 if (e->item->flags & UNINTERESTING)
331                         continue;
332                 if (dwim_ref(e->name, strlen(e->name), oid.hash, &ref) != 1)
333                         goto skip_write_ref;
334                 if (read_ref_full(e->name, RESOLVE_REF_READING, oid.hash, &flag))
335                         flag = 0;
336                 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
337
338                 if (e->item->type == OBJ_TAG &&
339                                 !is_tag_in_date_range(e->item, revs)) {
340                         e->item->flags |= UNINTERESTING;
341                         goto skip_write_ref;
342                 }
343
344                 /*
345                  * Make sure the refs we wrote out is correct; --max-count and
346                  * other limiting options could have prevented all the tips
347                  * from getting output.
348                  *
349                  * Non commit objects such as tags and blobs do not have
350                  * this issue as they are not affected by those extra
351                  * constraints.
352                  */
353                 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
354                         warning(_("ref '%s' is excluded by the rev-list options"),
355                                 e->name);
356                         goto skip_write_ref;
357                 }
358                 /*
359                  * If you run "git bundle create bndl v1.0..v2.0", the
360                  * name of the positive ref is "v2.0" but that is the
361                  * commit that is referenced by the tag, and not the tag
362                  * itself.
363                  */
364                 if (oidcmp(&oid, &e->item->oid)) {
365                         /*
366                          * Is this the positive end of a range expressed
367                          * in terms of a tag (e.g. v2.0 from the range
368                          * "v1.0..v2.0")?
369                          */
370                         struct commit *one = lookup_commit_reference(&oid);
371                         struct object *obj;
372
373                         if (e->item == &(one->object)) {
374                                 /*
375                                  * Need to include e->name as an
376                                  * independent ref to the pack-objects
377                                  * input, so that the tag is included
378                                  * in the output; otherwise we would
379                                  * end up triggering "empty bundle"
380                                  * error.
381                                  */
382                                 obj = parse_object_or_die(oid.hash, e->name);
383                                 obj->flags |= SHOWN;
384                                 add_pending_object(revs, obj, e->name);
385                         }
386                         goto skip_write_ref;
387                 }
388
389                 ref_count++;
390                 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), 40);
391                 write_or_die(bundle_fd, " ", 1);
392                 write_or_die(bundle_fd, display_ref, strlen(display_ref));
393                 write_or_die(bundle_fd, "\n", 1);
394  skip_write_ref:
395                 free(ref);
396         }
397
398         /* end header */
399         write_or_die(bundle_fd, "\n", 1);
400         return ref_count;
401 }
402
403 int create_bundle(struct bundle_header *header, const char *path,
404                   int argc, const char **argv)
405 {
406         static struct lock_file lock;
407         int bundle_fd = -1;
408         int bundle_to_stdout;
409         int ref_count = 0;
410         struct rev_info revs;
411
412         bundle_to_stdout = !strcmp(path, "-");
413         if (bundle_to_stdout)
414                 bundle_fd = 1;
415         else {
416                 bundle_fd = hold_lock_file_for_update(&lock, path,
417                                                       LOCK_DIE_ON_ERROR);
418
419                 /*
420                  * write_pack_data() will close the fd passed to it,
421                  * but commit_lock_file() will also try to close the
422                  * lockfile's fd. So make a copy of the file
423                  * descriptor to avoid trying to close it twice.
424                  */
425                 bundle_fd = dup(bundle_fd);
426                 if (bundle_fd < 0)
427                         die_errno("unable to dup file descriptor");
428         }
429
430         /* write signature */
431         write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
432
433         /* init revs to list objects for pack-objects later */
434         save_commit_buffer = 0;
435         init_revisions(&revs, NULL);
436
437         /* write prerequisites */
438         if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
439                 goto err;
440
441         argc = setup_revisions(argc, argv, &revs, NULL);
442
443         if (argc > 1) {
444                 error(_("unrecognized argument: %s"), argv[1]);
445                 goto err;
446         }
447
448         object_array_remove_duplicates(&revs.pending);
449
450         ref_count = write_bundle_refs(bundle_fd, &revs);
451         if (!ref_count)
452                 die(_("Refusing to create empty bundle."));
453         else if (ref_count < 0)
454                 goto err;
455
456         /* write pack */
457         if (write_pack_data(bundle_fd, &revs)) {
458                 bundle_fd = -1; /* already closed by the above call */
459                 goto err;
460         }
461
462         if (!bundle_to_stdout) {
463                 if (commit_lock_file(&lock))
464                         die_errno(_("cannot create '%s'"), path);
465         }
466         return 0;
467 err:
468         if (!bundle_to_stdout) {
469                 if (0 <= bundle_fd)
470                         close(bundle_fd);
471                 rollback_lock_file(&lock);
472         }
473         return -1;
474 }
475
476 int unbundle(struct bundle_header *header, int bundle_fd, int flags)
477 {
478         const char *argv_index_pack[] = {"index-pack",
479                                          "--fix-thin", "--stdin", NULL, NULL};
480         struct child_process ip = CHILD_PROCESS_INIT;
481
482         if (flags & BUNDLE_VERBOSE)
483                 argv_index_pack[3] = "-v";
484
485         if (verify_bundle(header, 0))
486                 return -1;
487         ip.argv = argv_index_pack;
488         ip.in = bundle_fd;
489         ip.no_stdout = 1;
490         ip.git_cmd = 1;
491         if (run_command(&ip))
492                 return error(_("index-pack died"));
493         return 0;
494 }