Skip to content

Commit a8bfa99

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef3b291 commit a8bfa99

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

abspath.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,10 @@ char *prefix_filename(const char *pfx, const char *arg)
280280
#endif
281281
return strbuf_detach(&path, NULL);
282282
}
283+
284+
char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
285+
{
286+
if (!strcmp(arg, "-"))
287+
return xstrdup(arg);
288+
return prefix_filename(pfx, arg);
289+
}

builtin/bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int parse_options_cmd_bundle(int argc,
5959
PARSE_OPT_STOP_AT_NON_OPTION);
6060
if (!argc)
6161
usage_msg_opt(_("need a <file> argument"), usagestr, options);
62-
*bundle_file = prefix_filename(prefix, argv[0]);
62+
*bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
6363
return argc;
6464
}
6565

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ char *prefix_path_gently(const char *prefix, int len, int *remaining, const char
634634
*/
635635
char *prefix_filename(const char *prefix, const char *path);
636636

637+
/* Likewise, but path=="-" always yields "-" */
638+
char *prefix_filename_except_for_dash(const char *prefix, const char *path);
639+
637640
int check_filename(const char *prefix, const char *name);
638641
void verify_filename(const char *prefix,
639642
const char *name,

t/t6020-bundle-misc.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,15 @@ test_expect_success 'read bundle over stdin' '
581581
test_cmp expect actual
582582
'
583583

584+
test_expect_success 'send a bundle to standard output' '
585+
git bundle create - --all HEAD >bundle-one &&
586+
mkdir -p down &&
587+
git -C down bundle create - --all HEAD >bundle-two &&
588+
git bundle verify bundle-one &&
589+
git bundle verify bundle-two &&
590+
git ls-remote bundle-one >expect &&
591+
git ls-remote bundle-two >actual &&
592+
test_cmp expect actual
593+
'
594+
584595
test_done

0 commit comments

Comments
 (0)