auto-fetch mob if it exists
[~madcoder/dotfiles.git] / +bin / 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 remote update
56 if git config remote.mob.fetch >/dev/null 2>/dev/null; then
57     git remote prune mob
58 fi
59 if test `git rev-list .."${remote}/${branch#refs/heads/}" -- | wc -l` = 0; then
60     echo "Current branch $lbranch is up to date."
61     exit 0
62 fi
63
64 $gitk HEAD..."${remote}/${branch#refs/heads/}" --
65 if test -z "$action"; then
66     echo -n "(r)ebase/(m)erge/(q)uit ? "
67     read ans
68     case "$ans" in
69         r*) action=rebase;;
70         m*) action=merge;;
71         *)  exit 0;;
72     esac
73 fi
74
75 no_changes () {
76         git diff-index --quiet --cached HEAD --ignore-submodules -- &&
77         git diff-files --quiet --ignore-submodules
78 }
79
80 unclean=
81 no_changes || unclean=t
82
83
84 case "$action" in
85     rebase)
86         test -z "$unclean" || git stash save "git-up stash"
87         git rebase "${remote}/${branch#refs/heads/}"
88         cd "$(dirname "$(git rev-parse --git-dir)")" && git submodule update --rebase
89         ;;
90     merge)
91         test -z "$unclean" || git stash save "git-up stash"
92         git merge "${remote}/${branch#refs/heads/}"
93         ;;
94     *)
95         echo 1>&2 "no action specified"
96         exit 1
97         ;;
98 esac
99
100 if test -n "$unclean"; then
101     if test  -d "$(git rev-parse --git-dir)/../.dotest"; then
102         echo ""
103         echo "run 'git stash apply' when rebase is finished"
104     else
105         git stash pop
106     fi
107 fi