Code

GIT 1.5.4.4
[git.git] / builtin-bundle.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "bundle.h"
5 /*
6  * Basic handler for bundle files to connect repositories via sneakernet.
7  * Invocation must include action.
8  * This function can create a bundle or provide information on an existing
9  * bundle supporting git-fetch, git-pull, and git-ls-remote
10  */
12 static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
14 int cmd_bundle(int argc, const char **argv, const char *prefix)
15 {
16         struct bundle_header header;
17         int nongit = 0;
18         const char *cmd, *bundle_file;
19         int bundle_fd = -1;
20         char buffer[PATH_MAX];
22         if (argc < 3)
23                 usage(bundle_usage);
25         cmd = argv[1];
26         bundle_file = argv[2];
27         argc -= 2;
28         argv += 2;
30         prefix = setup_git_directory_gently(&nongit);
31         if (prefix && bundle_file[0] != '/') {
32                 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
33                 bundle_file = buffer;
34         }
36         memset(&header, 0, sizeof(header));
37         if (strcmp(cmd, "create") && (bundle_fd =
38                                 read_bundle_header(bundle_file, &header)) < 0)
39                 return 1;
41         if (!strcmp(cmd, "verify")) {
42                 close(bundle_fd);
43                 if (verify_bundle(&header, 1))
44                         return 1;
45                 fprintf(stderr, "%s is okay\n", bundle_file);
46                 return 0;
47         }
48         if (!strcmp(cmd, "list-heads")) {
49                 close(bundle_fd);
50                 return !!list_bundle_refs(&header, argc, argv);
51         }
52         if (!strcmp(cmd, "create")) {
53                 if (nongit)
54                         die("Need a repository to create a bundle.");
55                 return !!create_bundle(&header, bundle_file, argc, argv);
56         } else if (!strcmp(cmd, "unbundle")) {
57                 if (nongit)
58                         die("Need a repository to unbundle.");
59                 return !!unbundle(&header, bundle_fd) ||
60                         list_bundle_refs(&header, argc, argv);
61         } else
62                 usage(bundle_usage);
63 }