blob: d0de59b94ff23eebe03984dc670babf793bdd5ff [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;
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010023
24 if (argc < 3)
Thiago Farina1f986c42009-09-17 13:20:53 -040025 usage(builtin_bundle_usage);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010026
27 cmd = argv[1];
Jeff King3b754ee2017-03-20 21:31:27 -040028 bundle_file = prefix_filename(prefix, argv[2]);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010029 argc -= 2;
30 argv += 2;
31
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010032 memset(&header, 0, sizeof(header));
Johannes Schindelin30415d52007-09-10 23:03:15 -040033 if (strcmp(cmd, "create") && (bundle_fd =
34 read_bundle_header(bundle_file, &header)) < 0)
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010035 return 1;
36
37 if (!strcmp(cmd, "verify")) {
38 close(bundle_fd);
Patrick Steinhardt7886cfa2015-05-08 10:02:00 +020039 if (argc != 1) {
40 usage(builtin_bundle_usage);
41 return 1;
42 }
Junio C Hamano80e25ce2007-03-05 16:17:27 -080043 if (verify_bundle(&header, 1))
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010044 return 1;
Ævar Arnfjörð Bjarmasonc7c4efa2011-02-22 23:42:20 +000045 fprintf(stderr, _("%s is okay\n"), bundle_file);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010046 return 0;
47 }
48 if (!strcmp(cmd, "list-heads")) {
49 close(bundle_fd);
Johannes Schindelin30415d52007-09-10 23:03:15 -040050 return !!list_bundle_refs(&header, argc, argv);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010051 }
52 if (!strcmp(cmd, "create")) {
Patrick Steinhardt7886cfa2015-05-08 10:02:00 +020053 if (argc < 2) {
54 usage(builtin_bundle_usage);
55 return 1;
56 }
Nguyễn Thái Ngọc Duy2cb60092010-08-05 22:12:46 -050057 if (!startup_info->have_repository)
Ævar Arnfjörð Bjarmasonc7c4efa2011-02-22 23:42:20 +000058 die(_("Need a repository to create a bundle."));
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010059 return !!create_bundle(&header, bundle_file, argc, argv);
60 } else if (!strcmp(cmd, "unbundle")) {
Nguyễn Thái Ngọc Duy2cb60092010-08-05 22:12:46 -050061 if (!startup_info->have_repository)
Ævar Arnfjörð Bjarmasonc7c4efa2011-02-22 23:42:20 +000062 die(_("Need a repository to unbundle."));
Junio C Hamanobe042af2011-09-18 16:52:32 -070063 return !!unbundle(&header, bundle_fd, 0) ||
Johannes Schindelin30415d52007-09-10 23:03:15 -040064 list_bundle_refs(&header, argc, argv);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010065 } else
Thiago Farina1f986c42009-09-17 13:20:53 -040066 usage(builtin_bundle_usage);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010067}