Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git rev-parse diagnosis for invalid argument' |
| 4 | |
| 5 | exec </dev/null |
| 6 | |
| 7 | . ./test-lib.sh |
| 8 | |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 9 | test_did_you_mean () |
| 10 | { |
Junio C Hamano | 365c2aa | 2011-05-08 21:43:20 -0700 | [diff] [blame] | 11 | sq="'" && |
| 12 | cat >expected <<-EOF && |
| 13 | fatal: Path '$2$3' $4, but not ${5:-$sq$3$sq}. |
| 14 | Did you mean '$1:$2$3'${2:+ aka $sq$1:./$3$sq}? |
| 15 | EOF |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 16 | test_cmp expected error |
| 17 | } |
| 18 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 19 | HASH_file= |
| 20 | |
| 21 | test_expect_success 'set up basic repo' ' |
| 22 | echo one > file.txt && |
| 23 | mkdir subdir && |
| 24 | echo two > subdir/file.txt && |
| 25 | echo three > subdir/file2.txt && |
| 26 | git add . && |
| 27 | git commit -m init && |
| 28 | echo four > index-only.txt && |
| 29 | git add index-only.txt && |
| 30 | echo five > disk-only.txt |
| 31 | ' |
| 32 | |
| 33 | test_expect_success 'correct file objects' ' |
| 34 | HASH_file=$(git rev-parse HEAD:file.txt) && |
| 35 | git rev-parse HEAD:subdir/file.txt && |
| 36 | git rev-parse :index-only.txt && |
| 37 | (cd subdir && |
| 38 | git rev-parse HEAD:subdir/file2.txt && |
| 39 | test $HASH_file = $(git rev-parse HEAD:file.txt) && |
| 40 | test $HASH_file = $(git rev-parse :file.txt) && |
| 41 | test $HASH_file = $(git rev-parse :0:file.txt) ) |
| 42 | ' |
| 43 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 44 | test_expect_success 'correct relative file objects (0)' ' |
| 45 | git rev-parse :file.txt >expected && |
| 46 | git rev-parse :./file.txt >result && |
Junio C Hamano | 3d6e0f7 | 2010-12-09 13:38:05 -0800 | [diff] [blame] | 47 | test_cmp expected result && |
| 48 | git rev-parse :0:./file.txt >result && |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 49 | test_cmp expected result |
| 50 | ' |
| 51 | |
| 52 | test_expect_success 'correct relative file objects (1)' ' |
| 53 | git rev-parse HEAD:file.txt >expected && |
| 54 | git rev-parse HEAD:./file.txt >result && |
| 55 | test_cmp expected result |
| 56 | ' |
| 57 | |
| 58 | test_expect_success 'correct relative file objects (2)' ' |
| 59 | ( |
| 60 | cd subdir && |
| 61 | git rev-parse HEAD:../file.txt >result && |
| 62 | test_cmp ../expected result |
| 63 | ) |
| 64 | ' |
| 65 | |
| 66 | test_expect_success 'correct relative file objects (3)' ' |
| 67 | ( |
| 68 | cd subdir && |
| 69 | git rev-parse HEAD:../subdir/../file.txt >result && |
| 70 | test_cmp ../expected result |
| 71 | ) |
| 72 | ' |
| 73 | |
| 74 | test_expect_success 'correct relative file objects (4)' ' |
| 75 | git rev-parse HEAD:subdir/file.txt >expected && |
| 76 | ( |
| 77 | cd subdir && |
| 78 | git rev-parse HEAD:./file.txt >result && |
| 79 | test_cmp ../expected result |
| 80 | ) |
| 81 | ' |
| 82 | |
Junio C Hamano | 3d6e0f7 | 2010-12-09 13:38:05 -0800 | [diff] [blame] | 83 | test_expect_success 'correct relative file objects (5)' ' |
| 84 | git rev-parse :subdir/file.txt >expected && |
| 85 | ( |
| 86 | cd subdir && |
| 87 | git rev-parse :./file.txt >result && |
| 88 | test_cmp ../expected result && |
| 89 | git rev-parse :0:./file.txt >result && |
| 90 | test_cmp ../expected result |
| 91 | ) |
| 92 | ' |
| 93 | |
| 94 | test_expect_success 'correct relative file objects (6)' ' |
| 95 | git rev-parse :file.txt >expected && |
| 96 | ( |
| 97 | cd subdir && |
| 98 | git rev-parse :../file.txt >result && |
| 99 | test_cmp ../expected result && |
| 100 | git rev-parse :0:../file.txt >result && |
| 101 | test_cmp ../expected result |
| 102 | ) |
| 103 | ' |
| 104 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 105 | test_expect_success 'incorrect revision id' ' |
| 106 | test_must_fail git rev-parse foobar:file.txt 2>error && |
| 107 | grep "Invalid object name '"'"'foobar'"'"'." error && |
| 108 | test_must_fail git rev-parse foobar 2> error && |
Vasco Almeida | ab33a76 | 2016-06-17 20:21:06 +0000 | [diff] [blame] | 109 | test_i18ngrep "unknown revision or path not in the working tree." error |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 110 | ' |
| 111 | |
| 112 | test_expect_success 'incorrect file in sha1:path' ' |
| 113 | test_must_fail git rev-parse HEAD:nothing.txt 2> error && |
| 114 | grep "fatal: Path '"'"'nothing.txt'"'"' does not exist in '"'"'HEAD'"'"'" error && |
| 115 | test_must_fail git rev-parse HEAD:index-only.txt 2> error && |
| 116 | grep "fatal: Path '"'"'index-only.txt'"'"' exists on disk, but not in '"'"'HEAD'"'"'." error && |
| 117 | (cd subdir && |
| 118 | test_must_fail git rev-parse HEAD:file2.txt 2> error && |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 119 | test_did_you_mean HEAD subdir/ file2.txt exists ) |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 120 | ' |
| 121 | |
| 122 | test_expect_success 'incorrect file in :path and :N:path' ' |
| 123 | test_must_fail git rev-parse :nothing.txt 2> error && |
| 124 | grep "fatal: Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error && |
| 125 | test_must_fail git rev-parse :1:nothing.txt 2> error && |
| 126 | grep "Path '"'"'nothing.txt'"'"' does not exist (neither on disk nor in the index)." error && |
| 127 | test_must_fail git rev-parse :1:file.txt 2> error && |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 128 | test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" && |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 129 | (cd subdir && |
| 130 | test_must_fail git rev-parse :1:file.txt 2> error && |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 131 | test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" && |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 132 | test_must_fail git rev-parse :file2.txt 2> error && |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 133 | test_did_you_mean ":0" subdir/ file2.txt "is in the index" && |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 134 | test_must_fail git rev-parse :2:file2.txt 2> error && |
Michael J Gruber | 34df9ab | 2011-03-31 11:17:33 +0200 | [diff] [blame] | 135 | test_did_you_mean :0 subdir/ file2.txt "is in the index") && |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 136 | test_must_fail git rev-parse :disk-only.txt 2> error && |
| 137 | grep "fatal: Path '"'"'disk-only.txt'"'"' exists on disk, but not in the index." error |
| 138 | ' |
| 139 | |
Jon Seymour | 9c46c05 | 2010-08-24 14:52:44 +1000 | [diff] [blame] | 140 | test_expect_success 'invalid @{n} reference' ' |
| 141 | test_must_fail git rev-parse master@{99999} >output 2>error && |
| 142 | test -z "$(cat output)" && |
| 143 | grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error && |
| 144 | test_must_fail git rev-parse --verify master@{99999} >output 2>error && |
| 145 | test -z "$(cat output)" && |
| 146 | grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error |
| 147 | ' |
| 148 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 149 | test_expect_success 'relative path not found' ' |
| 150 | ( |
| 151 | cd subdir && |
| 152 | test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error && |
| 153 | grep subdir/nonexistent.txt error |
| 154 | ) |
| 155 | ' |
| 156 | |
| 157 | test_expect_success 'relative path outside worktree' ' |
| 158 | test_must_fail git rev-parse HEAD:../file.txt >output 2>error && |
| 159 | test -z "$(cat output)" && |
| 160 | grep "outside repository" error |
| 161 | ' |
| 162 | |
| 163 | test_expect_success 'relative path when cwd is outside worktree' ' |
| 164 | test_must_fail git --git-dir=.git --work-tree=subdir rev-parse HEAD:./file.txt >output 2>error && |
| 165 | test -z "$(cat output)" && |
| 166 | grep "relative path syntax can.t be used outside working tree." error |
| 167 | ' |
| 168 | |
Matthieu Moy | d7236c4 | 2012-06-18 20:18:20 +0200 | [diff] [blame] | 169 | test_expect_success '<commit>:file correctly diagnosed after a pathname' ' |
| 170 | test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error && |
| 171 | test_i18ngrep ! "exists on disk" error && |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 172 | test_i18ngrep "no such path in the working tree" error && |
Matthieu Moy | d7236c4 | 2012-06-18 20:18:20 +0200 | [diff] [blame] | 173 | cat >expect <<-\EOF && |
| 174 | file.txt |
| 175 | HEAD:file.txt |
| 176 | EOF |
| 177 | test_cmp expect actual |
| 178 | ' |
| 179 | |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 180 | test_expect_success 'dotdot is not an empty set' ' |
| 181 | ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect && |
| 182 | |
| 183 | git rev-parse HEAD.. >actual && |
| 184 | test_cmp expect actual && |
| 185 | |
| 186 | git rev-parse ..HEAD >actual && |
| 187 | test_cmp expect actual && |
| 188 | |
| 189 | echo .. >expect && |
| 190 | git rev-parse .. >actual && |
| 191 | test_cmp expect actual |
| 192 | ' |
| 193 | |
Jeff King | 1418567 | 2013-12-06 17:05:48 -0500 | [diff] [blame] | 194 | test_expect_success 'arg before dashdash must be a revision (missing)' ' |
| 195 | test_must_fail git rev-parse foobar -- 2>stderr && |
| 196 | test_i18ngrep "bad revision" stderr |
| 197 | ' |
| 198 | |
| 199 | test_expect_success 'arg before dashdash must be a revision (file)' ' |
| 200 | >foobar && |
| 201 | test_must_fail git rev-parse foobar -- 2>stderr && |
| 202 | test_i18ngrep "bad revision" stderr |
| 203 | ' |
| 204 | |
| 205 | test_expect_success 'arg before dashdash must be a revision (ambiguous)' ' |
| 206 | >foobar && |
| 207 | git update-ref refs/heads/foobar HEAD && |
| 208 | { |
| 209 | # we do not want to use rev-parse here, because |
| 210 | # we are testing it |
| 211 | cat .git/refs/heads/foobar && |
| 212 | printf "%s\n" -- |
| 213 | } >expect && |
| 214 | git rev-parse foobar -- >actual && |
| 215 | test_cmp expect actual |
| 216 | ' |
| 217 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 218 | test_done |