0d53aa6d6948640d287bc514ae9b001cfbf71a42
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 SZEDER Gábor
   4#
   5
   6test_description='test git-specific bash prompt functions'
   7
   8. ./lib-bash.sh
   9
  10. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
  11
  12actual="$TRASH_DIRECTORY/actual"
  13
  14test_expect_success 'setup for prompt tests' '
  15        git init otherrepo &&
  16        echo 1 >file &&
  17        git add file &&
  18        test_tick &&
  19        git commit -m initial &&
  20        git tag -a -m msg1 t1 &&
  21        git checkout -b b1 &&
  22        echo 2 >file &&
  23        git commit -m "second b1" file &&
  24        echo 3 >file &&
  25        git commit -m "third b1" file &&
  26        git tag -a -m msg2 t2 &&
  27        git checkout -b b2 master &&
  28        echo 0 >file &&
  29        git commit -m "second b2" file &&
  30        echo 00 >file &&
  31        git commit -m "another b2" file &&
  32        echo 000 >file &&
  33        git commit -m "yet another b2" file &&
  34        git checkout master
  35'
  36
  37test_expect_success 'prompt - branch name' '
  38        printf " (master)" >expected &&
  39        __git_ps1 >"$actual" &&
  40        test_cmp expected "$actual"
  41'
  42
  43test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
  44        printf " (master)" >expected &&
  45        test_when_finished "git checkout master" &&
  46        test_config core.preferSymlinkRefs true &&
  47        git checkout master &&
  48        __git_ps1 >"$actual" &&
  49        test_cmp expected "$actual"
  50'
  51
  52test_expect_success 'prompt - detached head' '
  53        printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
  54        test_config core.abbrev 13 &&
  55        git checkout b1^ &&
  56        test_when_finished "git checkout master" &&
  57        __git_ps1 >"$actual" &&
  58        test_cmp expected "$actual"
  59'
  60
  61test_expect_success 'prompt - describe detached head - contains' '
  62        printf " ((t2~1))" >expected &&
  63        git checkout b1^ &&
  64        test_when_finished "git checkout master" &&
  65        (
  66                GIT_PS1_DESCRIBE_STYLE=contains &&
  67                __git_ps1 >"$actual"
  68        ) &&
  69        test_cmp expected "$actual"
  70'
  71
  72test_expect_success 'prompt - describe detached head - branch' '
  73        printf " ((b1~1))" >expected &&
  74        git checkout b1^ &&
  75        test_when_finished "git checkout master" &&
  76        (
  77                GIT_PS1_DESCRIBE_STYLE=branch &&
  78                __git_ps1 >"$actual"
  79        ) &&
  80        test_cmp expected "$actual"
  81'
  82
  83test_expect_success 'prompt - describe detached head - describe' '
  84        printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
  85        git checkout b1^ &&
  86        test_when_finished "git checkout master" &&
  87        (
  88                GIT_PS1_DESCRIBE_STYLE=describe &&
  89                __git_ps1 >"$actual"
  90        ) &&
  91        test_cmp expected "$actual"
  92'
  93
  94test_expect_success 'prompt - describe detached head - default' '
  95        printf " ((t2))" >expected &&
  96        git checkout --detach b1 &&
  97        test_when_finished "git checkout master" &&
  98        __git_ps1 >"$actual" &&
  99        test_cmp expected "$actual"
 100'
 101
 102test_expect_success 'prompt - inside .git directory' '
 103        printf " (GIT_DIR!)" >expected &&
 104        (
 105                cd .git &&
 106                __git_ps1 >"$actual"
 107        ) &&
 108        test_cmp expected "$actual"
 109'
 110
 111test_expect_success 'prompt - deep inside .git directory' '
 112        printf " (GIT_DIR!)" >expected &&
 113        (
 114                cd .git/refs/heads &&
 115                __git_ps1 >"$actual"
 116        ) &&
 117        test_cmp expected "$actual"
 118'
 119
 120test_expect_success 'prompt - inside bare repository' '
 121        printf " (BARE:master)" >expected &&
 122        git init --bare bare.git &&
 123        test_when_finished "rm -rf bare.git" &&
 124        (
 125                cd bare.git &&
 126                __git_ps1 >"$actual"
 127        ) &&
 128        test_cmp expected "$actual"
 129'
 130
 131test_expect_success 'prompt - interactive rebase' '
 132        printf " (b1|REBASE-i 2/3)" >expected
 133        write_script fake_editor.sh <<-\EOF &&
 134                echo "exec echo" >"$1"
 135                echo "edit $(git log -1 --format="%h")" >>"$1"
 136                echo "exec echo" >>"$1"
 137        EOF
 138        test_when_finished "rm -f fake_editor.sh" &&
 139        test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
 140        git checkout b1 &&
 141        test_when_finished "git checkout master" &&
 142        git rebase -i HEAD^ &&
 143        test_when_finished "git rebase --abort"
 144        __git_ps1 >"$actual" &&
 145        test_cmp expected "$actual"
 146'
 147
 148test_expect_success 'prompt - rebase merge' '
 149        printf " (b2|REBASE-m 1/3)" >expected &&
 150        git checkout b2 &&
 151        test_when_finished "git checkout master" &&
 152        test_must_fail git rebase --merge b1 b2 &&
 153        test_when_finished "git rebase --abort" &&
 154        __git_ps1 >"$actual" &&
 155        test_cmp expected "$actual"
 156'
 157
 158test_expect_success 'prompt - rebase' '
 159        printf " (b2|REBASE 1/3)" >expected &&
 160        git checkout b2 &&
 161        test_when_finished "git checkout master" &&
 162        test_must_fail git rebase b1 b2 &&
 163        test_when_finished "git rebase --abort" &&
 164        __git_ps1 >"$actual" &&
 165        test_cmp expected "$actual"
 166'
 167
 168test_expect_success 'prompt - merge' '
 169        printf " (b1|MERGING)" >expected &&
 170        git checkout b1 &&
 171        test_when_finished "git checkout master" &&
 172        test_must_fail git merge b2 &&
 173        test_when_finished "git reset --hard" &&
 174        __git_ps1 >"$actual" &&
 175        test_cmp expected "$actual"
 176'
 177
 178test_expect_success 'prompt - cherry-pick' '
 179        printf " (master|CHERRY-PICKING)" >expected &&
 180        test_must_fail git cherry-pick b1 &&
 181        test_when_finished "git reset --hard" &&
 182        __git_ps1 >"$actual" &&
 183        test_cmp expected "$actual"
 184'
 185
 186test_expect_success 'prompt - bisect' '
 187        printf " (master|BISECTING)" >expected &&
 188        git bisect start &&
 189        test_when_finished "git bisect reset" &&
 190        __git_ps1 >"$actual" &&
 191        test_cmp expected "$actual"
 192'
 193
 194test_expect_success 'prompt - dirty status indicator - clean' '
 195        printf " (master)" >expected &&
 196        (
 197                GIT_PS1_SHOWDIRTYSTATE=y &&
 198                __git_ps1 >"$actual"
 199        ) &&
 200        test_cmp expected "$actual"
 201'
 202
 203test_expect_success 'prompt - dirty status indicator - dirty worktree' '
 204        printf " (master *)" >expected &&
 205        echo "dirty" >file &&
 206        test_when_finished "git reset --hard" &&
 207        (
 208                GIT_PS1_SHOWDIRTYSTATE=y &&
 209                __git_ps1 >"$actual"
 210        ) &&
 211        test_cmp expected "$actual"
 212'
 213
 214test_expect_success 'prompt - dirty status indicator - dirty index' '
 215        printf " (master +)" >expected &&
 216        echo "dirty" >file &&
 217        test_when_finished "git reset --hard" &&
 218        git add -u &&
 219        (
 220                GIT_PS1_SHOWDIRTYSTATE=y &&
 221                __git_ps1 >"$actual"
 222        ) &&
 223        test_cmp expected "$actual"
 224'
 225
 226test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
 227        printf " (master *+)" >expected &&
 228        echo "dirty index" >file &&
 229        test_when_finished "git reset --hard" &&
 230        git add -u &&
 231        echo "dirty worktree" >file &&
 232        (
 233                GIT_PS1_SHOWDIRTYSTATE=y &&
 234                __git_ps1 >"$actual"
 235        ) &&
 236        test_cmp expected "$actual"
 237'
 238
 239test_expect_success 'prompt - dirty status indicator - before root commit' '
 240        printf " (master #)" >expected &&
 241        (
 242                GIT_PS1_SHOWDIRTYSTATE=y &&
 243                cd otherrepo &&
 244                __git_ps1 >"$actual"
 245        ) &&
 246        test_cmp expected "$actual"
 247'
 248
 249test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
 250        printf " (master)" >expected &&
 251        echo "dirty" >file &&
 252        test_when_finished "git reset --hard" &&
 253        test_config bash.showDirtyState false &&
 254        (
 255                sane_unset GIT_PS1_SHOWDIRTYSTATE &&
 256                __git_ps1 >"$actual"
 257        ) &&
 258        test_cmp expected "$actual"
 259'
 260
 261test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
 262        printf " (master)" >expected &&
 263        echo "dirty" >file &&
 264        test_when_finished "git reset --hard" &&
 265        test_config bash.showDirtyState true &&
 266        (
 267                sane_unset GIT_PS1_SHOWDIRTYSTATE &&
 268                __git_ps1 >"$actual"
 269        ) &&
 270        test_cmp expected "$actual"
 271'
 272
 273test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
 274        printf " (master)" >expected &&
 275        echo "dirty" >file &&
 276        test_when_finished "git reset --hard" &&
 277        test_config bash.showDirtyState false &&
 278        (
 279                GIT_PS1_SHOWDIRTYSTATE=y &&
 280                __git_ps1 >"$actual"
 281        ) &&
 282        test_cmp expected "$actual"
 283'
 284
 285test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
 286        printf " (master *)" >expected &&
 287        echo "dirty" >file &&
 288        test_when_finished "git reset --hard" &&
 289        test_config bash.showDirtyState true &&
 290        (
 291                GIT_PS1_SHOWDIRTYSTATE=y &&
 292                __git_ps1 >"$actual"
 293        ) &&
 294        test_cmp expected "$actual"
 295'
 296
 297test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
 298        printf " (GIT_DIR!)" >expected &&
 299        echo "dirty" >file &&
 300        test_when_finished "git reset --hard" &&
 301        (
 302                GIT_PS1_SHOWDIRTYSTATE=y &&
 303                cd .git &&
 304                __git_ps1 >"$actual"
 305        ) &&
 306        test_cmp expected "$actual"
 307'
 308
 309test_expect_success 'prompt - stash status indicator - no stash' '
 310        printf " (master)" >expected &&
 311        (
 312                GIT_PS1_SHOWSTASHSTATE=y &&
 313                __git_ps1 >"$actual"
 314        ) &&
 315        test_cmp expected "$actual"
 316'
 317
 318test_expect_success 'prompt - stash status indicator - stash' '
 319        printf " (master $)" >expected &&
 320        echo 2 >file &&
 321        git stash &&
 322        test_when_finished "git stash drop" &&
 323        (
 324                GIT_PS1_SHOWSTASHSTATE=y &&
 325                __git_ps1 >"$actual"
 326        ) &&
 327        test_cmp expected "$actual"
 328'
 329
 330test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
 331        printf " (GIT_DIR!)" >expected &&
 332        echo 2 >file &&
 333        git stash &&
 334        test_when_finished "git stash drop" &&
 335        (
 336                GIT_PS1_SHOWSTASHSTATE=y &&
 337                cd .git &&
 338                __git_ps1 >"$actual"
 339        ) &&
 340        test_cmp expected "$actual"
 341'
 342
 343test_expect_success 'prompt - untracked files status indicator - no untracked files' '
 344        printf " (master)" >expected &&
 345        (
 346                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 347                cd otherrepo &&
 348                __git_ps1 >"$actual"
 349        ) &&
 350        test_cmp expected "$actual"
 351'
 352
 353test_expect_success 'prompt - untracked files status indicator - untracked files' '
 354        printf " (master %%)" >expected &&
 355        (
 356                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 357                __git_ps1 >"$actual"
 358        ) &&
 359        test_cmp expected "$actual"
 360'
 361
 362test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
 363        printf " (master)" >expected &&
 364        test_config bash.showUntrackedFiles false &&
 365        (
 366                sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
 367                __git_ps1 >"$actual"
 368        ) &&
 369        test_cmp expected "$actual"
 370'
 371
 372test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
 373        printf " (master)" >expected &&
 374        test_config bash.showUntrackedFiles true &&
 375        (
 376                sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
 377                __git_ps1 >"$actual"
 378        ) &&
 379        test_cmp expected "$actual"
 380'
 381
 382test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
 383        printf " (master)" >expected &&
 384        test_config bash.showUntrackedFiles false &&
 385        (
 386                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 387                __git_ps1 >"$actual"
 388        ) &&
 389        test_cmp expected "$actual"
 390'
 391
 392test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
 393        printf " (master %%)" >expected &&
 394        test_config bash.showUntrackedFiles true &&
 395        (
 396                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 397                __git_ps1 >"$actual"
 398        ) &&
 399        test_cmp expected "$actual"
 400'
 401
 402test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
 403        printf " (GIT_DIR!)" >expected &&
 404        (
 405                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 406                cd .git &&
 407                __git_ps1 >"$actual"
 408        ) &&
 409        test_cmp expected "$actual"
 410'
 411
 412test_expect_success 'prompt - format string starting with dash' '
 413        printf -- "-master" >expected &&
 414        __git_ps1 "-%s" >"$actual" &&
 415        test_cmp expected "$actual"
 416'
 417
 418test_done