blob: 55fe8d56c9d5107df0843378ef137d64a48b2980 [file] [log] [blame]
Jonathan Nieder11d62142013-11-25 13:03:52 -08001# 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.
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -07004
Junio C Hamanoe8cc80d2005-11-23 23:46:13 -08005# 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.
Dan Loewenherz7bd93c12009-04-22 21:46:02 -04007GIT_DIR=$(git rev-parse -q --git-dir) || :;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -07008
Santi Béjar648ad182006-09-23 12:05:43 +02009get_default_remote () {
Stephen Boyd9ecd3ad2011-03-30 01:48:40 -070010 curr_branch=$(git symbolic-ref -q HEAD)
Junio C Hamano23525702011-03-30 19:33:33 -070011 curr_branch="${curr_branch#refs/heads/}"
Junio C Hamano5be60072007-07-02 22:52:14 -070012 origin=$(git config --get "branch.$curr_branch.remote")
Santi Béjar648ad182006-09-23 12:05:43 +020013 echo ${origin:-origin}
14}
15
Santi Béjar97af7ff2009-06-12 00:39:18 +020016get_remote_merge_branch () {
17 case "$#" in
18 0|1)
Santi Béjare9460a62009-06-12 00:39:19 +020019 origin="$1"
20 default=$(get_default_remote)
21 test -z "$origin" && origin=$default
Santi Béjarf864f262010-12-06 11:20:11 +010022 curr_branch=$(git symbolic-ref -q HEAD) &&
Santi Béjare9460a62009-06-12 00:39:19 +020023 [ "$origin" = "$default" ] &&
24 echo $(git for-each-ref --format='%(upstream)' $curr_branch)
25 ;;
Santi Béjar97af7ff2009-06-12 00:39:18 +020026 *)
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+\(.*\)')
35 ;;
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
Martin von Zweigbergkfe249b42010-11-13 23:58:22 +010045 [ -n "$remote" ] && case "$repo" in
46 .)
47 echo "refs/heads/$remote"
48 ;;
49 *)
50 echo "refs/remotes/$repo/$remote"
51 ;;
52 esac
Santi Béjar97af7ff2009-06-12 00:39:18 +020053 esac
54}
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050055
56error_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)
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010062 # 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)
67 fi
68
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050069 if test -z "$branch_name"
70 then
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010071 echo "You are not currently on a branch. Please specify which
72branch you want to $op_type $op_prep. See git-${cmd}(1) for details.
73
74 $example
75"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050076 else
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010077 echo "There is no tracking information for the current branch.
78Please specify which branch you want to $op_type $op_prep.
79See git-${cmd}(1) for details
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050080
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010081 $example
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050082
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010083If you wish to set tracking information for this branch you can do so with:
84
Michael J Grubera3a43912012-11-06 11:29:01 +010085 git branch --set-upstream-to=$remote/<branch> ${branch_name#refs/heads/}
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010086"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050087 fi
88 exit 1
89}