-
Notifications
You must be signed in to change notification settings - Fork 5
/
regularAssets.js
271 lines (228 loc) · 5.59 KB
/
regularAssets.js
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
// Update the bottom tree
function updateBottomTree({
baseTrees,
commitType,
fileContent,
fileInfo
}) {
let action;
switch (commitType) {
case REQ_DELETE:
action = deleteBlob;
break;
case REQ_EDIT:
action = modifyBlob;
break;
case REQ_NEW:
action = addBlob;
break;
case REQ_UPLOAD: //TODO: Support uploading several files
action = addBlob;
break;
}
return action({
fileContent,
fileInfo,
baseTrees
});
}
// Delete a blob
function deleteBlob({
baseTrees,
fileInfo
}) {
// Check for deleted dirs
let {
dirs,
deletedPath
} = findDeletedTree(fileInfo.dirs, baseTrees);
// Delete the file from bottom tree if no dir is deleted
// Otherwise, remove the toppest deleted dir from trees
let deleted, parent;
if (!deletedPath) { // Case 1: parent dir should be deleted
deleted = fileInfo.fname;
parent = fileInfo.dname;
} else { // Case 2: no dir is deleted
deleted = removeParentPath(deletedPath);
parent = getParentPath(deletedPath);
}
// Update bottom tree
delete baseTrees[parent][deleted];
return {
baseTrees,
objects: []
}; // no GitObject is created
}
// Edit a blob
function modifyBlob({
baseTrees,
fileInfo,
fileContent
}) {
let {
dirs,
newdirs,
fname,
rename,
oldPath
} = fileInfo;
let objects = [];
let bottom = dirs[0];
// FIXME: remove the file from previous path
// TODO: Make sure new dir does not exist on the repo
// TODO: Check if file is already exists
// CASE 1: new dir is created
if (newdirs.length > 0) { // Case 1: new_dir is created
//Create new blob object
let {
entry,
binary
} = addEntry({
mode: MODE_BLOB,
type: TYPE_BLOB,
name: fname,
content: fileContent
});
objects.push([TYPE_BLOB, binary]);
//Create tree for newdirs
let {
dname,
treeEntry,
binaries
} = addSubTrees(newdirs, entry);
objects = [...objects, ...binaries];
// delete old file
let oldDir = getParentPath(oldPath);
let oldName = removeParentPath(oldPath);
delete baseTrees[oldDir][oldName];
// Update bottom tree
baseTrees[bottom][dname] = treeEntry;
// CASE 2: new dir is not created
} else {
let obj = createGitObject(TYPE_BLOB, fileContent);
objects.push([TYPE_BLOB, obj.object]);
//TODO: CASE 3: file is moved
// CASE 4: file is renamed
if (rename) {
let oldName = removeParentPath(oldPath);
//TODO: Update the following with key rename
baseTrees[bottom][fname] = baseTrees[bottom][oldName];
delete baseTrees[bottom][oldName];
baseTrees[bottom][fname].name = fname
}
// Update bottom tree
baseTrees[bottom][fname].id = obj.id;
}
return {
baseTrees,
objects
};}
// Add a new blob
function addBlob({
baseTrees,
fileInfo,
fileContent
}) {
let {
dirs,
newdirs,
fname
} = fileInfo;
// Create new blob object
let {
entry,
binary
} = addEntry({
mode: MODE_BLOB,
type: TYPE_BLOB,
name: fname,
content: fileContent
});
let objects = [];
objects.push([TYPE_BLOB, binary]);
//TODO: check if file/dir already exists
let bottom = dirs[0];
// Check if there is new dir
if (newdirs.length > 0) { // Case 1: new_dir is added
let {
dname,
treeEntry,
binaries
} = addSubTrees(newdirs, entry);
objects = [...objects, ...binaries];
// Update bottom tree
baseTrees[bottom][dname] = treeEntry;
} else { // Case 2: no new_dir is created
baseTrees[bottom][fname] = entry;
}
return {
baseTrees,
objects
};
}
// Check for deleted parent dirs
function findDeletedTree(dirs, baseTrees) {
/*
* If a file deletion results in an empty directory
* delete the corresponding tree
* root tree can be empty
*/
let deletedPath;
if (dirs.length > 1) {
let i = 0;
//TODO: repalce this "while" with an alternative (similar to isEmpty)
while (Object.keys(baseTrees[dirs[i]]).length == 1) {
deletedPath = dirs[i];
i++;
}
// remove deleted dirs from the list
dirs.splice(0, i);
}
return {
dirs,
deletedPath
};
}
// Create Git tree objects for an array of dirs
function addSubTrees(newdirs, treeEntry) {
let dir;
let objects = [];
while (newdirs.length > 0) {
dir = newdirs.pop();
let {
entry,
binary
} = addEntry({
mode: MODE_TREE,
type: TYPE_TREE,
name: dir,
content: [treeEntry]
});
treeEntry = entry;
objects.push([TYPE_TREE, binary]);
}
return {
dname: dir,
treeEntry,
binaries: objects
}
}
// Create a new Git blob/tree entry
function addEntry({
mode,
type,
name,
content
}) {
let obj = createGitObject(type, content);
let entry = {
mode,
type,
id: obj.id,
name
};
return {
entry,
binary: obj.object
};
}