-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-pullme
executable file
·91 lines (80 loc) · 2.38 KB
/
git-pullme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
#
# This will fetch and merge only the branch you're currently in, avoiding
# fetches that you aren't interested in and possibly avoiding "non-fast-forward"
# messages on pushes
#
function usage() {
echo "Usage: git pullme [-r | --no-rebase]" >&2
}
# Default is that we will allow a rebase
rebase_ok=yes
GETOPT=$(getopt -u -o hr --long no-rebase -- "$@")
if [[ $? != 0 ]]; then
usage
exit 1
fi
set -- $GETOPT
while [[ 0 != $# ]]; do
case "$1" in
-r|--no-rebase)
rebase_ok=no
;;
-h)
usage
exit 0
;;
--)
shift
break
;;
*)
echo "Unrecognized option ($1)??" >&2
usage
exit 1
;;
esac
shift
done
ref=$(git symbolic-ref HEAD 2>/dev/null)
branch=${ref##refs/heads/}
if [[ -z $branch ]]; then
echo "Not on any branch that I can find!"
exit 1
fi
remote=$(git config --get "branch.$branch.remote")
merge_ref=$(git config --get "branch.$branch.merge")
merge=${merge_ref##refs/heads/}
if [[ -z $remote || -z $merge ]]; then
echo 'Your branch is not tracking a remote branch. Consider using "git corb"'
exit 1
fi
echo "] git fetch $remote $merge:refs/remotes/$remote/$merge"
git fetch $remote $merge:refs/remotes/$remote/$merge
echo
behind=$(git rev-list "$branch..$remote/$merge" 2>/dev/null | wc -l)
ahead=$(git rev-list "$remote/$merge..$branch" 2>/dev/null | wc -l)
local_merges=$(git rev-list --parents "$remote/$merge..$branch" | awk 'NF > 2 {print}' | wc -l)
# If we are behind the remote, we'll need to merge
if [[ $behind -gt 0 ]]; then
# If we are ahead of the remote, it's a diverge
if [[ $ahead -gt 0 ]]; then
# If our changes contain no merges, and we are allowed to rebase, we will do that,
# avoiding superfluous merges
if [[ $rebase_ok = yes && $local_merges = 0 ]]; then
echo "Your local commits do not contain any merges, rebasing on $remote/$merge"
echo "] git rebase $remote/$merge"
git rebase "$remote/$merge"
# Otherwise, we must merge
else
echo "] git merge $remote/$merge"
git merge "$remote/$merge"
fi
# We're behind only, do a merge
else
echo "] git merge $remote/$merge"
git merge "$remote/$merge"
fi
else
echo "Already up-to-date."
fi