t / t4041-diff-submodule-option.shon commit git p4: handle servers without move support (249da4c)
   1#!/bin/sh
   2#
   3# Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin
   4#
   5
   6test_description='Support for verbose submodule differences in git diff
   7
   8This test tries to verify the sanity of the --submodule option of git diff.
   9'
  10
  11. ./test-lib.sh
  12
  13add_file () {
  14        sm=$1
  15        shift
  16        owd=$(pwd)
  17        cd "$sm"
  18        for name; do
  19                echo "$name" > "$name" &&
  20                git add "$name" &&
  21                test_tick &&
  22                git commit -m "Add $name"
  23        done >/dev/null
  24        git rev-parse --verify HEAD | cut -c1-7
  25        cd "$owd"
  26}
  27commit_file () {
  28        test_tick &&
  29        git commit "$@" -m "Commit $*" >/dev/null
  30}
  31
  32test_create_repo sm1 &&
  33add_file . foo >/dev/null
  34
  35head1=$(add_file sm1 foo1 foo2)
  36fullhead1=$(cd sm1; git rev-list --max-count=1 $head1)
  37
  38test_expect_success 'added submodule' "
  39        git add sm1 &&
  40        git diff-index -p --submodule=log HEAD >actual &&
  41        cat >expected <<-EOF &&
  42Submodule sm1 0000000...$head1 (new submodule)
  43EOF
  44        test_cmp expected actual
  45"
  46
  47test_expect_success 'added submodule, set diff.submodule' "
  48        git config diff.submodule log &&
  49        git add sm1 &&
  50        git diff --cached >actual &&
  51        cat >expected <<-EOF &&
  52Submodule sm1 0000000...$head1 (new submodule)
  53EOF
  54        git config --unset diff.submodule &&
  55        test_cmp expected actual
  56"
  57
  58test_expect_success '--submodule=short overrides diff.submodule' "
  59        test_config diff.submodule log &&
  60        git add sm1 &&
  61        git diff --submodule=short --cached >actual &&
  62        cat >expected <<-EOF &&
  63diff --git a/sm1 b/sm1
  64new file mode 160000
  65index 0000000..a2c4dab
  66--- /dev/null
  67+++ b/sm1
  68@@ -0,0 +1 @@
  69+Subproject commit $fullhead1
  70EOF
  71        test_cmp expected actual
  72"
  73
  74test_expect_success 'diff.submodule does not affect plumbing' '
  75        test_config diff.submodule log &&
  76        git diff-index -p HEAD >actual &&
  77        cat >expected <<-EOF &&
  78        diff --git a/sm1 b/sm1
  79        new file mode 160000
  80        index 0000000..a2c4dab
  81        --- /dev/null
  82        +++ b/sm1
  83        @@ -0,0 +1 @@
  84        +Subproject commit $fullhead1
  85        EOF
  86        test_cmp expected actual
  87'
  88
  89commit_file sm1 &&
  90head2=$(add_file sm1 foo3)
  91
  92test_expect_success 'modified submodule(forward)' "
  93        git diff-index -p --submodule=log HEAD >actual &&
  94        cat >expected <<-EOF &&
  95Submodule sm1 $head1..$head2:
  96  > Add foo3
  97EOF
  98        test_cmp expected actual
  99"
 100
 101test_expect_success 'modified submodule(forward)' "
 102        git diff --submodule=log >actual &&
 103        cat >expected <<-EOF &&
 104Submodule sm1 $head1..$head2:
 105  > Add foo3
 106EOF
 107        test_cmp expected actual
 108"
 109
 110test_expect_success 'modified submodule(forward) --submodule' "
 111        git diff --submodule >actual &&
 112        cat >expected <<-EOF &&
 113Submodule sm1 $head1..$head2:
 114  > Add foo3
 115EOF
 116        test_cmp expected actual
 117"
 118
 119fullhead2=$(cd sm1; git rev-list --max-count=1 $head2)
 120test_expect_success 'modified submodule(forward) --submodule=short' "
 121        git diff --submodule=short >actual &&
 122        cat >expected <<-EOF &&
 123diff --git a/sm1 b/sm1
 124index $head1..$head2 160000
 125--- a/sm1
 126+++ b/sm1
 127@@ -1 +1 @@
 128-Subproject commit $fullhead1
 129+Subproject commit $fullhead2
 130EOF
 131        test_cmp expected actual
 132"
 133
 134commit_file sm1 &&
 135head3=$(
 136        cd sm1 &&
 137        git reset --hard HEAD~2 >/dev/null &&
 138        git rev-parse --verify HEAD | cut -c1-7
 139)
 140
 141test_expect_success 'modified submodule(backward)' "
 142        git diff-index -p --submodule=log HEAD >actual &&
 143        cat >expected <<-EOF &&
 144Submodule sm1 $head2..$head3 (rewind):
 145  < Add foo3
 146  < Add foo2
 147EOF
 148        test_cmp expected actual
 149"
 150
 151head4=$(add_file sm1 foo4 foo5) &&
 152head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD)
 153test_expect_success 'modified submodule(backward and forward)' "
 154        git diff-index -p --submodule=log HEAD >actual &&
 155        cat >expected <<-EOF &&
 156Submodule sm1 $head2...$head4:
 157  > Add foo5
 158  > Add foo4
 159  < Add foo3
 160  < Add foo2
 161EOF
 162        test_cmp expected actual
 163"
 164
 165commit_file sm1 &&
 166mv sm1 sm1-bak &&
 167echo sm1 >sm1 &&
 168head5=$(git hash-object sm1 | cut -c1-7) &&
 169git add sm1 &&
 170rm -f sm1 &&
 171mv sm1-bak sm1
 172
 173test_expect_success 'typechanged submodule(submodule->blob), --cached' "
 174        git diff --submodule=log --cached >actual &&
 175        cat >expected <<-EOF &&
 176Submodule sm1 41fbea9...0000000 (submodule deleted)
 177diff --git a/sm1 b/sm1
 178new file mode 100644
 179index 0000000..9da5fb8
 180--- /dev/null
 181+++ b/sm1
 182@@ -0,0 +1 @@
 183+sm1
 184EOF
 185        test_cmp expected actual
 186"
 187
 188test_expect_success 'typechanged submodule(submodule->blob)' "
 189        git diff --submodule=log >actual &&
 190        cat >expected <<-EOF &&
 191diff --git a/sm1 b/sm1
 192deleted file mode 100644
 193index 9da5fb8..0000000
 194--- a/sm1
 195+++ /dev/null
 196@@ -1 +0,0 @@
 197-sm1
 198Submodule sm1 0000000...$head4 (new submodule)
 199EOF
 200        test_cmp expected actual
 201"
 202
 203rm -rf sm1 &&
 204git checkout-index sm1
 205test_expect_success 'typechanged submodule(submodule->blob)' "
 206        git diff-index -p --submodule=log HEAD >actual &&
 207        cat >expected <<-EOF &&
 208Submodule sm1 $head4...0000000 (submodule deleted)
 209diff --git a/sm1 b/sm1
 210new file mode 100644
 211index 0000000..$head5
 212--- /dev/null
 213+++ b/sm1
 214@@ -0,0 +1 @@
 215+sm1
 216EOF
 217        test_cmp expected actual
 218"
 219
 220rm -f sm1 &&
 221test_create_repo sm1 &&
 222head6=$(add_file sm1 foo6 foo7)
 223fullhead6=$(cd sm1; git rev-list --max-count=1 $head6)
 224test_expect_success 'nonexistent commit' "
 225        git diff-index -p --submodule=log HEAD >actual &&
 226        cat >expected <<-EOF &&
 227Submodule sm1 $head4...$head6 (commits not present)
 228EOF
 229        test_cmp expected actual
 230"
 231
 232commit_file
 233test_expect_success 'typechanged submodule(blob->submodule)' "
 234        git diff-index -p --submodule=log HEAD >actual &&
 235        cat >expected <<-EOF &&
 236diff --git a/sm1 b/sm1
 237deleted file mode 100644
 238index $head5..0000000
 239--- a/sm1
 240+++ /dev/null
 241@@ -1 +0,0 @@
 242-sm1
 243Submodule sm1 0000000...$head6 (new submodule)
 244EOF
 245        test_cmp expected actual
 246"
 247
 248commit_file sm1 &&
 249test_expect_success 'submodule is up to date' "
 250        git diff-index -p --submodule=log HEAD >actual &&
 251        cat >expected <<-EOF &&
 252EOF
 253        test_cmp expected actual
 254"
 255
 256test_expect_success 'submodule contains untracked content' "
 257        echo new > sm1/new-file &&
 258        git diff-index -p --submodule=log HEAD >actual &&
 259        cat >expected <<-EOF &&
 260Submodule sm1 contains untracked content
 261EOF
 262        test_cmp expected actual
 263"
 264
 265test_expect_success 'submodule contains untracked content (untracked ignored)' "
 266        git diff-index -p --ignore-submodules=untracked --submodule=log HEAD >actual &&
 267        ! test -s actual
 268"
 269
 270test_expect_success 'submodule contains untracked content (dirty ignored)' "
 271        git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
 272        ! test -s actual
 273"
 274
 275test_expect_success 'submodule contains untracked content (all ignored)' "
 276        git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
 277        ! test -s actual
 278"
 279
 280test_expect_success 'submodule contains untracked and modifed content' "
 281        echo new > sm1/foo6 &&
 282        git diff-index -p --submodule=log HEAD >actual &&
 283        cat >expected <<-EOF &&
 284Submodule sm1 contains untracked content
 285Submodule sm1 contains modified content
 286EOF
 287        test_cmp expected actual
 288"
 289
 290test_expect_success 'submodule contains untracked and modifed content (untracked ignored)' "
 291        echo new > sm1/foo6 &&
 292        git diff-index -p --ignore-submodules=untracked --submodule=log HEAD >actual &&
 293        cat >expected <<-EOF &&
 294Submodule sm1 contains modified content
 295EOF
 296        test_cmp expected actual
 297"
 298
 299test_expect_success 'submodule contains untracked and modifed content (dirty ignored)' "
 300        echo new > sm1/foo6 &&
 301        git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
 302        ! test -s actual
 303"
 304
 305test_expect_success 'submodule contains untracked and modifed content (all ignored)' "
 306        echo new > sm1/foo6 &&
 307        git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
 308        ! test -s actual
 309"
 310
 311test_expect_success 'submodule contains modifed content' "
 312        rm -f sm1/new-file &&
 313        git diff-index -p --submodule=log HEAD >actual &&
 314        cat >expected <<-EOF &&
 315Submodule sm1 contains modified content
 316EOF
 317        test_cmp expected actual
 318"
 319
 320(cd sm1; git commit -mchange foo6 >/dev/null) &&
 321head8=$(cd sm1; git rev-parse --verify HEAD | cut -c1-7) &&
 322test_expect_success 'submodule is modified' "
 323        git diff-index -p --submodule=log HEAD >actual &&
 324        cat >expected <<-EOF &&
 325Submodule sm1 $head6..$head8:
 326  > change
 327EOF
 328        test_cmp expected actual
 329"
 330
 331test_expect_success 'modified submodule contains untracked content' "
 332        echo new > sm1/new-file &&
 333        git diff-index -p --submodule=log HEAD >actual &&
 334        cat >expected <<-EOF &&
 335Submodule sm1 contains untracked content
 336Submodule sm1 $head6..$head8:
 337  > change
 338EOF
 339        test_cmp expected actual
 340"
 341
 342test_expect_success 'modified submodule contains untracked content (untracked ignored)' "
 343        git diff-index -p --ignore-submodules=untracked --submodule=log HEAD >actual &&
 344        cat >expected <<-EOF &&
 345Submodule sm1 $head6..$head8:
 346  > change
 347EOF
 348        test_cmp expected actual
 349"
 350
 351test_expect_success 'modified submodule contains untracked content (dirty ignored)' "
 352        git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
 353        cat >expected <<-EOF &&
 354Submodule sm1 $head6..$head8:
 355  > change
 356EOF
 357        test_cmp expected actual
 358"
 359
 360test_expect_success 'modified submodule contains untracked content (all ignored)' "
 361        git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
 362        ! test -s actual
 363"
 364
 365test_expect_success 'modified submodule contains untracked and modifed content' "
 366        echo modification >> sm1/foo6 &&
 367        git diff-index -p --submodule=log HEAD >actual &&
 368        cat >expected <<-EOF &&
 369Submodule sm1 contains untracked content
 370Submodule sm1 contains modified content
 371Submodule sm1 $head6..$head8:
 372  > change
 373EOF
 374        test_cmp expected actual
 375"
 376
 377test_expect_success 'modified submodule contains untracked and modifed content (untracked ignored)' "
 378        echo modification >> sm1/foo6 &&
 379        git diff-index -p --ignore-submodules=untracked --submodule=log HEAD >actual &&
 380        cat >expected <<-EOF &&
 381Submodule sm1 contains modified content
 382Submodule sm1 $head6..$head8:
 383  > change
 384EOF
 385        test_cmp expected actual
 386"
 387
 388test_expect_success 'modified submodule contains untracked and modifed content (dirty ignored)' "
 389        echo modification >> sm1/foo6 &&
 390        git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
 391        cat >expected <<-EOF &&
 392Submodule sm1 $head6..$head8:
 393  > change
 394EOF
 395        test_cmp expected actual
 396"
 397
 398test_expect_success 'modified submodule contains untracked and modifed content (all ignored)' "
 399        echo modification >> sm1/foo6 &&
 400        git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
 401        ! test -s actual
 402"
 403
 404test_expect_success 'modified submodule contains modifed content' "
 405        rm -f sm1/new-file &&
 406        git diff-index -p --submodule=log HEAD >actual &&
 407        cat >expected <<-EOF &&
 408Submodule sm1 contains modified content
 409Submodule sm1 $head6..$head8:
 410  > change
 411EOF
 412        test_cmp expected actual
 413"
 414
 415rm -rf sm1
 416test_expect_success 'deleted submodule' "
 417        git diff-index -p --submodule=log HEAD >actual &&
 418        cat >expected <<-EOF &&
 419Submodule sm1 $head6...0000000 (submodule deleted)
 420EOF
 421        test_cmp expected actual
 422"
 423
 424test_create_repo sm2 &&
 425head7=$(add_file sm2 foo8 foo9) &&
 426git add sm2
 427
 428test_expect_success 'multiple submodules' "
 429        git diff-index -p --submodule=log HEAD >actual &&
 430        cat >expected <<-EOF &&
 431Submodule sm1 $head6...0000000 (submodule deleted)
 432Submodule sm2 0000000...$head7 (new submodule)
 433EOF
 434        test_cmp expected actual
 435"
 436
 437test_expect_success 'path filter' "
 438        git diff-index -p --submodule=log HEAD sm2 >actual &&
 439        cat >expected <<-EOF &&
 440Submodule sm2 0000000...$head7 (new submodule)
 441EOF
 442        test_cmp expected actual
 443"
 444
 445commit_file sm2
 446test_expect_success 'given commit' "
 447        git diff-index -p --submodule=log HEAD^ >actual &&
 448        cat >expected <<-EOF &&
 449Submodule sm1 $head6...0000000 (submodule deleted)
 450Submodule sm2 0000000...$head7 (new submodule)
 451EOF
 452        test_cmp expected actual
 453"
 454
 455test_expect_success 'given commit --submodule' "
 456        git diff-index -p --submodule HEAD^ >actual &&
 457        cat >expected <<-EOF &&
 458Submodule sm1 $head6...0000000 (submodule deleted)
 459Submodule sm2 0000000...$head7 (new submodule)
 460EOF
 461        test_cmp expected actual
 462"
 463
 464fullhead7=$(cd sm2; git rev-list --max-count=1 $head7)
 465
 466test_expect_success 'given commit --submodule=short' "
 467        git diff-index -p --submodule=short HEAD^ >actual &&
 468        cat >expected <<-EOF &&
 469diff --git a/sm1 b/sm1
 470deleted file mode 160000
 471index $head6..0000000
 472--- a/sm1
 473+++ /dev/null
 474@@ -1 +0,0 @@
 475-Subproject commit $fullhead6
 476diff --git a/sm2 b/sm2
 477new file mode 160000
 478index 0000000..$head7
 479--- /dev/null
 480+++ b/sm2
 481@@ -0,0 +1 @@
 482+Subproject commit $fullhead7
 483EOF
 484        test_cmp expected actual
 485"
 486
 487test_expect_success 'setup .git file for sm2' '
 488        (cd sm2 &&
 489         REAL="$(pwd)/../.real" &&
 490         mv .git "$REAL"
 491         echo "gitdir: $REAL" >.git)
 492'
 493
 494test_expect_success 'diff --submodule with .git file' '
 495        git diff --submodule HEAD^ >actual &&
 496        cat >expected <<-EOF &&
 497Submodule sm1 $head6...0000000 (submodule deleted)
 498Submodule sm2 0000000...$head7 (new submodule)
 499EOF
 500        test_cmp expected actual
 501'
 502
 503test_expect_success 'diff --submodule with objects referenced by alternates' '
 504        mkdir sub_alt &&
 505        (cd sub_alt &&
 506                git init &&
 507                echo a >a &&
 508                git add a &&
 509                git commit -m a
 510        ) &&
 511        mkdir super &&
 512        (cd super &&
 513                git clone -s ../sub_alt sub &&
 514                git init &&
 515                git add sub &&
 516                git commit -m "sub a"
 517        ) &&
 518        (cd sub_alt &&
 519                sha1_before=$(git rev-parse --short HEAD)
 520                echo b >b &&
 521                git add b &&
 522                git commit -m b
 523                sha1_after=$(git rev-parse --short HEAD)
 524                echo "Submodule sub $sha1_before..$sha1_after:
 525  > b" >../expected
 526        ) &&
 527        (cd super &&
 528                (cd sub &&
 529                        git fetch &&
 530                        git checkout origin/master
 531                ) &&
 532                git diff --submodule > ../actual
 533        )
 534        test_cmp expected actual
 535'
 536
 537test_done