2009-12-07 13:10:50 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='test git rev-parse diagnosis for invalid argument'
|
|
|
|
|
|
|
|
exec </dev/null
|
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
2011-03-31 13:17:33 +04:00
|
|
|
test_did_you_mean ()
|
|
|
|
{
|
2011-05-09 08:43:20 +04:00
|
|
|
cat >expected <<-EOF &&
|
2020-01-25 03:13:01 +03:00
|
|
|
fatal: path '$2$3' $4, but not ${5:-$SQ$3$SQ}
|
|
|
|
hint: Did you mean '$1:$2$3'${2:+ aka $SQ$1:./$3$SQ}?
|
2011-05-09 08:43:20 +04:00
|
|
|
EOF
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ncmp expected error
|
2011-03-31 13:17:33 +04:00
|
|
|
}
|
|
|
|
|
2009-12-07 13:10:50 +03:00
|
|
|
HASH_file=
|
|
|
|
|
|
|
|
test_expect_success 'set up basic repo' '
|
|
|
|
echo one > file.txt &&
|
|
|
|
mkdir subdir &&
|
|
|
|
echo two > subdir/file.txt &&
|
|
|
|
echo three > subdir/file2.txt &&
|
|
|
|
git add . &&
|
|
|
|
git commit -m init &&
|
|
|
|
echo four > index-only.txt &&
|
|
|
|
git add index-only.txt &&
|
|
|
|
echo five > disk-only.txt
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'correct file objects' '
|
|
|
|
HASH_file=$(git rev-parse HEAD:file.txt) &&
|
|
|
|
git rev-parse HEAD:subdir/file.txt &&
|
|
|
|
git rev-parse :index-only.txt &&
|
|
|
|
(cd subdir &&
|
|
|
|
git rev-parse HEAD:subdir/file2.txt &&
|
|
|
|
test $HASH_file = $(git rev-parse HEAD:file.txt) &&
|
|
|
|
test $HASH_file = $(git rev-parse :file.txt) &&
|
|
|
|
test $HASH_file = $(git rev-parse :0:file.txt) )
|
|
|
|
'
|
|
|
|
|
2010-11-28 06:37:32 +03:00
|
|
|
test_expect_success 'correct relative file objects (0)' '
|
|
|
|
git rev-parse :file.txt >expected &&
|
|
|
|
git rev-parse :./file.txt >result &&
|
2010-12-10 00:38:05 +03:00
|
|
|
test_cmp expected result &&
|
|
|
|
git rev-parse :0:./file.txt >result &&
|
2010-11-28 06:37:32 +03:00
|
|
|
test_cmp expected result
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'correct relative file objects (1)' '
|
|
|
|
git rev-parse HEAD:file.txt >expected &&
|
|
|
|
git rev-parse HEAD:./file.txt >result &&
|
|
|
|
test_cmp expected result
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'correct relative file objects (2)' '
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
git rev-parse HEAD:../file.txt >result &&
|
|
|
|
test_cmp ../expected result
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'correct relative file objects (3)' '
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
git rev-parse HEAD:../subdir/../file.txt >result &&
|
|
|
|
test_cmp ../expected result
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'correct relative file objects (4)' '
|
|
|
|
git rev-parse HEAD:subdir/file.txt >expected &&
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
git rev-parse HEAD:./file.txt >result &&
|
|
|
|
test_cmp ../expected result
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2010-12-10 00:38:05 +03:00
|
|
|
test_expect_success 'correct relative file objects (5)' '
|
|
|
|
git rev-parse :subdir/file.txt >expected &&
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
git rev-parse :./file.txt >result &&
|
|
|
|
test_cmp ../expected result &&
|
|
|
|
git rev-parse :0:./file.txt >result &&
|
|
|
|
test_cmp ../expected result
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'correct relative file objects (6)' '
|
|
|
|
git rev-parse :file.txt >expected &&
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
git rev-parse :../file.txt >result &&
|
|
|
|
test_cmp ../expected result &&
|
|
|
|
git rev-parse :0:../file.txt >result &&
|
|
|
|
test_cmp ../expected result
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2009-12-07 13:10:50 +03:00
|
|
|
test_expect_success 'incorrect revision id' '
|
|
|
|
test_must_fail git rev-parse foobar:file.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "invalid object name .foobar." error &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse foobar 2>error &&
|
2016-06-17 23:21:06 +03:00
|
|
|
test_i18ngrep "unknown revision or path not in the working tree." error
|
2009-12-07 13:10:50 +03:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'incorrect file in sha1:path' '
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse HEAD:nothing.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "path .nothing.txt. does not exist in .HEAD." error &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse HEAD:index-only.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "path .index-only.txt. exists on disk, but not in .HEAD." error &&
|
2009-12-07 13:10:50 +03:00
|
|
|
(cd subdir &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse HEAD:file2.txt 2>error &&
|
2011-03-31 13:17:33 +04:00
|
|
|
test_did_you_mean HEAD subdir/ file2.txt exists )
|
2009-12-07 13:10:50 +03:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'incorrect file in :path and :N:path' '
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :nothing.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :1:nothing.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :1:file.txt 2>error &&
|
2011-03-31 13:17:33 +04:00
|
|
|
test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
|
2009-12-07 13:10:50 +03:00
|
|
|
(cd subdir &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :1:file.txt 2>error &&
|
2011-03-31 13:17:33 +04:00
|
|
|
test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :file2.txt 2>error &&
|
2011-03-31 13:17:33 +04:00
|
|
|
test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :2:file2.txt 2>error &&
|
2011-03-31 13:17:33 +04:00
|
|
|
test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
|
2020-01-25 03:06:31 +03:00
|
|
|
test_must_fail git rev-parse :disk-only.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "path .disk-only.txt. exists on disk, but not in the index" error
|
2009-12-07 13:10:50 +03:00
|
|
|
'
|
|
|
|
|
2010-08-24 08:52:44 +04:00
|
|
|
test_expect_success 'invalid @{n} reference' '
|
|
|
|
test_must_fail git rev-parse master@{99999} >output 2>error &&
|
2019-11-26 22:46:07 +03:00
|
|
|
test_must_be_empty output &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "log for [^ ]* only has [0-9][0-9]* entries" error &&
|
2010-08-24 08:52:44 +04:00
|
|
|
test_must_fail git rev-parse --verify master@{99999} >output 2>error &&
|
2019-11-26 22:46:07 +03:00
|
|
|
test_must_be_empty output &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "log for [^ ]* only has [0-9][0-9]* entries" error
|
2010-08-24 08:52:44 +04:00
|
|
|
'
|
|
|
|
|
2010-11-28 06:37:32 +03:00
|
|
|
test_expect_success 'relative path not found' '
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep subdir/nonexistent.txt error
|
2010-11-28 06:37:32 +03:00
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'relative path outside worktree' '
|
|
|
|
test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
|
2019-11-26 22:46:07 +03:00
|
|
|
test_must_be_empty output &&
|
2018-02-13 16:19:15 +03:00
|
|
|
test_i18ngrep "outside repository" error
|
2010-11-28 06:37:32 +03:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'relative path when cwd is outside worktree' '
|
|
|
|
test_must_fail git --git-dir=.git --work-tree=subdir rev-parse HEAD:./file.txt >output 2>error &&
|
2019-11-26 22:46:07 +03:00
|
|
|
test_must_be_empty output &&
|
2020-01-25 03:13:01 +03:00
|
|
|
test_i18ngrep "relative path syntax can.t be used outside working tree" error
|
2010-11-28 06:37:32 +03:00
|
|
|
'
|
|
|
|
|
2012-06-18 22:18:20 +04:00
|
|
|
test_expect_success '<commit>:file correctly diagnosed after a pathname' '
|
|
|
|
test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error &&
|
|
|
|
test_i18ngrep ! "exists on disk" error &&
|
2012-06-18 22:18:21 +04:00
|
|
|
test_i18ngrep "no such path in the working tree" error &&
|
2012-06-18 22:18:20 +04:00
|
|
|
cat >expect <<-\EOF &&
|
|
|
|
file.txt
|
|
|
|
HEAD:file.txt
|
|
|
|
EOF
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2011-05-03 00:39:16 +04:00
|
|
|
test_expect_success 'dotdot is not an empty set' '
|
|
|
|
( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect &&
|
|
|
|
|
|
|
|
git rev-parse HEAD.. >actual &&
|
|
|
|
test_cmp expect actual &&
|
|
|
|
|
|
|
|
git rev-parse ..HEAD >actual &&
|
|
|
|
test_cmp expect actual &&
|
|
|
|
|
|
|
|
echo .. >expect &&
|
|
|
|
git rev-parse .. >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2020-09-25 04:44:46 +03:00
|
|
|
test_expect_success 'dotdot does not peel endpoints' '
|
|
|
|
git tag -a -m "annote" annotated HEAD &&
|
|
|
|
A=$(git rev-parse annotated) &&
|
|
|
|
H=$(git rev-parse annotated^0) &&
|
|
|
|
{
|
|
|
|
echo $A && echo ^$A
|
|
|
|
} >expect-with-two-dots &&
|
|
|
|
{
|
|
|
|
echo $A && echo $A && echo ^$H
|
|
|
|
} >expect-with-merge-base &&
|
|
|
|
|
|
|
|
git rev-parse annotated..annotated >actual-with-two-dots &&
|
|
|
|
test_cmp expect-with-two-dots actual-with-two-dots &&
|
|
|
|
|
|
|
|
git rev-parse annotated...annotated >actual-with-merge-base &&
|
|
|
|
test_cmp expect-with-merge-base actual-with-merge-base
|
|
|
|
'
|
|
|
|
|
rev-parse: correctly diagnose revision errors before "--"
Rev-parse understands that a "--" may separate revisions and
filenames, and that anything after the "--" is taken as-is.
However, it does not understand that anything before the
token must be a revision (which is the usual rule
implemented by the setup_revisions parser).
Since rev-parse prefers revisions to files when parsing
before the "--", we end up with the correct result (if such
an argument is a revision, we parse it as one, and if it is
not, it is an error either way). However, we misdiagnose
the errors:
$ git rev-parse foobar -- >/dev/null
fatal: ambiguous argument 'foobar': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ >foobar
$ git rev-parse foobar -- >/dev/null
fatal: bad flag '--' used after filename
In both cases, we should know that the real error is that
"foobar" is meant to be a revision, but could not be
resolved.
Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-07 02:05:48 +04:00
|
|
|
test_expect_success 'arg before dashdash must be a revision (missing)' '
|
|
|
|
test_must_fail git rev-parse foobar -- 2>stderr &&
|
|
|
|
test_i18ngrep "bad revision" stderr
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'arg before dashdash must be a revision (file)' '
|
|
|
|
>foobar &&
|
|
|
|
test_must_fail git rev-parse foobar -- 2>stderr &&
|
|
|
|
test_i18ngrep "bad revision" stderr
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
|
|
|
|
>foobar &&
|
|
|
|
git update-ref refs/heads/foobar HEAD &&
|
|
|
|
{
|
|
|
|
# we do not want to use rev-parse here, because
|
|
|
|
# we are testing it
|
2020-05-20 20:36:08 +03:00
|
|
|
git show-ref -s refs/heads/foobar &&
|
rev-parse: correctly diagnose revision errors before "--"
Rev-parse understands that a "--" may separate revisions and
filenames, and that anything after the "--" is taken as-is.
However, it does not understand that anything before the
token must be a revision (which is the usual rule
implemented by the setup_revisions parser).
Since rev-parse prefers revisions to files when parsing
before the "--", we end up with the correct result (if such
an argument is a revision, we parse it as one, and if it is
not, it is an error either way). However, we misdiagnose
the errors:
$ git rev-parse foobar -- >/dev/null
fatal: ambiguous argument 'foobar': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ >foobar
$ git rev-parse foobar -- >/dev/null
fatal: bad flag '--' used after filename
In both cases, we should know that the real error is that
"foobar" is meant to be a revision, but could not be
resolved.
Signed-off-by: Jeff King <peff@peff.net>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-07 02:05:48 +04:00
|
|
|
printf "%s\n" --
|
|
|
|
} >expect &&
|
|
|
|
git rev-parse foobar -- >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2019-09-15 15:10:28 +03:00
|
|
|
test_expect_success 'reject Nth parent if N is too high' '
|
2019-09-15 15:03:25 +03:00
|
|
|
test_must_fail git rev-parse HEAD^100000000000000000000000000000000
|
|
|
|
'
|
|
|
|
|
2019-09-15 15:10:28 +03:00
|
|
|
test_expect_success 'reject Nth ancestor if N is too high' '
|
2019-09-15 15:03:25 +03:00
|
|
|
test_must_fail git rev-parse HEAD~100000000000000000000000000000000
|
|
|
|
'
|
|
|
|
|
verify_filename(): handle backslashes in "wildcards are pathspecs" rule
Commit 28fcc0b71a (pathspec: avoid the need of "--" when wildcard is
used, 2015-05-02) allowed:
git rev-parse '*.c'
without the double-dash. But the rule it uses to check for wildcards
actually looks for any glob special. This is overly liberal, as it means
that a pattern that doesn't actually do any wildcard matching, like
"a\b", will be considered a pathspec.
If you do have such a file on disk, that's presumably what you wanted.
But if you don't, the results are confusing: rather than say "there's no
such path a\b", we'll quietly accept it as a pathspec which very likely
matches nothing (or at least not what you intended). Likewise, looking
for path "a\*b" doesn't expand the search at all; it would only find a
single entry, "a*b".
This commit switches the rule to trigger only when glob metacharacters
would expand the search, meaning both of those cases will now report an
error (you can still disambiguate using "--", of course; we're just
tightening the DWIM heuristic).
Note that we didn't test the original feature in 28fcc0b71a at all. So
this patch not only tests for these corner cases, but also adds a
regression test for the existing behavior.
Reported-by: David Burström <davidburstrom@spotify.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-01-25 03:00:51 +03:00
|
|
|
test_expect_success 'pathspecs with wildcards are not ambiguous' '
|
|
|
|
echo "*.c" >expect &&
|
|
|
|
git rev-parse "*.c" >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'backslash does not trigger wildcard rule' '
|
|
|
|
test_must_fail git rev-parse "foo\\bar"
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'escaped char does not trigger wildcard rule' '
|
|
|
|
test_must_fail git rev-parse "foo\\*bar"
|
|
|
|
'
|
|
|
|
|
2020-11-11 00:37:27 +03:00
|
|
|
test_expect_success 'arg after dashdash not interpreted as option' '
|
|
|
|
cat >expect <<-\EOF &&
|
|
|
|
--
|
|
|
|
--local-env-vars
|
|
|
|
EOF
|
|
|
|
git rev-parse -- --local-env-vars >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
rev-parse: handle --end-of-options
We taught rev-list a new way to separate options from revisions in
19e8789b23 (revision: allow --end-of-options to end option parsing,
2019-08-06), but rev-parse uses its own parser. It should know about
--end-of-options not only for consistency, but because it may be
presented with similarly ambiguous cases. E.g., if a caller does:
git rev-parse "$rev" -- "$path"
to parse an untrusted input, then it will get confused if $rev contains
an option-like string like "--local-env-vars". Or even "--not-real",
which we'd keep as an option to pass along to rev-list.
Or even more importantly:
git rev-parse --verify "$rev"
can be confused by options, even though its purpose is safely parsing
untrusted input. On the plus side, it will always fail the --verify
part, as it will not have parsed a revision, so the caller will
generally "fail closed" rather than continue to use the untrusted
string. But it will still trigger whatever option was in "$rev"; this
should be mostly harmless, since rev-parse options are all read-only,
but I didn't carefully audit all paths.
This patch lets callers write:
git rev-parse --end-of-options "$rev" -- "$path"
and:
git rev-parse --verify --end-of-options "$rev"
which will both treat "$rev" always as a revision parameter. The latter
is a bit clunky. It would be nicer if we had defined "--verify" to
require that its next argument be the revision. But we have not
historically done so, and:
git rev-parse --verify -q "$rev"
does currently work. I added a test here to confirm that we didn't break
that.
A few implementation notes:
- We don't document --end-of-options explicitly in commands, but rather
in gitcli(7). So I didn't give it its own section in git-rev-parse(1).
But I did call it out specifically in the --verify section, and
include it in the examples, which should show best practices.
- We don't have to re-indent the main option-parsing block, because we
can combine our "did we see end of options" check with "does it start
with a dash". The exception is the pre-setup options, which need
their own block.
- We do however have to pull the "--" parsing out of the "does it start
with dash" block, because we want to parse it even if we've seen
--end-of-options.
- We'll leave "--end-of-options" in the output. This is probably not
technically necessary, as a careful caller will do:
git rev-parse --end-of-options $revs -- $paths
and anything in $revs will be resolved to an object id. However, it
does help a slightly less careful caller like:
git rev-parse --end-of-options $revs_or_paths
where a path "--foo" will remain in the output as long as it also
exists on disk. In that case, it's helpful to retain --end-of-options
to get passed along to rev-list, s it would otherwise see just
"--foo".
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-11-11 00:40:19 +03:00
|
|
|
test_expect_success 'arg after end-of-options not interpreted as option' '
|
|
|
|
test_must_fail git rev-parse --end-of-options --not-real -- 2>err &&
|
|
|
|
test_i18ngrep bad.revision.*--not-real err
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'end-of-options still allows --' '
|
|
|
|
cat >expect <<-EOF &&
|
|
|
|
--end-of-options
|
|
|
|
$(git rev-parse --verify HEAD)
|
|
|
|
--
|
|
|
|
path
|
|
|
|
EOF
|
|
|
|
git rev-parse --end-of-options HEAD -- path >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2009-12-07 13:10:50 +03:00
|
|
|
test_done
|