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