-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgit-update
executable file
·64 lines (54 loc) · 1.59 KB
/
git-update
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
#!/usr/bin/env bash
#
# NOTE: with git 2.6+ you can just use:
# > git -c rebase.autoStash=true pull --rebase
#
# Source: http://jasoncodes.com/posts/gup-git-rebase
set -e
git fetch
BRANCH=$(git symbolic-ref -q HEAD)
BRANCH=${BRANCH##refs/heads/}
BRANCH=${BRANCH:-HEAD}
if [ -z "$(git config branch.$BRANCH.remote)" -o -z "$(git config branch.$BRANCH.merge)" ]
then
echo "\"$BRANCH\" is not a tracking branch." >&2
exit 1
fi
# create a temp file for capturing command output
TEMPFILE="`mktemp -t gup.XXXXXX`"
trap '{ rm -f "$TEMPFILE"; }' EXIT
# if we're behind upstream, we need to update
# NOTE: `grep -E` is more portable than `grep -P` (busybox)
if git status | grep -E "^(# )?Your branch" > "$TEMPFILE"
then
# extract tracking branch from message
UPSTREAM=$(cat "$TEMPFILE" | cut -d "'" -f 2)
if [ -z "$UPSTREAM" ]
then
echo Could not detect upstream branch >&2
exit 1
fi
# can we fast-forward?
CAN_FF=1
grep -q "can be fast-forwarded" "$TEMPFILE" || CAN_FF=0
# stash any uncommitted changes
git stash save "Automatic stash by gup on $(date +'%F %T')." | tee "$TEMPFILE"
[ "${PIPESTATUS[0]}" -eq 0 ] || exit 1
# take note if anything was stashed
HAVE_STASH=0
grep -q "No local changes" "$TEMPFILE" || HAVE_STASH=1
if [ "$CAN_FF" -ne 0 ]
then
# if nothing has changed locally, just fast foward.
git merge --ff "$UPSTREAM"
else
# rebase our changes on top of upstream, but keep any merges
git rebase -p "$UPSTREAM"
fi
# restore any stashed changes
if [ "$HAVE_STASH" -ne 0 ]
then
echo "Popping stashed changes.."
git stash pop
fi
fi