Set u+x on scripts.
[git-central.git] / scripts / checkout
1 #!/bin/sh
2 #
3 # Makes checkout "just work" in terms of checking out the branch whether or not
4 # it exists locally or remotely.
5 #
6 # If the branch exists locally (e.g. foo), then just check it out
7 # If the branch exists remotely (e.g. origin/foo), then create a new local branch
8 # If the branch does not exist remote, then make a new local branch and push it
9 #
10
11 branch_name=$1
12
13 # See if it already exists
14 git rev-parse --verify --quiet $branch_name >/dev/null
15 if [[ $? -eq 0 ]] ; then
16         # Just checkout what they asked
17         git checkout "$branch_name"
18 else
19         # Make sure we have the latest origin/$branch_name to branch
20         git fetch
21         exists_remote=$(git branch -r | grep -x "  origin/$branch_name" | wc -l)
22         if [[ $exists_remote -eq 0 ]] ; then
23                 # Specifying stable to get the last released code
24                 git checkout -b "$branch_name" origin/stable
25                 # Go ahead and put the branch out on the server
26                 git push origin "$branch_name"
27                 # Setup the merge property so that pulls come from the right place (instead of stable)
28                 git config --replace-all "branch.$branch_name.merge" "refs/heads/$branch_name"
29         else
30                 # Do the checkout
31                 git checkout -b "$branch_name" "origin/$branch_name"
32         fi
33 fi
34