-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvimbundle
executable file
·174 lines (148 loc) · 4.19 KB
/
vimbundle
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
# This script is used to install a vim bundle (for use with pathogen) from git.
if [ "x$1" = "x" ]
then
echo "Usage: $0 -u|-r|repo [base]" >&2
echo " -u updates the bundles to the latest commit on github"
echo " -r removes the bundle"
exit 1
fi
if [ -z "$(which jshon)" -o ! -x "$(which jshon)" ]
then
echo "Could not locate the jshon command" >&2
exit 1
else
JSHON="$(which jshon)"
fi
BUNDLE_DIR="$HOME/.dotfiles/vim/bundle"
case "$1" in
-u) MODE="update"
base="$2" ;;
-r) MODE="remove"
base="$2" ;;
*) MODE="install"
location="$1"
base="$2";;
esac
# source ~/.vimbundlerc for configuration overrides
[ -e "$HOME/.vimbundlerc" ] && . "$HOME/.vimbundlerc"
if [ ! -d "$BUNDLE_DIR" ]
then
echo "Bundle directory not found: $BUNDLE_DIR" >&2
exit 1
fi
function ingit {
[ "x$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "xtrue" ]
}
function find_git_repo {
typeset url="$1"
typeset giturl
if echo "$url" | grep -q '^https\?://'
then
giturl="$url"
elif [ "x$(echo "$url" | awk -F/ '{print NF}')" = "x2" ]
then
giturl="https://github.com/${url}.git"
else
echo "Could not determine git repo name from $url" >&2
exit 1
fi
if curl -L -s -D - -o /dev/null "$giturl" | grep -q '^Status: 200 OK'
then
echo "$giturl"
else
echo "Nothing found at the url: $giturl" >&2
exit 1
fi
}
function find_base_from_url {
echo "$1" | sed -e 's,^.*/\(.*\)\.git$,\1,' -e 's/[-_.]*vim[-_]*//g'
}
function setup_in_git {
TOPLEVEL="$(git rev-parse --show-toplevel)"
cd "${TOPLEVEL}"
}
if [ "x$MODE" = "xinstall" ]
then
giturl=$(find_git_repo "$location") || exit 1
BUNDLE_NAME="$base"
if [ "x$BUNDLE_NAME" = "x" ]
then
BUNDLE_NAME=$(find_base_from_url "$giturl")
fi
cd "${BUNDLE_DIR}"
# See if this bundle dir is under revision control
if ingit
then
setup_in_git
RELBUNDLEDIR="${BUNDLE_DIR#${TOPLEVEL}/}"
git submodule add --name "${BUNDLE_NAME}" "$giturl" "${RELBUNDLEDIR}/${BUNDLE_NAME}"
else
git clone "$giturl" "${RELBUNDLEDIR}/${BUNDLE_NAME}"
fi
fi
if [ "x$MODE" = "xupdate" ]
then
cd "${BUNDLE_DIR}"
if ingit
then
setup_in_git
if [ -n "$base" ]
then
BUNDLE_LIST="$(git config -f .gitmodules submodule."$base".path)"
else
BUNDLE_LIST="$(git submodule foreach --quiet pwd)"
fi
else
BUNDLE_LIST="$(find "$(pwd)" -name '.git' | sed -e 's,/\.git$,,')"
fi
oldIFS="$IFS"
IFS="
"
for bundle in $BUNDLE_LIST
do
cd "$bundle"
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
echo "Updating $(basename "$bundle")"
remote_url="$(git config remote.origin.url)"
if echo "$remote_url" | grep -q "github"
then
repo_name="$(echo "$remote_url" | sed -e 's,^.*/\(.*/.*\)\.git$,\1,')"
master_branch="$(curl -s -H "Authentication: token $(git config github.token)" \
https://api.github.com/repos/"$repo_name" 2>/dev/null | \
"$JSHON" -e 'default_branch' | sed -e 's/"//g')"
fi
: "${master_branch:="master"}"
# Checkout the $master_branch, update from the origin, then put us back into a detached HEAD state
git checkout "$master_branch"
git pull origin "$master_branch"
git checkout -q --detach "$master_branch"
# git submodule update
done
IFS="$oldIFS"
fi
if [ "x$MODE" = "xremove" ]
then
BUNDLE_NAME="$base"
cd "${BUNDLE_DIR}"
if ingit
then
set -x
setup_in_git
test ! -f .gitmodules && echo ".gitmodules file not found" >&2 && exit 1
RELBUNDLEDIR="$(git config -f .gitmodules submodule."$BUNDLE_NAME".path)"
[ -z "$RELBUNDLEDIR" -o ! -d "$RELBUNDLEDIR" ] && \
echo "Could not find bundle named $BUNDLE_NAME" >&2 && exit 1
git submodule deinit "$RELBUNDLEDIR"
# git config -f .gitmodules --remove-section submodule.$BUNDLE_NAME
# rm -Rf "$RELBUNDLEDIR" >/dev/null 2>&1
git rm -f -r -q --ignore-unmatch "$RELBUNDLEDIR"
[ -d ".git/modules/${BUNDLE_NAME}" ] && rm -Rf ".git/modules/${BUNDLE_NAME}"
# git add .gitmodules
else
RELBUNDLEDIR="${BUNDLE_DIR#${TOPLEVEL}/}"
[ ! -d "${RELBUNDLEDIR}/${BUNDLE_NAME}" ] && \
echo "Coiuld not find bundle named $BUNDLE_NAME" >&2 && exit 1
rm -Rf "${RELBUNDLEDIR:?}/$BUNDLE_NAME"
fi
fi