File: setup

package info (click to toggle)
git-subrepo 0.4.9-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,552 kB
  • sloc: sh: 7,074; makefile: 273; perl: 226
file content (227 lines) | stat: -rw-r--r-- 5,334 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env bash

set -e

if [ "$(uname)" == "Linux" ]; then
	export LC_ALL=C.UTF-8
fi

# Get the location of this script
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

# Disable any GIT configuration set outside this 'git-subrepo' project.
# Real GIT configuration for tests is set through the first test
# (00-git-config.t).
export XDG_CONFIG_HOME=$PWD
export HOME=$PWD
export GIT_CONFIG_NOSYSTEM=1

# Generate additional testing git repos, if not already present.
mkdir -p  "${SCRIPT_DIR}/repo"
if [ ! -d "${SCRIPT_DIR}/repo/bar" ]; then
	"${SCRIPT_DIR}/genbar" "${SCRIPT_DIR}/repo"
fi
if [ ! -d "${SCRIPT_DIR}/repo/foo" ]; then
	"${SCRIPT_DIR}/genfoo" "${SCRIPT_DIR}/repo"
fi
if [ ! -d "${SCRIPT_DIR}/repo/init" ]; then
	"${SCRIPT_DIR}/geninit" "${SCRIPT_DIR}/repo"
fi

BASHLIB=$(
  find "$PWD"/ -type d -name bin -o -type d -name lib | grep -v "\/\.pc\/" | tr '\n' ':'
)
export BASHLIB

export PATH=$BASHLIB:$PATH
source bash+ :std

source 'lib/git-subrepo'

export TMP=$SCRIPT_DIR/tmp
rm -fr "$TMP"

#export GIT_EXEC_PATH=$PWD/lib:$(git --exec-path)
export UPSTREAM=$TMP/upstream
export OWNER=$TMP/owner
export COLLAB=$TMP/collab

mkdir -p "$UPSTREAM" "$OWNER" "$COLLAB"

cp -r test/repo/{foo,bar,init} "$UPSTREAM/"

DEFAULTBRANCH=$( git config --global --get init.defaultbranch || true )
[[ -z "${DEFAULTBRANCH}" ]] && DEFAULTBRANCH="master"
export DEFAULTBRANCH

###
# Test helper functions:
###
clone-foo-and-bar() {
  (
    # foo will act as the main repo
    git clone "$UPSTREAM/foo" "$OWNER/foo"
    (
        cd "$OWNER/foo"
        git config user.name "FooUser"
        git config user.email "foo@foo"
    )
    # bar will act as the subrepo
    git clone "$UPSTREAM/bar" "$OWNER/bar"
    (
        cd "$OWNER/bar"
        git config user.name "BarUser"
        git config user.email "bar@bar"
    )
  ) &> /dev/null || die
}

subrepo-clone-bar-into-foo() {
  (
    cd "$OWNER/foo"
    git subrepo clone "$UPSTREAM/bar"
  ) &> /dev/null || die
}

add-new-files() {
  local file
  for file in "$@"; do
    echo "new file $file" > "$file"
    git add --force "$file"
  done
  git commit --quiet -m "add new file: $file" &> /dev/null
}

remove-files() {
  local file
  for file in "$@"; do
    git rm "$file"
  done
  git commit --quiet -m "Removed file: $file" &> /dev/null
}

modify-files() {
  local file
  for file in "$@"; do
    echo 'a new line' >> "$file"
    git add "$file"
  done
  git commit -m "modified file: $file"
}

modify-files-ex() {
  local file
  for file in "$@"; do
    echo "$file" >> "$file"
    git add "$file"
  done
  git commit -m "modified file: $file"
}

test-exists() {
  for f in "$@"; do
    if [[ $f =~ ^! ]]; then
      f=${f#!}
      if [[ $f =~ /$ ]]; then
        ok "$([ ! -d "$f" ])" \
          "Directory '$f' does not exist"
      else
        ok "$([ ! -f "$f" ])" \
          "File '$f' does not exist"
      fi
    else
      if [[ $f =~ /$ ]]; then
        ok "$([ -d "$f" ])" \
          "Directory '$f' exists"
      else
        ok "$([ -f "$f" ])" \
          "File '$f' exists"
      fi
    fi
  done
}

test-exists-in-index() {
  for f in "$@"; do
    if [[ $f =~ ^! ]]; then
      f=${f#!}
      if [[ $f =~ /$ ]]; then
        ok "$([ ! "$(git ls-tree --full-tree --name-only -r HEAD "$f")" ])" \
          "Directory '$f' does not exist in index"
      else
        ok "$([ ! "$(git ls-tree --full-tree --name-only -r HEAD "$f")" ])" \
          "File '$f' does not exist in index"
      fi
    else
      if [[ $f =~ /$ ]]; then
        ok "$([ "$(git ls-tree --full-tree --name-only -r HEAD "$f")" ])" \
          "Directory '$f' exists in index"
      else
        ok "$([ "$(git ls-tree --full-tree --name-only -r HEAD "$f")" ])" \
          "File '$f' exists in index"
      fi
    fi
  done
}

test-gitrepo-comment-block() {
  # shellcheck disable=2154
  is "$(grep -E '^;' "$gitrepo")" "\
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git \"subrepo\", and this file is maintained by the
; git-subrepo command. See https://github.com/ingydotnet/git-subrepo#readme
;" \
    'Comment header block is correct'
}

test-gitrepo-field() {
  is "$(git config -f "$gitrepo" subrepo."$1")" \
    "$2" \
    ".gitrepo $1 is correct"
}

test-commit-count() {
  is "$(cd "$1"; git rev-list --count "$2")" \
    "$3" \
    "commit count is correct"
}

save-original-state() {
  original_head_ref=$(cd "$1"; cat .git/HEAD)
  original_branch=${original_head_ref#ref: refs/heads/}
  original_head_commit=$(cd "$1"; git rev-parse HEAD)
  original_gitrepo=$(cd "$1"; cat "$2"/.gitrepo)
}

assert-original-state() {
  current_head_ref=$(cd "$1"; cat .git/HEAD)
  current_branch=${current_head_ref#ref: refs/heads/}
  current_head_commit=$(cd "$1"; git rev-parse HEAD)
  current_gitrepo=$(cd "$1"; cat "$2"/.gitrepo)

  is "$current_head_ref" \
    "$original_head_ref" \
    "Current HEAD is still $original_head_ref"

  is "$current_branch" \
    "$original_branch" \
    "Current branch is still $original_branch"

  is "$current_head_commit" \
    "$original_head_commit" \
    "Current HEAD commit is still $original_head_commit"

  is "$current_gitrepo" \
    "$original_gitrepo" \
    "$2/.gitrepo has not changed"
}

catch() {
  local error=; error=$("$@" 2>&1 || true)
  echo "$error"
}

teardown() {
  rm -fr "$TMP"
}