da40169df18d0d9041d79649f1706d4607cb4c07
[~madcoder/dotfiles.git] / git-up
1 #!/bin/sh
2
3 OPTIONS_SPEC="\
4 $(basename $0) [options] [<remote> [<branch>]]
5 --
6 k,gitk      visualize unmerged differences
7 r,rebase    perform a rebase
8 m,merge     perform a merge
9 "
10 SUBDIRECTORY_OK=t
11 . "$(git --exec-path)/git-sh-setup"
12 require_work_tree
13
14 lbranch=$(git symbolic-ref HEAD | sed -e s~refs/heads/~~)
15 remote=$(git config --get "branch.$lbranch.remote" || echo origin)
16 branch=$(git config --get "branch.$lbranch.merge" || echo "refs/heads/$lbranch")
17
18 case "$(git config --bool --get madcoder.up-gitk)" in
19     true) gitk=gitk;;
20     *)    gitk=:
21 esac
22 case "$(git config --bool --get "branch.$lbranch.rebase")" in
23     true) action=rebase;;
24     *)    action=;;
25 esac
26
27 while test $# != 0; do
28     case "$1" in
29         -k|--gitk)
30             shift; gitk=gitk;;
31         --no-gitk)
32             shift; gitk=:;;
33         -r|--rebase)
34             shift; action=rebase;;
35         --no-rebase)
36             shift; rebase=${rebase#rebase};;
37         -m|--merge)
38             shift; action=merge;;
39         --no-merge)
40             shift; rebase=${rebase#merge};;
41         --)
42             shift; break;;
43         *)
44             usage;;
45     esac
46 done
47
48 case $# in
49     0) ;;
50     1) remote="$1";;
51     2) remote="$1"; branch="$2";;
52     *) usage;;
53 esac
54
55 git fetch "${remote}"
56 if test `git rev-list .."${remote}/${branch#refs/heads/}" -- | wc -l` = 0; then
57     echo "Current branch $lbranch is up to date."
58     exit 0
59 fi
60
61 $gitk HEAD..."${remote}/${branch#refs/heads/}" --
62 if test -z "$action"; then
63     echo -n "(r)ebase/(m)erge/(q)uit ? "
64     read ans
65     case "$ans" in
66         r*) action=rebase;;
67         m*) action=merge;;
68         *)  exit 0;;
69     esac
70 fi
71
72 unclean=
73 git rev-parse --verify HEAD > /dev/null && \
74     git update-index --refresh && \
75     git diff-files --quiet && \
76     git diff-index --cached --quiet HEAD -- || unclean=t
77
78 case "$action" in
79     rebase)
80         test -z "$unclean" || git stash save "git-up stash"
81         git rebase "${remote}/${branch#refs/heads/}"
82         ;;
83     merge)
84         test -z "$unclean" || git stash save "git-up stash"
85         git merge "${remote}/${branch#refs/heads/}"
86         ;;
87     *)
88         echo 1>&2 "no action specified"
89         exit 1
90         ;;
91 esac
92
93 if test -n "$unclean"; then
94     if test  -d "$(git rev-parse --git-dir)/../.dotest"; then
95         echo ""
96         echo "run 'git stash apply' when rebase is finished"
97     else
98         git stash apply
99     fi
100 fi