-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-diff-add
More file actions
executable file
·113 lines (92 loc) · 2.47 KB
/
git-diff-add
File metadata and controls
executable file
·113 lines (92 loc) · 2.47 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
## --------------------------------------------------------------------
## git-diff-add 1.0.0
##
## This is free software: you are free to change and redistribute it.
## There is NO WARRANTY, to the extent permitted by law.
## --------------------------------------------------------------------
### This script goes through all paths that are modified/not added (anything
### that shows up in `git status` that isn't staged), prints out a diff for
### each one, and then prompts you for whether you want to stage those changes.
###
### Usage:
### git-diff-add [options]
### git-diff-add <folder>
###
### Arguments:
### folder An optional argument to specify the folder to operate on
###
### Options:
### -h, --help Show help options.
### -v, --version Print program version.
help=$(grep "^### " "$0" | cut -c 5-)
version=$(grep "^## " "$0" | cut -c 4-)
eval "$(docopts -A args -h "$help" -V "$version" : "$@")"
function do_status() {
folder="$1"
if [ -z "$folder" ]; then
folder="$(git rev-parse --show-toplevel)"
fi
local IFS=$'\n'
entries=($(git status -u "$folder" --porcelain=2))
unset IFS
for ((i = 0; i < ${#entries[@]}; i++)); do
handle_line ${entries[$i]}
done
}
function handle_line() {
modification_type="$1"
shift
case $modification_type in
1) handle_changed $@
;;
2) handle_renamed $@
;;
u) handle_unmerged $@
;;
?) handle_untracked $@
;;
*) echo UNKNOWN MODIFICATION TYPE \"$modification_type\"
esac
}
function handle_changed() {
change_type="$1"
before=${change_type:0:1}
after=${change_type:1:1}
path="$8"
if [[ "$after" != "." ]]; then
normal_diff "$path"
fi
}
function handle_renamed() {
change_type="$1"
before=${change_type:0:1}
after=${change_type:1:1}
path="$9"
orig_path="${10}"
if [[ "$after" != "." ]]; then
normal_diff "$path"
fi
}
function handle_unmerged() {
path="${10}"
normal_diff "$path"
}
function handle_untracked() {
untracked_diff "$1"
}
function normal_diff() {
git diff -- "$1"
prompt_to_stage "$1"
}
function untracked_diff() {
git diff /dev/null "$1"
prompt_to_stage "$1"
}
function prompt_to_stage() {
read -r -p "Add to staging area? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
git add "$1"
fi
}
do_status "${args[<folder>]}"