-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_test
executable file
·192 lines (152 loc) · 5.4 KB
/
read_test
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
vrs='v.2'
lastchange='2/28/14'
echo -e "\n--> git-redact $vrs $lastchange <--\n" > /dev/tty
# capture stdin
commit_content=$(more)
# OS-specific word boundary syntax
if [ $(uname) == 'Darwin' ]; then
wordboundary_left="[[:<:]]"
wordboundary_right="[[:>:]]"
fi
if [ $1 == 'init' ]; then
echo -e 'papermill'
exit 0
fi
if [ $1 == 'uninstall' ]; then
echo -e 'there was free beer'
exit 0
fi
#
### SET UP ###
#
# create gitredact and gitattributes if they doesn't already exist.
if [ ! -f .gitattributes ]; then
echo -e "* filter=redact" > .gitattributes
echo -e " I created a $(tput setaf 4).gitattributes$(tput sgr0) file and added the redact filter to it.\n" > /dev/tty
else
grep -q "filter=redact" .gitattributes
if [ $? -ne 0 ]; then
echo -e "* filter=redact" > .gitattributes
echo -e " I added the redact filter to your $(tput setaf 4).gitattributes$(tput sgr0) file.\n" > /dev/tty
fi
fi
if [ -f .git/config ]; then
grep -q '\[filter "redact"\]' .git/config
if [ $? -ne 0 ]; then
echo -e '[filter "redact"]\n clean = ./soapy %f\n smudge = cat' >> .git/config
echo -e " I added filter settings to your $(tput setaf 4).git/config$(tput sgr0) file.\n" > /dev/tty
fi
if [ -f .gitignore ]; then
grep -q ".gitredact" .gitignore
if [ $? -ne 0 ]; then
echo -e ".gitredact" >> .gitignore
echo -e " I added $(tput setaf 4)\".gitredact\"$(tput sgr0) to your $(tput setaf 4).gitignore$(tput sgr0) file.\n" > /dev/tty
fi
grep -q ".gitattributes" .gitignore
if [ $? -ne 0 ]; then
echo -e ".gitattributes" >> .gitignore
echo -e " I added $(tput setaf 4)\".gitattributes\"$(tput sgr0) to your $(tput setaf 4).gitignore$(tput sgr0) file.\n" > /dev/tty
fi
else
echo -e ".gitredact\n.gitattributes" >> .gitignore
echo -e " I created a $(tput setaf 4).gitignore$(tput sgr0) file and added $(tput setaf 4)\".gitredact\"$(tput sgr0) and $(tput setaf 4)\".gitattributes\"$(tput sgr0) to it.\n" > /dev/tty
fi
if [ ! -f .gitredact ]; then
echo -e "# These are patterns for sed matching, not strings.\n# pattern_to_be_replaced = pattern_to_replace_it\n# pattern_to_be_removed" > .gitredact
echo -e " I created a $(tput setaf 4).gitredact$(tput sgr0) file. It's hidden, so use 'ls -a' to see it in directory listings. Before I can do any redactions, you'll need to add patterns to it.\n\n Your next 'git add' will stage your unredacted code.\n" > /dev/tty
exit 1
fi
#
### CHECK ###
#
# before redacting, make sure old redactions have been cleared and reverted
if [ -f redact.sed ]; then
rm redact.sed
fi
redact_sed_written=false
#
### COLLECT ###
#
# store the values to be swapped in an array
OLD_IFS=$IFS
IFS=$'\n'
swap_array=($(sed -n '/^#/ ! {
/ \= / ! {
s/^\(.*\)$/\1\
remove/p
}
s/\(.*\) \= \(.*\)$/\1\
\2/p
}
' < .gitredact))
IFS=$OLD_IFS
len=${#swap_array[*]}
# by default, there are zero matches
no_matches=true
# write the redaction rules to a file
# store our redact/replace values and their line numbers in an array
for ((i=0; i<$len; i=(i+2))); do
# replace any word boundaries with the proper versions for the OS
swap_array[i]=$(echo "${swap_array[i]}" | sed -E "
s/^\\\b/$wordboundary_left/
s/\\\b$/$wordboundary_right/
")
# store the line number of the match
results[${#results[*]}]=$(echo "$commit_content" | sed -n "/${swap_array[i]}/ =")
if $no_matches && [ -n "${results[(${#results[*]}-1)]}" ]; then
no_matches=false
fi
# store the actual match, not just the user\'s pattern
the_match=$(echo "$commit_content" | sed -n "s/.*\(${swap_array[i]}\).*/\1/p")
# the matched value only needs to be printed once in the results
condensed_match=$(echo $the_match | sed -n "s/.*\(${swap_array[i]}\).*/\1/p")
# if the pattern originally included word boundaries, restore them in the result
if [[ "${swap_array[i]}" =~ "$wordboundary_left" ||
"${swap_array[i]}" =~ "$wordboundary_right" ]]; then
condensed_match=$(echo $condensed_match | sed "
s/$wordboundary_left/\\\b/g
s/$wordboundary_right/\\\b/g
")
fi
results[${#results[*]}]=$condensed_match
# write redaction instructions to a file
if [ "${swap_array[(i+1)]}" == 'remove' ]; then
if ! $redact_sed_written; then
echo -e "/${swap_array[i]}/ d" >> redact.sed
fi
results[${#results[*]}]='remove'
else
if ! $redact_sed_written; then
echo -e "s/${swap_array[i]}/${swap_array[(i+1)]}/g" >> redact.sed
fi
results[${#results[*]}]=${swap_array[(i+1)]}
fi
done
redact_sed_written=true
r_len=${#results[*]}
#
### CONFIRM ###
#
# print the results to standard output and prompt for approval
if ! $no_matches; then
echo -e "\n$(tput smul)I'm preparing the following changes in $(tput bold)$1$(tput sgr0):$(tput rmul)\n" >> redact_conf.txt
for ((i=0; i<$r_len; i=(i+3))); do
# if redactions were made on multiple lines, separate line numbers with commas
lines=$(echo ${results[i]} | sed -E '
s/[0-9]+/line &/
s/([0-9]+) /\1, /g
s/line ([0-9]+,)/lines \1/
s/([0-9]+), ([0-9]+)$/\1 and \2/
')
if [ "${results[i]}" != '' ]; then
if [ "${results[(i+2)]}" == 'remove' ]; then
echo " I removed $lines, which contained \"${results[(i+1)]}\"." >> redact_conf.txt
else
echo " On $lines, I replaced \"${results[(i+1)]}\" with \"${results[(i+2)]}\"." >> redact_conf.txt
fi
fi
done
fi
echo "$commit_content" | sed 's/h/ /g' > /dev/tty
echo "$1" > /dev/tty