Code

Merge branch 'sb/fetch'
[git.git] / git-parse-remote.sh
index 8ce57c80fbcfae101d22fa3a6f2ebffa2ec01cc3..c325ef761e4c558ab5c7c560da942e127e1be040 100755 (executable)
@@ -4,33 +4,6 @@
 # this would fail in that case and would issue an error message.
 GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :;
 
-if [ -d "$GIT_DIR"/remotes -a "$GIT_REWRITE_REMOTES" = true ]; then
-       echo "Rewriting $GIT_DIR/remotes" >&2
-       error=0
-       # rewrite into config
-       {
-               cd "$GIT_DIR"/remotes
-               ls | while read f; do
-                       name=$(echo -n "$f" | tr -c "A-Za-z0-9" ".")
-                       sed -n \
-                       -e "s/^URL: /remote.$name.url . /p" \
-                       -e "s/^Pull: /remote.$name.pull ^$ /p" \
-                       -e "s/^Push: /remote.$name.push ^$ /p" \
-                       < "$f"
-               done
-               echo done
-       } | while read key regex value; do
-               case $key in
-               done)
-                       if [ $error = 0 ]; then
-                               mv "$GIT_DIR"/remotes "$GIT_DIR"/remotes.old
-                       fi ;;
-               *)
-                       git-repo-config $key "$value" $regex || error=1 ;;
-               esac
-       done
-fi
-
 get_data_source () {
        case "$1" in
        */*)
@@ -69,8 +42,8 @@ get_remote_url () {
        '')
                echo "$1" ;;
        config-partial)
-               token=$(expr "z$1" : '\([^/]*\)/')
-               remainder=$(expr "z$1" : '[^/]*/\(.*\)')
+               token=$(expr "z$1" : 'z\([^/]*\)/')
+               remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
                url=$(git-repo-config --get "remote.$token.url")
                echo "$url/$remainder"
                ;;
@@ -95,6 +68,12 @@ get_remote_url () {
        esac
 }
 
+get_default_remote () {
+       curr_branch=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
+       origin=$(git-repo-config --get "branch.$curr_branch.remote")
+       echo ${origin:-origin}
+}
+
 get_remote_default_refs_for_push () {
        data_source=$(get_data_source "$1")
        case "$data_source" in
@@ -113,9 +92,22 @@ get_remote_default_refs_for_push () {
 
 # Subroutine to canonicalize remote:local notation.
 canon_refs_list_for_fetch () {
-       # Leave only the first one alone; add prefix . to the rest
+       # If called from get_remote_default_refs_for_fetch
+       # leave the branches in branch.${curr_branch}.merge alone,
+       # or the first one otherwise; add prefix . to the rest
        # to prevent the secondary branches to be merged by default.
-       dot_prefix=
+       merge_branches=
+       if test "$1" = "-d"
+       then
+               shift ; remote="$1" ; shift
+               if test "$remote" = "$(get_default_remote)"
+               then
+                       curr_branch=$(git-symbolic-ref HEAD | \
+                           sed -e 's|^refs/heads/||')
+                       merge_branches=$(git-repo-config \
+                           --get-all "branch.${curr_branch}.merge")
+               fi
+       fi
        for ref
        do
                force=
@@ -128,6 +120,18 @@ canon_refs_list_for_fetch () {
                expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
                remote=$(expr "z$ref" : 'z\([^:]*\):')
                local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
+               dot_prefix=.
+               if test -z "$merge_branches"
+               then
+                       merge_branches=$remote
+                       dot_prefix=
+               else
+                       for merge_branch in $merge_branches
+                       do
+                           [ "$remote" = "$merge_branch" ] &&
+                           dot_prefix= && break
+                       done
+               fi
                case "$remote" in
                '') remote=HEAD ;;
                refs/heads/* | refs/tags/* | refs/remotes/*) ;;
@@ -147,7 +151,6 @@ canon_refs_list_for_fetch () {
                   die "* refusing to create funny ref '$local_ref_name' locally"
                fi
                echo "${dot_prefix}${force}${remote}:${local}"
-               dot_prefix=.
        done
 }
 
@@ -158,18 +161,15 @@ get_remote_default_refs_for_fetch () {
        '' | config-partial | branches-partial)
                echo "HEAD:" ;;
        config)
-               canon_refs_list_for_fetch \
-                       $(git-repo-config --get-all "remote.$1.pull") ;;
+               canon_refs_list_for_fetch -d "$1" \
+                       $(git-repo-config --get-all "remote.$1.fetch") ;;
        branches)
                remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
                case "$remote_branch" in '') remote_branch=master ;; esac
                echo "refs/heads/${remote_branch}:refs/heads/$1"
                ;;
        remotes)
-               # This prefixes the second and later default refspecs
-               # with a '.', to signal git-fetch to mark them
-               # not-for-merge.
-               canon_refs_list_for_fetch $(sed -ne '/^Pull: */{
+               canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
                                                s///p
                                        }' "$GIT_DIR/remotes/$1")
                ;;