The second batch
[git] / t / lib-t6000.sh
1 : included from 6002 and others
2
3 >sed.script
4
5 # Answer the sha1 has associated with the tag. The tag must exist under refs/tags
6 tag () {
7         _tag=$1
8         git rev-parse --verify "refs/tags/$_tag" ||
9         error "tag: \"$_tag\" does not exist"
10 }
11
12 # Generate a commit using the text specified to make it unique and the tree
13 # named by the tag specified.
14 unique_commit () {
15         _text=$1
16         _tree=$2
17         shift 2
18         echo "$_text" | git commit-tree $(tag "$_tree") "$@"
19 }
20
21 # Save the output of a command into the tag specified. Prepend
22 # a substitution script for the tag onto the front of sed.script
23 save_tag () {
24         _tag=$1
25         test -n "$_tag" || error "usage: save_tag tag commit-args ..."
26         shift 1
27
28         git update-ref "refs/tags/$_tag" $("$@")
29
30         echo "s/$(tag $_tag)/$_tag/g" >sed.script.tmp
31         cat sed.script >>sed.script.tmp
32         rm sed.script
33         mv sed.script.tmp sed.script
34 }
35
36 # Replace unhelpful sha1 hashes with their symbolic equivalents
37 entag () {
38         sed -f sed.script
39 }
40
41 # Execute a command after first saving, then setting the GIT_AUTHOR_EMAIL
42 # tag to a specified value. Restore the original value on return.
43 as_author () {
44         _author=$1
45         shift 1
46         _save=$GIT_AUTHOR_EMAIL
47
48         GIT_AUTHOR_EMAIL="$_author"
49         export GIT_AUTHOR_EMAIL
50         "$@"
51         if test -z "$_save"
52         then
53                 unset GIT_AUTHOR_EMAIL
54         else
55                 GIT_AUTHOR_EMAIL="$_save"
56                 export GIT_AUTHOR_EMAIL
57         fi
58 }
59
60 commit_date () {
61         _commit=$1
62         git cat-file commit $_commit |
63         sed -n "s/^committer .*> \([0-9]*\) .*/\1/p"
64 }
65
66 # Assign the value of fake date to a variable, but
67 # allow fairly common "1971-08-16 00:00" to be omittd
68 assign_fake_date () {
69         case "$2" in
70         ??:??:??)       eval "$1='1971-08-16 $2'" ;;
71         ??:??)          eval "$1='1971-08-16 00:$2'" ;;
72         ??)             eval "$1='1971-08-16 00:00:$2'" ;;
73         *)              eval "$1='$2'" ;;
74         esac
75 }
76
77 on_committer_date () {
78         assign_fake_date GIT_COMMITTER_DATE "$1"
79         export GIT_COMMITTER_DATE
80         shift 1
81         "$@"
82 }
83
84 on_dates () {
85         assign_fake_date GIT_COMMITTER_DATE "$1"
86         assign_fake_date GIT_AUTHOR_DATE "$2"
87         export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
88         shift 2
89         "$@"
90 }
91
92 # Execute a command and suppress any error output.
93 hide_error () {
94         "$@" 2>/dev/null
95 }
96
97 check_output () {
98         _name=$1
99         shift 1
100         if eval "$*" | entag >"$_name.actual"
101         then
102                 test_cmp "$_name.expected" "$_name.actual"
103         else
104                 return 1
105         fi
106 }
107
108 # Turn a reasonable test description into a reasonable test name.
109 # All alphanums translated into -'s which are then compressed and stripped
110 # from front and back.
111 name_from_description () {
112         perl -pe '
113                 s/[^A-Za-z0-9.]/-/g;
114                 s/-+/-/g;
115                 s/-$//;
116                 s/^-//;
117                 y/A-Z/a-z/;
118         '
119 }
120
121
122 # Execute the test described by the first argument, by eval'ing
123 # command line specified in the 2nd argument. Check the status code
124 # is zero and that the output matches the stream read from
125 # stdin.
126 test_output_expect_success()
127 {
128         _description=$1
129         _test=$2
130         test $# -eq 2 ||
131         error "usage: test_output_expect_success description test <<EOF ... EOF"
132
133         _name=$(echo $_description | name_from_description)
134         cat >"$_name.expected"
135         test_expect_success "$_description" "check_output $_name \"$_test\""
136 }