-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitext
137 lines (116 loc) · 4.01 KB
/
gitext
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
function source_dir {
local SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
}
set -e
set -o pipefail
source_dir
COMMAND=$1
case "$COMMAND" in
branch)
git rev-parse --symbolic-full-name --abbrev-ref HEAD
;;
revision)
git rev-parse $(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
;;
rev)
git rev-parse $(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
;;
checkout)
if [ $(git branch | grep -cw $2) == 0 ]; then
git checkout --track -b $2 remotes/origin/$2
else
git checkout $2
fi
;;
remoteexists)
if [ $(git branch -r | grep -cw origin/$2) == 0 ]; then
# echo remote branch $2 does not exists
exit 2
else
# echo remote branch $2 exists
exit 0
fi
;;
localexists)
if [ $(git branch | grep -cw $2) == 0 ]; then
# echo local branch $2 does not exists
exit 2
else
# echo local branch $2 exists
exit 0
fi
;;
remoteurl)
echo "$(git config --get remote.origin.url)"
;;
meta)
SUB_COMMAND=$2
FILES="${PWD}/*"
for file in ${FILES}; do
if [ -d $file ]; then
case "$SUB_COMMAND" in
list)
cd $file
echo $(gitext remoteurl)
cd ..
;;
listbranch)
cd $file
echo -n "$(${DIR}/gitext remoteurl) "
${DIR}/gitext branch
cd ..
;;
branchOf)
cd $file
branch=$(${DIR}/gitext branch)
if [[ $branch == $3 ]]; then
echo "$(${DIR}/gitext remoteurl)"
fi
cd ..
;;
hasBranch)
if [ -z $3 ]; then
exit 0
fi
cd $file
if [ $(git branch | grep -cw $3) != 0 ]; then
gitext remoteurl
else
if [ $(git branch -r | grep -cw origin/$3) != 0 ]; then
gitext remoteurl
fi
fi
cd ..
;;
checkout)
cd $file
gitext checkout $3
cd ..
;;
reset)
cd $file
echo -n "$(${DIR}/gitext remoteurl) "
git reset $3 $4
cd ..
;;
*)
echo "Unknown meta command: $SUB_COMMAND"
echo "Use {list|listbranch|branchOf BRANCH|hasBranch BRANCH|checkout BRANCH|reset TYPE BRANCH}"
exit 1
esac
fi
done
;;
*)
echo "Usage: $0 {branch|rev|checkout BRANCH|remoteexists BRANCH|localexists BRANCH|meta SUB-COMMAND{list|listbranch|checkoutIf BRANCH}}"
exit 1
esac