6 #include "list-objects.h"
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
16 static const char *bundle_usage
="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
18 static const char bundle_signature
[] = "# v2 git bundle\n";
21 unsigned int nr
, alloc
;
22 struct ref_list_entry
{
23 unsigned char sha1
[20];
28 static void add_to_ref_list(const unsigned char *sha1
, const char *name
,
29 struct ref_list
*list
)
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]));
36 memcpy(list
->list
[list
->nr
].sha1
, sha1
, 20);
37 list
->list
[list
->nr
].name
= xstrdup(name
);
41 struct bundle_header
{
42 struct ref_list prerequisites
;
43 struct ref_list references
;
46 /* this function returns the length of the string */
47 static int read_string(int fd
, char *buffer
, int size
)
50 for (i
= 0; i
< size
- 1; i
++) {
51 int count
= xread(fd
, buffer
+ i
, 1);
53 return error("Read error: %s", strerror(errno
));
58 if (buffer
[i
] == '\n')
66 static int read_header(const char *path
, struct bundle_header
*header
) {
68 int fd
= open(path
, O_RDONLY
);
71 return error("could not open '%s'", path
);
72 if (read_string(fd
, buffer
, sizeof(buffer
)) < 0 ||
73 strcmp(buffer
, bundle_signature
)) {
75 return error("'%s' does not look like a v2 bundle file", path
);
77 while (read_string(fd
, buffer
, sizeof(buffer
)) > 0
78 && buffer
[0] != '\n') {
79 int is_prereq
= buffer
[0] == '-';
80 int offset
= is_prereq
? 1 : 0;
81 int len
= strlen(buffer
);
82 unsigned char sha1
[20];
83 struct ref_list
*list
= is_prereq
? &header
->prerequisites
84 : &header
->references
;
87 if (buffer
[len
- 1] == '\n')
88 buffer
[len
- 1] = '\0';
89 if (get_sha1_hex(buffer
+ offset
, sha1
)) {
90 warn("unrecognized header: %s", buffer
);
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
);
102 /* if in && *in >= 0, take that as input file descriptor instead */
103 static int fork_with_pipe(const char **argv
, int *in
, int *out
)
105 int needs_in
, needs_out
;
106 int fdin
[2], fdout
[2], pid
;
108 needs_in
= in
&& *in
< 0;
111 return error("could not setup pipe");
115 needs_out
= out
&& *out
< 0;
118 return error("could not setup pipe");
122 if ((pid
= fork()) < 0) {
131 return error("could not fork");
150 exit(execv_git_cmd(argv
));
163 static int list_refs(struct ref_list
*r
, int argc
, const char **argv
)
167 for (i
= 0; i
< r
->nr
; i
++) {
170 for (j
= 1; j
< argc
; j
++)
171 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
176 printf("%s %s\n", sha1_to_hex(r
->list
[i
].sha1
),
182 #define PREREQ_MARK (1u<<16)
184 static int verify_bundle(struct bundle_header
*header
, int verbose
)
187 * Do fast check, then if any prereqs are missing then go line by line
188 * to be verbose about the errors
190 struct ref_list
*p
= &header
->prerequisites
;
191 struct rev_info revs
;
192 const char *argv
[] = {NULL
, "--all"};
193 struct object_array refs
;
194 struct commit
*commit
;
195 int i
, ret
= 0, req_nr
;
196 const char *message
= "Repository lacks these prerequisite commits:";
198 init_revisions(&revs
, NULL
);
199 for (i
= 0; i
< p
->nr
; i
++) {
200 struct ref_list_entry
*e
= p
->list
+ i
;
201 struct object
*o
= parse_object(e
->sha1
);
203 o
->flags
|= PREREQ_MARK
;
204 add_pending_object(&revs
, o
, e
->name
);
209 error("%s %s", sha1_to_hex(e
->sha1
), e
->name
);
211 if (revs
.pending
.nr
!= p
->nr
)
213 req_nr
= revs
.pending
.nr
;
214 setup_revisions(2, argv
, &revs
, NULL
);
216 memset(&refs
, 0, sizeof(struct object_array
));
217 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
218 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
219 add_object_array(e
->item
, e
->name
, &refs
);
222 prepare_revision_walk(&revs
);
225 while (i
&& (commit
= get_revision(&revs
)))
226 if (commit
->object
.flags
& PREREQ_MARK
)
229 for (i
= 0; i
< req_nr
; i
++)
230 if (!(refs
.objects
[i
].item
->flags
& SHOWN
)) {
233 error("%s %s", sha1_to_hex(refs
.objects
[i
].item
->sha1
),
234 refs
.objects
[i
].name
);
237 for (i
= 0; i
< refs
.nr
; i
++)
238 clear_commit_marks((struct commit
*)refs
.objects
[i
].item
, -1);
243 r
= &header
->references
;
244 printf("The bundle contains %d ref%s\n",
245 r
->nr
, (1 < r
->nr
) ? "s" : "");
246 list_refs(r
, 0, NULL
);
247 r
= &header
->prerequisites
;
248 printf("The bundle requires these %d ref%s\n",
249 r
->nr
, (1 < r
->nr
) ? "s" : "");
250 list_refs(r
, 0, NULL
);
255 static int list_heads(struct bundle_header
*header
, int argc
, const char **argv
)
257 return list_refs(&header
->references
, argc
, argv
);
260 static void show_commit(struct commit
*commit
)
262 write_or_die(1, sha1_to_hex(commit
->object
.sha1
), 40);
263 write_or_die(1, "\n", 1);
264 if (commit
->parents
) {
265 free_commit_list(commit
->parents
);
266 commit
->parents
= NULL
;
270 static void show_object(struct object_array_entry
*p
)
272 /* An object with name "foo\n0000000..." can be used to
273 * confuse downstream git-pack-objects very badly.
275 const char *ep
= strchr(p
->name
, '\n');
276 int len
= ep
? ep
- p
->name
: strlen(p
->name
);
277 write_or_die(1, sha1_to_hex(p
->item
->sha1
), 40);
278 write_or_die(1, " ", 1);
280 write_or_die(1, p
->name
, len
);
281 write_or_die(1, "\n", 1);
284 static void show_edge(struct commit
*commit
)
286 ; /* nothing to do */
289 static int create_bundle(struct bundle_header
*header
, const char *path
,
290 int argc
, const char **argv
)
293 const char **argv_boundary
= xmalloc((argc
+ 4) * sizeof(const char *));
294 const char **argv_pack
= xmalloc(4 * sizeof(const char *));
295 int pid
, in
, out
, i
, status
;
297 struct rev_info revs
;
298 struct object_array tips
;
300 bundle_fd
= (!strcmp(path
, "-") ? 1 :
301 open(path
, O_CREAT
| O_WRONLY
, 0666));
303 return error("Could not write to '%s'", path
);
305 /* write signature */
306 write_or_die(bundle_fd
, bundle_signature
, strlen(bundle_signature
));
308 /* write prerequisites */
309 memcpy(argv_boundary
+ 3, argv
+ 1, argc
* sizeof(const char *));
310 argv_boundary
[0] = "rev-list";
311 argv_boundary
[1] = "--boundary";
312 argv_boundary
[2] = "--pretty=oneline";
313 argv_boundary
[argc
+ 2] = NULL
;
315 pid
= fork_with_pipe(argv_boundary
, NULL
, &out
);
318 while ((i
= read_string(out
, buffer
, sizeof(buffer
))) > 0)
319 if (buffer
[0] == '-')
320 write_or_die(bundle_fd
, buffer
, i
);
321 while ((i
= waitpid(pid
, &status
, 0)) < 0)
323 return error("rev-list died");
324 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
325 return error("rev-list died %d", WEXITSTATUS(status
));
327 /* write references */
328 save_commit_buffer
= 0;
329 init_revisions(&revs
, NULL
);
330 revs
.tag_objects
= 1;
331 revs
.tree_objects
= 1;
332 revs
.blob_objects
= 1;
333 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
335 return error("unrecognized argument: %s'", argv
[1]);
337 memset(&tips
, 0, sizeof(tips
));
338 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
339 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
340 unsigned char sha1
[20];
343 if (e
->item
->flags
& UNINTERESTING
)
345 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
347 write_or_die(bundle_fd
, sha1_to_hex(e
->item
->sha1
), 40);
348 write_or_die(bundle_fd
, " ", 1);
349 write_or_die(bundle_fd
, ref
, strlen(ref
));
350 write_or_die(bundle_fd
, "\n", 1);
351 add_object_array(e
->item
, e
->name
, &tips
);
356 write_or_die(bundle_fd
, "\n", 1);
359 argv_pack
[0] = "pack-objects";
360 argv_pack
[1] = "--all-progress";
361 argv_pack
[2] = "--stdout";
365 pid
= fork_with_pipe(argv_pack
, &in
, &out
);
367 return error("Could not spawn pack-objects");
371 prepare_revision_walk(&revs
);
372 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
373 traverse_commit_list(&revs
, show_commit
, show_object
);
375 while (waitpid(pid
, &status
, 0) < 0)
378 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
379 return error ("pack-objects died");
382 * Make sure the refs we wrote out is correct; --max-count and
383 * other limiting options could have prevented all the tips
384 * from getting output.
387 for (i
= 0; i
< tips
.nr
; i
++) {
388 if (!(tips
.objects
[i
].item
->flags
& SHOWN
)) {
390 error("%s: not included in the resulting pack",
391 tips
.objects
[i
].name
);
398 static int unbundle(struct bundle_header
*header
, int bundle_fd
,
399 int argc
, const char **argv
)
401 const char *argv_index_pack
[] = {"index-pack", "--stdin", NULL
};
402 int pid
, status
, dev_null
;
404 if (verify_bundle(header
, 0))
406 dev_null
= open("/dev/null", O_WRONLY
);
407 pid
= fork_with_pipe(argv_index_pack
, &bundle_fd
, &dev_null
);
409 return error("Could not spawn index-pack");
410 while (waitpid(pid
, &status
, 0) < 0)
412 return error("index-pack died");
413 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
414 return error("index-pack exited with status %d",
415 WEXITSTATUS(status
));
416 return list_heads(header
, argc
, argv
);
419 int cmd_bundle(int argc
, const char **argv
, const char *prefix
)
421 struct bundle_header header
;
423 const char *cmd
, *bundle_file
;
425 char buffer
[PATH_MAX
];
431 bundle_file
= argv
[2];
435 prefix
= setup_git_directory_gently(&nongit
);
436 if (prefix
&& bundle_file
[0] != '/') {
437 snprintf(buffer
, sizeof(buffer
), "%s/%s", prefix
, bundle_file
);
438 bundle_file
= buffer
;
441 memset(&header
, 0, sizeof(header
));
442 if (strcmp(cmd
, "create") &&
443 !(bundle_fd
= read_header(bundle_file
, &header
)))
446 if (!strcmp(cmd
, "verify")) {
448 if (verify_bundle(&header
, 1))
450 fprintf(stderr
, "%s is okay\n", bundle_file
);
453 if (!strcmp(cmd
, "list-heads")) {
455 return !!list_heads(&header
, argc
, argv
);
457 if (!strcmp(cmd
, "create")) {
459 die("Need a repository to create a bundle.");
460 return !!create_bundle(&header
, bundle_file
, argc
, argv
);
461 } else if (!strcmp(cmd
, "unbundle")) {
463 die("Need a repository to unbundle.");
464 return !!unbundle(&header
, bundle_fd
, argc
, argv
);