Git 2.32
[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" &&
10         if ! parent=$(git -C "$repo" rev-parse HEAD^{} --)
11         then
12                 parent=
13         fi &&
14         T=$(git -C "$repo" write-tree) &&
15         shift &&
16         while test $# -gt 0
17         do
18                 name=$1 &&
19                 test_tick &&
20                 if test -z "$parent"
21                 then
22                         oid=$(echo $name | git -C "$repo" commit-tree $T)
23                 else
24                         oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T)
25                 fi &&
26                 eval $name=$oid &&
27                 parent=$oid &&
28                 shift ||
29                 return 1
30         done &&
31         git -C "$repo" update-ref refs/heads/main $oid
32 }
33
34 # Format the output of git-push, git-show-ref and other commands to make a
35 # user-friendly and stable text.  We can easily prepare the expect text
36 # without having to worry about future changes of the commit ID and spaces
37 # of the output.  Single quotes are replaced with double quotes, because
38 # it is boring to prepare unquoted single quotes in expect text.  We also
39 # remove some locale error messages. The emitted human-readable errors are
40 # redundant to the more machine-readable output the tests already assert.
41 make_user_friendly_and_stable_output () {
42         sed \
43                 -e "s/  *\$//" \
44                 -e "s/  */ /g" \
45                 -e "s/'/\"/g" \
46                 -e "s/  /    /g" \
47                 -e "s/$A/<COMMIT-A>/g" \
48                 -e "s/$B/<COMMIT-B>/g" \
49                 -e "s/$TAG/<TAG-v123>/g" \
50                 -e "s/$ZERO_OID/<ZERO-OID>/g" \
51                 -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \
52                 -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" \
53                 -e "s#To $URL_PREFIX/upstream.git#To <URL/of/upstream.git>#" \
54                 -e "/^error: / d"
55 }
56
57 filter_out_user_friendly_and_stable_output () {
58         make_user_friendly_and_stable_output |
59                 sed -n ${1+"$@"}
60 }
61
62 test_cmp_refs () {
63         indir=
64         if test "$1" = "-C"
65         then
66                 shift
67                 indir="$1"
68                 shift
69         fi
70         indir=${indir:+"$indir"/}
71         cat >show-ref.expect &&
72         git ${indir:+ -C "$indir"} show-ref >show-ref.pristine &&
73         make_user_friendly_and_stable_output <show-ref.pristine >show-ref.filtered &&
74         test_cmp show-ref.expect show-ref.filtered
75 }