Use git-update-ref in scripts.
This uses the git-update-ref command in scripts for safer updates. Also places where we used to read HEAD ref by using "cat" were fixed to use git-rev-parse. This will matter when we start using symbolic references. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 27, 2005 at 18:14 UTC
bf7960eb51a26bcf52f27a60bfcf005661266b1e
9 files changed
+39
-32
git-applypatch.sh
+3
-2
@@ -108,9 +108,10 @@ fi
108
109
tree=$(git-write-tree) || exit 1
110
echo Wrote tree $tree
111
-commit=$(git-commit-tree $tree -p $(cat "$GIT_DIR"/HEAD) < "$final") || exit 1
111
+parent=$(git-rev-parse --verify HEAD) &&
112
+commit=$(git-commit-tree $tree -p $parent <"$final") || exit 1
113
echo Committed: $commit
113
-echo $commit > "$GIT_DIR"/HEAD
114
+git-update-ref HEAD $commit $parent || exit
115
116
if test -x "$GIT_DIR"/hooks/post-applypatch
117
then
git-commit.sh
+3
-1
@@ -159,7 +159,9 @@ if [ ! -r "$GIT_DIR/HEAD" ]; then
159
exit 1
160
fi
161
PARENTS=""
162
+ current=
163
else
164
+ current=$(git-rev-parse --verify HEAD)
165
if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
166
PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
167
fi
@@ -220,7 +222,7 @@ if test -s .cmitchk
222
then
223
tree=$(git-write-tree) &&
224
commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
223
- echo $commit > "$GIT_DIR/HEAD" &&
225
+ git-update-ref HEAD $commit $current &&
226
rm -f -- "$GIT_DIR/MERGE_HEAD"
227
else
228
echo >&2 "* no commit message? aborting commit."
git-fetch.sh
+18
-16
@@ -110,14 +110,16 @@ fast_forward_local () {
110
else
111
echo >&2 "* $1: storing $3"
112
fi
113
- echo "$2" >"$GIT_DIR/$1" ;;
113
+ git-update-ref "$1" "$2"
114
+ ;;
115
116
refs/heads/*)
116
- # NEEDSWORK: use the same cmpxchg protocol here.
117
- echo "$2" >"$GIT_DIR/$1.lock"
118
- if test -f "$GIT_DIR/$1"
117
+ # $1 is the ref being updated.
118
+ # $2 is the new value for the ref.
119
+ local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
120
+ if test "$local"
121
then
120
- local=$(git-rev-parse --verify "$1^0") &&
122
+ # Require fast-forward.
123
mb=$(git-merge-base "$local" "$2") &&
124
case "$2,$mb" in
125
$local,*)
@@ -125,34 +127,34 @@ fast_forward_local () {
127
;;
128
*,$local)
129
echo >&2 "* $1: fast forward to $3"
130
+ git-update-ref "$1" "$2" "$local"
131
;;
132
*)
133
false
134
;;
135
esac || {
136
echo >&2 "* $1: does not fast forward to $3;"
134
- case "$force,$single_force" in
135
- t,* | *,t)
137
+ case ",$force,$single_force," in
138
+ *,t,*)
139
echo >&2 " forcing update."
140
+ git-update-ref "$1" "$2" "$local"
141
;;
142
*)
139
- mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote"
140
- echo >&2 " leaving it in '$1.remote'"
143
+ echo >&2 " not updating."
144
;;
145
esac
146
}
147
else
145
- echo >&2 "* $1: storing $3"
148
+ echo >&2 "* $1: storing $3"
149
+ git-update-ref "$1" "$2"
150
fi
147
- test -f "$GIT_DIR/$1.lock" &&
148
- mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
151
;;
152
esac
153
}
154
155
case "$update_head_ok" in
156
'')
155
- orig_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
157
+ orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
158
;;
159
esac
160
@@ -196,7 +198,7 @@ do
198
rsync://*)
199
TMP_HEAD="$GIT_DIR/TMP_HEAD"
200
rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
199
- head=$(git-rev-parse TMP_HEAD)
201
+ head=$(git-rev-parse --verify TMP_HEAD)
202
rm -f "$TMP_HEAD"
203
test "$rsync_slurped_objects" || {
204
rsync -av --ignore-existing --exclude info \
@@ -285,10 +287,10 @@ case ",$update_head_ok,$orig_head," in
287
*,, | t,* )
288
;;
289
*)
288
- curr_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
290
+ curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
291
if test "$curr_head" != "$orig_head"
292
then
291
- echo "$orig_head" >$GIT_DIR/HEAD
293
+ git-update-ref HEAD "$orig_head"
294
die "Cannot fetch into the current branch."
295
fi
296
;;
git-merge.sh
+5
-4
@@ -116,8 +116,9 @@ case "$#,$common" in
116
# Again the most common case of merging one remote.
117
echo "Updating from $head to $1."
118
git-update-index --refresh 2>/dev/null
119
- git-read-tree -u -m $head "$1" || exit 1
120
- git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
119
+ git-read-tree -u -m $head "$1" &&
120
+ new_head=$(git-rev-parse --verify "$1^0") &&
121
+ git-update-ref HEAD "$new_head" "$head" || exit 1
122
summary "$1"
123
dropsave
124
exit 0
@@ -215,9 +216,9 @@ then
216
do
217
parents="$parents -p $remote"
218
done
218
- result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)
219
+ result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
220
echo "Committed merge $result_commit, made by $wt_strategy."
220
- echo $result_commit >"$GIT_DIR/HEAD"
221
+ git-update-ref HEAD $result_commit $head
222
summary $result_commit
223
dropsave
224
exit 0
git-octopus.sh
+1
-1
@@ -86,5 +86,5 @@ esac
86
result_commit=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD" |
87
git-commit-tree $MRT $PARENT)
88
echo "Committed merge $result_commit"
89
-echo $result_commit >"$GIT_DIR"/HEAD
89
+git-update-ref HEAD $result_commit $head
90
git-diff-tree -p $head $result_commit | git-apply --stat
git-pull.sh
+3
-3
@@ -6,10 +6,10 @@
6
7
. git-sh-setup || die "Not a git archive"
8
9
-orig_head=$(cat "$GIT_DIR/HEAD") || die "Pulling into a black hole?"
9
+orig_head=$(git-rev-parse --verify HEAD) || die "Pulling into a black hole?"
10
git-fetch --update-head-ok "$@" || exit 1
11
12
-curr_head=$(cat "$GIT_DIR/HEAD")
12
+curr_head=$(git-rev-parse --verify HEAD)
13
if test "$curr_head" != "$orig_head"
14
then
15
# The fetch involved updating the current branch.
@@ -40,4 +40,4 @@ case "$merge_head" in
40
esac
41
42
merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD")
43
-git-resolve "$(cat "$GIT_DIR"/HEAD)" $merge_head "$merge_name"
43
+git-resolve "$curr_head" $merge_head "$merge_name"
git-rebase.sh
+3
-2
@@ -33,7 +33,8 @@ test "$different1$different2" = "" ||
33
die "Your working tree does not match $ours_symbolic."
34
35
git-read-tree -m -u $ours $upstream &&
36
-git-rev-parse --verify "$upstream^0" >"$GIT_DIR/HEAD" || exit
36
+new_head=$(git-rev-parse --verify "$upstream^0") &&
37
+git-update-ref HEAD "$new_head" || exit
38
39
tmp=.rebase-tmp$$
40
fail=$tmp-fail
@@ -50,7 +51,7 @@ do
51
continue ;;
52
esac
53
echo >&2 "* Applying: $msg"
53
- S=`cat "$GIT_DIR/HEAD"` &&
54
+ S=$(git-rev-parse --verify HEAD) &&
55
git-cherry-pick --replay $commit || {
56
echo >&2 "* Not applying the patch and continuing."
57
echo $commit >>$fail
git-reset.sh
+1
-1
@@ -60,7 +60,7 @@ then
60
else
61
rm -f "$GIT_DIR/ORIG_HEAD"
62
fi
63
-echo "$rev" >"$GIT_DIR/HEAD"
63
+git-update-ref HEAD "$rev"
64
65
case "$reset_type" in
66
--hard )
git-resolve.sh
+2
-2
@@ -45,7 +45,7 @@ case "$common" in
45
"$head")
46
echo "Updating from $head to $merge."
47
git-read-tree -u -m $head $merge || exit 1
48
- echo $merge > "$GIT_DIR"/HEAD
48
+ git-update-ref HEAD "$merge" "$head"
49
git-diff-tree -p $head $merge | git-apply --stat
50
dropheads
51
exit 0
@@ -99,6 +99,6 @@ if [ $? -ne 0 ]; then
99
fi
100
result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
101
echo "Committed merge $result_commit"
102
-echo $result_commit > "$GIT_DIR"/HEAD
102
+git-update-ref HEAD "$result_commit" "$head"
103
git-diff-tree -p $head $result_commit | git-apply --stat
104
dropheads