rehabilitate some t5302 tests on 32-bit off_t machines
[git/dscho.git] / bundle.c
blobe4d60cde6f1b9dd7a2aab36ebc4f2a97a9d61b22
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"
10 static const char bundle_signature[] = "# v2 git bundle\n";
12 static void add_to_ref_list(const unsigned char *sha1, const char *name,
13 struct ref_list *list)
15 if (list->nr + 1 >= list->alloc) {
16 list->alloc = alloc_nr(list->nr + 1);
17 list->list = xrealloc(list->list,
18 list->alloc * sizeof(list->list[0]));
20 memcpy(list->list[list->nr].sha1, sha1, 20);
21 list->list[list->nr].name = xstrdup(name);
22 list->nr++;
25 /* returns an fd */
26 int read_bundle_header(const char *path, struct bundle_header *header)
28 char buffer[1024];
29 int fd;
30 long fpos;
31 FILE *ffd = fopen(path, "rb");
33 if (!ffd)
34 return error("could not open '%s'", path);
35 if (!fgets(buffer, sizeof(buffer), ffd) ||
36 strcmp(buffer, bundle_signature)) {
37 fclose(ffd);
38 return error("'%s' does not look like a v2 bundle file", path);
40 while (fgets(buffer, sizeof(buffer), ffd)
41 && buffer[0] != '\n') {
42 int is_prereq = buffer[0] == '-';
43 int offset = is_prereq ? 1 : 0;
44 int len = strlen(buffer);
45 unsigned char sha1[20];
46 struct ref_list *list = is_prereq ? &header->prerequisites
47 : &header->references;
48 char delim;
50 if (buffer[len - 1] == '\n')
51 buffer[len - 1] = '\0';
52 if (get_sha1_hex(buffer + offset, sha1)) {
53 warning("unrecognized header: %s", buffer);
54 continue;
56 delim = buffer[40 + offset];
57 if (!isspace(delim) && (delim != '\0' || !is_prereq))
58 die ("invalid header: %s", buffer);
59 add_to_ref_list(sha1, isspace(delim) ?
60 buffer + 41 + offset : "", list);
62 fpos = ftell(ffd);
63 fclose(ffd);
64 fd = open(path, O_RDONLY);
65 if (fd < 0)
66 return error("could not open '%s'", path);
67 lseek(fd, fpos, SEEK_SET);
68 return fd;
71 static int list_refs(struct ref_list *r, int argc, const char **argv)
73 int i;
75 for (i = 0; i < r->nr; i++) {
76 if (argc > 1) {
77 int j;
78 for (j = 1; j < argc; j++)
79 if (!strcmp(r->list[i].name, argv[j]))
80 break;
81 if (j == argc)
82 continue;
84 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
85 r->list[i].name);
87 return 0;
90 #define PREREQ_MARK (1u<<16)
92 int verify_bundle(struct bundle_header *header, int verbose)
95 * Do fast check, then if any prereqs are missing then go line by line
96 * to be verbose about the errors
98 struct ref_list *p = &header->prerequisites;
99 struct rev_info revs;
100 const char *argv[] = {NULL, "--all"};
101 struct object_array refs;
102 struct commit *commit;
103 int i, ret = 0, req_nr;
104 const char *message = "Repository lacks these prerequisite commits:";
106 init_revisions(&revs, NULL);
107 for (i = 0; i < p->nr; i++) {
108 struct ref_list_entry *e = p->list + i;
109 struct object *o = parse_object(e->sha1);
110 if (o) {
111 o->flags |= PREREQ_MARK;
112 add_pending_object(&revs, o, e->name);
113 continue;
115 if (++ret == 1)
116 error(message);
117 error("%s %s", sha1_to_hex(e->sha1), e->name);
119 if (revs.pending.nr != p->nr)
120 return ret;
121 req_nr = revs.pending.nr;
122 setup_revisions(2, argv, &revs, NULL);
124 memset(&refs, 0, sizeof(struct object_array));
125 for (i = 0; i < revs.pending.nr; i++) {
126 struct object_array_entry *e = revs.pending.objects + i;
127 add_object_array(e->item, e->name, &refs);
130 prepare_revision_walk(&revs);
132 i = req_nr;
133 while (i && (commit = get_revision(&revs)))
134 if (commit->object.flags & PREREQ_MARK)
135 i--;
137 for (i = 0; i < req_nr; i++)
138 if (!(refs.objects[i].item->flags & SHOWN)) {
139 if (++ret == 1)
140 error(message);
141 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
142 refs.objects[i].name);
145 for (i = 0; i < refs.nr; i++)
146 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
148 if (verbose) {
149 struct ref_list *r;
151 r = &header->references;
152 printf("The bundle contains %d ref%s\n",
153 r->nr, (1 < r->nr) ? "s" : "");
154 list_refs(r, 0, NULL);
155 r = &header->prerequisites;
156 printf("The bundle requires these %d ref%s\n",
157 r->nr, (1 < r->nr) ? "s" : "");
158 list_refs(r, 0, NULL);
160 return ret;
163 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
165 return list_refs(&header->references, argc, argv);
168 int create_bundle(struct bundle_header *header, const char *path,
169 int argc, const char **argv)
171 static struct lock_file lock;
172 int bundle_fd = -1;
173 int bundle_to_stdout;
174 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
175 const char **argv_pack = xmalloc(5 * sizeof(const char *));
176 int i, ref_count = 0;
177 char buffer[1024];
178 struct rev_info revs;
179 struct child_process rls;
180 FILE *rls_fout;
182 bundle_to_stdout = !strcmp(path, "-");
183 if (bundle_to_stdout)
184 bundle_fd = 1;
185 else
186 bundle_fd = hold_lock_file_for_update(&lock, path, 1);
188 /* write signature */
189 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
191 /* init revs to list objects for pack-objects later */
192 save_commit_buffer = 0;
193 init_revisions(&revs, NULL);
195 /* write prerequisites */
196 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
197 argv_boundary[0] = "rev-list";
198 argv_boundary[1] = "--boundary";
199 argv_boundary[2] = "--pretty=oneline";
200 argv_boundary[argc + 2] = NULL;
201 memset(&rls, 0, sizeof(rls));
202 rls.argv = argv_boundary;
203 rls.out = -1;
204 rls.git_cmd = 1;
205 if (start_command(&rls))
206 return -1;
207 rls_fout = fdopen(rls.out, "r");
208 while (fgets(buffer, sizeof(buffer), rls_fout)) {
209 unsigned char sha1[20];
210 if (buffer[0] == '-') {
211 write_or_die(bundle_fd, buffer, strlen(buffer));
212 if (!get_sha1_hex(buffer + 1, sha1)) {
213 struct object *object = parse_object(sha1);
214 object->flags |= UNINTERESTING;
215 add_pending_object(&revs, object, buffer);
217 } else if (!get_sha1_hex(buffer, sha1)) {
218 struct object *object = parse_object(sha1);
219 object->flags |= SHOWN;
222 fclose(rls_fout);
223 if (finish_command(&rls))
224 return error("rev-list died");
226 /* write references */
227 argc = setup_revisions(argc, argv, &revs, NULL);
228 if (argc > 1)
229 return error("unrecognized argument: %s'", argv[1]);
231 for (i = 0; i < revs.pending.nr; i++) {
232 struct object_array_entry *e = revs.pending.objects + i;
233 unsigned char sha1[20];
234 char *ref;
236 if (e->item->flags & UNINTERESTING)
237 continue;
238 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
239 continue;
241 * Make sure the refs we wrote out is correct; --max-count and
242 * other limiting options could have prevented all the tips
243 * from getting output.
245 * Non commit objects such as tags and blobs do not have
246 * this issue as they are not affected by those extra
247 * constraints.
249 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
250 warning("ref '%s' is excluded by the rev-list options",
251 e->name);
252 free(ref);
253 continue;
256 * If you run "git bundle create bndl v1.0..v2.0", the
257 * name of the positive ref is "v2.0" but that is the
258 * commit that is referenced by the tag, and not the tag
259 * itself.
261 if (hashcmp(sha1, e->item->sha1)) {
263 * Is this the positive end of a range expressed
264 * in terms of a tag (e.g. v2.0 from the range
265 * "v1.0..v2.0")?
267 struct commit *one = lookup_commit_reference(sha1);
268 struct object *obj;
270 if (e->item == &(one->object)) {
272 * Need to include e->name as an
273 * independent ref to the pack-objects
274 * input, so that the tag is included
275 * in the output; otherwise we would
276 * end up triggering "empty bundle"
277 * error.
279 obj = parse_object(sha1);
280 obj->flags |= SHOWN;
281 add_pending_object(&revs, obj, e->name);
283 free(ref);
284 continue;
287 ref_count++;
288 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
289 write_or_die(bundle_fd, " ", 1);
290 write_or_die(bundle_fd, ref, strlen(ref));
291 write_or_die(bundle_fd, "\n", 1);
292 free(ref);
294 if (!ref_count)
295 die ("Refusing to create empty bundle.");
297 /* end header */
298 write_or_die(bundle_fd, "\n", 1);
300 /* write pack */
301 argv_pack[0] = "pack-objects";
302 argv_pack[1] = "--all-progress";
303 argv_pack[2] = "--stdout";
304 argv_pack[3] = "--thin";
305 argv_pack[4] = NULL;
306 memset(&rls, 0, sizeof(rls));
307 rls.argv = argv_pack;
308 rls.in = -1;
309 rls.out = bundle_fd;
310 rls.git_cmd = 1;
311 if (start_command(&rls))
312 return error("Could not spawn pack-objects");
313 for (i = 0; i < revs.pending.nr; i++) {
314 struct object *object = revs.pending.objects[i].item;
315 if (object->flags & UNINTERESTING)
316 write(rls.in, "^", 1);
317 write(rls.in, sha1_to_hex(object->sha1), 40);
318 write(rls.in, "\n", 1);
320 if (finish_command(&rls))
321 return error ("pack-objects died");
322 close(bundle_fd);
323 if (!bundle_to_stdout)
324 commit_lock_file(&lock);
325 return 0;
328 int unbundle(struct bundle_header *header, int bundle_fd)
330 const char *argv_index_pack[] = {"index-pack",
331 "--fix-thin", "--stdin", NULL};
332 struct child_process ip;
334 if (verify_bundle(header, 0))
335 return -1;
336 memset(&ip, 0, sizeof(ip));
337 ip.argv = argv_index_pack;
338 ip.in = bundle_fd;
339 ip.no_stdout = 1;
340 ip.git_cmd = 1;
341 if (run_command(&ip))
342 return error("index-pack died");
343 return 0;