about summary refs log tree commit diff
path: root/xlocate
blob: 7294790e5ec1d3a87ee7be556cf35f4341b5acb7 (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
#!/bin/sh
# xlocate [-g | -S | PATTERN] - locate files in all XBPS packages

: ${XDG_CACHE_HOME:=~/.cache}
: ${XDG_CONFIG_HOME:=~/.config}
: ${XLOCATE_CONF:="${XDG_CONFIG_HOME}/xlocate.conf"}

if [ -f "${XLOCATE_CONF}" ]; then
	. "${XLOCATE_CONF}";
fi

: ${XLOCATE_GIT:=$XDG_CACHE_HOME/xlocate.git}
: ${XLOCATE_REPO:=https://repo-default.voidlinux.org/xlocate/xlocate.git}

if command -v git >/dev/null 2>&1; then
	GIT_CMD=$(command -v git)
elif command -v chroot-git >/dev/null 2>&1; then
	GIT_CMD=$(command -v chroot-git)
else
	echo "xlocate: git command not found." 1>&2
	exit 1
fi

if command -v pv >/dev/null; then
	PROGRESS="pv -l"
else
	PROGRESS=cat
fi

xupdategit() {
	set -e
	DIR=$(mktemp -dt xlocate.XXXXXX)
	DIR=$(/usr/bin/realpath -e "$DIR")
	$GIT_CMD init -q $DIR
	cd $DIR
	xbps-query -M -Ro '*' | $PROGRESS | awk '
		$0 ~ ": " {
			s = index($0, ": ")
			pkg = substr($0, 1, s-1)
			file = substr($0, s+2)
			sub(" *\\([^)]*\\)$", "", file)
			print file >>pkg
			}'
	printf '%s\n' ./* |
		LC_ALL= xargs -d'\n' -I'{}' -n1 -P "$(nproc)" -r -- \
			sort -o {} {}
	$GIT_CMD add ./*
	$GIT_CMD -c user.name=xupdategit -c user.email=xupdategit@none commit -q -m 'xupdategit'
	$GIT_CMD repack -ad
	rm -rf "$XLOCATE_GIT" .git/COMMIT_EDITMSG .git/description \
		.git/index .git/hooks .git/logs
	[ -n "${XLOCATE_GIT%/*}" ] && mkdir -p "${XLOCATE_GIT%/*}"
	mv .git "$XLOCATE_GIT"
	rm -rf "$DIR"
}

xsyncgit() {
	if ! [ -d "$XLOCATE_GIT" ]; then
		[ -n "${XLOCATE_GIT%/*}" ] && mkdir -p "${XLOCATE_GIT%/*}"
		$GIT_CMD clone --bare "$XLOCATE_REPO" "$XLOCATE_GIT"
	fi
	$GIT_CMD -C "$XLOCATE_GIT" fetch -u -f "$XLOCATE_REPO" master:master
}

case "$1" in
-g)
	xupdategit
	exit $?;;
-S)
	xsyncgit
	exit $?;;
'')
	echo "Usage: xlocate [-g | -S | PATTERN]" 1>&2
	exit 1;;
esac

if [ -d "$XLOCATE_GIT" ]; then
	if [ -f "$XLOCATE_GIT/refs/heads/master" ]; then
		BASE="$XLOCATE_GIT/refs/heads/master"
	elif [ -f "$XLOCATE_GIT/FETCH_HEAD" ]; then
		BASE="$XLOCATE_GIT/FETCH_HEAD"
	fi
	if [ -z "$BASE" ] || find /var/db/xbps/ -name '*repodata' -newer "$BASE" | grep -q .; then
		if grep -q origin "$XLOCATE_GIT/config"; then
			echo "xlocate: database outdated, please run xlocate -S." 1>&2
		else
			echo "xlocate: database outdated, please run xlocate -g." 1>&2
		fi
	fi
	$GIT_CMD -c grep.lineNumber=false --git-dir="$XLOCATE_GIT" grep -- "$@" @ |
		sed 's/^@://; s/:/\t/' | grep .
else
	echo "xlocate: database not found, please use xlocate -S." 1>&2
	exit 1
fi