提交 bf7960eb 编写于 作者: J Junio C Hamano

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: NJunio C Hamano <junkio@cox.net>
上级 a7928f8e
...@@ -108,9 +108,10 @@ fi ...@@ -108,9 +108,10 @@ fi
tree=$(git-write-tree) || exit 1 tree=$(git-write-tree) || exit 1
echo Wrote tree $tree echo Wrote tree $tree
commit=$(git-commit-tree $tree -p $(cat "$GIT_DIR"/HEAD) < "$final") || exit 1 parent=$(git-rev-parse --verify HEAD) &&
commit=$(git-commit-tree $tree -p $parent <"$final") || exit 1
echo Committed: $commit echo Committed: $commit
echo $commit > "$GIT_DIR"/HEAD git-update-ref HEAD $commit $parent || exit
if test -x "$GIT_DIR"/hooks/post-applypatch if test -x "$GIT_DIR"/hooks/post-applypatch
then then
......
...@@ -159,7 +159,9 @@ if [ ! -r "$GIT_DIR/HEAD" ]; then ...@@ -159,7 +159,9 @@ if [ ! -r "$GIT_DIR/HEAD" ]; then
exit 1 exit 1
fi fi
PARENTS="" PARENTS=""
current=
else else
current=$(git-rev-parse --verify HEAD)
if [ -f "$GIT_DIR/MERGE_HEAD" ]; then if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"` PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
fi fi
...@@ -220,7 +222,7 @@ if test -s .cmitchk ...@@ -220,7 +222,7 @@ if test -s .cmitchk
then then
tree=$(git-write-tree) && tree=$(git-write-tree) &&
commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) && commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
echo $commit > "$GIT_DIR/HEAD" && git-update-ref HEAD $commit $current &&
rm -f -- "$GIT_DIR/MERGE_HEAD" rm -f -- "$GIT_DIR/MERGE_HEAD"
else else
echo >&2 "* no commit message? aborting commit." echo >&2 "* no commit message? aborting commit."
......
...@@ -110,14 +110,16 @@ fast_forward_local () { ...@@ -110,14 +110,16 @@ fast_forward_local () {
else else
echo >&2 "* $1: storing $3" echo >&2 "* $1: storing $3"
fi fi
echo "$2" >"$GIT_DIR/$1" ;; git-update-ref "$1" "$2"
;;
refs/heads/*) refs/heads/*)
# NEEDSWORK: use the same cmpxchg protocol here. # $1 is the ref being updated.
echo "$2" >"$GIT_DIR/$1.lock" # $2 is the new value for the ref.
if test -f "$GIT_DIR/$1" local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
if test "$local"
then then
local=$(git-rev-parse --verify "$1^0") && # Require fast-forward.
mb=$(git-merge-base "$local" "$2") && mb=$(git-merge-base "$local" "$2") &&
case "$2,$mb" in case "$2,$mb" in
$local,*) $local,*)
...@@ -125,34 +127,34 @@ fast_forward_local () { ...@@ -125,34 +127,34 @@ fast_forward_local () {
;; ;;
*,$local) *,$local)
echo >&2 "* $1: fast forward to $3" echo >&2 "* $1: fast forward to $3"
git-update-ref "$1" "$2" "$local"
;; ;;
*) *)
false false
;; ;;
esac || { esac || {
echo >&2 "* $1: does not fast forward to $3;" echo >&2 "* $1: does not fast forward to $3;"
case "$force,$single_force" in case ",$force,$single_force," in
t,* | *,t) *,t,*)
echo >&2 " forcing update." echo >&2 " forcing update."
git-update-ref "$1" "$2" "$local"
;; ;;
*) *)
mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote" echo >&2 " not updating."
echo >&2 " leaving it in '$1.remote'"
;; ;;
esac esac
} }
else else
echo >&2 "* $1: storing $3" echo >&2 "* $1: storing $3"
git-update-ref "$1" "$2"
fi fi
test -f "$GIT_DIR/$1.lock" &&
mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
;; ;;
esac esac
} }
case "$update_head_ok" in case "$update_head_ok" in
'') '')
orig_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null) orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
;; ;;
esac esac
...@@ -196,7 +198,7 @@ do ...@@ -196,7 +198,7 @@ do
rsync://*) rsync://*)
TMP_HEAD="$GIT_DIR/TMP_HEAD" TMP_HEAD="$GIT_DIR/TMP_HEAD"
rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
head=$(git-rev-parse TMP_HEAD) head=$(git-rev-parse --verify TMP_HEAD)
rm -f "$TMP_HEAD" rm -f "$TMP_HEAD"
test "$rsync_slurped_objects" || { test "$rsync_slurped_objects" || {
rsync -av --ignore-existing --exclude info \ rsync -av --ignore-existing --exclude info \
...@@ -285,10 +287,10 @@ case ",$update_head_ok,$orig_head," in ...@@ -285,10 +287,10 @@ case ",$update_head_ok,$orig_head," in
*,, | t,* ) *,, | t,* )
;; ;;
*) *)
curr_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null) curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
if test "$curr_head" != "$orig_head" if test "$curr_head" != "$orig_head"
then then
echo "$orig_head" >$GIT_DIR/HEAD git-update-ref HEAD "$orig_head"
die "Cannot fetch into the current branch." die "Cannot fetch into the current branch."
fi fi
;; ;;
......
...@@ -116,8 +116,9 @@ case "$#,$common" in ...@@ -116,8 +116,9 @@ case "$#,$common" in
# Again the most common case of merging one remote. # Again the most common case of merging one remote.
echo "Updating from $head to $1." echo "Updating from $head to $1."
git-update-index --refresh 2>/dev/null git-update-index --refresh 2>/dev/null
git-read-tree -u -m $head "$1" || exit 1 git-read-tree -u -m $head "$1" &&
git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD" new_head=$(git-rev-parse --verify "$1^0") &&
git-update-ref HEAD "$new_head" "$head" || exit 1
summary "$1" summary "$1"
dropsave dropsave
exit 0 exit 0
...@@ -215,9 +216,9 @@ then ...@@ -215,9 +216,9 @@ then
do do
parents="$parents -p $remote" parents="$parents -p $remote"
done done
result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
echo "Committed merge $result_commit, made by $wt_strategy." echo "Committed merge $result_commit, made by $wt_strategy."
echo $result_commit >"$GIT_DIR/HEAD" git-update-ref HEAD $result_commit $head
summary $result_commit summary $result_commit
dropsave dropsave
exit 0 exit 0
......
...@@ -86,5 +86,5 @@ esac ...@@ -86,5 +86,5 @@ esac
result_commit=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD" | result_commit=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD" |
git-commit-tree $MRT $PARENT) git-commit-tree $MRT $PARENT)
echo "Committed merge $result_commit" echo "Committed merge $result_commit"
echo $result_commit >"$GIT_DIR"/HEAD git-update-ref HEAD $result_commit $head
git-diff-tree -p $head $result_commit | git-apply --stat git-diff-tree -p $head $result_commit | git-apply --stat
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
. git-sh-setup || die "Not a git archive" . git-sh-setup || die "Not a git archive"
orig_head=$(cat "$GIT_DIR/HEAD") || die "Pulling into a black hole?" orig_head=$(git-rev-parse --verify HEAD) || die "Pulling into a black hole?"
git-fetch --update-head-ok "$@" || exit 1 git-fetch --update-head-ok "$@" || exit 1
curr_head=$(cat "$GIT_DIR/HEAD") curr_head=$(git-rev-parse --verify HEAD)
if test "$curr_head" != "$orig_head" if test "$curr_head" != "$orig_head"
then then
# The fetch involved updating the current branch. # The fetch involved updating the current branch.
...@@ -40,4 +40,4 @@ case "$merge_head" in ...@@ -40,4 +40,4 @@ case "$merge_head" in
esac esac
merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD")
git-resolve "$(cat "$GIT_DIR"/HEAD)" $merge_head "$merge_name" git-resolve "$curr_head" $merge_head "$merge_name"
...@@ -33,7 +33,8 @@ test "$different1$different2" = "" || ...@@ -33,7 +33,8 @@ test "$different1$different2" = "" ||
die "Your working tree does not match $ours_symbolic." die "Your working tree does not match $ours_symbolic."
git-read-tree -m -u $ours $upstream && git-read-tree -m -u $ours $upstream &&
git-rev-parse --verify "$upstream^0" >"$GIT_DIR/HEAD" || exit new_head=$(git-rev-parse --verify "$upstream^0") &&
git-update-ref HEAD "$new_head" || exit
tmp=.rebase-tmp$$ tmp=.rebase-tmp$$
fail=$tmp-fail fail=$tmp-fail
...@@ -50,7 +51,7 @@ do ...@@ -50,7 +51,7 @@ do
continue ;; continue ;;
esac esac
echo >&2 "* Applying: $msg" echo >&2 "* Applying: $msg"
S=`cat "$GIT_DIR/HEAD"` && S=$(git-rev-parse --verify HEAD) &&
git-cherry-pick --replay $commit || { git-cherry-pick --replay $commit || {
echo >&2 "* Not applying the patch and continuing." echo >&2 "* Not applying the patch and continuing."
echo $commit >>$fail echo $commit >>$fail
......
...@@ -60,7 +60,7 @@ then ...@@ -60,7 +60,7 @@ then
else else
rm -f "$GIT_DIR/ORIG_HEAD" rm -f "$GIT_DIR/ORIG_HEAD"
fi fi
echo "$rev" >"$GIT_DIR/HEAD" git-update-ref HEAD "$rev"
case "$reset_type" in case "$reset_type" in
--hard ) --hard )
......
...@@ -45,7 +45,7 @@ case "$common" in ...@@ -45,7 +45,7 @@ case "$common" in
"$head") "$head")
echo "Updating from $head to $merge." echo "Updating from $head to $merge."
git-read-tree -u -m $head $merge || exit 1 git-read-tree -u -m $head $merge || exit 1
echo $merge > "$GIT_DIR"/HEAD git-update-ref HEAD "$merge" "$head"
git-diff-tree -p $head $merge | git-apply --stat git-diff-tree -p $head $merge | git-apply --stat
dropheads dropheads
exit 0 exit 0
...@@ -99,6 +99,6 @@ if [ $? -ne 0 ]; then ...@@ -99,6 +99,6 @@ if [ $? -ne 0 ]; then
fi fi
result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge) result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
echo "Committed merge $result_commit" echo "Committed merge $result_commit"
echo $result_commit > "$GIT_DIR"/HEAD git-update-ref HEAD "$result_commit" "$head"
git-diff-tree -p $head $result_commit | git-apply --stat git-diff-tree -p $head $result_commit | git-apply --stat
dropheads dropheads
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册