Merge branch 'ph/push-to-delete-nothing'
[git] / builtin / bundle.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "bundle.h"
4
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 "fetch", "pull", and "ls-remote".
10  */
11
12 static const char builtin_bundle_usage[] =
13   "git bundle create <file> <git-rev-list args>\n"
14   "   or: git bundle verify <file>\n"
15   "   or: git bundle list-heads <file> [<refname>...]\n"
16   "   or: git bundle unbundle <file> [<refname>...]";
17
18 int cmd_bundle(int argc, const char **argv, const char *prefix)
19 {
20         struct bundle_header header;
21         const char *cmd, *bundle_file;
22         int bundle_fd = -1;
23         char buffer[PATH_MAX];
24
25         if (argc < 3)
26                 usage(builtin_bundle_usage);
27
28         cmd = argv[1];
29         bundle_file = argv[2];
30         argc -= 2;
31         argv += 2;
32
33         if (prefix && bundle_file[0] != '/') {
34                 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
35                 bundle_file = buffer;
36         }
37
38         memset(&header, 0, sizeof(header));
39         if (strcmp(cmd, "create") && (bundle_fd =
40                                 read_bundle_header(bundle_file, &header)) < 0)
41                 return 1;
42
43         if (!strcmp(cmd, "verify")) {
44                 close(bundle_fd);
45                 if (verify_bundle(&header, 1))
46                         return 1;
47                 fprintf(stderr, _("%s is okay\n"), bundle_file);
48                 return 0;
49         }
50         if (!strcmp(cmd, "list-heads")) {
51                 close(bundle_fd);
52                 return !!list_bundle_refs(&header, argc, argv);
53         }
54         if (!strcmp(cmd, "create")) {
55                 if (!startup_info->have_repository)
56                         die(_("Need a repository to create a bundle."));
57                 return !!create_bundle(&header, bundle_file, argc, argv);
58         } else if (!strcmp(cmd, "unbundle")) {
59                 if (!startup_info->have_repository)
60                         die(_("Need a repository to unbundle."));
61                 return !!unbundle(&header, bundle_fd, 0) ||
62                         list_bundle_refs(&header, argc, argv);
63         } else
64                 usage(builtin_bundle_usage);
65 }