bundle.c 1.77 KB
Newer Older
1
#include "builtin.h"
2
#include "cache.h"
3
#include "bundle.h"
4 5 6 7 8

/*
 * Basic handler for bundle files to connect repositories via sneakernet.
 * Invocation must include action.
 * This function can create a bundle or provide information on an existing
9
 * bundle supporting "fetch", "pull", and "ls-remote".
10 11
 */

12 13 14
static const char builtin_bundle_usage[] =
  "git bundle create <file> <git-rev-list args>\n"
  "   or: git bundle verify <file>\n"
15 16
  "   or: git bundle list-heads <file> [<refname>...]\n"
  "   or: git bundle unbundle <file> [<refname>...]";
17 18 19 20 21 22 23 24 25

int cmd_bundle(int argc, const char **argv, const char *prefix)
{
	struct bundle_header header;
	const char *cmd, *bundle_file;
	int bundle_fd = -1;
	char buffer[PATH_MAX];

	if (argc < 3)
26
		usage(builtin_bundle_usage);
27 28 29 30 31 32 33 34 35 36 37 38

	cmd = argv[1];
	bundle_file = argv[2];
	argc -= 2;
	argv += 2;

	if (prefix && bundle_file[0] != '/') {
		snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
		bundle_file = buffer;
	}

	memset(&header, 0, sizeof(header));
39 40
	if (strcmp(cmd, "create") && (bundle_fd =
				read_bundle_header(bundle_file, &header)) < 0)
41 42 43 44
		return 1;

	if (!strcmp(cmd, "verify")) {
		close(bundle_fd);
45
		if (verify_bundle(&header, 1))
46 47 48 49 50 51
			return 1;
		fprintf(stderr, "%s is okay\n", bundle_file);
		return 0;
	}
	if (!strcmp(cmd, "list-heads")) {
		close(bundle_fd);
52
		return !!list_bundle_refs(&header, argc, argv);
53 54
	}
	if (!strcmp(cmd, "create")) {
55
		if (!startup_info->have_repository)
56 57 58
			die("Need a repository to create a bundle.");
		return !!create_bundle(&header, bundle_file, argc, argv);
	} else if (!strcmp(cmd, "unbundle")) {
59
		if (!startup_info->have_repository)
60
			die("Need a repository to unbundle.");
61 62
		return !!unbundle(&header, bundle_fd) ||
			list_bundle_refs(&header, argc, argv);
63
	} else
64
		usage(builtin_bundle_usage);
65
}