#!/usr/bin/env dash NAME="git-edit-index" warn() { echo "WARNING: $1" } help() { cat <"$GIT_DIR/.git/EDIT_INDEX_PATCH" git add "$1" git restore --staged "$1" cat "$1" >"$GIT_DIR/.git/EDIT_INDEX_FILE" git restore "$1" git apply "$GIT_DIR/.git/EDIT_INDEX_PATCH" "$EDITOR" "$1" git add "$1" mv "$GIT_DIR/.git/EDIT_INDEX_FILE" "$1" } edit() { files_to_add="$(mktemp)" cleanup() { rm "$files_to_add" } trap cleanup EXIT realpath --relative-to=. "$@" >"$files_to_add" git diff --name-only --cached --diff-filter=AM | while read -r index_file; do if grep -q "$index_file" "$files_to_add"; then sed -i "s|$index_file||" "$files_to_add" materialize_file "$index_file" fi done unedided_files="$(sed '/^\s*$/d' "$files_to_add" | wc -l)" if [ "$unedided_files" -gt 0 ]; then warn "Failed to edit $unedided_files file(s):" cat "$files_to_add" fi } for arg in "$@"; do case "$arg" in "--help" | "-h") help exit 0 ;; "--") end_of_cli_options=true ;; esac [ "$end_of_cli_options" = "true" ] && break done edit "$@" # vim: ft=sh