@matheusbsbs / matheus-bagliano / commits / d8ee483250
Fork 0

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>

Adam Roben committed May 23, 2008 at 16:19 UTC d8ee4832509dd2d7448a49920f5cba2fc979283d
3 files changed +80 -2
Documentation/git-hash-object.txt
+4 -1
@@ -8,7 +8,7 @@ git-hash-object - Compute object ID and optionally creates a blob from a file
8
9 SYNOPSIS
10 --------
11 -'git-hash-object' [-t <type>] [-w] [--stdin] [--] <file>...
11 +'git-hash-object' [-t <type>] [-w] [--stdin | --stdin-paths] [--] <file>...
12
13 DESCRIPTION
14 -----------
@@ -32,6 +32,9 @@ OPTIONS
32 --stdin::
33 Read the object from standard input instead of from a file.
34
35 +--stdin-paths::
36 + Read file names from stdin instead of from the command-line.
37 +
38 Author
39 ------
40 Written by Junio C Hamano <junkio@cox.net>
hash-object.c
+44 -1
@@ -6,6 +6,7 @@
6 */
7 #include "cache.h"
8 #include "blob.h"
9 +#include "quote.h"
10
11 static void hash_object(const char *path, enum object_type type, int write_object)
12 {
@@ -20,6 +21,7 @@ static void hash_object(const char *path, enum object_type type, int write_objec
21 ? "Unable to add %s to database"
22 : "Unable to hash %s", path);
23 printf("%s\n", sha1_to_hex(sha1));
24 + maybe_flush_or_die(stdout, "hash to stdout");
25 }
26
27 static void hash_stdin(const char *type, int write_object)
@@ -30,8 +32,27 @@ static void hash_stdin(const char *type, int write_object)
32 printf("%s\n", sha1_to_hex(sha1));
33 }
34
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 +
54 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> ]";
56
57 int main(int argc, char **argv)
58 {
@@ -42,6 +63,7 @@ int main(int argc, char **argv)
63 int prefix_length = -1;
64 int no_more_flags = 0;
65 int hashstdin = 0;
66 + int stdin_paths = 0;
67
68 git_config(git_default_config);
69
@@ -65,7 +87,19 @@ int main(int argc, char **argv)
87 }
88 else if (!strcmp(argv[i], "--help"))
89 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 + }
98 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 + }
103 if (hashstdin)
104 die("Multiple --stdin arguments are not supported");
105 hashstdin = 1;
@@ -76,6 +110,11 @@ int main(int argc, char **argv)
110 else {
111 const char *arg = argv[i];
112
113 + if (stdin_paths) {
114 + error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
115 + usage(hash_object_usage);
116 + }
117 +
118 if (hashstdin) {
119 hash_stdin(type, write_object);
120 hashstdin = 0;
@@ -87,6 +126,10 @@ int main(int argc, char **argv)
126 no_more_flags = 1;
127 }
128 }
129 +
130 + if (stdin_paths)
131 + hash_stdin_paths(type, write_object);
132 +
133 if (hashstdin)
134 hash_stdin(type, write_object);
135 return 0;
t/t1007-hash-object.sh
+32
@@ -52,6 +52,15 @@ test_expect_success "multiple '--stdin's are rejected" '
52 test_must_fail git hash-object --stdin --stdin < example
53 '
54
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 +
64 # Behavior
65
66 push_repo
@@ -98,4 +107,27 @@ for args in "-w --stdin" "--stdin -w"; do
107 pop_repo
108 done
109
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 +
133 test_done