-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdarwin
executable file
·269 lines (216 loc) · 7.47 KB
/
darwin
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# $1: git command to run, yell if undefined
# $2-inf: arguments to git command, except as below:
# $2: if "darwin init" is run, this declares the version control system to use
# currently supports as a backend: git
# has newline at start
RECOGNIZED_VCS="git"
RECOGNIZED_FILE_EXTENSIONS="fasta
fas
fna"
INITIAL_DIR="$(pwd)"
# directory of script
WORKING_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DARWIN_FOLDER=".dwn"
DARWIN_BACKEND_FILE=".dwn-backend"
# returns ms since unix epoch
timestamp() {
date +"%s"
}
# zips files to .vcsfmt format
# $1: output directory
zip_fasta() {
dwndiff -o "$1" -z "${@:2}"
}
# unzips files from vcsfmt format to working dir from .dwn dir
# $1: output directory
# $2-@: file(s) to unzip
unzip_fasta() {
dwndiff -o "$1" -u "${@:2}"
}
ERROR_ROLLBACK="false"
# if command fails, set rollback to true
# $@: command to run, and arguments
upon_error_set_rollback() {
"$@"
if [ $? -ne 0 ]; then
ERROR_ROLLBACK="true"
echo "Failure at internal command: $@."
fi
}
display_help(){
echo "
Usage: darwin <command> [<args>]
Commands include:
init: Create a darwin repository in the current folder.
clone: Create a repository on the local machine mirroring a remote.
add: Add files [<args>] to index.
commit: Create an encapsulation of edits performed on files.
push: Upload commits to remote.
pull: Download commits from remote.
status: View currently changed files.
rm: Remove files from index.
log: View history of commits in the repository.
diff: View differences between files made between commits.
Darwin is a tool built on top of git, and as a result defaults to using
git's syntax and commands outside of the overridden functions above. Git
documentation can be viewed at http://git-scm.com.
This project's page is at http://github.com/igemsoftware/Vanderbilt_2014.
Report bugs to <[email protected]>.
"
}
# display help
if [ "$1" = "help" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ] || \
[ "$1" = "" ]; then
display_help
# the parser
elif [ "$1" = "init" ]; then
# setup
if [ -d "$DARWIN_FOLDER" ]; then
echo "A darwin repository already exists!" 1>&2
else
upon_error_set_rollback mkdir "$DARWIN_FOLDER"
upon_error_set_rollback cd "$DARWIN_FOLDER"
# backend-dependent
if [ "$2" = "git" ]; then
upon_error_set_rollback git init "${@:3}"
elif [ "$2" = "" ]; then
echo "Version control backend required in command, e.g.:
darwin init <backend>
Recognized systems include: $RECOGNIZED_VCS" 1>&2
ERROR_ROLLBACK="true"
else
echo "Version control backend $2 not recognized. Recognized systems
include: $RECOGNIZED_VCS" 1>&2
ERROR_ROLLBACK="true"
fi
# teardown
if [ "$ERROR_ROLLBACK" = "true" ]; then
cd "$INITIAL_DIR"
rm -rf "$DARWIN_FOLDER"
else
# store type of backend used
echo -n "$2" > "$DARWIN_BACKEND_FILE"
git add $DARWIN_BACKEND_FILE
fi
fi
elif [ "$1" = "clone" ]; then
if [ -d "$DARWIN_FOLDER" ]; then
echo "A darwin repository already exists!" 1>&2
else
# setup
upon_error_set_rollback cd "/tmp"
TMP_FOLDER_NAME="$DARWIN_FOLDER-$(upon_error_set_rollback timestamp)"
upon_error_set_rollback mkdir "$TMP_FOLDER_NAME"
upon_error_set_rollback cd "$TMP_FOLDER_NAME"
# backend-dependent
if [ "$2" = "git" ]; then
upon_error_set_rollback git clone "$3" "$DARWIN_FOLDER"
else
echo "Version control backend $2 not recognized. Recognized systems
include: $RECOGNIZED_VCS" 1>&2
ERROR_ROLLBACK="true"
fi
upon_error_set_rollback mv "$DARWIN_FOLDER" "$INITIAL_DIR"
upon_error_set_rollback cd ..
upon_error_set_rollback rm -rf "$TMP_FOLDER_NAME"
upon_error_set_rollback cd "$INITIAL_DIR"
dwndiff_helper unzip . $DARWIN_FOLDER/*.vcsfmt
# teardown
if [ "$ERROR_ROLLBACK" = "true" ]; then
cd "$INITIAL_DIR"
rm -rf "$DARWIN_FOLDER"
rm -rf "/tmp/$TMP_FOLDER_NAME"
else
# store type of backend used
cd $DARWIN_FOLDER
echo -n "$2" > "$DARWIN_BACKEND_FILE"
git add $DARWIN_BACKEND_FILE
fi
fi
else
# error handling
# if no folder found
if [ ! -d "$DARWIN_FOLDER" ]; then
echo "Error: no darwin repository was found." 1>&2
exit -1
fi
BACKEND_VCS_TYPE="$(cat $DARWIN_FOLDER/$DARWIN_BACKEND_FILE)"
# if no backend file found
if [ "$(echo $RECOGNIZED_VCS | grep $BACKEND_VCS_TYPE)" = "" ]; then
if [ -d ".git" ]; then
BACKEND_VCS_TYPE="git"
else
echo "Error: incorrectly initialized repository." 1>&2
echo "Try checking out again." 1>&2
exit -1
fi
echo -n "$BACKEND_VCS_TYPE" > "$DARWIN_FOLDER/$DARWIN_BACKEND_FILE"
fi
# actual commands
if [ "$1" = "add" ]; then
# setup
while read -r file_extension; do
dwndiff_helper zip "$DARWIN_FOLDER" *.$file_extension
done <<< "$RECOGNIZED_FILE_EXTENSIONS"
# backend-dependent
if [ "$BACKEND_VCS_TYPE" = "git" ]; then
cd "$DARWIN_FOLDER"
upon_error_set_rollback git add "$2.vcsfmt"
fi
elif [ "$1" = "commit" ]; then
if [ "$2" = "" ]; then
echo "Enter a message describing your changes, e.g.:"
echo " darwin commit \"slightly modified GFP sector\""
exit -1
else
# backend-dependent
# "BACKEND_VCS_TYPE" = "git"
cd "$DARWIN_FOLDER"
upon_error_set_rollback git commit -m "$2" \
| grep -v "$DARWIN_BACKEND_FILE"
fi
elif [ "$1" = "push" ]; then
# backend-dependent
# "$BACKEND_VCS_TYPE" = "git"
cd "$DARWIN_FOLDER"
upon_error_set_rollback git push "${@:2}"
elif [ "$1" = "pull" ]; then
# backend-dependent
# "$BACKEND_VCS_TYPE" = "git"
cd "$DARWIN_FOLDER"
upon_error_set_rollback git pull "${@:2}"
exist_changes="no"
git diff --name-only HEAD | while read changed_file; do
exist_changes="yes"
dwndiff_helper unzip-changes .. $changed_file
done
if [ "$exist-changes" != "" ]; then
echo "Changes were made upstream. You'll have to manually merge the
files with the extension .new-file."
fi
elif [ "$1" = "status" ]; then
# backend-dependent
# "$BACKEND_VCS_TYPE" = "git"
cd "$DARWIN_FOLDER"
upon_error_set_rollback git status "${@:2}" | sed -e "s/\.vcsfmt//g" \
| grep -v "$DARWIN_BACKEND_FILE"
elif [ "$1" = "rm" ]; then
# backend-dependent
# "$BACKEND_VCS_TYPE" = "git"
cd "$DARWIN_FOLDER"
upon_error_set_rollback git rm "$2.vcsfmt"
cd "$INITIAL_DIR"
upon_error_set_rollback rm "$2"
elif [ "cat $WORKING_DIR/all_possible_git_commands | grep $1" != "" ]; then
# if command is git command
# backend-dependent
# "$BACKEND_VCS_TYPE" = "git"
cd "$DARWIN_FOLDER"
upon_error_set_rollback git "$@"
else
echo "Command $1 not found." 1>&2
display_help
exit -1
fi
fi