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
;
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
, references
;
45 /* this function returns the length of the string */
46 static int read_string(int fd
, char *buffer
, int size
)
49 for (i
= 0; i
< size
- 1; i
++) {
50 int count
= read(fd
, buffer
+ i
, 1);
52 return error("Read error: %s", strerror(errno
));
57 if (buffer
[i
] == '\n')
65 static int read_header(const char *path
, struct bundle_header
*header
) {
67 int fd
= open(path
, O_RDONLY
);
70 return error("could not open '%s'", path
);
71 if (read_string(fd
, buffer
, sizeof(buffer
)) < 0 ||
72 strcmp(buffer
, bundle_signature
)) {
74 return error("'%s' does not look like a v2 bundle file", path
);
76 while (read_string(fd
, buffer
, sizeof(buffer
)) > 0
77 && buffer
[0] != '\n') {
78 int offset
= buffer
[0] == '-' ? 1 : 0;
79 int len
= strlen(buffer
);
80 unsigned char sha1
[20];
81 struct ref_list
*list
= offset
? &header
->prerequisites
82 : &header
->references
;
83 if (get_sha1_hex(buffer
+ offset
, sha1
)) {
85 return error("invalid SHA1: %s", buffer
);
87 if (buffer
[len
- 1] == '\n')
88 buffer
[len
- 1] = '\0';
89 add_to_ref_list(sha1
, buffer
+ 41 + offset
, list
);
94 /* if in && *in >= 0, take that as input file descriptor instead */
95 static int fork_with_pipe(const char **argv
, int *in
, int *out
)
97 int needs_in
, needs_out
;
98 int fdin
[2], fdout
[2], pid
;
100 needs_in
= in
&& *in
< 0;
103 return error("could not setup pipe");
107 needs_out
= out
&& *out
< 0;
110 return error("could not setup pipe");
114 if ((pid
= fork()) < 0) {
123 return error("could not fork");
138 exit(execv_git_cmd(argv
));
147 static int verify_bundle(struct bundle_header
*header
)
150 * Do fast check, then if any prereqs are missing then go line by line
151 * to be verbose about the errors
153 struct ref_list
*p
= &header
->prerequisites
;
154 const char *argv
[5] = {"rev-list", "--stdin", "--not", "--all", NULL
};
155 int pid
, in
, out
, i
, ret
= 0;
159 pid
= fork_with_pipe(argv
, &in
, &out
);
161 return error("Could not fork rev-list");
163 for (i
= 0; i
< p
->nr
; i
++) {
164 write(in
, sha1_to_hex(p
->list
[i
].sha1
), 40);
171 while (read_string(out
, buffer
, sizeof(buffer
)) > 0) {
173 error ("The bundle requires the following commits you lack:");
174 fprintf(stderr
, "%s", buffer
);
177 while (waitpid(pid
, &i
, 0) < 0)
180 if (!ret
&& (!WIFEXITED(i
) || WEXITSTATUS(i
)))
181 return error("At least one prerequisite is lacking.");
186 static int list_heads(struct bundle_header
*header
, int argc
, const char **argv
)
189 struct ref_list
*r
= &header
->references
;
191 for (i
= 0; i
< r
->nr
; i
++) {
194 for (j
= 1; j
< argc
; j
++)
195 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
200 printf("%s %s\n", sha1_to_hex(r
->list
[i
].sha1
),
206 static void show_commit(struct commit
*commit
)
208 write(1, sha1_to_hex(commit
->object
.sha1
), 40);
210 if (commit
->parents
) {
211 free_commit_list(commit
->parents
);
212 commit
->parents
= NULL
;
216 static void show_object(struct object_array_entry
*p
)
218 /* An object with name "foo\n0000000..." can be used to
219 * * confuse downstream git-pack-objects very badly.
221 const char *ep
= strchr(p
->name
, '\n');
222 int len
= ep
? ep
- p
->name
: strlen(p
->name
);
223 write(1, sha1_to_hex(p
->item
->sha1
), 40);
226 write(1, p
->name
, len
);
230 static int create_bundle(struct bundle_header
*header
, const char *path
,
231 int argc
, const char **argv
)
234 const char **argv_boundary
= xmalloc((argc
+ 3) * sizeof(const char *));
235 const char **argv_pack
= xmalloc(4 * sizeof(const char *));
236 int pid
, in
, out
, i
, status
;
238 struct rev_info revs
;
240 bundle_fd
= (!strcmp(path
, "-") ? 1 :
241 open(path
, O_CREAT
| O_WRONLY
, 0666));
243 return error("Could not write to '%s'", path
);
245 /* write signature */
246 write(bundle_fd
, bundle_signature
, strlen(bundle_signature
));
248 /* write prerequisites */
249 memcpy(argv_boundary
+ 2, argv
+ 1, argc
* sizeof(const char *));
250 argv_boundary
[0] = "rev-list";
251 argv_boundary
[1] = "--boundary";
252 argv_boundary
[argc
+ 1] = NULL
;
254 pid
= fork_with_pipe(argv_boundary
, NULL
, &out
);
257 while ((i
= read_string(out
, buffer
, sizeof(buffer
))) > 0)
258 if (buffer
[0] == '-')
259 write(bundle_fd
, buffer
, i
);
260 while ((i
= waitpid(pid
, &status
, 0)) < 0)
262 return error("rev-list died");
263 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
264 return error("rev-list died %d", WEXITSTATUS(status
));
266 /* write references */
267 save_commit_buffer
= 0;
268 init_revisions(&revs
, NULL
);
269 revs
.tag_objects
= 1;
270 revs
.tree_objects
= 1;
271 revs
.blob_objects
= 1;
272 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
274 return error("unrecognized argument: %s'", argv
[1]);
275 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
276 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
277 if (!(e
->item
->flags
& UNINTERESTING
)) {
278 unsigned char sha1
[20];
280 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
282 write(bundle_fd
, sha1_to_hex(e
->item
->sha1
), 40);
283 write(bundle_fd
, " ", 1);
284 write(bundle_fd
, ref
, strlen(ref
));
285 write(bundle_fd
, "\n", 1);
291 write(bundle_fd
, "\n", 1);
294 argv_pack
[0] = "pack-objects";
295 argv_pack
[1] = "--all-progress";
296 argv_pack
[2] = "--stdout";
300 pid
= fork_with_pipe(argv_pack
, &in
, &out
);
302 return error("Could not spawn pack-objects");
307 prepare_revision_walk(&revs
);
308 traverse_commit_list(&revs
, show_commit
, show_object
);
310 while (waitpid(pid
, &status
, 0) < 0)
313 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
314 return error ("pack-objects died");
318 static int unbundle(struct bundle_header
*header
, int bundle_fd
,
319 int argc
, const char **argv
)
321 const char *argv_index_pack
[] = {"index-pack", "--stdin", NULL
};
322 int pid
, status
, dev_null
;
324 if (verify_bundle(header
))
326 dev_null
= open("/dev/null", O_WRONLY
);
327 pid
= fork_with_pipe(argv_index_pack
, &bundle_fd
, &dev_null
);
329 return error("Could not spawn index-pack");
331 while (waitpid(pid
, &status
, 0) < 0)
333 return error("index-pack died");
334 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
335 return error("index-pack exited with status %d",
336 WEXITSTATUS(status
));
337 return list_heads(header
, argc
, argv
);
340 int cmd_bundle(int argc
, const char **argv
, const char *prefix
)
342 struct bundle_header header
;
344 const char *cmd
, *bundle_file
;
346 char buffer
[PATH_MAX
];
352 bundle_file
= argv
[2];
356 prefix
= setup_git_directory_gently(&nongit
);
357 if (prefix
&& bundle_file
[0] != '/') {
358 snprintf(buffer
, sizeof(buffer
), "%s/%s", prefix
, bundle_file
);
359 bundle_file
= buffer
;
362 memset(&header
, 0, sizeof(header
));
363 if (strcmp(cmd
, "create") &&
364 !(bundle_fd
= read_header(bundle_file
, &header
)))
367 if (!strcmp(cmd
, "verify")) {
369 if (verify_bundle(&header
))
371 fprintf(stderr
, "%s is okay\n", bundle_file
);
374 if (!strcmp(cmd
, "list-heads")) {
376 return !!list_heads(&header
, argc
, argv
);
378 if (!strcmp(cmd
, "create")) {
380 die("Need a repository to create a bundle.");
381 return !!create_bundle(&header
, bundle_file
, argc
, argv
);
382 } else if (!strcmp(cmd
, "unbundle")) {
384 die("Need a repository to unbundle.");
385 return !!unbundle(&header
, bundle_fd
, argc
, argv
);