#!/usr/bin/env bash # PKGBUILD review # Show a file with bat/less/cat _review_show_file() { local file=$1 [[ -f $file ]] || return 0 msg2 "File: ${file#"$2"/}" if command -v "${MIAUR_BAT}" >/dev/null 2>&1; then "${MIAUR_BAT}" --style=plain --paging=always --language=bash "$file" || true elif command -v less >/dev/null 2>&1; then less -R "$file" || true else cat "$file" fi } # List reviewable files in a package directory (relative paths) _review_list_files() { local dir=$1 ( cd "$dir" || exit 1 # PKGBUILD first, then install scripts and other shell helpers [[ -f PKGBUILD ]] && printf '%s\n' PKGBUILD find . -maxdepth 1 -type f \( \ -name '*.install' -o \ -name '*.sh' -o \ -name 'apply-patches' -o \ -name '*.hook' \ \) ! -name 'PKGBUILD' -printf '%P\n' 2>/dev/null | sort ) } # Review PKGBUILD in dir. Returns 0 to continue, 1 to abort, 2 to skip. review_pkgbuild() { local dir=$1 local name=${2:-$(basename "$dir")} [[ ${MIAUR_SKIPREVIEW:-0} -eq 1 ]] && return 0 local pb="$dir/PKGBUILD" [[ -f $pb ]] || die "PKGBUILD not found: $dir" msg "Reviewing $name" local -a files=() mapfile -t files < <(_review_list_files "$dir") if [[ ${#files[@]} -gt 1 ]]; then msg2 "Files: ${files[*]}" fi _review_show_file "$pb" "$dir" while true; do printf '%s::%s View diff (d), files (f), edit (e), continue (y), skip (s), or quit (q)? [Y/d/f/e/s/q] ' \ "${C_BOLD}${C_BLUE}" "${C_RESET}" local ans= read -r ans || return 1 ans=${ans:-y} case "${ans,,}" in y | yes | '') return 0 ;; q | quit | n | no) warn "Cancelled by user" return 1 ;; s | skip) return 2 ;; e | edit) "${MIAUR_EDITOR}" "$pb" ;; f | files) local -a again=() mapfile -t again < <(_review_list_files "$dir") local i for i in "${!again[@]}"; do printf ' %s%2d%s %s\n' "${C_DIM}" "$((i + 1))" "${C_RESET}" "${again[$i]}" done printf '%s::%s Open file number (empty to return): ' \ "${C_BOLD}${C_BLUE}" "${C_RESET}" local pick= read -r pick || true if [[ $pick =~ ^[0-9]+$ ]]; then local idx=$((pick - 1)) if ((idx >= 0 && idx < ${#again[@]})); then _review_show_file "$dir/${again[$idx]}" "$dir" else warn "Invalid number: $pick" fi fi ;; d | diff) if [[ -d $dir/.git ]]; then ( cd "$dir" || exit 0 # Prefer unstaged/staged local edits; fall back to last fetch range if ! ${MIAUR_GIT} diff --stat --quiet HEAD 2>/dev/null; then ${MIAUR_GIT} --no-pager diff -- HEAD elif ${MIAUR_GIT} rev-parse --verify 'HEAD@{1}' >/dev/null 2>&1; then ${MIAUR_GIT} --no-pager diff -- 'HEAD@{1}'..HEAD else warn "No prior revision available for comparison" ${MIAUR_GIT} --no-pager diff -- PKGBUILD || true fi ) || true else warn "No Git history is available for comparison" fi ;; *) warn "Invalid input" ;; esac done }