hash-object: cleanup handling of command line options
git hash-object used to process the --stdin command line argument before reading subsequent arguments. This caused 'git hash-object --stdin -w' to fail to actually write the object into the database, while '-w --stdin' properly did. Now git hash-object first reads all arguments, and then processes them. This regresses one insane use case. git hash-object used to allow multiple --stdin arguments on the command line: $ git hash-object --stdin --stdin foo ^D bar ^D Now git hash-object errors out if --stdin is given more than once. Reported by Josh Triplett through http://bugs.debian.org/464432 Signed-off-by: Gerrit Pape <pape@smarden.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Gerrit Pape committed
Feb 21, 2008 at 10:06 UTC
8a2f5e5b032ca73e19ad1425b75c63234eb166fa
2 files changed
+46
-1
hash-object.c
+11
-1
@@ -41,6 +41,7 @@ int main(int argc, char **argv)
41
const char *prefix = NULL;
42
int prefix_length = -1;
43
int no_more_flags = 0;
44
+ int hashstdin = 0;
45
46
git_config(git_default_config);
47
@@ -65,13 +66,20 @@ int main(int argc, char **argv)
66
else if (!strcmp(argv[i], "--help"))
67
usage(hash_object_usage);
68
else if (!strcmp(argv[i], "--stdin")) {
68
- hash_stdin(type, write_object);
69
+ if (hashstdin)
70
+ die("Multiple --stdin arguments are not supported");
71
+ hashstdin = 1;
72
}
73
else
74
usage(hash_object_usage);
75
}
76
else {
77
const char *arg = argv[i];
78
+
79
+ if (hashstdin) {
80
+ hash_stdin(type, write_object);
81
+ hashstdin = 0;
82
+ }
83
if (0 <= prefix_length)
84
arg = prefix_filename(prefix, prefix_length,
85
arg);
@@ -79,5 +87,7 @@ int main(int argc, char **argv)
87
no_more_flags = 1;
88
}
89
}
90
+ if (hashstdin)
91
+ hash_stdin(type, write_object);
92
return 0;
93
}
t/t5303-hash-object.sh
new
+35
@@ -0,0 +1,35 @@
1
+#!/bin/sh
2
+
3
+test_description=git-hash-object
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success \
8
+ 'git hash-object -w --stdin saves the object' \
9
+ 'obname=$(echo foo | git hash-object -w --stdin) &&
10
+ obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") &&
11
+ test -r .git/objects/"$obpath" &&
12
+ rm -f .git/objects/"$obpath"'
13
+
14
+test_expect_success \
15
+ 'git hash-object --stdin -w saves the object' \
16
+ 'obname=$(echo foo | git hash-object --stdin -w) &&
17
+ obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") &&
18
+ test -r .git/objects/"$obpath" &&
19
+ rm -f .git/objects/"$obpath"'
20
+
21
+test_expect_success \
22
+ 'git hash-object --stdin file1 <file0 first operates on file0, then file1' \
23
+ 'echo foo > file1 &&
24
+ obname0=$(echo bar | git hash-object --stdin) &&
25
+ obname1=$(git hash-object file1) &&
26
+ obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
27
+ obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
28
+ test "$obname0" = "$obname0new" &&
29
+ test "$obname1" = "$obname1new"'
30
+
31
+test_expect_success \
32
+ 'git hash-object refuses multiple --stdin arguments' \
33
+ '! git hash-object --stdin --stdin < file1'
34
+
35
+test_done