Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 62 lines (44 sloc) 1.09 KB
#!/usr/bin/env bash
###
### git-add: Add convenience options to git-add(1).
###
ERR_NOTREPO="Not a Git repository."
ARGV=()
FILES=()
REGEX='^%[0-9]+$'
STAGEABLE=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
exec $GIT add --help
exit 0
;;
%*)
if [[ ! "$($GIT rev-parse --is-inside-work-tree)" ]] 2>/dev/null; then
printf '%s\n' "$ERR_NOTREPO"
exit 1
fi
# Unstaged, untracked files.
STAGEABLE+=($($GIT status --porcelain=v1 | grep -E '^(\w|\s)(A|C|D|M|R|U)\s+' | awk '{$1=$1};1' | cut -d' ' -f2))
STAGEABLE+=($($GIT status --porcelain=v1 | grep '??' | cut -d' ' -f2))
INDEX="$1"
if [[ ! "$INDEX" =~ $REGEX ]]; then
printf '%s: %s is not a valid number.\n' "$0" "$INDEX"
exit 1
fi
INDEX="$(( $(echo $INDEX | tr -d '%') - 1 ))"
if [[ $INDEX -lt 0 ]] || \
[[ $(expr $INDEX + 1) -gt ${#STAGEABLE[@]} ]]; then
printf '%s: %s is not a valid index.\n' "$0" "$(expr $INDEX + 1)"
exit 1
fi
FILES+=("${STAGEABLE[$INDEX]}")
shift
;;
*)
ARGV+=("$1")
shift
;;
esac
done
exec $GIT add "${ARGV[@]}" "${FILES[@]}"