Merge branch 'jk/bundle-use-dash-for-stdfiles'

"git bundle" learned that "-" is a common way to say that the input
comes from the standard input and/or the output goes to the
standard output.  It used to work only for output and only from the
root level of the working tree.

* jk/bundle-use-dash-for-stdfiles:
  parse-options: use prefix_filename_except_for_dash() helper
  parse-options: consistently allocate memory in fix_filename()
  bundle: don't blindly apply prefix_filename() to "-"
  bundle: document handling of "-" as stdin
  bundle: let "-" mean stdin for reading operations
This commit is contained in:
Junio C Hamano
2023-03-19 15:03:12 -07:00
11 changed files with 82 additions and 19 deletions

View File

@ -59,7 +59,7 @@ static int parse_options_cmd_bundle(int argc,
PARSE_OPT_STOP_AT_NON_OPTION);
if (!argc)
usage_msg_opt(_("need a <file> argument"), usagestr, options);
*bundle_file = prefix_filename(prefix, argv[0]);
*bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
return argc;
}
@ -108,6 +108,23 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
return ret;
}
/*
* Similar to read_bundle_header(), but handle "-" as stdin.
*/
static int open_bundle(const char *path, struct bundle_header *header,
const char **name)
{
if (!strcmp(path, "-")) {
if (name)
*name = "<stdin>";
return read_bundle_header_fd(0, header, "<stdin>");
}
if (name)
*name = path;
return read_bundle_header(path, header);
}
static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
struct bundle_header header = BUNDLE_HEADER_INIT;
int bundle_fd = -1;
@ -119,12 +136,13 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
OPT_END()
};
char *bundle_file;
const char *name;
argc = parse_options_cmd_bundle(argc, argv, prefix,
builtin_bundle_verify_usage, options, &bundle_file);
/* bundle internals use argv[1] as further parameters */
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
ret = 1;
goto cleanup;
}
@ -135,7 +153,7 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
goto cleanup;
}
fprintf(stderr, _("%s is okay\n"), bundle_file);
fprintf(stderr, _("%s is okay\n"), name);
ret = 0;
cleanup:
free(bundle_file);
@ -156,7 +174,7 @@ static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix
builtin_bundle_list_heads_usage, options, &bundle_file);
/* bundle internals use argv[1] as further parameters */
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
ret = 1;
goto cleanup;
}
@ -186,7 +204,7 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix)
builtin_bundle_unbundle_usage, options, &bundle_file);
/* bundle internals use argv[1] as further parameters */
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
ret = 1;
goto cleanup;
}