-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitifier.sh
333 lines (270 loc) · 6.66 KB
/
gitifier.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
PATHS=()
MODE='DIR'
REPODIR=
REPONAME=
REPO_INPROGRESS=
FILENAME=
IGNOREFILE=
MERGEDIRS=0
FLATTENDIRS=0
DEBUGPRINT=0
debugPrint()
{
echo
echo "PATHS: ${PATHS[*]}"
echo "MODE: $MODE"
echo "REPODIR: $REPODIR"
echo "REPONAME: $REPONAME"
echo "REPO_INPROGRESS: $REPO_INPROGRESS"
echo "FILENAME: $FILENAME"
echo "IGNOREFILE: $IGNOREFILE"
echo "MERGEDIRS: $MERGEDIRS"
echo "FLATTENDIRS: $FLATTENDIRS"
}
usage()
{
usage="NoVC Gitifier
Usage: $0 [-f|-d|-z] reponame
$0 -f reponame
$0 -d [-i gitignore] [-m] reponame
$0 -z [-i gitignore] [-l] reponame
-f: files mode
-d: directories mode
-z: archives mode
-i: .gitignore file to use
-n: filename to use
-m: merge directories
-l: flatten directories
"
echo "$usage"
echo "$1"
# Show debug info
if (( "$DEBUGPRINT" )); then
debugPrint
fi
exit
}
parseArgs()
{
looseOpts=()
while [[ $# -gt 0 ]]; do
opt="$1"
case $opt in
-f|--file)
MODE='FILE'
shift # past argument
;;
-d|--dir)
MODE='DIR'
shift # past argument
;;
-z|--archive)
MODE='ZIP'
shift # past argument
;;
-i|--ignorefile|--gitignore)
IGNOREFILE="$2"
shift # past argument
shift # past value
;;
-n|--filename)
FILENAME="$2"
shift # past argument
shift # past value
;;
-m|--mergedirs)
MERGEDIRS=1
shift # past argument
;;
-l|--flattendirs)
FLATTENDIRS=1
shift # past argument
;;
-e|--debug)
DEBUGPRINT=1
shift # past argument
;;
*) # unknown option
looseOpts+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
# Grab REPONAME from end of supplied arguments
if [[ "${#looseOpts[@]}" -gt 0 ]]; then
REPONAME="${looseOpts[-1]}"
else
usage "ERR: No repo name"
fi
# Get the current directory (in Windows format, if possible)
REPODIR="$( pwd -W || pwd )"
# Set the FILENAME to the REPONAME if no FILENAME was specified
if [[ -z "$FILENAME" ]]; then
FILENAME="$REPONAME"
fi
# Set the REPO_INPROGRESS name
REPO_INPROGRESS="$REPONAME"_inprogress
}
readInput()
{
# Only print a prompt if nothing was piped in
if [ ! -p /dev/stdin ]; then
echo "Enter file paths:"
fi
# Stick it in an array
while read -r line; do
if [ -z "$line" ] || [[ "$line" =~ ^[[:space:]]*$ ]]; then
break
fi
PATHS+=("$line")
done
if [[ ${#PATHS[@]} == 0 ]]; then
usage "ERR: No paths provided"
fi
}
initRepo()
{
# Make REPONAME directory
mkdir "$REPONAME" || usage "ERR: $REPONAME exists"
# Add any ignore file
if [[ -f "$IGNOREFILE" ]]; then
cp "$IGNOREFILE" "$REPONAME"/.gitignore
fi
# Initialise the git repo
pushd "$REPONAME"
git init
popd
}
concludeRepo()
{
# Restore files from git history
pushd "$REPONAME"
git reset --hard
popd
}
makeGitPointer()
{
# Link this directory back to the main repo
# We're doing it this hacky way,
# because re-init'ing against the repo messes it up.
dotgit="gitdir: $REPODIR/$REPONAME/.git"
echo "$dotgit" > .git
# Add gitignore file
if [[ -f "$REPODIR/$REPONAME/.gitignore" ]]; then
cp "$REPODIR/$REPONAME/.gitignore" .gitignore
fi
}
makeCommit()
{
# Commit all changed files
git add --all
git commit -m "$1"
}
flattenDir()
{
# If there's only one directory in the folder,
# move everything out of it, and recurse
readarray fl < <(find . -mindepth 1 -maxdepth 1)
if (( ${#fl[@]} == 1 )) && [[ -d "${fl[0]}" ]]; then
mv "${fl[0]}"/* .
rmdir "${fl[0]}"
flattenDir
fi
}
addDir()
{
dir="$1"
# Make copy of dir, under REPONAME_inprogress
cp -r "$dir/" "$REPO_INPROGRESS"
pushd "$REPO_INPROGRESS"
# Link in git files
makeGitPointer
# Make commit
makeCommit "$dir"
popd
# Blow away folder
rm -rf "$REPO_INPROGRESS"
}
addDirMerge()
{
dir="$1"
# Make a copy of the repo, to overwrite into
mkdir "$REPO_INPROGRESS"
pushd "$REPO_INPROGRESS"
# Link in git files
makeGitPointer
# Restore files from git history
git reset --hard
popd
# Make copy of dir, overwriting REPONAME_inprogress
cp -fr "$dir/"* "$REPO_INPROGRESS"
pushd "$REPO_INPROGRESS"
# Make commit
makeCommit "$dir"
popd
# Blow away folder
rm -rf "$REPO_INPROGRESS"
}
addFile()
{
file="$1"
# Make copy of file, as FILENAME, in REPONAME directory
cp -r "$file" "$REPONAME/$FILENAME"
pushd "$REPONAME"
# Make commit
makeCommit "$file"
popd
}
addZip()
{
zip="$1"
# Unpack zip into dir, under REPONAME_inprogress
if [[ $zip == *.zip ]]; then
unzip "$zip" -d "$REPO_INPROGRESS"
else
mkdir "$REPO_INPROGRESS"
tar -xf "$zip" -C "$REPO_INPROGRESS"
fi
pushd "$REPO_INPROGRESS"
# Remove any nested directories
if (( "$FLATTENDIRS" )); then
flattenDir
fi
# Link in git files
makeGitPointer
# Make commit
makeCommit "$zip"
popd
# Blow away folder
rm -rf "$REPO_INPROGRESS"
}
# Parse arguments
parseArgs $@
# Read the input list of files/folders to gitify
readInput
# Initialise repo
initRepo
# Show debug info
if (( "$DEBUGPRINT" )); then
debugPrint
fi
# Iterate through the given PATHS
for ix in ${!PATHS[*]}; do
currentpath=${PATHS[$ix]//[$'\t\r\n']}
echo "Adding $currentpath to git repo"
if [ "$MODE" = 'DIR' ]; then
if (( "$MERGEDIRS" )); then
addDirMerge "$currentpath"
else
addDir "$currentpath"
fi
elif [ "$MODE" = 'FILE' ]; then
addFile "$currentpath"
elif [ "$MODE" = 'ZIP' ]; then
addZip "$currentpath"
fi
done
if [ "$MODE" = 'DIR' ] || [ "$MODE" = 'ZIP' ]; then
concludeRepo
fi