1#!/bin/sh
   2#
   3# Copyright (c) 2008 Ping Yin
   4#
   5test_description='Summary support for submodules
   7This test tries to verify the sanity of summary subcommand of git submodule.
   9'
  10. ./test-lib.sh
  12add_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}
  31test_create_repo sm1 &&
  33add_file . foo >/dev/null
  34head1=$(add_file sm1 foo1 foo2)
  36test_expect_success 'added submodule' "
  38        git add sm1 &&
  39        git submodule summary >actual &&
  40        cat >expected <<-EOF &&
  41* sm1 0000000...$head1 (2):
  42  > Add foo2
  43EOF
  45        test_cmp expected actual
  46"
  47commit_file sm1 &&
  49head2=$(add_file sm1 foo3)
  50test_expect_success 'modified submodule(forward)' "
  52        git submodule summary >actual &&
  53        cat >expected <<-EOF &&
  54* sm1 $head1...$head2 (1):
  55  > Add foo3
  56EOF
  58        test_cmp expected actual
  59"
  60test_expect_success 'modified submodule(forward), --files' "
  62        git submodule summary --files >actual &&
  63        cat >expected <<-EOF &&
  64* sm1 $head1...$head2 (1):
  65  > Add foo3
  66EOF
  68        test_cmp expected actual
  69"
  70commit_file sm1 &&
  72head3=$(
  73        cd sm1 &&
  74        git reset --hard HEAD~2 >/dev/null &&
  75        git rev-parse --verify HEAD | cut -c1-7
  76)
  77test_expect_success 'modified submodule(backward)' "
  79    git submodule summary >actual &&
  80    cat >expected <<-EOF &&
  81* sm1 $head2...$head3 (2):
  82  < Add foo3
  83  < Add foo2
  84EOF
  86        test_cmp expected actual
  87"
  88head4=$(add_file sm1 foo4 foo5) &&
  90head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD)
  91test_expect_success 'modified submodule(backward and forward)' "
  92    git submodule summary >actual &&
  93    cat >expected <<-EOF &&
  94* sm1 $head2...$head4 (4):
  95  > Add foo5
  96  > Add foo4
  97  < Add foo3
  98  < Add foo2
  99EOF
 101        test_cmp expected actual
 102"
 103test_expect_success '--summary-limit' "
 105    git submodule summary -n 3 >actual &&
 106    cat >expected <<-EOF &&
 107* sm1 $head2...$head4 (4):
 108  > Add foo5
 109  > Add foo4
 110  < Add foo3
 111EOF
 113    test_cmp expected actual
 114"
 115commit_file sm1 &&
 117mv sm1 sm1-bak &&
 118echo sm1 >sm1 &&
 119head5=$(git hash-object sm1 | cut -c1-7) &&
 120git add sm1 &&
 121rm -f sm1 &&
 122mv sm1-bak sm1
 123test_expect_success 'typechanged submodule(submodule->blob), --cached' "
 125    git submodule summary --cached >actual &&
 126    cat >expected <<-EOF &&
 127* sm1 $head4(submodule)->$head5(blob) (3):
 128  < Add foo5
 129EOF
 131        test_cmp actual expected
 132"
 133test_expect_success 'typechanged submodule(submodule->blob), --files' "
 135    git submodule summary --files >actual &&
 136    cat >expected <<-EOF &&
 137* sm1 $head5(blob)->$head4(submodule) (3):
 138  > Add foo5
 139EOF
 141    test_cmp actual expected
 142"
 143rm -rf sm1 &&
 145git checkout-index sm1
 146test_expect_success 'typechanged submodule(submodule->blob)' "
 147    git submodule summary >actual &&
 148    cat >expected <<-EOF &&
 149* sm1 $head4(submodule)->$head5(blob):
 150EOF
 152    test_cmp actual expected
 153"
 154rm -f sm1 &&
 156test_create_repo sm1 &&
 157head6=$(add_file sm1 foo6 foo7)
 158test_expect_success 'nonexistent commit' "
 159    git submodule summary >actual &&
 160    cat >expected <<-EOF &&
 161* sm1 $head4...$head6:
 162  Warn: sm1 doesn't contain commit $head4_full
 163EOF
 165    test_cmp actual expected
 166"
 167commit_file
 169test_expect_success 'typechanged submodule(blob->submodule)' "
 170    git submodule summary >actual &&
 171    cat >expected <<-EOF &&
 172* sm1 $head5(blob)->$head6(submodule) (2):
 173  > Add foo7
 174EOF
 176    test_cmp expected actual
 177"
 178commit_file sm1 &&
 180rm -rf sm1
 181test_expect_success 'deleted submodule' "
 182    git submodule summary >actual &&
 183    cat >expected <<-EOF &&
 184* sm1 $head6...0000000:
 185EOF
 187    test_cmp expected actual
 188"
 189test_create_repo sm2 &&
 191head7=$(add_file sm2 foo8 foo9) &&
 192git add sm2
 193test_expect_success 'multiple submodules' "
 195    git submodule summary >actual &&
 196    cat >expected <<-EOF &&
 197* sm1 $head6...0000000:
 198* sm2 0000000...$head7 (2):
 200  > Add foo9
 201EOF
 203    test_cmp expected actual
 204"
 205test_expect_success 'path filter' "
 207    git submodule summary sm2 >actual &&
 208    cat >expected <<-EOF &&
 209* sm2 0000000...$head7 (2):
 210  > Add foo9
 211EOF
 213    test_cmp expected actual
 214"
 215commit_file sm2
 217test_expect_success 'given commit' "
 218    git submodule summary HEAD^ >actual &&
 219    cat >expected <<-EOF &&
 220* sm1 $head6...0000000:
 221* sm2 0000000...$head7 (2):
 223  > Add foo9
 224EOF
 226    test_cmp expected actual
 227"
 228test_expect_success '--for-status' "
 230    git submodule summary --for-status HEAD^ >actual &&
 231    test_cmp actual - <<EOF
 232# Submodule changes to be committed:
 233#
 234# * sm1 $head6...0000000:
 235#
 236# * sm2 0000000...$head7 (2):
 237#   > Add foo9
 238#
 239EOF
 240"
 241test_expect_success 'fail when using --files together with --cached' "
 243    test_must_fail git submodule summary --files --cached
 244"
 245test_expect_success 'should not fail in an empty repo' "
 247    git init xyzzy &&
 248    cd xyzzy &&
 249    git submodule summary >output 2>&1 &&
 250    test_cmp output /dev/null
 251"
 252test_done