-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfstat
More file actions
executable file
·86 lines (72 loc) · 2.42 KB
/
fstat
File metadata and controls
executable file
·86 lines (72 loc) · 2.42 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
#!/usr/bin/env zsh
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0:#/*}:-$PWD/$0}"
# Desc: interactive git status menu; toggle stage and unstage
#
# @params
# Globals:
# ${mydir}: current directory of where the script is running
# ${selected_files}: raw selected file (with current git status prepend)
# ${selected_filenames}: bash array of names for the selected_files
# ${stage_file}: determine if current operation should be staging file or unstage
emulate -LR zsh
setopt warncreateglobal noshortloops
local mydir; mydir=${0:h:A}
builtin source ${mydir}/fgit_helper
function usage() {
builtin print -P -- "\
%F{1}%BUsage%f%b: %F{2}fstat%f [%F{13}-h%f] ...
Display interactive git status menu.
Toggle file stage/unstage interactively.
%F{1}%BOptions%f%f:
%F{13}-h%f, %F{13}--help%f Show this help message and exit"
}
(( $# )) && {
case "$1" in
(-h|--help) usage && exit 0 ;;
(*) print::error "Invalid option: $1" && { usage; exit 1 } ;;
esac
}
local -a mod_files
while :; do
# reset all variable and arrays for each loop
local -a selected_files selected_filenames
local stage_file
selected_files=( ${(@f)"$(get_modified_file 'select files to stage/unstage' 'all' 'raw')"} )
(( $#selected_files )) || break
# check if current operation should stage file or unstage file
# if any file start with M but has char immediately follow it, new changes are made, stage file
# if any file start with a space or tab, the file is not staged, stage file
# otherwise, we unstage
stage_file=$(print -lr -- "$selected_files[@]" \
| command awk '{
if ($0 ~ /^[[:alpha:]]{2}.*$/) {
print "stage"
} else if ($0 ~ /^[ \t].*$/) {
print "stage"
}
}'
)
while IFS= read -r line; do
selected_filenames+=("${line}")
# may be a bit much to have two arrays, inside/outside loop
# workaround to have a loop continuously ask
# as well as display git status when finished
mod_files+=("${line}")
done < <(
printf '%s\n' "${selected_files[@]}" \
| awk -v home="${FZFGIT_TREE}" '{
$1=""
gsub(/^[ \t]/, "", $0)
gsub(/"/, "", $0)
print home "/" $0
}'
)
if [[ -z "$stage_file" ]]; then
command git reset --quiet HEAD "$selected_filenames[@]"
else
command git add "$selected_filenames[@]"
fi
done
(( $#mod_files[@] )) && \
command git status -sb || print -Pr -- "%F{1}Nothing to add%f"