Skip to content

Commit b10ac50

Browse files
author
Junio C Hamano
committed
Fix pulling into the same branch.
When the "git pull" command updates the branch head you are currently on, before doing anything else, first update your index file and the working tree contents to that of the new branch head. Otherwise, the later resolving steps would think your index file is attempting to revert the change between the original head commit and the updated head commit. It uses two-tree fast-forward form of "read-tree -m -u" to prevent losing whatever local changes you may have in the working tree to do this update. I think this would at least make things safer (a lot safer), and prevent mistakes. Also "git fetch" command is forbidden from fetching and fast forwarding the current branch head unless --update-head-ok flag is given. "git pull" passes the flag when it internally calls "git fetch". Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 90bc118 commit b10ac50

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

git-fetch-script

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,33 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
77

88
append=
99
force=
10+
update_head_ok=
1011
while case "$#" in 0) break ;; esac
1112
do
1213
case "$1" in
1314
-a|--a|--ap|--app|--appe|--appen|--append)
1415
append=t
15-
shift
1616
;;
1717
-f|--f|--fo|--for|--forc|--force)
1818
force=t
19-
shift
19+
;;
20+
-u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
21+
--update-he|--update-hea|--update-head|--update-head-|\
22+
--update-head-o|--update-head-ok)
23+
update_head_ok=t
2024
;;
2125
*)
2226
break
2327
;;
2428
esac
29+
shift
2530
done
2631

2732
case "$#" in
2833
0)
2934
test -f "$GIT_DIR/branches/origin" ||
3035
test -f "$GIT_DIR/remotes/origin" ||
31-
die "Where do you want to fetch from?"
36+
die "Where do you want to fetch from today?"
3237
set origin ;;
3338
esac
3439

@@ -124,6 +129,12 @@ fast_forward_local () {
124129
esac
125130
}
126131

132+
case "$update_head_ok" in
133+
'')
134+
orig_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
135+
;;
136+
esac
137+
127138
for ref in $(get_remote_refs_for_fetch "$@")
128139
do
129140
refs="$refs $ref"
@@ -150,7 +161,7 @@ do
150161
fi
151162
head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
152163
expr "$head" : "$_x40\$" >/dev/null ||
153-
die "Failed to fetch $remote_name from $remote"
164+
die "Failed to fetch $remote_name from $remote"
154165
echo Fetching "$remote_name from $remote" using http
155166
git-http-pull -v -a "$head" "$remote/" || exit
156167
;;
@@ -201,3 +212,18 @@ http://* | https://* | rsync://* )
201212
done
202213
;;
203214
esac
215+
216+
# If the original head was empty (i.e. no "master" yet), or
217+
# if we were told not to worry, we do not have to check.
218+
case ",$update_head_ok,$orig_head," in
219+
*,, | t,* )
220+
;;
221+
*)
222+
curr_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
223+
if test "$curr_head" != "$orig_head"
224+
then
225+
echo "$orig_head" >$GIT_DIR/HEAD
226+
die "Cannot fetch into the current branch."
227+
fi
228+
;;
229+
esac

git-pull-script

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55
# Fetch one or more remote refs and merge it/them into the current HEAD.
66

77
. git-sh-setup-script || die "Not a git archive"
8-
git-fetch-script "$@" || exit 1
8+
9+
orig_head=$(cat "$GIT_DIR/HEAD") || die "Pulling into a black hole?"
10+
git-fetch-script --update-head-ok "$@" || exit 1
11+
12+
curr_head=$(cat "$GIT_DIR/HEAD")
13+
if test "$curr_head" != "$orig_head"
14+
then
15+
# The fetch involved updating the current branch.
16+
17+
# The working tree and the index file is still based on the
18+
# $orig_head commit, but we are merging into $curr_head.
19+
# First update the working tree to match $curr_head.
20+
21+
echo >&2 "Warning: fetch updated the current branch head."
22+
echo >&2 "Warning: fast forwarding your working tree."
23+
git-read-tree -u -m "$orig_head" "$curr_head" ||
24+
die "You need to first update your working tree."
25+
fi
26+
927
merge_head=$(sed -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | tr '\012' ' ')
1028
merge_name=$(sed -e 's/^[0-9a-f]* //' "$GIT_DIR"/FETCH_HEAD |
1129
tr '\012' ' ')

0 commit comments

Comments
 (0)