-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrug-rm
executable file
·204 lines (186 loc) · 8.93 KB
/
shrug-rm
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
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/dash
root_folder='.shrug'
if [ ! -d $root_folder ]; then
echo 'shrug-add: error: no .shrug directory containing shrug repository exists'
exit 1
else
if [ ! -f ".shrug/.git/COMMIT_EDITMSG" ]; then
# file COMMIT_EDITMSG creates when first commit
# if not exist
echo 'shrug-rm: error: your repository does not have any commits yet'
exit 1
else
# rm
if [ "$1" != '--force' ] && [ "$1" != '--cached' ]; then
requested_rm_filename=$1
if [ -z "$requested_rm_filename" ]; then
echo 'usage: shrug-rm [--force] [--cached] <filenames>'
exit 1
fi
for working_file in $@
do
# to find current staged file name(sha1sum-ed)
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1`
last_commit_seq=`cat .shrug/.git/COMMIT_EDITMSG | head -1 | cut -d' ' -f1`
# the corresponding file as given argv in last commit
repo_file=`cat .shrug/.git/objects/pack/pack-$last_commit_seq.idx | egrep " $working_file$" | cut -d' ' -f1`
if [ -z "$staged_file" ]; then
flag=1
echo "shrug-rm: error: '$working_file' is not in the shrug repository"
exit 1
else
# if last commit file differs from staged file
if ! diff -q ".shrug/.git/objects/pack/pack-$last_commit_seq.pack/$repo_file" ".shrug/.git/objects/$staged_file" > /dev/null 2>&1; then
# if current working file differs from staged file
if ! diff -q "$working_file" ".shrug/.git/objects/$staged_file" > /dev/null; then
# set flag 1 as the error occurs
flag=1
echo "shrug-rm: error: '$working_file' in index is different to both working file and repository"
exit 1
else
flag=1
echo "shrug-rm: error: '$working_file' has changes staged in the index"
exit 1
fi
else
# if last commit file differs from current working file
if ! diff -q ".shrug/.git/objects/pack/pack-$last_commit_seq.pack/$repo_file" "$working_file" > /dev/null; then
flag=1
echo "shrug-rm: error: '$working_file' in repository is different to working file"
exit 1
fi
fi
fi
done
# if no error occured above then do the rm operation
if [ "$flag" != '1' ]; then
for working_file in $@
do
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1`
# remove corresponding file and file index(sha1sum-ed filename - original filename)
sed -i -e "/$staged_file $working_file/d" ".shrug/.git/index" > /dev/null
rm ".shrug/.git/objects/$staged_file"
rm $working_file
done
fi
exit 0
fi
# rm --force
if [ "$1" = '--force' ] && [ "$2" != '--cached' ]; then
requested_rm_filename=$2
if [ -z "$requested_rm_filename" ]; then
echo 'usage: shrug-rm [--force] [--cached] <filenames>'
exit 1
fi
# for the below parts please read from line 25
for working_file in $@
do
p=`echo $working_file | egrep "^-"`
if [ "$p" = '' ]; then
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1`
if [ -z "$staged_file" ]; then
flag=1
echo "shrug-rm: error: '$working_file' is not in the shrug repository"
exit 1
fi
fi
done
if [ "$flag" != '1' ]; then
for working_file in $@
do
p=`echo $working_file | egrep "^-"`
if [ "$p" = '' ]; then
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1` > /dev/null 2>&1
sed -i -e "/$staged_file $working_file/d" ".shrug/.git/index" > /dev/null 2>&1
rm ".shrug/.git/objects/$staged_file" > /dev/null 2>&1
rm $working_file > /dev/null 2>&1
fi
done
fi
# for the above parts please read from line 25
exit 0
fi
# rm --cached
if [ "$1" != '--force' ] && [ "$1" = '--cached' ]; then
requested_rm_filename=$2
if [ -z "$requested_rm_filename" ]; then
echo'usage: shrug-rm [--force] [--cached] <filenames>'
exit 1
fi
for working_file in $@
do
# flag p to skip option argv then can process file
p=`echo $working_file | egrep "^-"`
if [ "$p" = '' ]; then
# to find current staged file name(sha1sum-ed)
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1`
last_commit_seq=`cat .shrug/.git/COMMIT_EDITMSG | head -1 | cut -d' ' -f1`
# the corresponding file as given argv in last commit
repo_file=`cat .shrug/.git/objects/pack/pack-$last_commit_seq.idx | egrep " $working_file$" | cut -d' ' -f1`
if [ -z "$staged_file" ]; then
flag=1
echo "shrug-rm: error: '$working_file' is not in the shrug repository"
exit 1
else
# if staged file differs from both last commit repo and working file then throw error
if ! diff -q ".shrug/.git/objects/pack/pack-$last_commit_seq.pack/$repo_file" ".shrug/.git/objects/$staged_file" > /dev/null 2>&1; then
if ! diff -q "$working_file" ".shrug/.git/objects/$staged_file" > /dev/null; then
flag=1
echo "shrug-rm: error: '$working_file' in index is different to both working file and repository"
exit 1
fi
fi
fi
fi
done
# if no error occured above then do the rm cached operation
if [ "$flag" != '1' ]; then
for working_file in $@
do
p=`echo $working_file | egrep "^-"`
if [ "$p" = '' ]; then
# remove corresponding file and file index(sha1sum-ed filename - original filename)
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1`
sed -i -e "/$staged_file $working_file/d" ".shrug/.git/index" > /dev/null
rm ".shrug/.git/objects/$staged_file"
fi
done
fi
exit 0
fi
# rm --force --cached
if [ "$1" = '--force' ] && [ "$2" = '--cached' ]; then
requested_rm_filename=$3
if [ -z "$requested_rm_filename" ]; then
echo'usage: shrug-rm [--force] [--cached] <filenames>'
exit 1
fi
# for the below parts please read from line 124
for working_file in $@
do
p=`echo $working_file | egrep "^-"`
if [ "$p" = '' ]; then
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1`
if [ -z "$staged_file" ]; then
flag=1
echo "shrug-rm: error: '$working_file' is not in the shrug repository"
exit 1
fi
fi
done
if [ "$flag" != '1' ]; then
for working_file in $@
do
p=`echo $working_file | egrep "^-"`
if [ "$p" = '' ]; then
staged_file=`cat .shrug/.git/index | egrep " $working_file$" | cut -d' ' -f1` > /dev/null 2>&1
sed -i -e "/$staged_file $working_file/d" ".shrug/.git/index" > /dev/null 2>&1
rm ".shrug/.git/objects/$staged_file" > /dev/null 2>&1
fi
done
fi
# for the above parts please read from line 124
exit 0
fi
fi
fi