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