Created
May 7, 2018 19:10
-
-
Save adrolter/9841f11d767f206041b2677921975d22 to your computer and use it in GitHub Desktop.
Arch Linux Bash script for diffing locally installed files with the current package version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Diff a file on the filesystem with the current package version | |
set -eu | |
file="${1:?No file specified}" | |
[ -e "$1" ] || { >&2 printf 'Path does not exist: %s\n' "$1"; exit 1; } | |
[ ! -d "$1" ] || { >&2 printf 'Path is a directory: %s\n' "$1"; exit 2; } | |
# Canonicalize $file without resolving the basename | |
dir="$(readlink -f "$(dirname "$file")")" | |
file="$(basename "$file")" | |
case "$dir" in | |
/) file="/${file}" ;; | |
*) file="${dir}/${file}" ;; | |
esac | |
read -r package version < <(pacman -Fo "$file" | awk '/ is owned by /{print $5, $6}') || : # Allow non-zero exit for read | |
case "${package-}" in '') >&2 printf '%s does not appear to be owned by a repository package\n' "$file"; exit 3; esac | |
>&2 printf '%s is owned by %s %s\n' "$file" "$package" "$version" | |
pkgfile=/var/cache/pacman/pkg/${package#*/}-$version-x86_64.pkg.tar.xz | |
if [ ! -e "$pkgfile" ]; then | |
>&2 printf 'Package %s is not cached, using "pacman -Sdw %s" to download it...\n' "$package" "${package#*/}" | |
sudo pacman -Sdw --quiet --noconfirm "${package#*/}" >/dev/null | |
fi | |
diff --color=${COLOR:-always} -u <(tar -xf "$pkgfile" "${file#/}" -O 2>/dev/null) "$file" \ | |
| tail -n+3 \ | |
| perl -pne 'END { print(STDERR "Files are identical\n") if($. == 0); }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment