Skip to content

Commit bf8b1e0

Browse files
peffgitster
authored andcommitted
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 e9ee84c (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 768bb23 commit bf8b1e0

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

builtin/bundle.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
107107
return ret;
108108
}
109109

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+
110127
static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
111128
struct bundle_header header = BUNDLE_HEADER_INIT;
112129
int bundle_fd = -1;
@@ -118,12 +135,13 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
118135
OPT_END()
119136
};
120137
char *bundle_file;
138+
const char *name;
121139

122140
argc = parse_options_cmd_bundle(argc, argv, prefix,
123141
builtin_bundle_verify_usage, options, &bundle_file);
124142
/* bundle internals use argv[1] as further parameters */
125143

126-
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
144+
if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
127145
ret = 1;
128146
goto cleanup;
129147
}
@@ -134,7 +152,7 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
134152
goto cleanup;
135153
}
136154

137-
fprintf(stderr, _("%s is okay\n"), bundle_file);
155+
fprintf(stderr, _("%s is okay\n"), name);
138156
ret = 0;
139157
cleanup:
140158
free(bundle_file);
@@ -155,7 +173,7 @@ static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix
155173
builtin_bundle_list_heads_usage, options, &bundle_file);
156174
/* bundle internals use argv[1] as further parameters */
157175

158-
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
176+
if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
159177
ret = 1;
160178
goto cleanup;
161179
}
@@ -185,7 +203,7 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix)
185203
builtin_bundle_unbundle_usage, options, &bundle_file);
186204
/* bundle internals use argv[1] as further parameters */
187205

188-
if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0) {
206+
if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
189207
ret = 1;
190208
goto cleanup;
191209
}

t/t6020-bundle-misc.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,19 @@ test_expect_success 'cloning from filtered bundle has useful error' '
566566
grep "cannot clone from filtered bundle" err
567567
'
568568

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+
569584
test_done

0 commit comments

Comments
 (0)