bin: improve some script handlings

This commit is contained in:
xeruf 2022-05-26 08:53:35 +02:00
parent 4b1a2f91db
commit 42019a53ab
3 changed files with 13 additions and 10 deletions

View File

@ -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; }

View File

@ -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
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
done
done' \;

View File

@ -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