-
Notifications
You must be signed in to change notification settings - Fork 5
/
ignore.sh
executable file
·120 lines (102 loc) · 2.79 KB
/
ignore.sh
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
114
115
116
117
118
119
120
#!/bin/bash
## /*
# @usage ignore [file-string]
#
# @description
# Allows for adding a file to ignore list if untracked, otherwise adds to
# assume unchanged list
# description@
#
# @examples
# 1) ignore untrackedFile.js
# # adds untrackedFile.js to .gitignore
# 2) ignore trackedFile.js
# # Runs `git update-index --assume-unchanged trackedFile.js
# examples@
#
# @dependencies
# functions/0300.menu.sh
# dependencies@
#
# @file ignore.sh
## */
$loadfuncs
echo ${X}
numArgs=$#
_file_selection=
_ignore_strategy=
_whitespace_only=false
# parse arguments
if (( numArgs < 2 )); then
until [ -z "$1" ]; do
if [[ ${#len} == 0 ]]; then
_file_selection=$1
fi
shift 1
done
else
__bad_usage ignore "Invalid number of parameters."
exit 1
fi
list=($(git status --porcelain | tr " " -))
#clean the list up so already added files aren't options
list=( ${list[@]/M--*/} )
list=( ${list[@]/D--*/} )
list=( ${list[@]/A--*/} )
# make sure they did not pass a partial filename, if so, strip it to bring up menu
_passed_partial=true
for e in "${list[@]}"; do
# $e at this point has the funny -M- prepended to it, ignore that stuff
if [[ "${e:3}" == "$_file_selection" ]]; then
# found the file, they did not pass a partial
_passed_partial=false
fi
done
if ( $_passed_partial ); then
# trash the selection. Maybe someday we can par the list down by partial matches instead...
_file_selection=''
fi
#no file specified, show menu
if [ -z "$_file_selection" ] && ( $_passed_partial ); then
msg="Please choose a file to stage."
__menu --prompt="$msg" ${list[@]}
# clean the flags out of the file name
shopt -s extglob #http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
_file_selection=${_menu_sel_value/@(M--|-M-|D--|-D-|\?\?-)/}
_file_status=$_menu_sel_value
else
_file_status=($(git status $_file_selection --porcelain))
fi
# determine which strategy we are using by looking for the ?? flag at the beginning
# of the menu selection (eg: ??--untracked.sh)
if [[ "${_file_status:0:2}" =~ "??" ]]; then
_ignore_strategy="gitignore"
else
_ignore_strategy="update-index"
fi
echo
if [ -n "$_file_selection" ]; then
echo
echo
echo "Ignoring file ${COL_GREEN}${_file_selection}${COL_NORM}."
echo ${O}${H2HL}
if [ $_ignore_strategy == "gitignore" ]; then
echo "File previously untracked. Adding to .gitignore"
echo "echo ${_file_selection} >> .gitignore"
echo ${_file_selection} >> .gitignore
elif [ $_ignore_strategy == "update-index" ]; then
echo "File previously tracked. Updating index to assume unchanged."
echo "git update-index --assume-unchanged ${_file_selection}"
git update-index --assume-unchanged ${_file_selection}
fi
echo ${O}${H2HL}${X}
echo
echo
# Show status for informational purposes
echo "$ git status"
git status
echo ${O}${H2HL}${X}
else
echo " aborting"
fi
exit