]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - bin/git-track
.vimrc: Don't use modelines to set foldmethod=expr for autofolds
[dotfiles.git] / bin / git-track
1 #!/bin/sh
2 ## Usage: git track
3 ## git track <branch>
4 ## Point the current local branch at <branch> for the purpose
5 ## of merge tracking, pull, and status features. With no <branch>,
6 ## write the currently tracked branch to standard output.
7 ##
8 ## If you have git's bash-completion support enabled, add this:
9 ## complete -o default -o nospace -F _git_checkout git-track
10 set -e
11
12 # bail out with message to stderr and exit status 1
13 die() {
14 echo "$(basename $0):" "$@" 1>&2
15 exit 1
16 }
17
18 # deal with argv
19 case "$1" in
20 --help)
21 grep '^##' "$0" | cut -c4-
22 exit
23 ;;
24 "")
25 remote=
26 merge=
27 ;;
28 */*)
29 git rev-parse "$1" >/dev/null
30 remote=$(echo "$1" | sed 's@^\(.*\)/.*@\1@')
31 merge=$(echo "$1" | sed 's@^.*/\(.*\)@\1@')
32 ;;
33 *)
34 git rev-parse "$1" >/dev/null
35 remote=
36 merge="$1"
37 ;;
38 esac
39
40 # get the current branch in refs/heads/<branch> form
41 ref=$(git symbolic-ref -q HEAD)
42 test -n "$ref" ||
43 die "you're not on a branch"
44
45 # just the branch name please
46 branch=$(echo "$ref" | sed 's@^refs/heads/@@')
47 test -n "$branch" ||
48 die "you're in a weird place; get on a local branch"
49
50 # if we don't have a target to track, show the
51 # currently tracked stuff.
52 test -z "$merge" && {
53 remote=$(git config --get "branch.$branch.remote" || true)
54 merge=$(
55 (git config --get "branch.$branch.merge") |
56 sed 's@refs/heads/@@'
57 )
58 if test -n "$remote" -a -n "$merge"; then
59 echo "$branch -> $remote/$merge"
60 elif test -n "$merge"; then
61 echo "$branch -> $merge"
62 else
63 echo "$branch is not tracking anything"
64 fi
65 exit 0
66 }
67
68 # set the remote if a full remote/branch ref was given
69 test -n "$remote" &&
70 git config "branch.$branch.remote" "$remote"
71
72 # set the ref in said remote we should track
73 git config "branch.$branch.merge" "refs/heads/$merge"