Skip to content

Commit 4a3d85d

Browse files
dmpotgitster
authored andcommitted
add --no-filters option to git hash-object
The new option allows the contents to be hashed as is, ignoring any input filter that would have been chosen by the attributes mechanism. This option is incompatible with --path and --stdin-paths options. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3970243 commit 4a3d85d

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

Documentation/git-hash-object.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-hash-object - Compute object ID and optionally creates a blob from a file
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git hash-object' [-t <type>] [-w] [--path=<file>] [--stdin] [--] <file>...
12+
'git hash-object' [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...
1313
'git hash-object' [-t <type>] [-w] --stdin-paths < <list-of-paths>
1414

1515
DESCRIPTION
@@ -47,6 +47,12 @@ OPTIONS
4747
temporary files located outside of the working directory or files
4848
read from stdin.
4949

50+
--no-filters::
51+
Hash the contents as is, ignoring any input filter that would
52+
have been chosen by the attributes mechanism, including crlf
53+
conversion. If the file is read from standard input then this
54+
is always implied, unless the --path option is given.
55+
5056
Author
5157
------
5258
Written by Junio C Hamano <gitster@pobox.com>

hash-object.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void hash_stdin_paths(const char *type, int write_objects)
5252
}
5353

5454
static const char * const hash_object_usage[] = {
55-
"git hash-object [-t <type>] [-w] [--path=<file>] [--stdin] [--] <file>...",
55+
"git hash-object [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...",
5656
"git hash-object --stdin-paths < <list-of-paths>",
5757
NULL
5858
};
@@ -61,13 +61,15 @@ static const char *type;
6161
static int write_object;
6262
static int hashstdin;
6363
static int stdin_paths;
64+
static int no_filters;
6465
static const char *vpath;
6566

6667
static const struct option hash_object_options[] = {
6768
OPT_STRING('t', NULL, &type, "type", "object type"),
6869
OPT_BOOLEAN('w', NULL, &write_object, "write the object into the object database"),
6970
OPT_BOOLEAN( 0 , "stdin", &hashstdin, "read the object from stdin"),
7071
OPT_BOOLEAN( 0 , "stdin-paths", &stdin_paths, "read file names from stdin"),
72+
OPT_BOOLEAN( 0 , "no-filters", &no_filters, "store file as is without filters"),
7173
OPT_STRING( 0 , "path", &vpath, "file", "process file as it were from this path"),
7274
OPT_END()
7375
};
@@ -99,9 +101,15 @@ int main(int argc, const char **argv)
99101
errstr = "Can't specify files with --stdin-paths";
100102
else if (vpath)
101103
errstr = "Can't use --stdin-paths with --path";
104+
else if (no_filters)
105+
errstr = "Can't use --stdin-paths with --no-filters";
106+
}
107+
else {
108+
if (hashstdin > 1)
109+
errstr = "Multiple --stdin arguments are not supported";
110+
if (vpath && no_filters)
111+
errstr = "Can't use --path with --no-filters";
102112
}
103-
else if (hashstdin > 1)
104-
errstr = "Multiple --stdin arguments are not supported";
105113

106114
if (errstr) {
107115
error (errstr);
@@ -116,7 +124,8 @@ int main(int argc, const char **argv)
116124

117125
if (0 <= prefix_length)
118126
arg = prefix_filename(prefix, prefix_length, arg);
119-
hash_object(arg, type, write_object, vpath ? vpath : arg);
127+
hash_object(arg, type, write_object,
128+
no_filters ? NULL : vpath ? vpath : arg);
120129
}
121130

122131
if (stdin_paths)

t/t1007-hash-object.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ test_expect_success "Can't use --path with --stdin-paths" '
6565
echo example | test_must_fail git hash-object --stdin-paths --path=foo
6666
'
6767

68+
test_expect_success "Can't use --stdin-paths with --no-filters" '
69+
echo example | test_must_fail git hash-object --stdin-paths --no-filters
70+
'
71+
72+
test_expect_success "Can't use --path with --no-filters" '
73+
test_must_fail git hash-object --no-filters --path=foo
74+
'
75+
6876
# Behavior
6977

7078
push_repo
@@ -117,6 +125,22 @@ test_expect_success 'check that appropriate filter is invoke when --path is used
117125
git config --unset core.autocrlf
118126
'
119127

128+
test_expect_success 'check that --no-filters option works' '
129+
echo fooQ | tr Q "\\015" >file0 &&
130+
cp file0 file1 &&
131+
echo "file0 -crlf" >.gitattributes &&
132+
echo "file1 crlf" >>.gitattributes &&
133+
git config core.autocrlf true &&
134+
file0_sha=$(git hash-object file0) &&
135+
file1_sha=$(git hash-object file1) &&
136+
test "$file0_sha" != "$file1_sha" &&
137+
nofilters_file1=$(git hash-object --no-filters file1) &&
138+
test "$file0_sha" = "$nofilters_file1" &&
139+
nofilters_file1=$(cat file1 | git hash-object --stdin) &&
140+
test "$file0_sha" = "$nofilters_file1" &&
141+
git config --unset core.autocrlf
142+
'
143+
120144
pop_repo
121145

122146
for args in "-w --stdin" "--stdin -w"; do

0 commit comments

Comments
 (0)