-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-is-current
executable file
·109 lines (91 loc) · 2.25 KB
/
git-is-current
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
#
# git-is-current
#
# License: MIT
# Copyright (c) 2012 Lennart C. L. Kats
set -e
# Internal config
USE_BETA=0
# Defaults
BRANCH=HEAD
NO_FETCH=
MASTER=
if [ "$1" == "-q" ]; then
QUIET=-q
shift
fi
if [ "$1" == "-f" ]; then
NO_FETCH=-f
shift
fi
if [ "$1" == "-q" ]; then
QUIET=-q
shift
fi
if echo "$1" | grep origin/ >/dev/null; then
MASTER=$1
shift
fi
if [ "$1" != "" ]; then
BRANCH="$1"
fi
if [ "$MASTER" == "" ]; then
if [ "$USE_BETA" != "1" ]; then
MASTER=origin/master
else
FAIL=0
if [ ! $NO_FETCH ]; then
echo -n 'Fetching... '
git fetch origin master beta &>/dev/null
echo -ne '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b'
fi
$0 -f origin/master $BRANCH || FAIL=1
if git branch --list origin/beta >/dev/null; then
$0 -f origin/beta $BRANCH || FAIL=1
fi
exit $FAIL
fi
fi
if [ ! $NO_FETCH ]; then
M=`echo $MASTER | perl -pe 's/^origin\/(.*)/$1/'`
echo -n 'Fetching... '
git fetch origin $M &>/dev/null
echo -ne '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b'
fi
# Non-destructive version based on http://stackoverflow.com/a/6283843: doesn't detect "both added file" conflicts
BASE=`git merge-base $MASTER $BRANCH`
DIFF=`git merge-tree $BASE $MASTER $BRANCH || echo "added in -- git merge-tree failed"`
if echo "$DIFF" | grep -E '^.<<<<<' &>/dev/null; then
echo -e "$BRANCH \033[01;32mCONFLICTS\033[00m with $MASTER"
exit 1
elif echo "$DIFF" | grep -Ev '^added in' &>/dev/null; then
if [ ! $QUIET ]; then
echo "$BRANCH does not conflict with $MASTER"
fi
exit 0
fi
# Fallback: slow, in-repository check
OLD_BRANCH=`git get-branch`
STASH=
if ! (git checkout $MASTER && git checkout `git rev-parse HEAD`) &>/dev/null; then
STASH=1
git stash save "changes stashed by git-is-current" &>/dev/null ||
(echo Could not stash current changes>&2; git stash; exit 1)
fi
git checkout $MASTER &>/dev/null
git checkout `git rev-parse HEAD` &>/dev/null
if git merge $BRANCH --no-ff --no-commit &>/dev/null; then
if [ ! $QUIET ]; then
echo "$BRANCH does not conflict with $MASTER"
fi
EXIT=0
else
echo -e "$BRANCH \033[01;31mCONFLICTS\033[00m with $MASTER"
EXIT=1
fi
git checkout -f $OLD_BRANCH &>/dev/null
if [ $STASH ]; then
git stash pop &>/dev/null
fi
exit $EXIT