7 #include "list-objects.h"
8 #include "run-command.h"
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
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";
23 unsigned int nr
, alloc
;
24 struct ref_list_entry
{
25 unsigned char sha1
[20];
30 static void add_to_ref_list(const unsigned char *sha1
, const char *name
,
31 struct ref_list
*list
)
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]));
38 memcpy(list
->list
[list
->nr
].sha1
, sha1
, 20);
39 list
->list
[list
->nr
].name
= xstrdup(name
);
43 struct bundle_header
{
44 struct ref_list prerequisites
;
45 struct ref_list references
;
49 static int read_header(const char *path
, struct bundle_header
*header
) {
53 FILE *ffd
= fopen(path
, "rb");
56 return error("could not open '%s'", path
);
57 if (!fgets(buffer
, sizeof(buffer
), ffd
) ||
58 strcmp(buffer
, bundle_signature
)) {
60 return error("'%s' does not look like a v2 bundle file", path
);
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
;
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
);
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
);
86 fd
= open(path
, O_RDONLY
);
88 return error("could not open '%s'", path
);
89 lseek(fd
, fpos
, SEEK_SET
);
93 static int list_refs(struct ref_list
*r
, int argc
, const char **argv
)
97 for (i
= 0; i
< r
->nr
; i
++) {
100 for (j
= 1; j
< argc
; j
++)
101 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
106 printf("%s %s\n", sha1_to_hex(r
->list
[i
].sha1
),
112 #define PREREQ_MARK (1u<<16)
114 static int verify_bundle(struct bundle_header
*header
, int verbose
)
117 * Do fast check, then if any prereqs are missing then go line by line
118 * to be verbose about the errors
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
);
133 o
->flags
|= PREREQ_MARK
;
134 add_pending_object(&revs
, o
, e
->name
);
139 error("%s %s", sha1_to_hex(e
->sha1
), e
->name
);
141 if (revs
.pending
.nr
!= p
->nr
)
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
);
152 prepare_revision_walk(&revs
);
155 while (i
&& (commit
= get_revision(&revs
)))
156 if (commit
->object
.flags
& PREREQ_MARK
)
159 for (i
= 0; i
< req_nr
; i
++)
160 if (!(refs
.objects
[i
].item
->flags
& SHOWN
)) {
163 error("%s %s", sha1_to_hex(refs
.objects
[i
].item
->sha1
),
164 refs
.objects
[i
].name
);
167 for (i
= 0; i
< refs
.nr
; i
++)
168 clear_commit_marks((struct commit
*)refs
.objects
[i
].item
, -1);
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
);
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
;
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;
200 struct rev_info revs
;
201 struct child_process rls
;
204 bundle_to_stdout
= !strcmp(path
, "-");
205 if (bundle_to_stdout
)
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
;
227 if (start_command(&rls
))
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
);
239 } else if (!get_sha1_hex(buffer
, sha1
)) {
240 struct object
*object
= parse_object(sha1
);
241 object
->flags
|= SHOWN
;
245 if (finish_command(&rls
))
246 return error("rev-list died");
248 /* write references */
249 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
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];
257 const char *display_ref
;
260 if (e
->item
->flags
& UNINTERESTING
)
262 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
264 if (!resolve_ref(e
->name
, sha1
, 1, &flag
))
266 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
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.
273 * Non commit objects such as tags and blobs do not have
274 * this issue as they are not affected by those extra
277 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
278 warning("ref '%s' is excluded by the rev-list options",
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
289 if (hashcmp(sha1
, e
->item
->sha1
)) {
291 * Is this the positive end of a range expressed
292 * in terms of a tag (e.g. v2.0 from the range
295 struct commit
*one
= lookup_commit_reference(sha1
);
298 if (e
->item
== &(one
->object
)) {
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"
307 obj
= parse_object(sha1
);
309 add_pending_object(&revs
, obj
, e
->name
);
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);
323 die ("Refusing to create empty bundle.");
326 write_or_die(bundle_fd
, "\n", 1);
329 argv_pack
[0] = "pack-objects";
330 argv_pack
[1] = "--all-progress";
331 argv_pack
[2] = "--stdout";
332 argv_pack
[3] = "--thin";
334 memset(&rls
, 0, sizeof(rls
));
335 rls
.argv
= argv_pack
;
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);
348 if (finish_command(&rls
))
349 return error ("pack-objects died");
351 if (!bundle_to_stdout
)
352 commit_lock_file(&lock
);
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))
365 memset(&ip
, 0, sizeof(ip
));
366 ip
.argv
= argv_index_pack
;
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
;
379 const char *cmd
, *bundle_file
;
381 char buffer
[PATH_MAX
];
387 bundle_file
= 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
;
397 memset(&header
, 0, sizeof(header
));
398 if (strcmp(cmd
, "create") &&
399 (bundle_fd
= read_header(bundle_file
, &header
)) < 0)
402 if (!strcmp(cmd
, "verify")) {
404 if (verify_bundle(&header
, 1))
406 fprintf(stderr
, "%s is okay\n", bundle_file
);
409 if (!strcmp(cmd
, "list-heads")) {
411 return !!list_heads(&header
, argc
, argv
);
413 if (!strcmp(cmd
, "create")) {
415 die("Need a repository to create a bundle.");
416 return !!create_bundle(&header
, bundle_file
, argc
, argv
);
417 } else if (!strcmp(cmd
, "unbundle")) {
419 die("Need a repository to unbundle.");
420 return !!unbundle(&header
, bundle_fd
, argc
, argv
);