-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-add
executable file
·62 lines (45 loc) · 1.13 KB
/
git-add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
###
### git-add: Add convenience options to git-add(1).
###
BASENAME="$(basename $0)"
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' "$BASENAME" "$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' "$BASENAME" "$(expr $INDEX + 1)"
exit 1
fi
FILES+=("${STAGEABLE[$INDEX]}")
shift
;;
*)
ARGV+=("$1")
shift
;;
esac
done
exec $GIT add "${ARGV[@]}" "${FILES[@]}"