Skip to content

Commit 83fb8a5

Browse files
committed
(master) cleaned up the add command. Added tab completion.
1 parent a6bfdc2 commit 83fb8a5

2 files changed

Lines changed: 73 additions & 15 deletions

File tree

add.sh

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,80 @@
1+
#!/bin/bash
2+
## /*
3+
# @usage add [file-name]
4+
#
5+
# @description
6+
# This script makes staging files for commit much easier. It fully supports
7+
# tab completion in a way that the built in git add does not. Also, if you
8+
# prefer to use a menu over tab completion, it supports that too.
9+
# description@
10+
#
11+
#
12+
# @examples
13+
# 1) add
14+
# # Will show a list of files available for staging.
15+
# 2) add fi[tab]
16+
# # tab completes the rest of the file name
17+
# 3) add file-path/file.sh
18+
# # deduces if you are doing an add or rm and then does the operation.
19+
# examples@
20+
#
21+
# @dependencies
22+
# functions/0300.menu.sh
23+
# dependencies@
24+
#
25+
# @file add.sh
26+
## */
127
$loadfuncs
228

329

430
echo ${X}
531

6-
#need to clean this list up
7-
list=($(git status --porcelain | tr " " -))
8-
msg="Please choose a file to stage. Files preceeded by a '-' are not staged; otherwise they are already staged"
9-
__menu --prompt="$msg" ${list[@]}
32+
_file_selection=
33+
while (( "$#" )); do
34+
_file_selection=$1
35+
36+
shift 1
37+
done
1038

1139
gitcommand="add"
12-
#determine if we are adding or deleting
13-
if [[ "$_menu_sel_value" =~ D ]]
14-
then
15-
gitcommand="rm"
16-
fi
40+
list=($(git status --porcelain | tr " " -))
41+
#clean the list up so already added files aren't options
42+
list=( ${list[@]/M--*/} )
43+
list=( ${list[@]/D--*/} )
44+
list=( ${list[@]/A--*/} )
1745

18-
# clean the flags out of the file name
19-
shopt -s extglob #http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
20-
item=${_menu_sel_value/@(M--|-M-|D--|-D-|\?\?-)/}
46+
#no file specified, show menu
47+
if [ -z "$_file_selection" ]; then
48+
msg="Please choose a file to stage."
49+
__menu --prompt="$msg" ${list[@]}
50+
51+
#determine if we are adding or deleting
52+
if [[ "$_menu_sel_value" =~ D ]]
53+
then
54+
gitcommand="rm"
55+
fi
56+
57+
# clean the flags out of the file name
58+
shopt -s extglob #http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
59+
_file_selection=${_menu_sel_value/@(M--|-M-|D--|-D-|\?\?-)/}
60+
else
61+
#they passed in the branch, now determine if we need to do an add or a delete
62+
for e in "${list[@]}"; do
63+
if [[ "$e" =~ "$_file_selection" ]]; then
64+
if [[ "$e" =~ D ]]; then
65+
gitcommand="rm"
66+
fi
67+
fi
68+
done
69+
70+
fi
2171

2272
echo
2373
echo
24-
echo "Staging file ${COL_GREEN}${item}${COL_NORM} for commit."
74+
echo "Staging file ${COL_GREEN}${_file_selection}${COL_NORM} for commit."
2575
echo ${O}${H2HL}
26-
echo "$ git ${gitcommand} ${item}"
27-
git ${gitcommand} ${item}
76+
echo "$ git ${gitcommand} ${_file_selection}"
77+
git ${gitcommand} ${_file_selection}
2878
echo ${O}${H2HL}${X}
2979
echo
3080
echo

cfg/completion.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ _tab_branches() {
1515
return 0
1616
}
1717

18+
_tab_files() {
19+
COMPREPLY=()
20+
curw=${COMP_WORDS[COMP_CWORD]}
21+
COMPREPLY=($(compgen -W '$(git status --porcelain | tr -d " M " | tr -d "M " | tr -d " D " | tr -d "D " | tr -d " A " | tr -d "A ")' -- $curw))
22+
return 0
23+
}
24+
1825
# tab completion for commands that specify branches
1926
complete -F _tab_branches checkout
2027
complete -F _tab_branches contains
@@ -25,3 +32,4 @@ complete -F _tab_branches new
2532
complete -F _tab_branches pull
2633
complete -F _tab_branches push
2734
complete -F _tab_branches trackbranch
35+
complete -F _tab_files add

0 commit comments

Comments
 (0)