-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfadd
executable file
·74 lines (59 loc) · 2.08 KB
/
fadd
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
#!/usr/bin/env zsh
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0:#/*}:-$PWD/$0}"
# Desc: stage the selected file to git bare repo
#
# @params
# Globals:
# ${mydir}: string, current directory of the executing script
# ${stage_type}: modified, new file, or directory to stage
# ${selected_files}: bash array of user selected files to stage
# Arguments:
# -h|--help: show help message
# -f|--file: select a file in PWD to stage
# -d|--dir: select a directory in PWD to stage
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}fadd%f [%F{13}-h%f] [%F{13}-f%f] [%F{13}-d%f] ...
Select files/directories or modified files through fzf.
Stage the selected file to the dotfile gitbare repo.
%F{4}Default%f: list all modified files and stage the selected files.
%F{1}%BOptions%f%b:
%F{13}-h%f, %F{13}--help%f Show this help message and exit
%F{13}-f%f, %F{13}--file%f Select files in current directory and stage the selected files
%F{13}-d%f, %F{13}--dir%f Select folders in current directory and stage the selected folders"
}
#######################################
# stage file
# Arguments:
# $1: array of files to stage
#######################################
function stage_file() {
local -a files && files=("$@")
(( $#files == 0 )) && { print -Pr -- "%F{1}Nothing added%f"; exit 1; }
command git add "${files[@]}"
}
local stage_type="modified"
local -a selected_files
while (( $# )) {
case "$1" in
(-f|--file) stage_type="file"; shift ;;
(-d|--dir) stage_type="dir"; shift ;;
(-h|--help) usage && exit 0 ;;
(*) print::error "Invalid option: $1" && { usage; exit 1 } ;;
esac
}
if [[ $stage_type = file ]] {
selected_files=( ${(@f)"$(search_file f)"} )
} elif [[ $stage_type = dir ]] {
selected_files=( ${(@f)"$(search_file d)"} )
} else {
selected_files=( ${(@f)"$(get_modified_file 'Select files to stage' 'unstaged')"} )
}
stage_file "$selected_files[@]"
(( $#selected_files )) && command git status -sb