bundle: don't blindly apply prefix_filename() to "-"

A user can specify a filename to a command from the command line,
either as the value given to a command line option, or a command
line argument.  When it is given as a relative filename, in the
user's mind, it is relative to the directory "git" was started from,
but by the time the filename is used, "git" would almost always have
chdir()'ed up to the root level of the working tree.

The given filename, if it is relative, needs to be prefixed with the
path to the current directory, and it typically is done by calling
prefix_filename() helper function.  For commands that can also take
"-" to use the standard input or the standard output, however, this
needs to be done with care.

"git bundle create" uses the next word on the command line as the
output filename, and can take "-" to mean "write to the standard
output".  It blindly called prefix_filename(), so running it in a
subdirectory did not quite work as expected.

Introduce a new helper, prefix_filename_except_for_dash(), and use
it to help "git bundle create" codepath.

Reported-by: Michael Henry
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Junio C Hamano 2023-03-04 05:27:56 -05:00
parent ef3b291a5f
commit a8bfa99d44
4 changed files with 22 additions and 1 deletions

View File

@ -280,3 +280,10 @@ char *prefix_filename(const char *pfx, const char *arg)
#endif
return strbuf_detach(&path, NULL);
}

char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
{
if (!strcmp(arg, "-"))
return xstrdup(arg);
return prefix_filename(pfx, arg);
}

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;
}


View File

@ -634,6 +634,9 @@ char *prefix_path_gently(const char *prefix, int len, int *remaining, const char
*/
char *prefix_filename(const char *prefix, const char *path);

/* Likewise, but path=="-" always yields "-" */
char *prefix_filename_except_for_dash(const char *prefix, const char *path);

int check_filename(const char *prefix, const char *name);
void verify_filename(const char *prefix,
const char *name,

View File

@ -581,4 +581,15 @@ test_expect_success 'read bundle over stdin' '
test_cmp expect actual
'

test_expect_success 'send a bundle to standard output' '
git bundle create - --all HEAD >bundle-one &&
mkdir -p down &&
git -C down bundle create - --all HEAD >bundle-two &&
git bundle verify bundle-one &&
git bundle verify bundle-two &&
git ls-remote bundle-one >expect &&
git ls-remote bundle-two >actual &&
test_cmp expect actual
'

test_done