update a few Porcelain-ish for ref lock safety.
This updates the use of git-update-ref in git-branch, git-tag and git-commit to make them safer in a few corner cases as demonstration. - git-tag makes sure that the named tag does not exist, allows you to edit tag message and then creates the tag. If a tag with the same name was created by somebody else in the meantime, it used to happily overwrote it. Now it notices the situation. - git-branch -d and git-commit (for the initial commit) had the same issue but with smaller race window, which is plugged with this. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 27, 2006 at 02:06 UTC
cede7526534c47436de17977eb39e55aa8c1e646
3 files changed
+13
-9
git-branch.sh
+4
-3
@@ -42,8 +42,7 @@ If you are sure you want to delete it, run 'git branch -D $branch_name'."
42
esac
43
;;
44
esac
45
- rm -f "$GIT_DIR/logs/refs/heads/$branch_name"
46
- rm -f "$GIT_DIR/refs/heads/$branch_name"
45
+ git update-ref -d "refs/heads/$branch_name" "$branch"
46
echo "Deleted branch $branch_name."
47
done
48
exit 0
@@ -112,6 +111,7 @@ rev=$(git-rev-parse --verify "$head") || exit
111
git-check-ref-format "heads/$branchname" ||
112
die "we do not like '$branchname' as a branch name."
113
114
+prev=''
115
if git-show-ref --verify --quiet -- "refs/heads/$branchname"
116
then
117
if test '' = "$force"
@@ -121,10 +121,11 @@ then
121
then
122
die "cannot force-update the current branch."
123
fi
124
+ prev=`git rev-parse --verify "refs/heads/$branchname"`
125
fi
126
if test "$create_log" = 'yes'
127
then
128
mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$branchname")
129
touch "$GIT_DIR/logs/refs/heads/$branchname"
130
fi
130
-git update-ref -m "branch: Created from $head" "refs/heads/$branchname" $rev
131
+git update-ref -m "branch: Created from $head" "refs/heads/$branchname" "$rev" "$prev"
git-commit.sh
+3
-3
@@ -547,15 +547,15 @@ then
547
PARENTS=$(git-cat-file commit HEAD |
548
sed -n -e '/^$/q' -e 's/^parent /-p /p')
549
fi
550
- current=$(git-rev-parse --verify HEAD)
550
+ current="$(git-rev-parse --verify HEAD)"
551
else
552
if [ -z "$(git-ls-files)" ]; then
553
echo >&2 Nothing to commit
554
exit 1
555
fi
556
PARENTS=""
557
- current=
557
rloga='commit (initial)'
558
+ current=''
559
fi
560
561
if test -z "$no_edit"
@@ -631,7 +631,7 @@ then
631
fi &&
632
commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
633
rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
634
- git-update-ref -m "$rloga: $rlogm" HEAD $commit $current &&
634
+ git-update-ref -m "$rloga: $rlogm" HEAD $commit "$current" &&
635
rm -f -- "$GIT_DIR/MERGE_HEAD" &&
636
if test -f "$NEXT_INDEX"
637
then
git-tag.sh
+6
-3
@@ -63,8 +63,11 @@ done
63
64
name="$1"
65
[ "$name" ] || usage
66
-if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
67
- die "tag '$name' already exists"
66
+prev=0000000000000000000000000000000000000000
67
+if test -e "$GIT_DIR/refs/tags/$name"
68
+then
69
+ test -n "$force" || die "tag '$name' already exists"
70
+ prev=`git rev-parse "refs/tags/$name"`
71
fi
72
shift
73
git-check-ref-format "tags/$name" ||
@@ -109,4 +112,4 @@ fi
112
113
leading=`expr "refs/tags/$name" : '\(.*\)/'` &&
114
mkdir -p "$GIT_DIR/$leading" &&
112
-echo $object > "$GIT_DIR/refs/tags/$name"
115
+GIT_DIR="$GIT_DIR" git update-ref "refs/tags/$name" "$object" "$prev"