Sync with maint
[git/mjg.git] / git-parse-remote.sh
blob55fe8d56c9d5107df0843378ef137d64a48b2980
1 # This is a shell library to calculate the remote repository and
2 # upstream branch that should be pulled by "git pull" from the current
3 # branch.
5 # git-ls-remote could be called from outside a git managed repository;
6 # this would fail in that case and would issue an error message.
7 GIT_DIR=$(git rev-parse -q --git-dir) || :;
9 get_default_remote () {
10 curr_branch=$(git symbolic-ref -q HEAD)
11 curr_branch="${curr_branch#refs/heads/}"
12 origin=$(git config --get "branch.$curr_branch.remote")
13 echo ${origin:-origin}
16 get_remote_merge_branch () {
17 case "$#" in
18 0|1)
19 origin="$1"
20 default=$(get_default_remote)
21 test -z "$origin" && origin=$default
22 curr_branch=$(git symbolic-ref -q HEAD) &&
23 [ "$origin" = "$default" ] &&
24 echo $(git for-each-ref --format='%(upstream)' $curr_branch)
27 repo=$1
28 shift
29 ref=$1
30 # FIXME: It should return the tracking branch
31 # Currently only works with the default mapping
32 case "$ref" in
33 +*)
34 ref=$(expr "z$ref" : 'z+\(.*\)')
36 esac
37 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
38 remote=$(expr "z$ref" : 'z\([^:]*\):')
39 case "$remote" in
40 '' | HEAD ) remote=HEAD ;;
41 heads/*) remote=${remote#heads/} ;;
42 refs/heads/*) remote=${remote#refs/heads/} ;;
43 refs/* | tags/* | remotes/* ) remote=
44 esac
45 [ -n "$remote" ] && case "$repo" in
47 echo "refs/heads/$remote"
50 echo "refs/remotes/$repo/$remote"
52 esac
53 esac
56 error_on_missing_default_upstream () {
57 cmd="$1"
58 op_type="$2"
59 op_prep="$3"
60 example="$4"
61 branch_name=$(git symbolic-ref -q HEAD)
62 # If there's only one remote, use that in the suggestion
63 remote="<remote>"
64 if test $(git remote | wc -l) = 1
65 then
66 remote=$(git remote)
69 if test -z "$branch_name"
70 then
71 echo "You are not currently on a branch. Please specify which
72 branch you want to $op_type $op_prep. See git-${cmd}(1) for details.
74 $example
76 else
77 echo "There is no tracking information for the current branch.
78 Please specify which branch you want to $op_type $op_prep.
79 See git-${cmd}(1) for details
81 $example
83 If you wish to set tracking information for this branch you can do so with:
85 git branch --set-upstream-to=$remote/<branch> ${branch_name#refs/heads/}
88 exit 1