#!/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 } GOT_E= 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"; GOT_E=true ;; h) usage; exit 0 ;; ?) usage; exit 1 ;; esac done shift $(( OPTIND - 1 )) if [ -n "$UPDATE" ]; then xupdategit exit $? elif [ -n "$SYNC" ]; then xsyncgit exit $? elif [ $# = 0 ] && [ -z "$GOT_E" ]; then usage exit 1 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