blob: 92a8a6026a9bd6da5394e7b37f07dbe91f73db9c [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +01002#include "cache.h"
Johannes Schindelin30415d52007-09-10 23:03:15 -04003#include "bundle.h"
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +01004
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
Heikki Orsila34baebc2008-08-30 14:12:53 +03009 * bundle supporting "fetch", "pull", and "ls-remote".
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010010 */
11
Thiago Farina1f986c42009-09-17 13:20:53 -040012static const char builtin_bundle_usage[] =
13 "git bundle create <file> <git-rev-list args>\n"
14 " or: git bundle verify <file>\n"
Štěpán Němec62b46982010-10-08 19:31:15 +020015 " or: git bundle list-heads <file> [<refname>...]\n"
16 " or: git bundle unbundle <file> [<refname>...]";
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010017
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010018int cmd_bundle(int argc, const char **argv, const char *prefix)
19{
20 struct bundle_header header;
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010021 const char *cmd, *bundle_file;
22 int bundle_fd = -1;
23 char buffer[PATH_MAX];
24
25 if (argc < 3)
Thiago Farina1f986c42009-09-17 13:20:53 -040026 usage(builtin_bundle_usage);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010027
28 cmd = argv[1];
29 bundle_file = argv[2];
30 argc -= 2;
31 argv += 2;
32
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010033 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));
Johannes Schindelin30415d52007-09-10 23:03:15 -040039 if (strcmp(cmd, "create") && (bundle_fd =
40 read_bundle_header(bundle_file, &header)) < 0)
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010041 return 1;
42
43 if (!strcmp(cmd, "verify")) {
44 close(bundle_fd);
Junio C Hamano80e25ce2007-03-05 16:17:27 -080045 if (verify_bundle(&header, 1))
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010046 return 1;
Ævar Arnfjörð Bjarmasonc7c4efa2011-02-22 23:42:20 +000047 fprintf(stderr, _("%s is okay\n"), bundle_file);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010048 return 0;
49 }
50 if (!strcmp(cmd, "list-heads")) {
51 close(bundle_fd);
Johannes Schindelin30415d52007-09-10 23:03:15 -040052 return !!list_bundle_refs(&header, argc, argv);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010053 }
54 if (!strcmp(cmd, "create")) {
Nguyễn Thái Ngọc Duy2cb60092010-08-05 22:12:46 -050055 if (!startup_info->have_repository)
Ævar Arnfjörð Bjarmasonc7c4efa2011-02-22 23:42:20 +000056 die(_("Need a repository to create a bundle."));
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010057 return !!create_bundle(&header, bundle_file, argc, argv);
58 } else if (!strcmp(cmd, "unbundle")) {
Nguyễn Thái Ngọc Duy2cb60092010-08-05 22:12:46 -050059 if (!startup_info->have_repository)
Ævar Arnfjörð Bjarmasonc7c4efa2011-02-22 23:42:20 +000060 die(_("Need a repository to unbundle."));
Junio C Hamanobe042af2011-09-18 16:52:32 -070061 return !!unbundle(&header, bundle_fd, 0) ||
Johannes Schindelin30415d52007-09-10 23:03:15 -040062 list_bundle_refs(&header, argc, argv);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010063 } else
Thiago Farina1f986c42009-09-17 13:20:53 -040064 usage(builtin_bundle_usage);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010065}