diff options
Diffstat (limited to 'git-parse-remote.sh')
-rw-r--r--[-rwxr-xr-x] | git-parse-remote.sh | 251 |
1 files changed, 75 insertions, 176 deletions
diff --git a/git-parse-remote.sh b/git-parse-remote.sh index 5f158c613f..b24119d69c 100755..100644 --- a/git-parse-remote.sh +++ b/git-parse-remote.sh @@ -2,190 +2,89 @@ # git-ls-remote could be called from outside a git managed repository; # this would fail in that case and would issue an error message. -GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :; +GIT_DIR=$(git rev-parse -q --git-dir) || :; -get_data_source () { - case "$1" in - */*) - # Not so fast. This could be the partial URL shorthand... - token=$(expr "$1" : '\([^/]*\)/') - remainder=$(expr "$1" : '[^/]*/\(.*\)') - if test -f "$GIT_DIR/branches/$token" - then - echo branches-partial - else - echo '' - fi - ;; - *) - if test -f "$GIT_DIR/remotes/$1" - then - echo remotes - elif test -f "$GIT_DIR/branches/$1" - then - echo branches - else - echo '' - fi ;; - esac -} - -get_remote_url () { - data_source=$(get_data_source "$1") - case "$data_source" in - '') - echo "$1" ;; - remotes) - sed -ne '/^URL: */{ - s///p - q - }' "$GIT_DIR/remotes/$1" ;; - branches) - sed -e 's/#.*//' "$GIT_DIR/branches/$1" ;; - branches-partial) - token=$(expr "$1" : '\([^/]*\)/') - remainder=$(expr "$1" : '[^/]*/\(.*\)') - url=$(sed -e 's/#.*//' "$GIT_DIR/branches/$token") - echo "$url/$remainder" - ;; - *) - die "internal error: get-remote-url $1" ;; - esac +get_default_remote () { + curr_branch=$(git symbolic-ref -q HEAD) + curr_branch="${curr_branch#refs/heads/}" + origin=$(git 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 - '' | branches | branches-partial) - ;; # no default push mapping, just send matching refs. - remotes) - sed -ne '/^Push: */{ - s///p - }' "$GIT_DIR/remotes/$1" ;; +get_remote_merge_branch () { + case "$#" in + 0|1) + origin="$1" + default=$(get_default_remote) + test -z "$origin" && origin=$default + curr_branch=$(git symbolic-ref -q HEAD) && + [ "$origin" = "$default" ] && + echo $(git for-each-ref --format='%(upstream)' $curr_branch) + ;; *) - die "internal error: get-remote-default-ref-for-push $1" ;; - esac -} - -# Subroutine to canonicalize remote:local notation. -canon_refs_list_for_fetch () { - # Leave only the first one alone; add prefix . to the rest - # to prevent the secondary branches to be merged by default. - dot_prefix= - for ref - do - force= - case "$ref" in - +*) - ref=$(expr "$ref" : '\+\(.*\)') - force=+ - ;; - esac - expr "$ref" : '.*:' >/dev/null || ref="${ref}:" - remote=$(expr "$ref" : '\([^:]*\):') - local=$(expr "$ref" : '[^:]*:\(.*\)') - case "$remote" in - '') remote=HEAD ;; - refs/heads/* | refs/tags/*) ;; - heads/* | tags/* ) remote="refs/$remote" ;; - *) remote="refs/heads/$remote" ;; - esac - case "$local" in - '') local= ;; - refs/heads/* | refs/tags/*) ;; - heads/* | tags/* ) local="refs/$local" ;; - *) local="refs/heads/$local" ;; - esac - - if local_ref_name=$(expr "$local" : 'refs/\(.*\)') - then - git-check-ref-format "$local_ref_name" || - die "* refusing to create funny ref '$local_ref_name' locally" - fi - echo "${dot_prefix}${force}${remote}:${local}" - dot_prefix=. - done -} - -# Returns list of src: (no store), or src:dst (store) -get_remote_default_refs_for_fetch () { - data_source=$(get_data_source "$1") - case "$data_source" in - '' | branches-partial) - echo "HEAD:" ;; - 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: */{ - s///p - }' "$GIT_DIR/remotes/$1") + repo=$1 + shift + ref=$1 + # FIXME: It should return the tracking branch + # Currently only works with the default mapping + case "$ref" in + +*) + ref=$(expr "z$ref" : 'z+\(.*\)') ;; - *) - die "internal error: get-remote-default-ref-for-push $1" ;; + esac + expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:" + remote=$(expr "z$ref" : 'z\([^:]*\):') + case "$remote" in + '' | HEAD ) remote=HEAD ;; + heads/*) remote=${remote#heads/} ;; + refs/heads/*) remote=${remote#refs/heads/} ;; + refs/* | tags/* | remotes/* ) remote= + esac + [ -n "$remote" ] && case "$repo" in + .) + echo "refs/heads/$remote" + ;; + *) + echo "refs/remotes/$repo/$remote" + ;; + esac esac } -get_remote_refs_for_push () { - case "$#" in - 0) die "internal error: get-remote-refs-for-push." ;; - 1) get_remote_default_refs_for_push "$@" ;; - *) shift; echo "$@" ;; - esac -} +error_on_missing_default_upstream () { + cmd="$1" + op_type="$2" + op_prep="$3" + example="$4" + branch_name=$(git symbolic-ref -q HEAD) + if test -z "$branch_name" + then + echo "You are not currently on a branch, so I cannot use any +'branch.<branchname>.merge' in your configuration file. +Please specify which branch you want to $op_type $op_prep on the command +line and try again (e.g. '$example'). +See git-${cmd}(1) for details." + else + echo "You asked me to $cmd without telling me which branch you +want to $op_type $op_prep, and 'branch.${branch_name#refs/heads/}.merge' in +your configuration file does not tell me, either. Please +specify which branch you want to use on the command line and +try again (e.g. '$example'). +See git-${cmd}(1) for details. -get_remote_refs_for_fetch () { - case "$#" in - 0) - die "internal error: get-remote-refs-for-fetch." ;; - 1) - get_remote_default_refs_for_fetch "$@" ;; - *) - shift - tag_just_seen= - for ref - do - if test "$tag_just_seen" - then - echo "refs/tags/${ref}:refs/tags/${ref}" - tag_just_seen= - continue - else - case "$ref" in - tag) - tag_just_seen=yes - continue - ;; - esac - fi - canon_refs_list_for_fetch "$ref" - done - ;; - esac -} +If you often $op_type $op_prep the same branch, you may want to +use something like the following in your configuration file: + [branch \"${branch_name#refs/heads/}\"] + remote = <nickname> + merge = <remote-ref>" + test rebase = "$op_type" && + echo " rebase = true" + echo " + [remote \"<nickname>\"] + url = <url> + fetch = <refspec> -resolve_alternates () { - # original URL (xxx.git) - top_=`expr "$1" : '\([^:]*:/*[^/]*\)/'` - while read path - do - case "$path" in - \#* | '') - continue ;; - /*) - echo "$top_$path/" ;; - ../*) - # relative -- ugly but seems to work. - echo "$1/objects/$path/" ;; - *) - # exit code may not be caught by the reader. - echo "bad alternate: $path" - exit 1 ;; - esac - done +See git-config(1) for details." + fi + exit 1 } |