t / t6120-describe.shon commit Merge branch 'pw/rebase-i-internal' (7ba06bc)
   1#!/bin/sh
   2
   3test_description='test describe
   4
   5                       B
   6        .--------------o----o----o----x
   7       /                   /    /
   8 o----o----o----o----o----.    /
   9       \        A    c        /
  10        .------------o---o---o
  11                   D,R   e
  12'
  13. ./test-lib.sh
  14
  15check_describe () {
  16        expect="$1"
  17        shift
  18        R=$(git describe "$@" 2>err.actual)
  19        S=$?
  20        cat err.actual >&3
  21        test_expect_success "describe $*" '
  22        test $S = 0 &&
  23        case "$R" in
  24        $expect)        echo happy ;;
  25        *)      echo "Oops - $R is not $expect";
  26                false ;;
  27        esac
  28        '
  29}
  30
  31test_expect_success setup '
  32
  33        test_tick &&
  34        echo one >file && git add file && git commit -m initial &&
  35        one=$(git rev-parse HEAD) &&
  36
  37        git describe --always HEAD &&
  38
  39        test_tick &&
  40        echo two >file && git add file && git commit -m second &&
  41        two=$(git rev-parse HEAD) &&
  42
  43        test_tick &&
  44        echo three >file && git add file && git commit -m third &&
  45
  46        test_tick &&
  47        echo A >file && git add file && git commit -m A &&
  48        test_tick &&
  49        git tag -a -m A A &&
  50
  51        test_tick &&
  52        echo c >file && git add file && git commit -m c &&
  53        test_tick &&
  54        git tag c &&
  55
  56        git reset --hard $two &&
  57        test_tick &&
  58        echo B >side && git add side && git commit -m B &&
  59        test_tick &&
  60        git tag -a -m B B &&
  61
  62        test_tick &&
  63        git merge -m Merged c &&
  64        merged=$(git rev-parse HEAD) &&
  65
  66        git reset --hard $two &&
  67        test_tick &&
  68        echo D >another && git add another && git commit -m D &&
  69        test_tick &&
  70        git tag -a -m D D &&
  71        test_tick &&
  72        git tag -a -m R R &&
  73
  74        test_tick &&
  75        echo DD >another && git commit -a -m another &&
  76
  77        test_tick &&
  78        git tag e &&
  79
  80        test_tick &&
  81        echo DDD >another && git commit -a -m "yet another" &&
  82
  83        test_tick &&
  84        git merge -m Merged $merged &&
  85
  86        test_tick &&
  87        echo X >file && echo X >side && git add file side &&
  88        git commit -m x
  89
  90'
  91
  92check_describe A-* HEAD
  93check_describe A-* HEAD^
  94check_describe R-* HEAD^^
  95check_describe A-* HEAD^^2
  96check_describe B HEAD^^2^
  97check_describe R-* HEAD^^^
  98
  99check_describe c-* --tags HEAD
 100check_describe c-* --tags HEAD^
 101check_describe e-* --tags HEAD^^
 102check_describe c-* --tags HEAD^^2
 103check_describe B --tags HEAD^^2^
 104check_describe e --tags HEAD^^^
 105
 106check_describe heads/master --all HEAD
 107check_describe tags/c-* --all HEAD^
 108check_describe tags/e --all HEAD^^^
 109
 110check_describe B-0-* --long HEAD^^2^
 111check_describe A-3-* --long HEAD^^2
 112
 113check_describe c-7-* --tags
 114check_describe e-3-* --first-parent --tags
 115
 116test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
 117        echo "A^0" >expect &&
 118        git checkout A &&
 119        test_when_finished "git checkout -" &&
 120        git describe --contains >actual &&
 121        test_cmp expect actual
 122'
 123
 124check_describe tags/A --all A^0
 125test_expect_success 'no warning was displayed for A' '
 126        test_must_be_empty err.actual
 127'
 128
 129test_expect_success 'rename tag A to Q locally' '
 130        mv .git/refs/tags/A .git/refs/tags/Q
 131'
 132cat - >err.expect <<EOF
 133warning: tag 'A' is really 'Q' here
 134EOF
 135check_describe A-* HEAD
 136test_expect_success 'warning was displayed for Q' '
 137        test_i18ncmp err.expect err.actual
 138'
 139test_expect_success 'rename tag Q back to A' '
 140        mv .git/refs/tags/Q .git/refs/tags/A
 141'
 142
 143test_expect_success 'pack tag refs' 'git pack-refs'
 144check_describe A-* HEAD
 145
 146test_expect_success 'describe works from outside repo using --git-dir' '
 147        git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
 148        git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
 149        grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
 150'
 151
 152check_describe "A-*[0-9a-f]" --dirty
 153
 154test_expect_success 'describe --dirty with --work-tree' '
 155        (
 156                cd "$TEST_DIRECTORY" &&
 157                git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
 158        ) &&
 159        grep -E "^A-[1-9][0-9]?-g[0-9a-f]+$" out
 160'
 161
 162test_expect_success 'set-up dirty work tree' '
 163        echo >>file
 164'
 165
 166check_describe "A-*[0-9a-f]-dirty" --dirty
 167
 168test_expect_success 'describe --dirty with --work-tree (dirty)' '
 169        (
 170                cd "$TEST_DIRECTORY" &&
 171                git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
 172        ) &&
 173        grep -E "^A-[1-9][0-9]?-g[0-9a-f]+-dirty$" out
 174'
 175
 176check_describe "A-*[0-9a-f].mod" --dirty=.mod
 177
 178test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
 179        (
 180                cd "$TEST_DIRECTORY" &&
 181                git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
 182        ) &&
 183        grep -E "^A-[1-9][0-9]?-g[0-9a-f]+.mod$" out
 184'
 185
 186test_expect_success 'describe --dirty HEAD' '
 187        test_must_fail git describe --dirty HEAD
 188'
 189
 190test_expect_success 'set-up matching pattern tests' '
 191        git tag -a -m test-annotated test-annotated &&
 192        echo >>file &&
 193        test_tick &&
 194        git commit -a -m "one more" &&
 195        git tag test1-lightweight &&
 196        echo >>file &&
 197        test_tick &&
 198        git commit -a -m "yet another" &&
 199        git tag test2-lightweight &&
 200        echo >>file &&
 201        test_tick &&
 202        git commit -a -m "even more"
 203
 204'
 205
 206check_describe "test-annotated-*" --match="test-*"
 207
 208check_describe "test1-lightweight-*" --tags --match="test1-*"
 209
 210check_describe "test2-lightweight-*" --tags --match="test2-*"
 211
 212check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
 213
 214check_describe "test2-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
 215
 216check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
 217
 218check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test3-*" HEAD
 219
 220check_describe "test1-lightweight-*" --long --tags --match="test3-*" --match="test1-*" HEAD
 221
 222test_expect_success 'set-up branches' '
 223        git branch branch_A A &&
 224        git branch branch_C c &&
 225        git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
 226        git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
 227        git update-ref refs/original/original_branch_A test-annotated~2
 228'
 229
 230check_describe "heads/branch_A*" --all --match="branch_*" --exclude="branch_C" HEAD
 231
 232check_describe "remotes/origin/remote_branch_A*" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
 233
 234check_describe "original/original_branch_A*" --all test-annotated~1
 235
 236test_expect_success '--match does not work for other types' '
 237        test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
 238'
 239
 240test_expect_success '--exclude does not work for other types' '
 241        R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
 242        case "$R" in
 243        *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
 244                false;;
 245        *) echo ok: Found some known type;;
 246        esac
 247'
 248
 249test_expect_success 'name-rev with exact tags' '
 250        echo A >expect &&
 251        tag_object=$(git rev-parse refs/tags/A) &&
 252        git name-rev --tags --name-only $tag_object >actual &&
 253        test_cmp expect actual &&
 254
 255        echo "A^0" >expect &&
 256        tagged_commit=$(git rev-parse "refs/tags/A^0") &&
 257        git name-rev --tags --name-only $tagged_commit >actual &&
 258        test_cmp expect actual
 259'
 260
 261test_expect_success 'name-rev --all' '
 262        >expect.unsorted &&
 263        for rev in $(git rev-list --all)
 264        do
 265                git name-rev $rev >>expect.unsorted
 266        done &&
 267        sort <expect.unsorted >expect &&
 268        git name-rev --all >actual.unsorted &&
 269        sort <actual.unsorted >actual &&
 270        test_cmp expect actual
 271'
 272
 273test_expect_success 'name-rev --stdin' '
 274        >expect.unsorted &&
 275        for rev in $(git rev-list --all)
 276        do
 277                name=$(git name-rev --name-only $rev) &&
 278                echo "$rev ($name)" >>expect.unsorted
 279        done &&
 280        sort <expect.unsorted >expect &&
 281        git rev-list --all | git name-rev --stdin >actual.unsorted &&
 282        sort <actual.unsorted >actual &&
 283        test_cmp expect actual
 284'
 285
 286test_expect_success 'describe --contains with the exact tags' '
 287        echo "A^0" >expect &&
 288        tag_object=$(git rev-parse refs/tags/A) &&
 289        git describe --contains $tag_object >actual &&
 290        test_cmp expect actual &&
 291
 292        echo "A^0" >expect &&
 293        tagged_commit=$(git rev-parse "refs/tags/A^0") &&
 294        git describe --contains $tagged_commit >actual &&
 295        test_cmp expect actual
 296'
 297
 298test_expect_success 'describe --contains and --match' '
 299        echo "A^0" >expect &&
 300        tagged_commit=$(git rev-parse "refs/tags/A^0") &&
 301        test_must_fail git describe --contains --match="B" $tagged_commit &&
 302        git describe --contains --match="B" --match="A" $tagged_commit >actual &&
 303        test_cmp expect actual
 304'
 305
 306test_expect_success 'describe --exclude' '
 307        echo "c~1" >expect &&
 308        tagged_commit=$(git rev-parse "refs/tags/A^0") &&
 309        test_must_fail git describe --contains --match="B" $tagged_commit &&
 310        git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
 311        test_cmp expect actual
 312'
 313
 314test_expect_success 'describe --contains and --no-match' '
 315        echo "A^0" >expect &&
 316        tagged_commit=$(git rev-parse "refs/tags/A^0") &&
 317        git describe --contains --match="B" --no-match $tagged_commit >actual &&
 318        test_cmp expect actual
 319'
 320
 321test_expect_success 'setup and absorb a submodule' '
 322        test_create_repo sub1 &&
 323        test_commit -C sub1 initial &&
 324        git submodule add ./sub1 &&
 325        git submodule absorbgitdirs &&
 326        git commit -a -m "add submodule" &&
 327        git describe --dirty >expect &&
 328        git describe --broken >out &&
 329        test_cmp expect out
 330'
 331
 332test_expect_success 'describe chokes on severely broken submodules' '
 333        mv .git/modules/sub1/ .git/modules/sub_moved &&
 334        test_must_fail git describe --dirty
 335'
 336
 337test_expect_success 'describe ignoring a broken submodule' '
 338        git describe --broken >out &&
 339        grep broken out
 340'
 341
 342test_expect_success 'describe with --work-tree ignoring a broken submodule' '
 343        (
 344                cd "$TEST_DIRECTORY" &&
 345                git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
 346        ) &&
 347        test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
 348        grep broken out
 349'
 350
 351test_expect_success 'describe a blob at a directly tagged commit' '
 352        echo "make it a unique blob" >file &&
 353        git add file && git commit -m "content in file" &&
 354        git tag -a -m "latest annotated tag" unique-file &&
 355        git describe HEAD:file >actual &&
 356        echo "unique-file:file" >expect &&
 357        test_cmp expect actual
 358'
 359
 360test_expect_success 'describe a blob with its first introduction' '
 361        git commit --allow-empty -m "empty commit" &&
 362        git rm file &&
 363        git commit -m "delete blob" &&
 364        git revert HEAD &&
 365        git commit --allow-empty -m "empty commit" &&
 366        git describe HEAD:file >actual &&
 367        echo "unique-file:file" >expect &&
 368        test_cmp expect actual
 369'
 370
 371test_expect_success 'describe directly tagged blob' '
 372        git tag test-blob unique-file:file &&
 373        git describe test-blob >actual &&
 374        echo "unique-file:file" >expect &&
 375        # suboptimal: we rather want to see "test-blob"
 376        test_cmp expect actual
 377'
 378
 379test_expect_success 'describe tag object' '
 380        git tag test-blob-1 -a -m msg unique-file:file &&
 381        test_must_fail git describe test-blob-1 2>actual &&
 382        test_i18ngrep "fatal: test-blob-1 is neither a commit nor blob" actual
 383'
 384
 385test_expect_failure ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
 386        i=1 &&
 387        while test $i -lt 8000
 388        do
 389                echo "commit refs/heads/master
 390committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
 391data <<EOF
 392commit #$i
 393EOF"
 394                test $i = 1 && echo "from refs/heads/master^0"
 395                i=$(($i + 1))
 396        done | git fast-import &&
 397        git checkout master &&
 398        git tag far-far-away HEAD^ &&
 399        echo "HEAD~4000 tags/far-far-away~3999" >expect &&
 400        git name-rev HEAD~4000 >actual &&
 401        test_cmp expect actual &&
 402        run_with_limited_stack git name-rev HEAD~4000 >actual &&
 403        test_cmp expect actual
 404'
 405
 406test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
 407        git tag -f far-far-away HEAD~7999 &&
 408        echo "far-far-away" >expect &&
 409        git describe --tags --abbrev=0 HEAD~4000 >actual &&
 410        test_cmp expect actual &&
 411        run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
 412        test_cmp expect actual
 413'
 414
 415check_describe tags/A --all A
 416check_describe tags/c --all c
 417check_describe heads/branch_A --all --match='branch_*' branch_A
 418
 419test_expect_success 'describe complains about tree object' '
 420        test_must_fail git describe HEAD^{tree}
 421'
 422
 423test_expect_success 'describe complains about missing object' '
 424        test_must_fail git describe $ZERO_OID
 425'
 426
 427test_done