|
| 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 | +## */ |
1 | 27 | $loadfuncs |
2 | 28 |
|
3 | 29 |
|
4 | 30 | echo ${X} |
5 | 31 |
|
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 |
10 | 38 |
|
11 | 39 | 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--*/} ) |
17 | 45 |
|
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 |
21 | 71 |
|
22 | 72 | echo |
23 | 73 | echo |
24 | | -echo "Staging file ${COL_GREEN}${item}${COL_NORM} for commit." |
| 74 | +echo "Staging file ${COL_GREEN}${_file_selection}${COL_NORM} for commit." |
25 | 75 | echo ${O}${H2HL} |
26 | | -echo "$ git ${gitcommand} ${item}" |
27 | | -git ${gitcommand} ${item} |
| 76 | +echo "$ git ${gitcommand} ${_file_selection}" |
| 77 | +git ${gitcommand} ${_file_selection} |
28 | 78 | echo ${O}${H2HL}${X} |
29 | 79 | echo |
30 | 80 | echo |
|
0 commit comments