]>
Commit | Line | Data |
---|---|---|
2e0afafe JS |
1 | #include "cache.h" |
2 | #include "object.h" | |
3 | #include "commit.h" | |
4 | #include "diff.h" | |
5 | #include "revision.h" | |
6 | #include "list-objects.h" | |
b1daf300 | 7 | #include "run-command.h" |
2e0afafe JS |
8 | |
9 | /* | |
10 | * Basic handler for bundle files to connect repositories via sneakernet. | |
11 | * Invocation must include action. | |
12 | * This function can create a bundle or provide information on an existing | |
13 | * bundle supporting git-fetch, git-pull, and git-ls-remote | |
14 | */ | |
15 | ||
fa257b05 | 16 | static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )"; |
2e0afafe JS |
17 | |
18 | static const char bundle_signature[] = "# v2 git bundle\n"; | |
19 | ||
20 | struct ref_list { | |
21 | unsigned int nr, alloc; | |
fb9a5415 | 22 | struct ref_list_entry { |
2e0afafe JS |
23 | unsigned char sha1[20]; |
24 | char *name; | |
25 | } *list; | |
26 | }; | |
27 | ||
28 | static void add_to_ref_list(const unsigned char *sha1, const char *name, | |
29 | struct ref_list *list) | |
30 | { | |
31 | if (list->nr + 1 >= list->alloc) { | |
32 | list->alloc = alloc_nr(list->nr + 1); | |
33 | list->list = xrealloc(list->list, | |
34 | list->alloc * sizeof(list->list[0])); | |
35 | } | |
36 | memcpy(list->list[list->nr].sha1, sha1, 20); | |
37 | list->list[list->nr].name = xstrdup(name); | |
38 | list->nr++; | |
39 | } | |
40 | ||
41 | struct bundle_header { | |
fa257b05 JS |
42 | struct ref_list prerequisites; |
43 | struct ref_list references; | |
2e0afafe JS |
44 | }; |
45 | ||
46 | /* this function returns the length of the string */ | |
47 | static int read_string(int fd, char *buffer, int size) | |
48 | { | |
49 | int i; | |
50 | for (i = 0; i < size - 1; i++) { | |
8a912bcb | 51 | ssize_t count = xread(fd, buffer + i, 1); |
2e0afafe JS |
52 | if (count < 0) |
53 | return error("Read error: %s", strerror(errno)); | |
54 | if (count == 0) { | |
55 | i--; | |
56 | break; | |
57 | } | |
58 | if (buffer[i] == '\n') | |
59 | break; | |
60 | } | |
61 | buffer[i + 1] = '\0'; | |
62 | return i + 1; | |
63 | } | |
64 | ||
65 | /* returns an fd */ | |
66 | static int read_header(const char *path, struct bundle_header *header) { | |
67 | char buffer[1024]; | |
68 | int fd = open(path, O_RDONLY); | |
69 | ||
70 | if (fd < 0) | |
71 | return error("could not open '%s'", path); | |
72 | if (read_string(fd, buffer, sizeof(buffer)) < 0 || | |
73 | strcmp(buffer, bundle_signature)) { | |
74 | close(fd); | |
75 | return error("'%s' does not look like a v2 bundle file", path); | |
76 | } | |
77 | while (read_string(fd, buffer, sizeof(buffer)) > 0 | |
78 | && buffer[0] != '\n') { | |
fa257b05 JS |
79 | int is_prereq = buffer[0] == '-'; |
80 | int offset = is_prereq ? 1 : 0; | |
2e0afafe JS |
81 | int len = strlen(buffer); |
82 | unsigned char sha1[20]; | |
fa257b05 | 83 | struct ref_list *list = is_prereq ? &header->prerequisites |
2e0afafe | 84 | : &header->references; |
fa257b05 JS |
85 | char delim; |
86 | ||
2e0afafe JS |
87 | if (buffer[len - 1] == '\n') |
88 | buffer[len - 1] = '\0'; | |
fa257b05 | 89 | if (get_sha1_hex(buffer + offset, sha1)) { |
46efd2d9 | 90 | warning("unrecognized header: %s", buffer); |
fa257b05 JS |
91 | continue; |
92 | } | |
93 | delim = buffer[40 + offset]; | |
94 | if (!isspace(delim) && (delim != '\0' || !is_prereq)) | |
95 | die ("invalid header: %s", buffer); | |
96 | add_to_ref_list(sha1, isspace(delim) ? | |
97 | buffer + 41 + offset : "", list); | |
2e0afafe JS |
98 | } |
99 | return fd; | |
100 | } | |
101 | ||
80e25cee JH |
102 | static int list_refs(struct ref_list *r, int argc, const char **argv) |
103 | { | |
104 | int i; | |
105 | ||
106 | for (i = 0; i < r->nr; i++) { | |
107 | if (argc > 1) { | |
108 | int j; | |
109 | for (j = 1; j < argc; j++) | |
110 | if (!strcmp(r->list[i].name, argv[j])) | |
111 | break; | |
112 | if (j == argc) | |
113 | continue; | |
114 | } | |
115 | printf("%s %s\n", sha1_to_hex(r->list[i].sha1), | |
116 | r->list[i].name); | |
117 | } | |
118 | return 0; | |
119 | } | |
120 | ||
2b064697 JH |
121 | #define PREREQ_MARK (1u<<16) |
122 | ||
80e25cee | 123 | static int verify_bundle(struct bundle_header *header, int verbose) |
2e0afafe JS |
124 | { |
125 | /* | |
126 | * Do fast check, then if any prereqs are missing then go line by line | |
127 | * to be verbose about the errors | |
128 | */ | |
129 | struct ref_list *p = &header->prerequisites; | |
fb9a5415 JS |
130 | struct rev_info revs; |
131 | const char *argv[] = {NULL, "--all"}; | |
132 | struct object_array refs; | |
133 | struct commit *commit; | |
134 | int i, ret = 0, req_nr; | |
64d99e9c | 135 | const char *message = "Repository lacks these prerequisite commits:"; |
fb9a5415 JS |
136 | |
137 | init_revisions(&revs, NULL); | |
138 | for (i = 0; i < p->nr; i++) { | |
139 | struct ref_list_entry *e = p->list + i; | |
140 | struct object *o = parse_object(e->sha1); | |
141 | if (o) { | |
2b064697 | 142 | o->flags |= PREREQ_MARK; |
fb9a5415 JS |
143 | add_pending_object(&revs, o, e->name); |
144 | continue; | |
145 | } | |
146 | if (++ret == 1) | |
147 | error(message); | |
148 | error("%s %s", sha1_to_hex(e->sha1), e->name); | |
149 | } | |
c2dea5a1 | 150 | if (revs.pending.nr != p->nr) |
fb9a5415 JS |
151 | return ret; |
152 | req_nr = revs.pending.nr; | |
153 | setup_revisions(2, argv, &revs, NULL); | |
154 | ||
155 | memset(&refs, 0, sizeof(struct object_array)); | |
156 | for (i = 0; i < revs.pending.nr; i++) { | |
157 | struct object_array_entry *e = revs.pending.objects + i; | |
158 | add_object_array(e->item, e->name, &refs); | |
159 | } | |
160 | ||
161 | prepare_revision_walk(&revs); | |
162 | ||
163 | i = req_nr; | |
164 | while (i && (commit = get_revision(&revs))) | |
2b064697 | 165 | if (commit->object.flags & PREREQ_MARK) |
fb9a5415 JS |
166 | i--; |
167 | ||
168 | for (i = 0; i < req_nr; i++) | |
169 | if (!(refs.objects[i].item->flags & SHOWN)) { | |
170 | if (++ret == 1) | |
171 | error(message); | |
172 | error("%s %s", sha1_to_hex(refs.objects[i].item->sha1), | |
173 | refs.objects[i].name); | |
174 | } | |
2e0afafe | 175 | |
fb9a5415 JS |
176 | for (i = 0; i < refs.nr; i++) |
177 | clear_commit_marks((struct commit *)refs.objects[i].item, -1); | |
2e0afafe | 178 | |
80e25cee JH |
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 | } | |
2e0afafe JS |
191 | return ret; |
192 | } | |
193 | ||
194 | static int list_heads(struct bundle_header *header, int argc, const char **argv) | |
195 | { | |
80e25cee | 196 | return list_refs(&header->references, argc, argv); |
2e0afafe JS |
197 | } |
198 | ||
2e0afafe JS |
199 | static int create_bundle(struct bundle_header *header, const char *path, |
200 | int argc, const char **argv) | |
201 | { | |
202 | int bundle_fd = -1; | |
23929677 | 203 | const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *)); |
9e64d109 | 204 | const char **argv_pack = xmalloc(5 * sizeof(const char *)); |
b1daf300 | 205 | int i, ref_count = 0; |
2e0afafe JS |
206 | char buffer[1024]; |
207 | struct rev_info revs; | |
b1daf300 | 208 | struct child_process rls; |
2e0afafe JS |
209 | |
210 | bundle_fd = (!strcmp(path, "-") ? 1 : | |
2e578f9a | 211 | open(path, O_CREAT | O_EXCL | O_WRONLY, 0666)); |
2e0afafe | 212 | if (bundle_fd < 0) |
2e578f9a | 213 | return error("Could not create '%s': %s", path, strerror(errno)); |
2e0afafe JS |
214 | |
215 | /* write signature */ | |
fa257b05 | 216 | write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature)); |
2e0afafe | 217 | |
18449ab0 JS |
218 | /* init revs to list objects for pack-objects later */ |
219 | save_commit_buffer = 0; | |
220 | init_revisions(&revs, NULL); | |
221 | ||
2e0afafe | 222 | /* write prerequisites */ |
23929677 | 223 | memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *)); |
2e0afafe JS |
224 | argv_boundary[0] = "rev-list"; |
225 | argv_boundary[1] = "--boundary"; | |
23929677 JS |
226 | argv_boundary[2] = "--pretty=oneline"; |
227 | argv_boundary[argc + 2] = NULL; | |
b1daf300 SP |
228 | memset(&rls, 0, sizeof(rls)); |
229 | rls.argv = argv_boundary; | |
230 | rls.out = -1; | |
231 | rls.git_cmd = 1; | |
232 | if (start_command(&rls)) | |
2e0afafe | 233 | return -1; |
b1daf300 | 234 | while ((i = read_string(rls.out, buffer, sizeof(buffer))) > 0) { |
9e64d109 | 235 | unsigned char sha1[20]; |
18449ab0 | 236 | if (buffer[0] == '-') { |
fa257b05 | 237 | write_or_die(bundle_fd, buffer, i); |
18449ab0 JS |
238 | if (!get_sha1_hex(buffer + 1, sha1)) { |
239 | struct object *object = parse_object(sha1); | |
240 | object->flags |= UNINTERESTING; | |
241 | add_pending_object(&revs, object, buffer); | |
242 | } | |
9e64d109 JS |
243 | } else if (!get_sha1_hex(buffer, sha1)) { |
244 | struct object *object = parse_object(sha1); | |
245 | object->flags |= SHOWN; | |
18449ab0 | 246 | } |
9e64d109 | 247 | } |
b1daf300 SP |
248 | if (finish_command(&rls)) |
249 | return error("rev-list died"); | |
2e0afafe JS |
250 | |
251 | /* write references */ | |
2e0afafe JS |
252 | argc = setup_revisions(argc, argv, &revs, NULL); |
253 | if (argc > 1) | |
254 | return error("unrecognized argument: %s'", argv[1]); | |
c2dea5a1 | 255 | |
2e0afafe JS |
256 | for (i = 0; i < revs.pending.nr; i++) { |
257 | struct object_array_entry *e = revs.pending.objects + i; | |
c2dea5a1 JH |
258 | unsigned char sha1[20]; |
259 | char *ref; | |
260 | ||
261 | if (e->item->flags & UNINTERESTING) | |
262 | continue; | |
263 | if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1) | |
264 | continue; | |
9e64d109 JS |
265 | /* |
266 | * Make sure the refs we wrote out is correct; --max-count and | |
267 | * other limiting options could have prevented all the tips | |
268 | * from getting output. | |
269 | */ | |
34572ed2 | 270 | if (!(e->item->flags & SHOWN)) { |
46efd2d9 | 271 | warning("ref '%s' is excluded by the rev-list options", |
9e64d109 | 272 | e->name); |
34572ed2 JS |
273 | continue; |
274 | } | |
275 | ref_count++; | |
c2dea5a1 JH |
276 | write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40); |
277 | write_or_die(bundle_fd, " ", 1); | |
278 | write_or_die(bundle_fd, ref, strlen(ref)); | |
279 | write_or_die(bundle_fd, "\n", 1); | |
c2dea5a1 | 280 | free(ref); |
2e0afafe | 281 | } |
34572ed2 JS |
282 | if (!ref_count) |
283 | die ("Refusing to create empty bundle."); | |
2e0afafe JS |
284 | |
285 | /* end header */ | |
fa257b05 | 286 | write_or_die(bundle_fd, "\n", 1); |
2e0afafe JS |
287 | |
288 | /* write pack */ | |
289 | argv_pack[0] = "pack-objects"; | |
290 | argv_pack[1] = "--all-progress"; | |
291 | argv_pack[2] = "--stdout"; | |
9e64d109 JS |
292 | argv_pack[3] = "--thin"; |
293 | argv_pack[4] = NULL; | |
b1daf300 SP |
294 | memset(&rls, 0, sizeof(rls)); |
295 | rls.argv = argv_pack; | |
296 | rls.in = -1; | |
297 | rls.out = bundle_fd; | |
298 | rls.git_cmd = 1; | |
299 | if (start_command(&rls)) | |
2e0afafe | 300 | return error("Could not spawn pack-objects"); |
9e64d109 JS |
301 | for (i = 0; i < revs.pending.nr; i++) { |
302 | struct object *object = revs.pending.objects[i].item; | |
303 | if (object->flags & UNINTERESTING) | |
b1daf300 SP |
304 | write(rls.in, "^", 1); |
305 | write(rls.in, sha1_to_hex(object->sha1), 40); | |
306 | write(rls.in, "\n", 1); | |
9e64d109 | 307 | } |
b1daf300 | 308 | if (finish_command(&rls)) |
2e0afafe | 309 | return error ("pack-objects died"); |
b1daf300 | 310 | return 0; |
2e0afafe JS |
311 | } |
312 | ||
313 | static int unbundle(struct bundle_header *header, int bundle_fd, | |
314 | int argc, const char **argv) | |
315 | { | |
263703ff JS |
316 | const char *argv_index_pack[] = {"index-pack", |
317 | "--fix-thin", "--stdin", NULL}; | |
b1daf300 | 318 | struct child_process ip; |
2e0afafe | 319 | |
80e25cee | 320 | if (verify_bundle(header, 0)) |
2e0afafe | 321 | return -1; |
b1daf300 SP |
322 | memset(&ip, 0, sizeof(ip)); |
323 | ip.argv = argv_index_pack; | |
324 | ip.in = bundle_fd; | |
325 | ip.no_stdout = 1; | |
326 | ip.git_cmd = 1; | |
327 | if (run_command(&ip)) | |
328 | return error("index-pack died"); | |
2e0afafe JS |
329 | return list_heads(header, argc, argv); |
330 | } | |
331 | ||
332 | int cmd_bundle(int argc, const char **argv, const char *prefix) | |
333 | { | |
334 | struct bundle_header header; | |
335 | int nongit = 0; | |
336 | const char *cmd, *bundle_file; | |
337 | int bundle_fd = -1; | |
338 | char buffer[PATH_MAX]; | |
339 | ||
340 | if (argc < 3) | |
341 | usage(bundle_usage); | |
342 | ||
343 | cmd = argv[1]; | |
344 | bundle_file = argv[2]; | |
345 | argc -= 2; | |
346 | argv += 2; | |
347 | ||
348 | prefix = setup_git_directory_gently(&nongit); | |
349 | if (prefix && bundle_file[0] != '/') { | |
350 | snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file); | |
351 | bundle_file = buffer; | |
352 | } | |
353 | ||
354 | memset(&header, 0, sizeof(header)); | |
355 | if (strcmp(cmd, "create") && | |
8315588b | 356 | (bundle_fd = read_header(bundle_file, &header)) < 0) |
2e0afafe JS |
357 | return 1; |
358 | ||
359 | if (!strcmp(cmd, "verify")) { | |
360 | close(bundle_fd); | |
80e25cee | 361 | if (verify_bundle(&header, 1)) |
2e0afafe JS |
362 | return 1; |
363 | fprintf(stderr, "%s is okay\n", bundle_file); | |
364 | return 0; | |
365 | } | |
366 | if (!strcmp(cmd, "list-heads")) { | |
367 | close(bundle_fd); | |
368 | return !!list_heads(&header, argc, argv); | |
369 | } | |
370 | if (!strcmp(cmd, "create")) { | |
371 | if (nongit) | |
372 | die("Need a repository to create a bundle."); | |
373 | return !!create_bundle(&header, bundle_file, argc, argv); | |
374 | } else if (!strcmp(cmd, "unbundle")) { | |
375 | if (nongit) | |
376 | die("Need a repository to unbundle."); | |
377 | return !!unbundle(&header, bundle_fd, argc, argv); | |
378 | } else | |
379 | usage(bundle_usage); | |
380 | } |