Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2009 Johan Herland |
| 4 | # |
| 5 | |
| 6 | test_description='Test "git submodule foreach" |
| 7 | |
| 8 | This test verifies that "git submodule foreach" correctly visits all submodules |
| 9 | that are currently checked out. |
| 10 | ' |
| 11 | |
| 12 | . ./test-lib.sh |
| 13 | |
| 14 | |
| 15 | test_expect_success 'setup a submodule tree' ' |
| 16 | echo file > file && |
| 17 | git add file && |
| 18 | test_tick && |
Jens Lehmann | 4bf9dd9 | 2010-09-01 23:28:27 +0200 | [diff] [blame] | 19 | git commit -m upstream && |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 20 | git clone . super && |
| 21 | git clone super submodule && |
| 22 | ( |
| 23 | cd super && |
| 24 | git submodule add ../submodule sub1 && |
| 25 | git submodule add ../submodule sub2 && |
| 26 | git submodule add ../submodule sub3 && |
| 27 | git config -f .gitmodules --rename-section \ |
| 28 | submodule.sub1 submodule.foo1 && |
| 29 | git config -f .gitmodules --rename-section \ |
| 30 | submodule.sub2 submodule.foo2 && |
| 31 | git config -f .gitmodules --rename-section \ |
| 32 | submodule.sub3 submodule.foo3 && |
Jens Lehmann | 4bf9dd9 | 2010-09-01 23:28:27 +0200 | [diff] [blame] | 33 | git add .gitmodules && |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 34 | test_tick && |
| 35 | git commit -m "submodules" && |
| 36 | git submodule init sub1 && |
| 37 | git submodule init sub2 && |
| 38 | git submodule init sub3 |
| 39 | ) && |
| 40 | ( |
| 41 | cd submodule && |
| 42 | echo different > file && |
| 43 | git add file && |
| 44 | test_tick && |
| 45 | git commit -m "different" |
| 46 | ) && |
| 47 | ( |
| 48 | cd super && |
| 49 | ( |
| 50 | cd sub3 && |
| 51 | git pull |
| 52 | ) && |
| 53 | git add sub3 && |
| 54 | test_tick && |
| 55 | git commit -m "update sub3" |
| 56 | ) |
| 57 | ' |
| 58 | |
| 59 | sub1sha1=$(cd super/sub1 && git rev-parse HEAD) |
| 60 | sub3sha1=$(cd super/sub3 && git rev-parse HEAD) |
| 61 | |
Ævar Arnfjörð Bjarmason | f030c96 | 2010-05-21 16:10:10 +0000 | [diff] [blame] | 62 | pwd=$(pwd) |
| 63 | |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 64 | cat > expect <<EOF |
| 65 | Entering 'sub1' |
Ævar Arnfjörð Bjarmason | f030c96 | 2010-05-21 16:10:10 +0000 | [diff] [blame] | 66 | $pwd/clone-foo1-sub1-$sub1sha1 |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 67 | Entering 'sub3' |
Ævar Arnfjörð Bjarmason | f030c96 | 2010-05-21 16:10:10 +0000 | [diff] [blame] | 68 | $pwd/clone-foo3-sub3-$sub3sha1 |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 69 | EOF |
| 70 | |
| 71 | test_expect_success 'test basic "submodule foreach" usage' ' |
| 72 | git clone super clone && |
| 73 | ( |
| 74 | cd clone && |
| 75 | git submodule update --init -- sub1 sub3 && |
Ævar Arnfjörð Bjarmason | f030c96 | 2010-05-21 16:10:10 +0000 | [diff] [blame] | 76 | git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual && |
| 77 | git config foo.bar zar && |
| 78 | git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar" |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 79 | ) && |
Ævar Arnfjörð Bjarmason | 490b6d5 | 2011-05-21 18:44:06 +0000 | [diff] [blame] | 80 | test_i18ncmp expect actual |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 81 | ' |
| 82 | |
John Keeping | 091a6eb | 2013-06-16 15:18:18 +0100 | [diff] [blame] | 83 | cat >expect <<EOF |
| 84 | Entering '../sub1' |
| 85 | $pwd/clone-foo1-../sub1-$sub1sha1 |
| 86 | Entering '../sub3' |
| 87 | $pwd/clone-foo3-../sub3-$sub3sha1 |
| 88 | EOF |
| 89 | |
| 90 | test_expect_success 'test "submodule foreach" from subdirectory' ' |
| 91 | mkdir clone/sub && |
| 92 | ( |
| 93 | cd clone/sub && |
| 94 | git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$sha1" >../../actual |
| 95 | ) && |
| 96 | test_i18ncmp expect actual |
| 97 | ' |
| 98 | |
Johan Herland | 15fc56a | 2009-08-19 03:45:22 +0200 | [diff] [blame] | 99 | test_expect_success 'setup nested submodules' ' |
| 100 | git clone submodule nested1 && |
| 101 | git clone submodule nested2 && |
| 102 | git clone submodule nested3 && |
| 103 | ( |
| 104 | cd nested3 && |
| 105 | git submodule add ../submodule submodule && |
| 106 | test_tick && |
| 107 | git commit -m "submodule" && |
| 108 | git submodule init submodule |
| 109 | ) && |
| 110 | ( |
| 111 | cd nested2 && |
| 112 | git submodule add ../nested3 nested3 && |
| 113 | test_tick && |
| 114 | git commit -m "nested3" && |
| 115 | git submodule init nested3 |
| 116 | ) && |
| 117 | ( |
| 118 | cd nested1 && |
| 119 | git submodule add ../nested2 nested2 && |
| 120 | test_tick && |
| 121 | git commit -m "nested2" && |
| 122 | git submodule init nested2 |
| 123 | ) && |
| 124 | ( |
| 125 | cd super && |
| 126 | git submodule add ../nested1 nested1 && |
| 127 | test_tick && |
| 128 | git commit -m "nested1" && |
| 129 | git submodule init nested1 |
| 130 | ) |
| 131 | ' |
| 132 | |
| 133 | test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' ' |
| 134 | git clone super clone2 && |
| 135 | ( |
| 136 | cd clone2 && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 137 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
| 138 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && |
| 139 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && |
| 140 | test_must_fail git rev-parse --resolve-git-dir nested1/.git && |
Johan Herland | 15fc56a | 2009-08-19 03:45:22 +0200 | [diff] [blame] | 141 | git submodule update --init && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 142 | git rev-parse --resolve-git-dir sub1/.git && |
| 143 | git rev-parse --resolve-git-dir sub2/.git && |
| 144 | git rev-parse --resolve-git-dir sub3/.git && |
| 145 | git rev-parse --resolve-git-dir nested1/.git && |
| 146 | test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git && |
Johan Herland | 15fc56a | 2009-08-19 03:45:22 +0200 | [diff] [blame] | 147 | git submodule foreach "git submodule update --init" && |
Phil Hord | dfe338a | 2013-08-09 16:12:54 -0400 | [diff] [blame] | 148 | git rev-parse --resolve-git-dir nested1/nested2/.git && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 149 | test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git |
Johan Herland | 15fc56a | 2009-08-19 03:45:22 +0200 | [diff] [blame] | 150 | ) |
| 151 | ' |
| 152 | |
| 153 | test_expect_success 'use "foreach --recursive" to checkout all submodules' ' |
| 154 | ( |
| 155 | cd clone2 && |
| 156 | git submodule foreach --recursive "git submodule update --init" && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 157 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && |
| 158 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git |
Johan Herland | 15fc56a | 2009-08-19 03:45:22 +0200 | [diff] [blame] | 159 | ) |
| 160 | ' |
| 161 | |
| 162 | cat > expect <<EOF |
| 163 | Entering 'nested1' |
| 164 | Entering 'nested1/nested2' |
| 165 | Entering 'nested1/nested2/nested3' |
| 166 | Entering 'nested1/nested2/nested3/submodule' |
| 167 | Entering 'sub1' |
| 168 | Entering 'sub2' |
| 169 | Entering 'sub3' |
| 170 | EOF |
| 171 | |
| 172 | test_expect_success 'test messages from "foreach --recursive"' ' |
| 173 | ( |
| 174 | cd clone2 && |
| 175 | git submodule foreach --recursive "true" > ../actual |
| 176 | ) && |
Ævar Arnfjörð Bjarmason | 490b6d5 | 2011-05-21 18:44:06 +0000 | [diff] [blame] | 177 | test_i18ncmp expect actual |
Johan Herland | 15fc56a | 2009-08-19 03:45:22 +0200 | [diff] [blame] | 178 | ' |
| 179 | |
| 180 | cat > expect <<EOF |
| 181 | nested1-nested1 |
| 182 | nested2-nested2 |
| 183 | nested3-nested3 |
| 184 | submodule-submodule |
| 185 | foo1-sub1 |
| 186 | foo2-sub2 |
| 187 | foo3-sub3 |
| 188 | EOF |
| 189 | |
| 190 | test_expect_success 'test "foreach --quiet --recursive"' ' |
| 191 | ( |
| 192 | cd clone2 && |
| 193 | git submodule foreach -q --recursive "echo \$name-\$path" > ../actual |
| 194 | ) && |
| 195 | test_cmp expect actual |
| 196 | ' |
| 197 | |
Johan Herland | b13fd5c | 2009-08-19 03:45:23 +0200 | [diff] [blame] | 198 | test_expect_success 'use "update --recursive" to checkout all submodules' ' |
| 199 | git clone super clone3 && |
| 200 | ( |
| 201 | cd clone3 && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 202 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
| 203 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && |
| 204 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && |
| 205 | test_must_fail git rev-parse --resolve-git-dir nested1/.git && |
Johan Herland | b13fd5c | 2009-08-19 03:45:23 +0200 | [diff] [blame] | 206 | git submodule update --init --recursive && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 207 | git rev-parse --resolve-git-dir sub1/.git && |
| 208 | git rev-parse --resolve-git-dir sub2/.git && |
| 209 | git rev-parse --resolve-git-dir sub3/.git && |
| 210 | git rev-parse --resolve-git-dir nested1/.git && |
| 211 | git rev-parse --resolve-git-dir nested1/nested2/.git && |
| 212 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && |
| 213 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git |
Johan Herland | b13fd5c | 2009-08-19 03:45:23 +0200 | [diff] [blame] | 214 | ) |
| 215 | ' |
| 216 | |
Johan Herland | 64b19ff | 2009-08-19 03:45:24 +0200 | [diff] [blame] | 217 | nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD) |
| 218 | nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD) |
| 219 | nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD) |
| 220 | submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD) |
| 221 | sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD) |
| 222 | sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD) |
| 223 | sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD) |
Johan Herland | e3ae4a8 | 2009-08-20 11:24:54 +0200 | [diff] [blame] | 224 | sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD) |
| 225 | sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD) |
Johan Herland | 64b19ff | 2009-08-19 03:45:24 +0200 | [diff] [blame] | 226 | |
| 227 | cat > expect <<EOF |
| 228 | $nested1sha1 nested1 (heads/master) |
| 229 | $nested2sha1 nested1/nested2 (heads/master) |
| 230 | $nested3sha1 nested1/nested2/nested3 (heads/master) |
| 231 | $submodulesha1 nested1/nested2/nested3/submodule (heads/master) |
Johan Herland | e3ae4a8 | 2009-08-20 11:24:54 +0200 | [diff] [blame] | 232 | $sub1sha1 sub1 ($sub1sha1_short) |
| 233 | $sub2sha1 sub2 ($sub2sha1_short) |
Johan Herland | 64b19ff | 2009-08-19 03:45:24 +0200 | [diff] [blame] | 234 | $sub3sha1 sub3 (heads/master) |
| 235 | EOF |
| 236 | |
| 237 | test_expect_success 'test "status --recursive"' ' |
| 238 | ( |
| 239 | cd clone3 && |
| 240 | git submodule status --recursive > ../actual |
| 241 | ) && |
| 242 | test_cmp expect actual |
| 243 | ' |
| 244 | |
Phil Hord | 58ca9ad | 2012-10-26 15:13:54 -0400 | [diff] [blame] | 245 | sed -e "/nested2 /s/.*/+$nested2sha1 nested1\/nested2 (file2~1)/;/sub[1-3]/d" < expect > expect2 |
Kevin Ballard | 98dbe63 | 2010-11-02 23:26:25 -0700 | [diff] [blame] | 246 | mv -f expect2 expect |
| 247 | |
| 248 | test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' ' |
| 249 | ( |
| 250 | cd clone3 && |
| 251 | ( |
Phil Hord | 58ca9ad | 2012-10-26 15:13:54 -0400 | [diff] [blame] | 252 | cd nested1/nested2 && |
Kevin Ballard | 98dbe63 | 2010-11-02 23:26:25 -0700 | [diff] [blame] | 253 | test_commit file2 |
| 254 | ) && |
| 255 | git submodule status --cached --recursive -- nested1 > ../actual |
| 256 | ) && |
Pat Thoyts | c91897b | 2011-02-03 15:31:44 +0000 | [diff] [blame] | 257 | if test_have_prereq MINGW |
| 258 | then |
| 259 | dos2unix actual |
| 260 | fi && |
Kevin Ballard | 98dbe63 | 2010-11-02 23:26:25 -0700 | [diff] [blame] | 261 | test_cmp expect actual |
| 262 | ' |
| 263 | |
Johan Herland | e7fed18 | 2009-08-20 01:07:43 +0200 | [diff] [blame] | 264 | test_expect_success 'use "git clone --recursive" to checkout all submodules' ' |
| 265 | git clone --recursive super clone4 && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 266 | ( |
| 267 | cd clone4 && |
| 268 | git rev-parse --resolve-git-dir .git && |
| 269 | git rev-parse --resolve-git-dir sub1/.git && |
| 270 | git rev-parse --resolve-git-dir sub2/.git && |
| 271 | git rev-parse --resolve-git-dir sub3/.git && |
| 272 | git rev-parse --resolve-git-dir nested1/.git && |
| 273 | git rev-parse --resolve-git-dir nested1/nested2/.git && |
| 274 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && |
| 275 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git |
| 276 | ) |
Johan Herland | e7fed18 | 2009-08-20 01:07:43 +0200 | [diff] [blame] | 277 | ' |
| 278 | |
Kevin Ballard | a7eff1a | 2010-11-02 23:26:24 -0700 | [diff] [blame] | 279 | test_expect_success 'test "update --recursive" with a flag with spaces' ' |
| 280 | git clone super "common objects" && |
| 281 | git clone super clone5 && |
| 282 | ( |
| 283 | cd clone5 && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 284 | test_must_fail git rev-parse --resolve-git-dir d nested1/.git && |
Kevin Ballard | a7eff1a | 2010-11-02 23:26:24 -0700 | [diff] [blame] | 285 | git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 286 | git rev-parse --resolve-git-dir nested1/.git && |
| 287 | git rev-parse --resolve-git-dir nested1/nested2/.git && |
| 288 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && |
| 289 | test -f .git/modules/nested1/objects/info/alternates && |
| 290 | test -f .git/modules/nested1/modules/nested2/objects/info/alternates && |
| 291 | test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates |
Kevin Ballard | a7eff1a | 2010-11-02 23:26:24 -0700 | [diff] [blame] | 292 | ) |
| 293 | ' |
| 294 | |
Kevin Ballard | 98dbe63 | 2010-11-02 23:26:25 -0700 | [diff] [blame] | 295 | test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' ' |
| 296 | git clone super clone6 && |
| 297 | ( |
| 298 | cd clone6 && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 299 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
| 300 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && |
| 301 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && |
| 302 | test_must_fail git rev-parse --resolve-git-dir nested1/.git && |
Kevin Ballard | 98dbe63 | 2010-11-02 23:26:25 -0700 | [diff] [blame] | 303 | git submodule update --init --recursive -- nested1 && |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 304 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
| 305 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && |
| 306 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && |
| 307 | git rev-parse --resolve-git-dir nested1/.git && |
| 308 | git rev-parse --resolve-git-dir nested1/nested2/.git && |
| 309 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && |
| 310 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git |
Kevin Ballard | 98dbe63 | 2010-11-02 23:26:25 -0700 | [diff] [blame] | 311 | ) |
| 312 | ' |
| 313 | |
Brandon Casey | 4dca1aa | 2011-06-29 19:34:58 -0500 | [diff] [blame] | 314 | test_expect_success 'command passed to foreach retains notion of stdin' ' |
Brandon Casey | 91cd7e4 | 2011-06-29 19:34:57 -0500 | [diff] [blame] | 315 | ( |
| 316 | cd super && |
| 317 | git submodule foreach echo success >../expected && |
| 318 | yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual |
| 319 | ) && |
| 320 | test_cmp expected actual |
| 321 | ' |
| 322 | |
Brandon Casey | 4dca1aa | 2011-06-29 19:34:58 -0500 | [diff] [blame] | 323 | test_expect_success 'command passed to foreach --recursive retains notion of stdin' ' |
Brandon Casey | 91cd7e4 | 2011-06-29 19:34:57 -0500 | [diff] [blame] | 324 | ( |
| 325 | cd clone2 && |
| 326 | git submodule foreach --recursive echo success >../expected && |
| 327 | yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual |
| 328 | ) && |
| 329 | test_cmp expected actual |
| 330 | ' |
| 331 | |
Johan Herland | d69ecf6 | 2009-08-19 03:45:20 +0200 | [diff] [blame] | 332 | test_done |