From 42019a53ab8316ee147d6ba78d1c0adc1ec6ddd0 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 26 May 2022 08:53:35 +0200 Subject: [PATCH] bin: improve some script handlings --- .config/shell/functions | 3 ++- .local/bin/scripts/duploc | 14 +++++++------- .local/bin/scripts/git-fuzz | 6 ++++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.config/shell/functions b/.config/shell/functions index 1b11f12..063c7fe 100644 --- a/.config/shell/functions +++ b/.config/shell/functions @@ -275,7 +275,8 @@ alias grpc='grep --color=auto --line-number --binary-files=without-match --direc # Default grep with some niceties and ignoring case alias grp='grpc --ignore-case' # Grep recursively and paginate -grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x \([^ ]*/\)\?|--exclude-dir |g') "$@" | less -FX; } +# TODO remove some prefixes \([^ ]*/\)\? +grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | less -FX; } # Grep in shell config files grsh() { grpr --no-ignore-case "$@" $HOME/.{ba,z}sh* $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH; } diff --git a/.local/bin/scripts/duploc b/.local/bin/scripts/duploc index 6bb4167..f534af5 100755 --- a/.local/bin/scripts/duploc +++ b/.local/bin/scripts/duploc @@ -1,10 +1,10 @@ -#!/bin/sh +#!/bin/sh -e # Delete files under current or given path which exist elsewhere as listed in the locate database # Matches first by name, then by checksum (currently inefficiently via md5) -find "$@" -size +50M -type f | while read filepath - do target="$(basename "$filepath" | synct-unarchive)" +find "$@" -size +5M -type f -exec sh -c "IFS=$'\n'"' + filepath="{}" + target="$(basename "$filepath" | synct-unarchive)" highlight "$filepath" - locate -b "$target" | while read existing - do test "$(realpath "$target")" != "$(realpath "$existing")" -a -f "$existing" -a "$(md5sum "$existing" | cut -d\ -f1)" = "$(md5sum "$filepath" | cut -d\ -f1)" && echo "Found duplicate at $existing" && rm -vi "$filepath" && break - done - done + for existing in $(locate -b "$target") + do test "$(realpath "$target")" != "$(realpath "$existing")" -a -f "$existing" -a "$(md5sum "$existing" | cut -d\ -f1)" = "$(md5sum "$filepath" | cut -d\ -f1)" && echo "Found duplicate at $existing" && rm -vi "$filepath" && break + done' \; diff --git a/.local/bin/scripts/git-fuzz b/.local/bin/scripts/git-fuzz index 654aa74..2d49a0b 100755 --- a/.local/bin/scripts/git-fuzz +++ b/.local/bin/scripts/git-fuzz @@ -5,6 +5,8 @@ # TODO fix broken prep-commit-msg hook when there is no unifying path # TODO fix broken alt-enter not opening editor --bind='alt-enter:change-prompt(hi>)' +set -o pipefail + fzfpipe() { # Take nul-separated input from git-status short/porcelain # and return a newline-separated list of selected files @@ -22,10 +24,10 @@ case "$1" in (add) shift git status -z --porcelain --no-renames --untracked-files=all $wd | grep -zv '^\\w ' | fzfpipe | - xargs -d '\n' -L 1 git -c advice.addEmptyPathspec=false add --verbose "$@";; + xargs -rd '\n' -L 1 git -c advice.addEmptyPathspec=false add --verbose "$@";; (*) git status -z --porcelain --no-renames $wd | sed -z 's/^\\(\\w\\) / \\1/' | fzfpipe >"$prefix/files" - cat "$prefix/files" | xargs -d '\n' ls -fd 2>/dev/null >"$prefix/files-existing" || true + cat "$prefix/files" | xargs -rd '\n' ls -fd 2>/dev/null >"$prefix/files-existing" || true git -c advice.addEmptyPathspec=false add --intent-to-add --pathspec-from-file="$prefix/files-existing" git commit -v --only --pathspec-from-file="$prefix/files" "$@";; esac