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

: ${XLOCATE_DB:=~/.cache/xlocate.db}
: ${XLOCATE_GIT:=~/.cache/xlocate.git}
: ${XLOCATE_REPO:=https://repo.voidlinux.eu/xlocate/xlocate.git}

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

xupdatedb() {
	echo "xlocate: reindexing database..." 1>&2
	xbps-query -Ro '*' |
		$PROGRESS |
		sed 's/ *([^)]*)$//; s|: ||; s,^,/,' |
		/usr/libexec/frcode >"$XLOCATE_DB"
}

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
		git -C "$XLOCATE_GIT" fetch -u -f "$XLOCATE_REPO" master:master
	else
		git clone --bare "$XLOCATE_REPO" "$XLOCATE_GIT"
	fi
}

if [ "$1" = -u ]; then
	xupdatedb
	exit $?
elif [ "$1" = -g ]; then
	xupdategit
	exit $?
elif [ "$1" = -S ]; then
	xsyncgit
	exit $?
fi

if [ -d "$XLOCATE_GIT" ]; then
	if find /var/db/xbps/ -name '*repodata' -newer "$XLOCATE_GIT/HEAD" | grep -q .; then
		echo "xlocate: database outdated, please run xlocate -g." 1>&2
	fi
	git --git-dir="$XLOCATE_GIT" grep "$@" @ |
		sed 's/^@://; s/:/\t/' | grep .
elif [ -e "$XLOCATE_DB" ]; then
	if find /var/db/xbps/ -name '*repodata' -newer "$XLOCATE_DB" | grep -q .; then
		echo "xlocate: database outdated, please run xlocate -u." 1>&2
	fi
	rx=
	case "$1" in
		# Just . is unlikely to be a regex here...
		*[*+\\\[\]^$]*) rx=-r;;
	esac
	# XXX also matches template in package name.
	glocate $rx -d "$XLOCATE_DB" "$1" |
		sed 's|^/\([^/]*\)-[^-/]*/|\1\t/|'
else
	xbps-query --regex -Ro "$@" |
		sed 's/ *([^)]*)$//; s/^\([^ ]*\)-[^-]*: /\1\t/'
fi