2009-12-07 11:10:50 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='test git rev-parse diagnosis for invalid argument'
|
|
|
|
|
|
|
|
exec </dev/null
|
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
2011-03-31 11:17:33 +02:00
|
|
|
test_did_you_mean ()
|
|
|
|
{
|
2011-05-09 06:43:20 +02:00
|
|
|
sq="'" &&
|
|
|
|
cat >expected <<-EOF &&
|
|
|
|
fatal: Path '$2$3' $4, but not ${5:-$sq$3$sq}.
|
|
|
|
Did you mean '$1:$2$3'${2:+ aka $sq$1:./$3$sq}?
|
|
|
|
EOF
|
2011-03-31 11:17:33 +02:00
|
|
|
test_cmp expected error
|
|
|
|
}
|
|
|
|
|
2009-12-07 11:10:50 +01: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 04:37:32 +01:00
|
|
|
test_expect_success 'correct relative file objects (0)' '
|
|
|
|
git rev-parse :file.txt >expected &&
|
|
|
|
git rev-parse :./file.txt >result &&
|
2010-12-09 22:38:05 +01:00
|
|
|
test_cmp expected result &&
|
|
|
|
git rev-parse :0:./file.txt >result &&
|
2010-11-28 04:37:32 +01: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-09 22:38:05 +01: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 11:10:50 +01:00
|
|
|
test_expect_success 'incorrect revision id' '
|
|
|
|
test_must_fail git rev-parse foobar:file.txt 2>error &&
|
|
|
|
grep "Invalid object name '"'"'foobar'"'"'." error &&
|
|
|
|
test_must_fail git rev-parse foobar 2> error &&
|
2016-06-17 22:21:06 +02:00
|
|
|
test_i18ngrep "unknown revision or path not in the working tree." error
|
2009-12-07 11:10:50 +01:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'incorrect file in sha1:path' '
|
|
|
|
test_must_fail git rev-parse HEAD:nothing.txt 2> error &&
|
|
|
|
grep "fatal: Path '"'"'nothing.txt'"'"' does not exist in '"'"'HEAD'"'"'" error &&
|
|
|
|
test_must_fail git rev-parse HEAD:index-only.txt 2> error &&
|
|
|
|
grep "fatal: Path '"'"'index-only.txt'"'"' exists on disk, but not in '"'"'HEAD'"'"'." error &&
|
|
|
|
(cd subdir &&
|
|
|
|
test_must_fail git rev-parse HEAD:file2.txt 2> error &&
|
2011-03-31 11:17:33 +02:00
|
|
|
test_did_you_mean HEAD subdir/ file2.txt exists )
|
2009-12-07 11:10:50 +01:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'incorrect file in :path and :N:path' '
|
|
|
|
test_must_fail git rev-parse :nothing.txt 2> error &&
|
|
|
|
grep "fatal: Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error &&
|
|
|
|
test_must_fail git rev-parse :1:nothing.txt 2> error &&
|
|
|
|
grep "Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error &&
|
|
|
|
test_must_fail git rev-parse :1:file.txt 2> error &&
|
2011-03-31 11:17:33 +02:00
|
|
|
test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
|
2009-12-07 11:10:50 +01:00
|
|
|
(cd subdir &&
|
|
|
|
test_must_fail git rev-parse :1:file.txt 2> error &&
|
2011-03-31 11:17:33 +02:00
|
|
|
test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
|
2009-12-07 11:10:50 +01:00
|
|
|
test_must_fail git rev-parse :file2.txt 2> error &&
|
2011-03-31 11:17:33 +02:00
|
|
|
test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
|
2009-12-07 11:10:50 +01:00
|
|
|
test_must_fail git rev-parse :2:file2.txt 2> error &&
|
2011-03-31 11:17:33 +02:00
|
|
|
test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
|
2009-12-07 11:10:50 +01:00
|
|
|
test_must_fail git rev-parse :disk-only.txt 2> error &&
|
|
|
|
grep "fatal: Path '"'"'disk-only.txt'"'"' exists on disk, but not in the index." error
|
|
|
|
'
|
|
|
|
|
2010-08-24 06:52:44 +02:00
|
|
|
test_expect_success 'invalid @{n} reference' '
|
|
|
|
test_must_fail git rev-parse master@{99999} >output 2>error &&
|
|
|
|
test -z "$(cat output)" &&
|
|
|
|
grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error &&
|
|
|
|
test_must_fail git rev-parse --verify master@{99999} >output 2>error &&
|
|
|
|
test -z "$(cat output)" &&
|
|
|
|
grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error
|
|
|
|
'
|
|
|
|
|
2010-11-28 04:37:32 +01:00
|
|
|
test_expect_success 'relative path not found' '
|
|
|
|
(
|
|
|
|
cd subdir &&
|
|
|
|
test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
|
|
|
|
grep subdir/nonexistent.txt error
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'relative path outside worktree' '
|
|
|
|
test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
|
|
|
|
test -z "$(cat output)" &&
|
2018-02-13 14:19:15 +01:00
|
|
|
test_i18ngrep "outside repository" error
|
2010-11-28 04:37:32 +01: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 &&
|
|
|
|
test -z "$(cat output)" &&
|
|
|
|
grep "relative path syntax can.t be used outside working tree." error
|
|
|
|
'
|
|
|
|
|
2012-06-18 20:18:20 +02: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 20:18:21 +02:00
|
|
|
test_i18ngrep "no such path in the working tree" error &&
|
2012-06-18 20:18:20 +02:00
|
|
|
cat >expect <<-\EOF &&
|
|
|
|
file.txt
|
|
|
|
HEAD:file.txt
|
|
|
|
EOF
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2011-05-02 22:39:16 +02: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
|
|
|
|
'
|
|
|
|
|
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-06 23:05:48 +01: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
|
|
|
|
cat .git/refs/heads/foobar &&
|
|
|
|
printf "%s\n" --
|
|
|
|
} >expect &&
|
|
|
|
git rev-parse foobar -- >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2019-09-15 14:03:25 +02:00
|
|
|
test_expect_failure 'reject Nth parent if N is too high' '
|
|
|
|
test_must_fail git rev-parse HEAD^100000000000000000000000000000000
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_failure 'reject Nth ancestor if N is too high' '
|
|
|
|
test_must_fail git rev-parse HEAD~100000000000000000000000000000000
|
|
|
|
'
|
|
|
|
|
2009-12-07 11:10:50 +01:00
|
|
|
test_done
|