Jonathan Nieder | 11d6214 | 2013-11-25 13:03:52 -0800 | [diff] [blame] | 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. |
Junio C Hamano | ac4b0cf | 2005-08-20 02:52:24 -0700 | [diff] [blame] | 4 | |
Junio C Hamano | e8cc80d | 2005-11-23 23:46:13 -0800 | [diff] [blame] | 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. |
Dan Loewenherz | 7bd93c1 | 2009-04-22 21:46:02 -0400 | [diff] [blame] | 7 | GIT_DIR=$(git rev-parse -q --git-dir) || :; |
Junio C Hamano | ac4b0cf | 2005-08-20 02:52:24 -0700 | [diff] [blame] | 8 | |
Santi Béjar | 648ad18 | 2006-09-23 12:05:43 +0200 | [diff] [blame] | 9 | get_default_remote () { |
Stephen Boyd | 9ecd3ad | 2011-03-30 01:48:40 -0700 | [diff] [blame] | 10 | curr_branch=$(git symbolic-ref -q HEAD) |
Junio C Hamano | 2352570 | 2011-03-30 19:33:33 -0700 | [diff] [blame] | 11 | curr_branch="${curr_branch#refs/heads/}" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 12 | origin=$(git config --get "branch.$curr_branch.remote") |
Santi Béjar | 648ad18 | 2006-09-23 12:05:43 +0200 | [diff] [blame] | 13 | echo ${origin:-origin} |
| 14 | } |
| 15 | |
Santi Béjar | 97af7ff | 2009-06-12 00:39:18 +0200 | [diff] [blame] | 16 | get_remote_merge_branch () { |
| 17 | case "$#" in |
| 18 | 0|1) |
Santi Béjar | e9460a6 | 2009-06-12 00:39:19 +0200 | [diff] [blame] | 19 | origin="$1" |
| 20 | default=$(get_default_remote) |
| 21 | test -z "$origin" && origin=$default |
Santi Béjar | f864f26 | 2010-12-06 11:20:11 +0100 | [diff] [blame] | 22 | curr_branch=$(git symbolic-ref -q HEAD) && |
Santi Béjar | e9460a6 | 2009-06-12 00:39:19 +0200 | [diff] [blame] | 23 | [ "$origin" = "$default" ] && |
| 24 | echo $(git for-each-ref --format='%(upstream)' $curr_branch) |
| 25 | ;; |
Santi Béjar | 97af7ff | 2009-06-12 00:39:18 +0200 | [diff] [blame] | 26 | *) |
| 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 Zweigbergk | fe249b4 | 2010-11-13 23:58:22 +0100 | [diff] [blame] | 45 | [ -n "$remote" ] && case "$repo" in |
| 46 | .) |
| 47 | echo "refs/heads/$remote" |
| 48 | ;; |
| 49 | *) |
| 50 | echo "refs/remotes/$repo/$remote" |
| 51 | ;; |
| 52 | esac |
Santi Béjar | 97af7ff | 2009-06-12 00:39:18 +0200 | [diff] [blame] | 53 | esac |
| 54 | } |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 55 | |
| 56 | error_on_missing_default_upstream () { |
| 57 | cmd="$1" |
| 58 | op_type="$2" |
Vasco Almeida | 045fac5 | 2016-04-19 13:19:20 +0000 | [diff] [blame] | 59 | op_prep="$3" # FIXME: op_prep is no longer used |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 60 | example="$4" |
| 61 | branch_name=$(git symbolic-ref -q HEAD) |
Vasco Almeida | 045fac5 | 2016-04-19 13:19:20 +0000 | [diff] [blame] | 62 | display_branch_name="${branch_name#refs/heads/}" |
Carlos Martín Nieto | 3c02396 | 2012-03-04 05:41:26 +0100 | [diff] [blame] | 63 | # If there's only one remote, use that in the suggestion |
Vasco Almeida | 045fac5 | 2016-04-19 13:19:20 +0000 | [diff] [blame] | 64 | remote="$(gettext "<remote>")" |
| 65 | branch="$(gettext "<branch>")" |
Carlos Martín Nieto | 3c02396 | 2012-03-04 05:41:26 +0100 | [diff] [blame] | 66 | if test $(git remote | wc -l) = 1 |
| 67 | then |
| 68 | remote=$(git remote) |
| 69 | fi |
| 70 | |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 71 | if test -z "$branch_name" |
| 72 | then |
Vasco Almeida | 045fac5 | 2016-04-19 13:19:20 +0000 | [diff] [blame] | 73 | gettextln "You are not currently on a branch." |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 74 | else |
Vasco Almeida | 045fac5 | 2016-04-19 13:19:20 +0000 | [diff] [blame] | 75 | gettextln "There is no tracking information for the current branch." |
| 76 | fi |
| 77 | case "$op_type" in |
| 78 | rebase) |
| 79 | gettextln "Please specify which branch you want to rebase against." |
| 80 | ;; |
| 81 | merge) |
| 82 | gettextln "Please specify which branch you want to merge with." |
| 83 | ;; |
| 84 | *) |
| 85 | echo >&2 "BUG: unknown operation type: $op_type" |
| 86 | exit 1 |
| 87 | ;; |
| 88 | esac |
| 89 | eval_gettextln "See git-\${cmd}(1) for details." |
| 90 | echo |
| 91 | echo " $example" |
| 92 | echo |
| 93 | if test -n "$branch_name" |
| 94 | then |
| 95 | gettextln "If you wish to set tracking information for this branch you can do so with:" |
| 96 | echo |
| 97 | echo " git branch --set-upstream-to=$remote/$branch $display_branch_name" |
| 98 | echo |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 99 | fi |
| 100 | exit 1 |
| 101 | } |