t / t1500-rev-parse.shon commit Merge branch 'rs/strbuf-getwholeline-fix' into next (5f879d8)
   1#!/bin/sh
   2
   3test_description='test git rev-parse'
   4. ./test-lib.sh
   5
   6# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
   7test_rev_parse () {
   8        d=
   9        bare=
  10        gitdir=
  11        while :
  12        do
  13                case "$1" in
  14                -C) d="$2"; shift; shift ;;
  15                -b) case "$2" in
  16                    [tfu]*) bare="$2"; shift; shift ;;
  17                    *) error "test_rev_parse: bogus core.bare value '$2'" ;;
  18                    esac ;;
  19                -g) gitdir="$2"; shift; shift ;;
  20                -*) error "test_rev_parse: unrecognized option '$1'" ;;
  21                *) break ;;
  22                esac
  23        done
  24
  25        name=$1
  26        shift
  27
  28        for o in --is-bare-repository \
  29                 --is-inside-git-dir \
  30                 --is-inside-work-tree \
  31                 --show-prefix \
  32                 --git-dir \
  33                 --absolute-git-dir
  34        do
  35                test $# -eq 0 && break
  36                expect="$1"
  37                test_expect_success "$name: $o" '
  38                        if test -n "$gitdir"
  39                        then
  40                                test_when_finished "unset GIT_DIR" &&
  41                                GIT_DIR="$gitdir" &&
  42                                export GIT_DIR
  43                        fi &&
  44
  45                        case "$bare" in
  46                        t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
  47                        f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
  48                        u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
  49                        esac &&
  50
  51                        echo "$expect" >expect &&
  52                        git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
  53                        test_cmp expect actual
  54                '
  55                shift
  56        done
  57}
  58
  59ROOT=$(pwd)
  60
  61test_expect_success 'setup' '
  62        mkdir -p sub/dir work &&
  63        cp -R .git repo.git
  64'
  65
  66test_rev_parse toplevel false false true '' .git "$ROOT/.git"
  67
  68test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
  69test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
  70
  71test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
  72
  73test_rev_parse -b t 'core.bare = true' true false false
  74
  75test_rev_parse -b u 'core.bare undefined' false false true
  76
  77
  78test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
  79
  80test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
  81
  82test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
  83
  84
  85test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' "../repo.git" "$ROOT/repo.git"
  86
  87test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
  88
  89test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
  90
  91test_expect_success 'git-common-dir from worktree root' '
  92        echo .git >expect &&
  93        git rev-parse --git-common-dir >actual &&
  94        test_cmp expect actual
  95'
  96
  97test_expect_success 'git-common-dir inside sub-dir' '
  98        mkdir -p path/to/child &&
  99        test_when_finished "rm -rf path" &&
 100        echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
 101        git -C path/to/child rev-parse --git-common-dir >actual &&
 102        test_cmp expect actual
 103'
 104
 105test_expect_success 'git-path from worktree root' '
 106        echo .git/objects >expect &&
 107        git rev-parse --git-path objects >actual &&
 108        test_cmp expect actual
 109'
 110
 111test_expect_success 'git-path inside sub-dir' '
 112        mkdir -p path/to/child &&
 113        test_when_finished "rm -rf path" &&
 114        echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
 115        git -C path/to/child rev-parse --git-path objects >actual &&
 116        test_cmp expect actual
 117'
 118
 119test_expect_success 'showing the superproject correctly' '
 120        git rev-parse --show-superproject-working-tree >out &&
 121        test_must_be_empty out &&
 122
 123        test_create_repo super &&
 124        test_commit -C super test_commit &&
 125        test_create_repo sub &&
 126        test_commit -C sub test_commit &&
 127        git -C super submodule add ../sub dir/sub &&
 128        echo $(pwd)/super >expect  &&
 129        git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
 130        test_cmp expect out
 131'
 132
 133test_done