about summary refs log tree commit diff
path: root/xlocate
blob: 5892f5542271404175286f9db0d45199eac06a19 (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
#!/bin/sh
# xlocate [-g | -S | [-EFGPiw] 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." >&2
	exit 1
fi

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

if command -v column >/dev/null; then
	COLUMN="column -ts: -l2"
else
	COLUMN='sed s/:/\t/'
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
}

usage() {
	echo "Usage: xlocate [-g | -S | [-EFGPiw] PATTERN]" >&2
}

while getopts gSEFGPiwe:h flag; do
	case $flag in
		g) UPDATE=1 ;;
		S) SYNC=1 ;;
		[EFGPiw]) GREP_FLAGS="$GREP_FLAGS -$flag" ;;
		e) GREP_FLAGS="$GREP_FLAGS -e $OPTARG" ;;
		h) usage; exit 0 ;;
		?) usage; exit 1 ;;
	esac
done

shift $(( OPTIND - 1 ))

if [ -n "$UPDATE" ]; then
	xupdategit
	exit $?
elif [ -n "$SYNC" ]; then
	xsyncgit
	exit $?
fi

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." >&2
		else
			echo "xlocate: database outdated, please run xlocate -g." >&2
		fi
	fi
	$GIT_CMD -c grep.lineNumber=false -c grep.column=false --git-dir="$XLOCATE_GIT" grep $GREP_FLAGS "$@" @ |
		sed 's/^@://' | $COLUMN | grep .
else
	echo "xlocate: database not found, please use xlocate -S." >&2
	exit 1
fi