blob: 0e87e0915ee16e36385aa4182b3a4271d1308641 [file] [log] [blame]
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -07001#!/bin/sh
2
Junio C Hamanoe8cc80d2005-11-23 23:46:13 -08003# git-ls-remote could be called from outside a git managed repository;
4# this would fail in that case and would issue an error message.
Dan Loewenherz7bd93c12009-04-22 21:46:02 -04005GIT_DIR=$(git rev-parse -q --git-dir) || :;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -07006
Santi Béjar648ad182006-09-23 12:05:43 +02007get_default_remote () {
Stephen Boyd9ecd3ad2011-03-30 01:48:40 -07008 curr_branch=$(git symbolic-ref -q HEAD)
Junio C Hamano23525702011-03-30 19:33:33 -07009 curr_branch="${curr_branch#refs/heads/}"
Junio C Hamano5be60072007-07-02 22:52:14 -070010 origin=$(git config --get "branch.$curr_branch.remote")
Santi Béjar648ad182006-09-23 12:05:43 +020011 echo ${origin:-origin}
12}
13
Santi Béjar97af7ff2009-06-12 00:39:18 +020014get_remote_merge_branch () {
15 case "$#" in
16 0|1)
Santi Béjare9460a62009-06-12 00:39:19 +020017 origin="$1"
18 default=$(get_default_remote)
19 test -z "$origin" && origin=$default
Santi Béjarf864f262010-12-06 11:20:11 +010020 curr_branch=$(git symbolic-ref -q HEAD) &&
Santi Béjare9460a62009-06-12 00:39:19 +020021 [ "$origin" = "$default" ] &&
22 echo $(git for-each-ref --format='%(upstream)' $curr_branch)
23 ;;
Santi Béjar97af7ff2009-06-12 00:39:18 +020024 *)
25 repo=$1
26 shift
27 ref=$1
28 # FIXME: It should return the tracking branch
29 # Currently only works with the default mapping
30 case "$ref" in
31 +*)
32 ref=$(expr "z$ref" : 'z+\(.*\)')
33 ;;
34 esac
35 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
36 remote=$(expr "z$ref" : 'z\([^:]*\):')
37 case "$remote" in
38 '' | HEAD ) remote=HEAD ;;
39 heads/*) remote=${remote#heads/} ;;
40 refs/heads/*) remote=${remote#refs/heads/} ;;
41 refs/* | tags/* | remotes/* ) remote=
42 esac
Martin von Zweigbergkfe249b42010-11-13 23:58:22 +010043 [ -n "$remote" ] && case "$repo" in
44 .)
45 echo "refs/heads/$remote"
46 ;;
47 *)
48 echo "refs/remotes/$repo/$remote"
49 ;;
50 esac
Santi Béjar97af7ff2009-06-12 00:39:18 +020051 esac
52}
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050053
54error_on_missing_default_upstream () {
55 cmd="$1"
56 op_type="$2"
57 op_prep="$3"
58 example="$4"
59 branch_name=$(git symbolic-ref -q HEAD)
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010060 # If there's only one remote, use that in the suggestion
61 remote="<remote>"
62 if test $(git remote | wc -l) = 1
63 then
64 remote=$(git remote)
65 fi
66
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050067 if test -z "$branch_name"
68 then
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010069 echo "You are not currently on a branch. Please specify which
70branch you want to $op_type $op_prep. See git-${cmd}(1) for details.
71
72 $example
73"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050074 else
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010075 echo "There is no tracking information for the current branch.
76Please specify which branch you want to $op_type $op_prep.
77See git-${cmd}(1) for details
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050078
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010079 $example
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050080
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010081If you wish to set tracking information for this branch you can do so with:
82
Michael J Grubera3a43912012-11-06 11:29:01 +010083 git branch --set-upstream-to=$remote/<branch> ${branch_name#refs/heads/}
Carlos Martín Nieto3c023962012-03-04 05:41:26 +010084"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -050085 fi
86 exit 1
87}