2022-06-27 21:01:11 +00:00
|
|
|
#!/bin/bash
|
2022-02-11 10:45:12 +00:00
|
|
|
# Create a commit or stage files via fzf selection
|
2022-04-12 16:59:02 +00:00
|
|
|
# If the first arg is "add", files are staged rather than committed.
|
2022-05-24 17:44:36 +00:00
|
|
|
# All remaining args are passed to the last git command (add or commit).
|
2022-04-12 16:59:02 +00:00
|
|
|
# TODO fix broken prep-commit-msg hook when there is no unifying path
|
2022-05-24 17:44:36 +00:00
|
|
|
# TODO fix broken alt-enter not opening editor --bind='alt-enter:change-prompt(hi>)'
|
2022-02-11 10:45:12 +00:00
|
|
|
|
2022-06-27 21:01:11 +00:00
|
|
|
# using bash because of pipefail
|
2022-06-27 09:33:39 +00:00
|
|
|
set -eo pipefail
|
2022-05-26 06:53:35 +00:00
|
|
|
|
2022-02-11 10:45:12 +00:00
|
|
|
fzfpipe() {
|
|
|
|
# Take nul-separated input from git-status short/porcelain
|
|
|
|
# and return a newline-separated list of selected files
|
|
|
|
cut -z -c2- |
|
2022-04-12 17:30:12 +00:00
|
|
|
git fzf-diff --read0 -d' ' --nth=2.. --bind="alt-enter:execute($EDITOR '$(git rev-parse --show-toplevel)/{2..}')" \
|
2022-06-27 09:33:39 +00:00
|
|
|
--preview="test {1} != \? && git diff --color HEAD --unified=4 -- {2..} | $(git config interactive.diffFilter | grep . || echo $PAGER) || find {2..} -type f | xargs -I% diff --recursive --color=always -u /dev/null %" |
|
2022-02-11 10:45:12 +00:00
|
|
|
cut -c3-
|
|
|
|
}
|
|
|
|
|
2022-07-20 16:17:57 +00:00
|
|
|
test $(git ls-tree HEAD "$PWD" 2>/dev/null | wc -l) -gt 1 && wd=$PWD
|
2022-02-11 10:45:12 +00:00
|
|
|
cd "$(git rev-parse --show-toplevel)"
|
2022-03-06 21:04:22 +00:00
|
|
|
prefix="/tmp/git/fuzz"
|
|
|
|
mkdir -p "$prefix"
|
2022-02-11 10:45:12 +00:00
|
|
|
case "$1" in
|
|
|
|
(add) shift
|
2022-03-06 21:04:22 +00:00
|
|
|
git status -z --porcelain --no-renames --untracked-files=all $wd |
|
2022-02-11 10:45:12 +00:00
|
|
|
grep -zv '^\\w ' | fzfpipe |
|
2022-05-26 06:53:35 +00:00
|
|
|
xargs -rd '\n' -L 1 git -c advice.addEmptyPathspec=false add --verbose "$@";;
|
2022-03-06 21:04:22 +00:00
|
|
|
(*) git status -z --porcelain --no-renames $wd |
|
|
|
|
sed -z 's/^\\(\\w\\) / \\1/' | fzfpipe >"$prefix/files"
|
2022-05-26 06:53:35 +00:00
|
|
|
cat "$prefix/files" | xargs -rd '\n' ls -fd 2>/dev/null >"$prefix/files-existing" || true
|
2022-05-24 17:44:36 +00:00
|
|
|
git -c advice.addEmptyPathspec=false add --intent-to-add --pathspec-from-file="$prefix/files-existing"
|
2022-03-06 21:04:22 +00:00
|
|
|
git commit -v --only --pathspec-from-file="$prefix/files" "$@";;
|
2022-02-11 10:45:12 +00:00
|
|
|
esac
|