about summary refs log tree commit diff
path: root/xlocate
blob: fdd79d529215bb0dcd695af37669bb0368c96f97 (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
#!/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://alpha.de.repo.voidlinux.org/xlocate/xlocate.git}

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 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 add ./*
	git -c user.name=xupdategit -c user.email=xupdategit@none commit -q -m 'xupdategit'
	git 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 clone --bare "$XLOCATE_REPO" "$XLOCATE_GIT"
	fi
	git -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 -c grep.lineNumber=false --git-dir="$XLOCATE_GIT" grep -- "$@" @ |
		sed 's/^@://; s/:/\t/' | grep .
else
	xbps-query --regex -Ro -- "$@" |
		sed 's/ *([^)]*)$//; s/^\([^ ]*\)-[^-]*: /\1\t/'
fi