mirror of
https://github.com/git/git.git
synced 2024-11-30 21:44:02 +08:00
3b754eedd5
We may take the path to a bundle file as an argument, and need to adjust the filename based on the prefix we discovered while setting up the git directory. We do so manually into a fixed-size buffer, but using prefix_filename() is the normal way. Besides being more concise, there are two subtle improvements: 1. The original inserted a "/" between the two paths, even though the "prefix" argument always has the "/" appended. That means that: cd subdir && git bundle verify ../foo.bundle was looking at (and reporting) subdir//../foo.bundle. Harmless, but ugly. Using prefix_filename() gets this right. 2. The original checked for an absolute path by looking for a leading '/'. It should have been using is_absolute_path(), which also covers more cases on Windows (backslashes and dos drive prefixes). But it's easier still to just pass the name to prefix_filename(), which handles this case automatically. Note that we'll just leak the resulting buffer in the name of simplicity, since it needs to last through the duration of the program anyway. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
68 lines
1.8 KiB
C
68 lines
1.8 KiB
C
#include "builtin.h"
|
|
#include "cache.h"
|
|
#include "bundle.h"
|
|
|
|
/*
|
|
* 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
|
|
* bundle supporting "fetch", "pull", and "ls-remote".
|
|
*/
|
|
|
|
static const char builtin_bundle_usage[] =
|
|
"git bundle create <file> <git-rev-list args>\n"
|
|
" or: git bundle verify <file>\n"
|
|
" or: git bundle list-heads <file> [<refname>...]\n"
|
|
" or: git bundle unbundle <file> [<refname>...]";
|
|
|
|
int cmd_bundle(int argc, const char **argv, const char *prefix)
|
|
{
|
|
struct bundle_header header;
|
|
const char *cmd, *bundle_file;
|
|
int bundle_fd = -1;
|
|
|
|
if (argc < 3)
|
|
usage(builtin_bundle_usage);
|
|
|
|
cmd = argv[1];
|
|
bundle_file = prefix_filename(prefix, argv[2]);
|
|
argc -= 2;
|
|
argv += 2;
|
|
|
|
memset(&header, 0, sizeof(header));
|
|
if (strcmp(cmd, "create") && (bundle_fd =
|
|
read_bundle_header(bundle_file, &header)) < 0)
|
|
return 1;
|
|
|
|
if (!strcmp(cmd, "verify")) {
|
|
close(bundle_fd);
|
|
if (argc != 1) {
|
|
usage(builtin_bundle_usage);
|
|
return 1;
|
|
}
|
|
if (verify_bundle(&header, 1))
|
|
return 1;
|
|
fprintf(stderr, _("%s is okay\n"), bundle_file);
|
|
return 0;
|
|
}
|
|
if (!strcmp(cmd, "list-heads")) {
|
|
close(bundle_fd);
|
|
return !!list_bundle_refs(&header, argc, argv);
|
|
}
|
|
if (!strcmp(cmd, "create")) {
|
|
if (argc < 2) {
|
|
usage(builtin_bundle_usage);
|
|
return 1;
|
|
}
|
|
if (!startup_info->have_repository)
|
|
die(_("Need a repository to create a bundle."));
|
|
return !!create_bundle(&header, bundle_file, argc, argv);
|
|
} else if (!strcmp(cmd, "unbundle")) {
|
|
if (!startup_info->have_repository)
|
|
die(_("Need a repository to unbundle."));
|
|
return !!unbundle(&header, bundle_fd, 0) ||
|
|
list_bundle_refs(&header, argc, argv);
|
|
} else
|
|
usage(builtin_bundle_usage);
|
|
}
|