aboutsummaryrefslogtreecommitdiff
path: root/bin/vcs
blob: bf12527f2e5e788a146be8908dd877f1a87889b0 (plain) (blame)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/sh
set -eu


TYPES='
git
fossil
bitkeeper
darcs
mercurial
cvs
'


guess_type() {
	if [ -e "$1"/.git ] || { [ -n "${GIT_DIR:-}" ] && [ -n "$GIT_WORK_TREE" ]; }; then
		echo git
	elif [ -e "$1"/.hg ]; then
		echo mercurial
	elif [ -e "$1"/.bk ]; then
		echo bitkeeper
	elif [ -e "$1"/_darcs ]; then
		echo darcs
	elif [ -e "$1"/CVS/ ]; then
		echo cvs
	elif [ -e "$1"/.fslckout ]; then
		echo fossil
	else
		return 1
	fi
}




git_fetch() {
	git fetch --all "$@"
}

fossil_fetch() {
	fossil pull "$@"
}

darcs_fetch() {
	darcs fetch "$@"
}

mercurial_fetch() {
	hg pull "$@"
}

cvs_fetch() {
	timeout 300 cvs update "$@"
}

git_status() {
	git status "$@"
}

fossil_status() {
	fossil status "$@"
	fossil extras "$@"
}

git_diff() {
	git diff "$@"
}

fossil_diff() {
	fossil diff --unified | diff-so-fancy | less -R
}

git_ps1() {
	BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)"
	OUT="$(git status --short --branch --porcelain)"
	BRANCH_LINE="$(printf '%s\n' "$OUT" | head -n 1)"
	DIFF_LINES="$(printf '%s\n' "$OUT" | tail -n +2)"

	IS_AHEAD=false
	IS_BEHIND=false
	if printf '%s\n' "$BRANCH_LINE" | grep -q 'ahead'; then
		IS_AHEAD=true
	fi
	if printf '%s\n' "$BRANCH_LINE" | grep -q 'behind'; then
		IS_BEHIND=true
	fi

	LINE=''

	if [ "$IS_AHEAD" = true ] && [ "$IS_BEHIND" = true ]; then
		LINE="^^^ $BRANCH_NAME vvv"
	elif [ "$IS_AHEAD" = true ]; then
		LINE="^ $BRANCH_NAME ^"
	elif [ "$IS_BEHIND" = true ]; then
		LINE="v $BRANCH_NAME v"
	else
		LINE="$BRANCH_NAME"
	fi

	HAS_DIFF=false
	HAS_UNTRACKED=false
	if printf '%s\n' "$DIFF_LINES" | grep -q '^[A|D|M|R| ][M|D| ]'; then
		HAS_DIFF=true
	fi
	if printf '%s\n' "$DIFF_LINES" | grep -Eq '^([?][?]| A)'; then
		HAS_UNTRACKED=true
	fi

	if [ "$HAS_DIFF" = true ]; then
		COLOR_FN=redb
		LINE="{$LINE}"
	elif [ "$IS_AHEAD" = true ] || [ "$IS_BEHIND" = true ]; then
		COLOR_FN=bluei
		LINE="[$LINE]"
	elif [ "$HAS_UNTRACKED" = true ]; then
		COLOR_FN=lightblue
		LINE="{$LINE}"
	else
		COLOR_FN=green
		LINE="($LINE)"
	fi

	color -c "$COLOR_FN" "$LINE"

	BRANCH_COUNT="$(git branch --list | wc -l)"
	if [ "$BRANCH_COUNT" -gt 1 ]; then
		color -c lightblue "<$BRANCH_COUNT>"
	fi

	STASH_COUNT="$(git stash list | wc -l)"
	if [ "$STASH_COUNT" != 0 ]; then
		color -c red "*$STASH_COUNT"
	fi

	color -c blacki " - git/$(git rev-parse HEAD)"
}

bitkeeper_ps1() {
	printf ''
}

cvs_ps1() {
	printf ''
}

fossil_ps1() {
	BRANCH_NAME="$(fossil branch current)"

	if [ -n "$(fossil extras)" ]; then
		HAS_UNTRACKED=1
	fi

	BRANCH_MARKER="$BRANCH_NAME"

	if [ -n "${HAS_UNTRACKED:-}" ]; then
		COLOR_FN=lightblue
		LINE="($BRANCH_MARKER)"
	else
		COLOR_FN=green
		LINE="($BRANCH_MARKER)"
	fi

	color -c "$COLOR_FN" "$LINE"

	color -c blacki " - fossil/$(fossil info | awk '/^checkout:/ { print $2 }')"
}

mercurial_ps1() {
	BRANCH_NAME="$(hg branch)"
}

git_gc() {
	git remote prune origin "$@"
	git gc
}

git_ls() {
	git ls-files
}

fossil_ls() {
	fossil ls
}

git_clean() {
	git clean -nffdx
}

fossil_clean() {
	fossil clean -n -v -x --emptydirs
}


usage() {
	cat <<-'EOF'
		Usage:
		  vcs [-C DIR] ACTION
		  vcs [-C DIR] -t
		  vcs -l
		  vcs -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -C DIR        change to DIR instead of PWD
		  -l            list the supported VCS types
		  -t            show the guesses VCS type
		  -h, --help    show this message

		  ACTION        the action to be performed on the repository:
		                - fetch
		                - ps1
		                - status
		                - gc
		                - ls


		Wrapper command for normalizing options of differenct version
		control software.


		Examples:

		  Say the type of repo in "project/":

		    $ vcs -C project/ -t


		  Fetch updates for VCS in current directory:

		    $ vcs fetch
	EOF
}


for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

while getopts 'C:lth' flag; do
	case "$flag" in
		C)
			cd "$OPTARG"
			;;
		(l)
			# shellcheck disable=2086
			echo $TYPES | tr ' ' '\n'
			exit
			;;
		(t)
			guess_type "$PWD" || {
				printf 'Could not guess the type of the repository.\n' >&2
				exit 2
			}
			exit
			;;
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
	esac
done
shift $((OPTIND - 1))

ACTION="${1:-}"
eval "$(assert-arg -- "$ACTION" 'ACTION')"
shift

if [ "${1:-}" = '--' ]; then
	shift
fi

TYPE="$(guess_type "$PWD")"
if [ -z "$TYPE" ]; then
	printf 'Could not guess the type of the repository.\n' >&2
	exit 2
fi

CMD="$TYPE"_"$ACTION"
if ! command -v "$CMD" >/dev/null; then
	printf 'Invalid ACTION: "%s".\n\n' "$ACTION" >&2
	usage >&2
	exit 2
fi

"$CMD" "$@"