Skip to content

Commit d8ee483

Browse files
arobengitster
authored andcommitted
git-hash-object: Add --stdin-paths option
This allows multiple paths to be specified on stdin. Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3ea5a1b commit d8ee483

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

Documentation/git-hash-object.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-hash-object - Compute object ID and optionally creates a blob from a file
88

99
SYNOPSIS
1010
--------
11-
'git-hash-object' [-t <type>] [-w] [--stdin] [--] <file>...
11+
'git-hash-object' [-t <type>] [-w] [--stdin | --stdin-paths] [--] <file>...
1212

1313
DESCRIPTION
1414
-----------
@@ -32,6 +32,9 @@ OPTIONS
3232
--stdin::
3333
Read the object from standard input instead of from a file.
3434

35+
--stdin-paths::
36+
Read file names from stdin instead of from the command-line.
37+
3538
Author
3639
------
3740
Written by Junio C Hamano <junkio@cox.net>

hash-object.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include "cache.h"
88
#include "blob.h"
9+
#include "quote.h"
910

1011
static void hash_object(const char *path, enum object_type type, int write_object)
1112
{
@@ -20,6 +21,7 @@ static void hash_object(const char *path, enum object_type type, int write_objec
2021
? "Unable to add %s to database"
2122
: "Unable to hash %s", path);
2223
printf("%s\n", sha1_to_hex(sha1));
24+
maybe_flush_or_die(stdout, "hash to stdout");
2325
}
2426

2527
static void hash_stdin(const char *type, int write_object)
@@ -30,8 +32,27 @@ static void hash_stdin(const char *type, int write_object)
3032
printf("%s\n", sha1_to_hex(sha1));
3133
}
3234

35+
static void hash_stdin_paths(const char *type, int write_objects)
36+
{
37+
struct strbuf buf, nbuf;
38+
39+
strbuf_init(&buf, 0);
40+
strbuf_init(&nbuf, 0);
41+
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
42+
if (buf.buf[0] == '"') {
43+
strbuf_reset(&nbuf);
44+
if (unquote_c_style(&nbuf, buf.buf, NULL))
45+
die("line is badly quoted");
46+
strbuf_swap(&buf, &nbuf);
47+
}
48+
hash_object(buf.buf, type_from_string(type), write_objects);
49+
}
50+
strbuf_release(&buf);
51+
strbuf_release(&nbuf);
52+
}
53+
3354
static const char hash_object_usage[] =
34-
"git-hash-object [-t <type>] [-w] [--stdin] <file>...";
55+
"git-hash-object [ [-t <type>] [-w] [--stdin] <file>... | --stdin-paths < <list-of-paths> ]";
3556

3657
int main(int argc, char **argv)
3758
{
@@ -42,6 +63,7 @@ int main(int argc, char **argv)
4263
int prefix_length = -1;
4364
int no_more_flags = 0;
4465
int hashstdin = 0;
66+
int stdin_paths = 0;
4567

4668
git_config(git_default_config);
4769

@@ -65,7 +87,19 @@ int main(int argc, char **argv)
6587
}
6688
else if (!strcmp(argv[i], "--help"))
6789
usage(hash_object_usage);
90+
else if (!strcmp(argv[i], "--stdin-paths")) {
91+
if (hashstdin) {
92+
error("Can't use --stdin-paths with --stdin");
93+
usage(hash_object_usage);
94+
}
95+
stdin_paths = 1;
96+
97+
}
6898
else if (!strcmp(argv[i], "--stdin")) {
99+
if (stdin_paths) {
100+
error("Can't use %s with --stdin-paths", argv[i]);
101+
usage(hash_object_usage);
102+
}
69103
if (hashstdin)
70104
die("Multiple --stdin arguments are not supported");
71105
hashstdin = 1;
@@ -76,6 +110,11 @@ int main(int argc, char **argv)
76110
else {
77111
const char *arg = argv[i];
78112

113+
if (stdin_paths) {
114+
error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
115+
usage(hash_object_usage);
116+
}
117+
79118
if (hashstdin) {
80119
hash_stdin(type, write_object);
81120
hashstdin = 0;
@@ -87,6 +126,10 @@ int main(int argc, char **argv)
87126
no_more_flags = 1;
88127
}
89128
}
129+
130+
if (stdin_paths)
131+
hash_stdin_paths(type, write_object);
132+
90133
if (hashstdin)
91134
hash_stdin(type, write_object);
92135
return 0;

t/t1007-hash-object.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ test_expect_success "multiple '--stdin's are rejected" '
5252
test_must_fail git hash-object --stdin --stdin < example
5353
'
5454

55+
test_expect_success "Can't use --stdin and --stdin-paths together" '
56+
test_must_fail git hash-object --stdin --stdin-paths &&
57+
test_must_fail git hash-object --stdin-paths --stdin
58+
'
59+
60+
test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
61+
test_must_fail git hash-object --stdin-paths hello < example
62+
'
63+
5564
# Behavior
5665

5766
push_repo
@@ -98,4 +107,27 @@ for args in "-w --stdin" "--stdin -w"; do
98107
pop_repo
99108
done
100109

110+
filenames="hello
111+
example"
112+
113+
sha1s="$hello_sha1
114+
$example_sha1"
115+
116+
test_expect_success "hash two files with names on stdin" '
117+
test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
118+
'
119+
120+
for args in "-w --stdin-paths" "--stdin-paths -w"; do
121+
push_repo
122+
123+
test_expect_success "hash two files with names on stdin and write to database ($args)" '
124+
test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
125+
'
126+
127+
test_blob_exists $hello_sha1
128+
test_blob_exists $example_sha1
129+
130+
pop_repo
131+
done
132+
101133
test_done

0 commit comments

Comments
 (0)