Ensure a stable view column search
[tig.git] / test / tools / libgit.sh
blob639d80f385e80d8a41de2d768f962148d78b5049
1 #!/bin/sh
3 # Git test helpers.
5 # Copyright (c) 2014 Jonas Fonseca <jonas.fonseca@gmail.com>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of
10 # the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 set -eu
18 if [ -n "${BASH_VERSION:-}" ]; then
19 set -o pipefail
20 IFS=$'\n\t'
23 author_date=1234567890
24 author_date_delta=735730
26 IDENT_A="A. U. Thor <a.u.thor@example.com>"
27 IDENT_B="René Lévesque <rene.levesque@example.qc.ca>"
28 IDENT_C="作者 <zuozhea@example.ch>"
29 IDENT_D="Jørgen Thygesen Brahe <brache@example.dk>"
30 IDENT_E="Max Power <power123@example.org>"
32 in_work_dir()
34 (cd "$work_dir" && $@)
37 git_config()
39 git config --local user.name "Committer"
40 git config --local user.email "c.ommitter@example.net"
43 git_init()
45 dir="${1:-$work_dir}"
47 if [ ! -d "$dir/.git" ]; then
48 git init -q "$dir"
49 (cd "$dir" && git_config)
53 git_add()
55 [ -d .git ] || die "git_add called outside of git repo"
56 path="$1"; shift
58 mkdir -p "$(dirname "$path")"
59 file "$path" "$@"
60 git add "$path"
63 git_commit()
65 [ -d .git ] || die "git_commit called outside of git repo"
67 GIT_COMMITTER_NAME="$0"
68 GIT_COMMITTER_EMAIL="$0"
70 export GIT_AUTHOR_DATE="$author_date"
71 author_date="$(expr $author_date + $author_date_delta)"
72 [ -z "${GIT_COMMITTER_DATE:-}" ] &&
73 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
75 git commit -q --allow-empty "$@"
76 unset GIT_COMMITTER_DATE
79 create_dirty_workdir()
81 git init -q .
82 git_config
84 echo "*.o" > .gitignore
85 echo "*~" > .git/info/exclude
87 for file in a b.c "d~" e/f "g h" i.o .j "h~/k"; do
88 dir="$(dirname "$file")"
89 [ -n "$dir" ] && mkdir -p "$dir"
90 printf "%s\n%s" "$file" "$(seq 1 10)" > "$file"
91 done
93 git add .
94 git_commit --author="$IDENT_A" --message="Initial commit"
96 for file in a b.c "d~" e/f "g h" i.o .j "h~/k"; do
97 printf "%s\n%s" "$file CHANGED" "$(seq 1 8)" > "$file"
98 done
101 create_repo_one()
103 git_init "$1"
104 (cd "$1" && {
106 for i in $(seq 1 10); do
107 git_commit --author="$IDENT_A" --message="Commit $i A"
108 git_commit --author="$IDENT_B" --message="Commit $i B"
109 git_commit --author="$IDENT_C" --message="Commit $i C"
110 git_commit --author="$IDENT_D" --message="Commit $i D"
111 git_commit --author="$IDENT_E" --message="Commit $i E"
112 done
114 remote=upstream
115 git remote add $remote http://example.org/repo.git
116 mkdir -p .git/refs/remotes/$remote
117 echo 5633519083f21695dda4fe1f546272abb80668cd > .git/refs/remotes/$remote/master
119 tagged=957f2b368e6fa5c0757f36b1441e32729ee5e9c7
120 git tag v1.0 $tagged
124 submodule_pull()
126 repo_name="$(basename "$1")"
127 cd "$1"; shift
129 git submodule foreach git checkout master
130 git submodule foreach git pull
132 repos=""
133 repos_sep=
134 clog=""
135 for repo in $@; do
136 repo="$(basename "$repo")"
137 repos="$repos$repos_sep$repo"
138 repos_sep=", "
140 summary="$(git submodule summary "$repo")"
141 revspec="$(git diff --submodule=log "$repo" | sed -n '/Submodule/,1 s/Submodule [^ ]* \([^:]*\):/\1/p')"
142 diffstat="$(cd "$repo" && git diff --stat "$revspec")"
143 clog="$clog
145 $summary
147 $diffstat"
148 git add "$repo"
149 done
151 git_commit --author="$IDENT_A" --message="[$repo_name] Integrate feature from $repos"
154 submodule_commit()
156 repo_name="$(basename "$1")"
157 cd "$1"; shift
159 for file in $@; do
160 mkdir -p "$(dirname "$file")"
161 echo "$file" >> "$file"
162 git add "$file"
163 done
164 git_commit --author="$IDENT_A" --message="[$repo_name] Commit $(git rev-list HEAD 2> /dev/null | wc -l | sed 's/ *//')"
167 submodule_create()
169 repo_name="$(basename "$1")"
170 cd "$1"; shift
172 git submodule init
173 for repo in $@; do
174 git submodule add "../../git-repos/$(basename "$repo")"
175 done
177 git_commit --author="$IDENT_A" --message="[$repo_name] Creating repository"
180 create_repo_two()
182 repo_main="$1"
183 repo_a="$1-a"
184 repo_b="$1-b"
185 repo_c="$1-c"
187 for repo in "$repo_main" "$repo_a" "$repo_b" "$repo_c"; do
188 git_init "$repo"
190 submodule_commit "$repo" Makefile
191 submodule_commit "$repo" include/api.h
192 submodule_commit "$repo" src/impl.c
193 done
195 submodule_create "$repo_main" "$repo_a" "$repo_b" "$repo_c"
197 submodule_commit "$repo_a" include/api.h
198 submodule_commit "$repo_a" src/impl.c
200 submodule_commit "$repo_c" README
201 submodule_commit "$repo_c" INSTALL
203 submodule_pull "$repo_main" "$repo_a" "$repo_c"
205 submodule_commit "$repo_b" README
206 submodule_commit "$repo_b" Makefile
208 submodule_pull "$repo_main" "$repo_b"
210 submodule_commit "$repo_a" README
211 submodule_commit "$repo_c" src/impl.c
212 submodule_commit "$repo_b" include/api.h
214 submodule_pull "$repo_main" "$repo_a" "$repo_b" "$repo_c"
217 create_repo()
219 if [ ! -d "$1" ]; then
220 case "$(basename "$1")" in
221 repo-one) create_repo_one "$1" ;;
222 repo-two) (create_repo_two "$1") ;;
223 *) die "No generator for $(basname "$1")" ;;
224 esac
228 create_repo_from_tgz()
230 git_init
231 tar zxf "$1"
232 git reset -q --hard
235 git_clone()
237 create_repo "$tmp_dir/git-repos/$1"
239 clone_dir="${2:-$work_dir}"
240 git clone -q "$tmp_dir/git-repos/$1" "$clone_dir"
241 (cd "$clone_dir" && git_config)