From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Michael Henry <git@drmikehenry.com>, git@vger.kernel.org
Subject: Re: `git bundle create -` may not write to `stdout`
Date: Fri, 03 Mar 2023 14:31:05 -0800 [thread overview]
Message-ID: <xmqqv8jhcvrq.fsf@gitster.g> (raw)
In-Reply-To: <Y/voNv1OQ1Cf/N5a@coredump.intra.peff.net> (Jeff King's message of "Sun, 26 Feb 2023 18:16:06 -0500")
Jeff King <peff@peff.net> writes:
> The most directed fix is this:
>
> diff --git a/builtin/bundle.c b/builtin/bundle.c
> index acceef6200..145b814f48 100644
> --- a/builtin/bundle.c
> +++ b/builtin/bundle.c
> @@ -59,7 +59,9 @@ 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 = strcmp(argv[0], "-") ?
> + prefix_filename(prefix, argv[0]) :
> + xstrdup(argv[0]);
> return argc;
> }
This fell thru cracks, it seems.
OPT_FILENAME() needs to do exactly this, and has its own helper
function in parse-options.c::fix_filename(), but its memory
ownership rules is somewhat screwed (it was perfectly fine in the
original context of "we parse the bounded number of options the user
would give us from the command line, and giving more than one
instance of these last-one-wins option may leak but we do not
care---if you do not like leaking, don't give duplicates", but with
automated leak checking that does not care about such nuances, it
will trigger unnecessary errors), and cannot be reused here.
Here is your "most directed fix" packaged into a call to a helper
function. Given that we may want to slim the cache.h header, it may
not want to be declared there, but for now, its declaration sits
next to prefix_filename().
diff --git c/abspath.c w/abspath.c
index 39e06b5848..b2fd9ff321 100644
--- c/abspath.c
+++ w/abspath.c
@@ -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);
+}
diff --git c/builtin/bundle.c w/builtin/bundle.c
index acceef6200..3056dbeee3 100644
--- c/builtin/bundle.c
+++ w/builtin/bundle.c
@@ -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;
}
diff --git c/cache.h w/cache.h
index 12789903e8..38867de41a 100644
--- c/cache.h
+++ w/cache.h
@@ -638,6 +638,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,
diff --git c/parse-options.c w/parse-options.c
index fd4743293f..bd1ed906a8 100644
--- c/parse-options.c
+++ w/parse-options.c
@@ -59,6 +59,10 @@ static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
return 0;
}
+/*
+ * NEEDSWORK: fix memory ownership rules around here and then use
+ * prefix_filename_except_for_dash() helper.
+ */
static void fix_filename(const char *prefix, const char **file)
{
if (!file || !*file || !prefix || is_absolute_path(*file)
diff --git c/t/t6020-bundle-misc.sh w/t/t6020-bundle-misc.sh
index 7d40994991..d14f7cea91 100755
--- c/t/t6020-bundle-misc.sh
+++ w/t/t6020-bundle-misc.sh
@@ -606,4 +606,15 @@ test_expect_success 'verify catches unreachable, broken prerequisites' '
)
'
+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
next prev parent reply other threads:[~2023-03-03 22:35 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-25 12:58 `git bundle create -` may not write to `stdout` Michael Henry
2023-02-26 23:16 ` Jeff King
2023-03-03 22:31 ` Junio C Hamano [this message]
2023-03-03 22:54 ` Jeff King
2023-03-03 23:05 ` Junio C Hamano
2023-03-04 1:28 ` Jeff King
2023-03-04 1:46 ` Jeff King
2023-03-04 10:22 ` [PATCH 0/5] handling "-" as stdin/stdout in git bundle Jeff King
2023-03-04 10:26 ` [PATCH 1/5] bundle: let "-" mean stdin for reading operations Jeff King
2023-03-04 10:26 ` [PATCH 2/5] bundle: document handling of "-" as stdin Jeff King
2023-03-04 10:27 ` [PATCH 3/5] bundle: don't blindly apply prefix_filename() to "-" Jeff King
2023-03-04 10:31 ` [PATCH 4/5] parse-options: consistently allocate memory in fix_filename() Jeff King
2023-03-04 10:31 ` [PATCH 5/5] parse-options: use prefix_filename_except_for_dash() helper Jeff King
2023-03-04 10:55 ` [RFC/PATCH] bundle: turn on --all-progress-implied by default Jeff King
2023-03-06 3:44 ` Robin H. Johnson
2023-03-06 5:38 ` Jeff King
2023-03-06 9:25 ` Jeff King
2023-03-06 17:41 ` Junio C Hamano
2023-03-06 17:34 ` `git bundle create -` may not write to `stdout` Junio C Hamano
2023-03-04 1:14 ` Junio C Hamano
2023-03-04 1:43 ` Jeff King
2023-03-03 23:59 ` Michael Henry
2023-03-04 2:22 ` Jeff King
2023-03-04 10:08 ` Michael Henry
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqv8jhcvrq.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@drmikehenry.com \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).