bundle: let "-" mean stdin for reading operations

For writing, "bundle create -" indicates that the bundle should be written to stdout. But there's no matching handling of "-" for reading operations. This is inconsistent, and a little inflexible (though one can always use "/dev/stdin" on systems that support it). However, it's easy to change. Once upon a time, the bundle-reading code required a seekable descriptor, but that was fixed long ago in e9ee84cf28b (bundle: allowing to read from an unseekable fd, 2011-10-13). So we just need to handle "-" explicitly when opening the file. We _could_ do this by handling "-" in read_bundle_header(), which the reading functions all call already. But that is probably a bad idea. It's also used by low-level code like the transport functions, and we may want to be more careful there. We do not know that stdin is even available to us, and certainly we would not want to get confused by a configured URL that happens to point to "-". So instead, let's add a helper to builtin/bundle.c. Since both the bundle code and some of the callers refer to the bundle by name for error messages, let's use the string "<stdin>" to make the output a bit nicer to read. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 4, 2023 at 05:26 UTC bf8b1e04ffa3bb6c64bb8ae50ec825b128ef957d
2 files changed +37 -4
builtin/bundle.c
+22 -4
@@ -107,6 +107,23 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
107 return ret;
108 }
109
110 +/*
111 + * Similar to read_bundle_header(), but handle "-" as stdin.
112 + */
113 +static int open_bundle(const char *path, struct bundle_header *header,
114 + const char **name)
115 +{
116 + if (!strcmp(path, "-")) {
117 + if (name)
118 + *name = "<stdin>";
119 + return read_bundle_header_fd(0, header, "<stdin>");
120 + }
121 +
122 + if (name)
123 + *name = path;
124 + return read_bundle_header(path, header);
125 +}
126 +
127 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
128 struct bundle_header header = BUNDLE_HEADER_INIT;
129 int bundle_fd = -1;
@@ -118,12 +135,13 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
135 OPT_END()
136 };
137 char *bundle_file;
138 + const char *name;
139
140 argc = parse_options_cmd_bundle(argc, argv, prefix,
141 builtin_bundle_verify_usage, options, &bundle_file);
142 /* bundle internals use argv[1] as further parameters */
143
126 - if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
144 + if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
145 ret = 1;
146 goto cleanup;
147 }
@@ -134,7 +152,7 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
152 goto cleanup;
153 }
154
137 - fprintf(stderr, _("%s is okay\n"), bundle_file);
155 + fprintf(stderr, _("%s is okay\n"), name);
156 ret = 0;
157 cleanup:
158 free(bundle_file);
@@ -155,7 +173,7 @@ static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix
173 builtin_bundle_list_heads_usage, options, &bundle_file);
174 /* bundle internals use argv[1] as further parameters */
175
158 - if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
176 + if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
177 ret = 1;
178 goto cleanup;
179 }
@@ -185,7 +203,7 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix)
203 builtin_bundle_unbundle_usage, options, &bundle_file);
204 /* bundle internals use argv[1] as further parameters */
205
188 - if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
206 + if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
207 ret = 1;
208 goto cleanup;
209 }
t/t6020-bundle-misc.sh
+15
@@ -566,4 +566,19 @@ test_expect_success 'cloning from filtered bundle has useful error' '
566 grep "cannot clone from filtered bundle" err
567 '
568
569 +test_expect_success 'read bundle over stdin' '
570 + git bundle create some.bundle HEAD &&
571 +
572 + git bundle verify - <some.bundle 2>err &&
573 + grep "<stdin> is okay" err &&
574 +
575 + git bundle list-heads some.bundle >expect &&
576 + git bundle list-heads - <some.bundle >actual &&
577 + test_cmp expect actual &&
578 +
579 + git bundle unbundle some.bundle >expect &&
580 + git bundle unbundle - <some.bundle >actual &&
581 + test_cmp expect actual
582 +'
583 +
584 test_done