@matheusbsbs / matheus-bagliano / commits / c72112e6e1
Fork 0

git-clone: fix handling of upsteram whose HEAD does not point at master.

When cloning from a remote repository that has master, main, and origin branches _and_ with the HEAD pointing at main branch, we did quite confused things during clone. So this cleans things up. The behaviour is a bit different between separate remotes/ layout and the mixed branches layout. The newer layout with $GIT_DIR/refs/remotes/$origin/, things are simpler and more transparent: - remote branches are copied to refs/remotes/$origin/. - HEAD points at the branch with the same name as the remote HEAD points at, and starts at where the remote HEAD points at. - $GIT_DIR/remotes/$origin file is set up to fetch all remote branches, and merge the branch HEAD pointed at at the time of the cloning. Everything-in-refs/heads layout was the more confused one, but cleaned up like this: - remote branches are copied to refs/heads, but the branch "$origin" is not copied, instead a copy of the branch the remote HEAD points at is created there. - HEAD points at the branch with the same name as the remote HEAD points at, and starts at where the remote HEAD points at. - $GIT_DIR/remotes/$origin file is set up to fetch all remote branches except "$origin", and merge the branch HEAD pointed at at the time of the cloning. With this, the remote has master, main and origin, and its HEAD points at main, you could: git clone $URL --origin upstream to use refs/heads/upstream as the tracking branch for remote "main", and your primary working branch will also be "main". "master" and "origin" are used to track the corresponding remote branches and with this setup they do not have any special meaning. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 2, 2006 at 16:25 UTC c72112e6e1593bd77132a815ba484d0ff880a7b4
1 file changed +28 -19
git-clone.sh
+28 -19
@@ -52,7 +52,8 @@ Perhaps git-update-server-info needs to be run there?"
52 git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
53 done <"$clone_tmp/refs"
54 rm -fr "$clone_tmp"
55 - http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD"
55 + http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
56 + rm -f "$GIT_DIR/REMOTE_HEAD"
57 }
58
59 # Read git-fetch-pack -k output and store the remote branches.
@@ -324,7 +325,7 @@ test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
325
326 if test -f "$GIT_DIR/CLONE_HEAD"
327 then
327 - # Figure out where the remote HEAD points at.
328 + # Read git-fetch-pack -k output and store the remote branches.
329 perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin"
330 fi
331
@@ -332,22 +333,25 @@ cd "$D" || exit
333
334 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
335 then
335 - head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
336 # Figure out which remote branch HEAD points at.
337 case "$use_separate_remote" in
338 '') remote_top=refs/heads ;;
339 *) remote_top="refs/remotes/$origin" ;;
340 esac
341
342 - # What to use to track the remote primary branch
343 - if test -n "$use_separate_remote"
344 - then
345 - origin_tracking="remotes/$origin/master"
346 - else
347 - origin_tracking="heads/$origin"
348 - fi
342 + head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
343 + case "$head_sha1" in
344 + 'ref: refs/'*)
345 + # Uh-oh, the remote told us (http transport done against
346 + # new style repository with a symref HEAD).
347 + # Ideally we should skip the guesswork but for now
348 + # opt for minimum change.
349 + head_sha1=`expr "$head_sha1" : 'ref: refs/heads/\(.*\)'`
350 + head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
351 + ;;
352 + esac
353
350 - # The name under $remote_top the remote HEAD seems to point at
354 + # The name under $remote_top the remote HEAD seems to point at.
355 head_points_at=$(
356 (
357 echo "master"
@@ -368,23 +372,28 @@ then
372 )
373 )
374
371 - # Write out remotes/$origin file.
375 + # Write out remotes/$origin file, and update our "$head_points_at".
376 case "$head_points_at" in
377 ?*)
378 mkdir -p "$GIT_DIR/remotes" &&
375 - echo >"$GIT_DIR/remotes/$origin" \
376 - "URL: $repo
377 -Pull: refs/heads/$head_points_at:refs/$origin_tracking" &&
379 + git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
380 case "$use_separate_remote" in
379 - t) git-update-ref HEAD "$head_sha1" ;;
380 - *) git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) ;;
381 + t) origin_track="$remote_top/$head_points_at"
382 + git-update-ref HEAD "$head_sha1" ;;
383 + *) origin_track="$remote_top/$origin"
384 + git-update-ref "refs/heads/$origin" "$head_sha1" ;;
385 esac &&
386 + echo >"$GIT_DIR/remotes/$origin" \
387 + "URL: $repo
388 +Pull: refs/heads/$head_points_at:$origin_track" &&
389 (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
390 while read dotslref
391 do
392 name=`expr "$dotslref" : './\(.*\)'` &&
386 - test "$head_points_at" = "$name" ||
387 - test "$origin" = "$name" ||
393 + test "$use_separate_remote" = '' && {
394 + test "$head_points_at" = "$name" ||
395 + test "$origin" = "$name"
396 + } ||
397 echo "Pull: refs/heads/${name}:$remote_top/${name}"
398 done >>"$GIT_DIR/remotes/$origin" &&
399 case "$use_separate_remote" in