test: refactor to use "test_commit" to create commits
[git] / t / t5411 / common-functions.sh
1 # Create commits in <repo> and assign each commit's oid to shell variables
2 # given in the arguments (A, B, and C). E.g.:
3 #
4 #     create_commits_in <repo> A B C
5 #
6 # NOTE: Never calling this function from a subshell since variable
7 # assignments will disappear when subshell exits.
8 create_commits_in () {
9         repo="$1" && test -d "$repo" ||
10         error "Repository $repo does not exist."
11         shift &&
12         while test $# -gt 0
13         do
14                 name=$1 &&
15                 shift &&
16                 test_commit -C "$repo" --no-tag "$name" &&
17                 eval $name=$(git -C "$repo" rev-parse HEAD)
18         done
19 }
20
21 # Format the output of git-push, git-show-ref and other commands to make a
22 # user-friendly and stable text.  We can easily prepare the expect text
23 # without having to worry about changes of the commit ID (full or abbrev.)
24 # of the output.  Single quotes are replaced with double quotes, because
25 # it is boring to prepare unquoted single quotes in expect text.  We also
26 # remove some locale error messages. The emitted human-readable errors are
27 # redundant to the more machine-readable output the tests already assert.
28 make_user_friendly_and_stable_output () {
29         sed \
30                 -e "s/'/\"/g" \
31                 -e "s/$A/<COMMIT-A>/g" \
32                 -e "s/$B/<COMMIT-B>/g" \
33                 -e "s/$TAG/<TAG-v123>/g" \
34                 -e "s/$ZERO_OID/<ZERO-OID>/g" \
35                 -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \
36                 -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" \
37                 -e "s#To $URL_PREFIX/upstream.git#To <URL/of/upstream.git>#" \
38                 -e "/^error: / d"
39 }
40
41 filter_out_user_friendly_and_stable_output () {
42         make_user_friendly_and_stable_output |
43                 sed -n ${1+"$@"}
44 }
45
46 format_and_save_expect () {
47         sed -e 's/^> //' -e 's/Z$//' >expect
48 }
49
50 test_cmp_refs () {
51         indir=
52         if test "$1" = "-C"
53         then
54                 shift
55                 indir="$1"
56                 shift
57         fi
58         indir=${indir:+"$indir"/}
59         cat >show-ref.expect &&
60         git ${indir:+ -C "$indir"} show-ref >show-ref.pristine &&
61         make_user_friendly_and_stable_output <show-ref.pristine >show-ref.filtered &&
62         test_cmp show-ref.expect show-ref.filtered
63 }