Code

t9600: test cvsimport from CVS working tree
[git.git] / builtin-bundle.c
1 #include "builtin.h"
2 #include "cache.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"
9 #include "refs.h"
11 /*
12  * Basic handler for bundle files to connect repositories via sneakernet.
13  * Invocation must include action.
14  * This function can create a bundle or provide information on an existing
15  * bundle supporting git-fetch, git-pull, and git-ls-remote
16  */
18 static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
20 static const char bundle_signature[] = "# v2 git bundle\n";
22 struct ref_list {
23         unsigned int nr, alloc;
24         struct ref_list_entry {
25                 unsigned char sha1[20];
26                 char *name;
27         } *list;
28 };
30 static void add_to_ref_list(const unsigned char *sha1, const char *name,
31                 struct ref_list *list)
32 {
33         if (list->nr + 1 >= list->alloc) {
34                 list->alloc = alloc_nr(list->nr + 1);
35                 list->list = xrealloc(list->list,
36                                 list->alloc * sizeof(list->list[0]));
37         }
38         memcpy(list->list[list->nr].sha1, sha1, 20);
39         list->list[list->nr].name = xstrdup(name);
40         list->nr++;
41 }
43 struct bundle_header {
44         struct ref_list prerequisites;
45         struct ref_list references;
46 };
48 /* returns an fd */
49 static int read_header(const char *path, struct bundle_header *header) {
50         char buffer[1024];
51         int fd;
52         long fpos;
53         FILE *ffd = fopen(path, "rb");
55         if (!ffd)
56                 return error("could not open '%s'", path);
57         if (!fgets(buffer, sizeof(buffer), ffd) ||
58                         strcmp(buffer, bundle_signature)) {
59                 fclose(ffd);
60                 return error("'%s' does not look like a v2 bundle file", path);
61         }
62         while (fgets(buffer, sizeof(buffer), ffd)
63                         && buffer[0] != '\n') {
64                 int is_prereq = buffer[0] == '-';
65                 int offset = is_prereq ? 1 : 0;
66                 int len = strlen(buffer);
67                 unsigned char sha1[20];
68                 struct ref_list *list = is_prereq ? &header->prerequisites
69                         : &header->references;
70                 char delim;
72                 if (buffer[len - 1] == '\n')
73                         buffer[len - 1] = '\0';
74                 if (get_sha1_hex(buffer + offset, sha1)) {
75                         warning("unrecognized header: %s", buffer);
76                         continue;
77                 }
78                 delim = buffer[40 + offset];
79                 if (!isspace(delim) && (delim != '\0' || !is_prereq))
80                         die ("invalid header: %s", buffer);
81                 add_to_ref_list(sha1, isspace(delim) ?
82                                 buffer + 41 + offset : "", list);
83         }
84         fpos = ftell(ffd);
85         fclose(ffd);
86         fd = open(path, O_RDONLY);
87         if (fd < 0)
88                 return error("could not open '%s'", path);
89         lseek(fd, fpos, SEEK_SET);
90         return fd;
91 }
93 static int list_refs(struct ref_list *r, int argc, const char **argv)
94 {
95         int i;
97         for (i = 0; i < r->nr; i++) {
98                 if (argc > 1) {
99                         int j;
100                         for (j = 1; j < argc; j++)
101                                 if (!strcmp(r->list[i].name, argv[j]))
102                                         break;
103                         if (j == argc)
104                                 continue;
105                 }
106                 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
107                                 r->list[i].name);
108         }
109         return 0;
112 #define PREREQ_MARK (1u<<16)
114 static int verify_bundle(struct bundle_header *header, int verbose)
116         /*
117          * Do fast check, then if any prereqs are missing then go line by line
118          * to be verbose about the errors
119          */
120         struct ref_list *p = &header->prerequisites;
121         struct rev_info revs;
122         const char *argv[] = {NULL, "--all"};
123         struct object_array refs;
124         struct commit *commit;
125         int i, ret = 0, req_nr;
126         const char *message = "Repository lacks these prerequisite commits:";
128         init_revisions(&revs, NULL);
129         for (i = 0; i < p->nr; i++) {
130                 struct ref_list_entry *e = p->list + i;
131                 struct object *o = parse_object(e->sha1);
132                 if (o) {
133                         o->flags |= PREREQ_MARK;
134                         add_pending_object(&revs, o, e->name);
135                         continue;
136                 }
137                 if (++ret == 1)
138                         error(message);
139                 error("%s %s", sha1_to_hex(e->sha1), e->name);
140         }
141         if (revs.pending.nr != p->nr)
142                 return ret;
143         req_nr = revs.pending.nr;
144         setup_revisions(2, argv, &revs, NULL);
146         memset(&refs, 0, sizeof(struct object_array));
147         for (i = 0; i < revs.pending.nr; i++) {
148                 struct object_array_entry *e = revs.pending.objects + i;
149                 add_object_array(e->item, e->name, &refs);
150         }
152         prepare_revision_walk(&revs);
154         i = req_nr;
155         while (i && (commit = get_revision(&revs)))
156                 if (commit->object.flags & PREREQ_MARK)
157                         i--;
159         for (i = 0; i < req_nr; i++)
160                 if (!(refs.objects[i].item->flags & SHOWN)) {
161                         if (++ret == 1)
162                                 error(message);
163                         error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
164                                 refs.objects[i].name);
165                 }
167         for (i = 0; i < refs.nr; i++)
168                 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
170         if (verbose) {
171                 struct ref_list *r;
173                 r = &header->references;
174                 printf("The bundle contains %d ref%s\n",
175                        r->nr, (1 < r->nr) ? "s" : "");
176                 list_refs(r, 0, NULL);
177                 r = &header->prerequisites;
178                 printf("The bundle requires these %d ref%s\n",
179                        r->nr, (1 < r->nr) ? "s" : "");
180                 list_refs(r, 0, NULL);
181         }
182         return ret;
185 static int list_heads(struct bundle_header *header, int argc, const char **argv)
187         return list_refs(&header->references, argc, argv);
190 static int create_bundle(struct bundle_header *header, const char *path,
191                 int argc, const char **argv)
193         static struct lock_file lock;
194         int bundle_fd = -1;
195         int bundle_to_stdout;
196         const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
197         const char **argv_pack = xmalloc(5 * sizeof(const char *));
198         int i, ref_count = 0;
199         char buffer[1024];
200         struct rev_info revs;
201         struct child_process rls;
202         FILE *rls_fout;
204         bundle_to_stdout = !strcmp(path, "-");
205         if (bundle_to_stdout)
206                 bundle_fd = 1;
207         else
208                 bundle_fd = hold_lock_file_for_update(&lock, path, 1);
210         /* write signature */
211         write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
213         /* init revs to list objects for pack-objects later */
214         save_commit_buffer = 0;
215         init_revisions(&revs, NULL);
217         /* write prerequisites */
218         memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
219         argv_boundary[0] = "rev-list";
220         argv_boundary[1] = "--boundary";
221         argv_boundary[2] = "--pretty=oneline";
222         argv_boundary[argc + 2] = NULL;
223         memset(&rls, 0, sizeof(rls));
224         rls.argv = argv_boundary;
225         rls.out = -1;
226         rls.git_cmd = 1;
227         if (start_command(&rls))
228                 return -1;
229         rls_fout = fdopen(rls.out, "r");
230         while (fgets(buffer, sizeof(buffer), rls_fout)) {
231                 unsigned char sha1[20];
232                 if (buffer[0] == '-') {
233                         write_or_die(bundle_fd, buffer, strlen(buffer));
234                         if (!get_sha1_hex(buffer + 1, sha1)) {
235                                 struct object *object = parse_object(sha1);
236                                 object->flags |= UNINTERESTING;
237                                 add_pending_object(&revs, object, buffer);
238                         }
239                 } else if (!get_sha1_hex(buffer, sha1)) {
240                         struct object *object = parse_object(sha1);
241                         object->flags |= SHOWN;
242                 }
243         }
244         fclose(rls_fout);
245         if (finish_command(&rls))
246                 return error("rev-list died");
248         /* write references */
249         argc = setup_revisions(argc, argv, &revs, NULL);
250         if (argc > 1)
251                 return error("unrecognized argument: %s'", argv[1]);
253         for (i = 0; i < revs.pending.nr; i++) {
254                 struct object_array_entry *e = revs.pending.objects + i;
255                 unsigned char sha1[20];
256                 char *ref;
257                 const char *display_ref;
258                 int flag;
260                 if (e->item->flags & UNINTERESTING)
261                         continue;
262                 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
263                         continue;
264                 if (!resolve_ref(e->name, sha1, 1, &flag))
265                         flag = 0;
266                 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
268                 /*
269                  * Make sure the refs we wrote out is correct; --max-count and
270                  * other limiting options could have prevented all the tips
271                  * from getting output.
272                  *
273                  * Non commit objects such as tags and blobs do not have
274                  * this issue as they are not affected by those extra
275                  * constraints.
276                  */
277                 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
278                         warning("ref '%s' is excluded by the rev-list options",
279                                 e->name);
280                         free(ref);
281                         continue;
282                 }
283                 /*
284                  * If you run "git bundle create bndl v1.0..v2.0", the
285                  * name of the positive ref is "v2.0" but that is the
286                  * commit that is referenced by the tag, and not the tag
287                  * itself.
288                  */
289                 if (hashcmp(sha1, e->item->sha1)) {
290                         /*
291                          * Is this the positive end of a range expressed
292                          * in terms of a tag (e.g. v2.0 from the range
293                          * "v1.0..v2.0")?
294                          */
295                         struct commit *one = lookup_commit_reference(sha1);
296                         struct object *obj;
298                         if (e->item == &(one->object)) {
299                                 /*
300                                  * Need to include e->name as an
301                                  * independent ref to the pack-objects
302                                  * input, so that the tag is included
303                                  * in the output; otherwise we would
304                                  * end up triggering "empty bundle"
305                                  * error.
306                                  */
307                                 obj = parse_object(sha1);
308                                 obj->flags |= SHOWN;
309                                 add_pending_object(&revs, obj, e->name);
310                         }
311                         free(ref);
312                         continue;
313                 }
315                 ref_count++;
316                 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
317                 write_or_die(bundle_fd, " ", 1);
318                 write_or_die(bundle_fd, display_ref, strlen(display_ref));
319                 write_or_die(bundle_fd, "\n", 1);
320                 free(ref);
321         }
322         if (!ref_count)
323                 die ("Refusing to create empty bundle.");
325         /* end header */
326         write_or_die(bundle_fd, "\n", 1);
328         /* write pack */
329         argv_pack[0] = "pack-objects";
330         argv_pack[1] = "--all-progress";
331         argv_pack[2] = "--stdout";
332         argv_pack[3] = "--thin";
333         argv_pack[4] = NULL;
334         memset(&rls, 0, sizeof(rls));
335         rls.argv = argv_pack;
336         rls.in = -1;
337         rls.out = bundle_fd;
338         rls.git_cmd = 1;
339         if (start_command(&rls))
340                 return error("Could not spawn pack-objects");
341         for (i = 0; i < revs.pending.nr; i++) {
342                 struct object *object = revs.pending.objects[i].item;
343                 if (object->flags & UNINTERESTING)
344                         write(rls.in, "^", 1);
345                 write(rls.in, sha1_to_hex(object->sha1), 40);
346                 write(rls.in, "\n", 1);
347         }
348         if (finish_command(&rls))
349                 return error ("pack-objects died");
350         close(bundle_fd);
351         if (!bundle_to_stdout)
352                 commit_lock_file(&lock);
353         return 0;
356 static int unbundle(struct bundle_header *header, int bundle_fd,
357                 int argc, const char **argv)
359         const char *argv_index_pack[] = {"index-pack",
360                 "--fix-thin", "--stdin", NULL};
361         struct child_process ip;
363         if (verify_bundle(header, 0))
364                 return -1;
365         memset(&ip, 0, sizeof(ip));
366         ip.argv = argv_index_pack;
367         ip.in = bundle_fd;
368         ip.no_stdout = 1;
369         ip.git_cmd = 1;
370         if (run_command(&ip))
371                 return error("index-pack died");
372         return list_heads(header, argc, argv);
375 int cmd_bundle(int argc, const char **argv, const char *prefix)
377         struct bundle_header header;
378         int nongit = 0;
379         const char *cmd, *bundle_file;
380         int bundle_fd = -1;
381         char buffer[PATH_MAX];
383         if (argc < 3)
384                 usage(bundle_usage);
386         cmd = argv[1];
387         bundle_file = argv[2];
388         argc -= 2;
389         argv += 2;
391         prefix = setup_git_directory_gently(&nongit);
392         if (prefix && bundle_file[0] != '/') {
393                 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
394                 bundle_file = buffer;
395         }
397         memset(&header, 0, sizeof(header));
398         if (strcmp(cmd, "create") &&
399                         (bundle_fd = read_header(bundle_file, &header)) < 0)
400                 return 1;
402         if (!strcmp(cmd, "verify")) {
403                 close(bundle_fd);
404                 if (verify_bundle(&header, 1))
405                         return 1;
406                 fprintf(stderr, "%s is okay\n", bundle_file);
407                 return 0;
408         }
409         if (!strcmp(cmd, "list-heads")) {
410                 close(bundle_fd);
411                 return !!list_heads(&header, argc, argv);
412         }
413         if (!strcmp(cmd, "create")) {
414                 if (nongit)
415                         die("Need a repository to create a bundle.");
416                 return !!create_bundle(&header, bundle_file, argc, argv);
417         } else if (!strcmp(cmd, "unbundle")) {
418                 if (nongit)
419                         die("Need a repository to unbundle.");
420                 return !!unbundle(&header, bundle_fd, argc, argv);
421         } else
422                 usage(bundle_usage);