1#!/bin/sh
2#
3# Copyright (c) 2008 Christian Couder
4#
5test_description='test git rev-parse --verify'
6
7exec </dev/null
8
9. ./test-lib.sh
10
11add_line_into_file()
12{
13 _line=$1
14 _file=$2
15
16 if [ -f "$_file" ]; then
17 echo "$_line" >> $_file || return $?
18 MSG="Add <$_line> into <$_file>."
19 else
20 echo "$_line" > $_file || return $?
21 git add $_file || return $?
22 MSG="Create file <$_file> with <$_line> inside."
23 fi
24
25 test_tick
26 git commit --quiet -m "$MSG" $_file
27}
28
29HASH1=
30HASH2=
31HASH3=
32HASH4=
33
34test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' '
35 add_line_into_file "1: Hello World" hello &&
36 HASH1=$(git rev-parse --verify HEAD) &&
37 add_line_into_file "2: A new day for git" hello &&
38 HASH2=$(git rev-parse --verify HEAD) &&
39 add_line_into_file "3: Another new day for git" hello &&
40 HASH3=$(git rev-parse --verify HEAD) &&
41 add_line_into_file "4: Ciao for now" hello &&
42 HASH4=$(git rev-parse --verify HEAD)
43'
44
45test_expect_success 'works with one good rev' '
46 rev_hash1=$(git rev-parse --verify $HASH1) &&
47 test "$rev_hash1" = "$HASH1" &&
48 rev_hash2=$(git rev-parse --verify $HASH2) &&
49 test "$rev_hash2" = "$HASH2" &&
50 rev_hash3=$(git rev-parse --verify $HASH3) &&
51 test "$rev_hash3" = "$HASH3" &&
52 rev_hash4=$(git rev-parse --verify $HASH4) &&
53 test "$rev_hash4" = "$HASH4" &&
54 rev_master=$(git rev-parse --verify master) &&
55 test "$rev_master" = "$HASH4" &&
56 rev_head=$(git rev-parse --verify HEAD) &&
57 test "$rev_head" = "$HASH4"
58'
59
60test_expect_success 'fails with any bad rev or many good revs' '
61 test_must_fail git rev-parse --verify 2>error &&
62 grep "single revision" error &&
63 test_must_fail git rev-parse --verify foo 2>error &&
64 grep "single revision" error &&
65 test_must_fail git rev-parse --verify HEAD bar 2>error &&
66 grep "single revision" error &&
67 test_must_fail git rev-parse --verify baz HEAD 2>error &&
68 grep "single revision" error &&
69 test_must_fail git rev-parse --verify $HASH2 HEAD 2>error &&
70 grep "single revision" error
71'
72
73test_expect_success 'fails silently when using -q' '
74 test_must_fail git rev-parse --verify --quiet 2>error &&
75 test_must_be_empty error &&
76 test_must_fail git rev-parse -q --verify foo 2>error &&
77 test_must_be_empty error &&
78 test_must_fail git rev-parse --verify -q HEAD bar 2>error &&
79 test_must_be_empty error &&
80 test_must_fail git rev-parse --quiet --verify baz HEAD 2>error &&
81 test_must_be_empty error &&
82 test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error &&
83 test_must_be_empty error
84'
85
86test_expect_success 'fails silently when using -q with deleted reflogs' '
87 ref=$(git rev-parse HEAD) &&
88 : >.git/logs/refs/test &&
89 git update-ref -m "message for refs/test" refs/test "$ref" &&
90 git reflog delete --updateref --rewrite refs/test@{0} &&
91 test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
92 test_must_be_empty error
93'
94
95test_expect_success 'fails silently when using -q with not enough reflogs' '
96 ref=$(git rev-parse HEAD) &&
97 : >.git/logs/refs/test2 &&
98 git update-ref -m "message for refs/test2" refs/test2 "$ref" &&
99 test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 &&
100 test_must_be_empty error
101'
102
103test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' '
104 ref=$(git rev-parse HEAD) &&
105 : >.git/logs/refs/test3 &&
106 git update-ref -m "message for refs/test3" refs/test3 "$ref" &&
107 git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error &&
108 test_must_be_empty error &&
109 echo "$ref" >expect &&
110 test_cmp expect actual
111'
112
113test_expect_success 'no stdout output on error' '
114 test -z "$(git rev-parse --verify)" &&
115 test -z "$(git rev-parse --verify foo)" &&
116 test -z "$(git rev-parse --verify baz HEAD)" &&
117 test -z "$(git rev-parse --verify HEAD bar)" &&
118 test -z "$(git rev-parse --verify $HASH2 HEAD)"
119'
120
121test_expect_success 'use --default' '
122 git rev-parse --verify --default master &&
123 git rev-parse --verify --default master HEAD &&
124 git rev-parse --default master --verify &&
125 git rev-parse --default master --verify HEAD &&
126 git rev-parse --verify HEAD --default master &&
127 test_must_fail git rev-parse --verify foo --default master &&
128 test_must_fail git rev-parse --default HEAD --verify bar &&
129 test_must_fail git rev-parse --verify --default HEAD baz &&
130 test_must_fail git rev-parse --default foo --verify &&
131 test_must_fail git rev-parse --verify --default bar
132'
133
134test_expect_success 'master@{n} for various n' '
135 N=$(git reflog | wc -l) &&
136 Nm1=$(($N-1)) &&
137 Np1=$(($N+1)) &&
138 git rev-parse --verify master@{0} &&
139 git rev-parse --verify master@{1} &&
140 git rev-parse --verify master@{$Nm1} &&
141 test_must_fail git rev-parse --verify master@{$N} &&
142 test_must_fail git rev-parse --verify master@{$Np1}
143'
144
145test_done