Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
58 lines (51 loc) · 1.44 KB
/
Copy pathhelpers.js
File metadata and controls
58 lines (51 loc) · 1.44 KB
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
'use strict';
/**
* Hey! Don't look here if you want to implement creating a project tree on your own
*/
const fs = require('fs');
const getSha1 = require('./util').getSha1;
function toObject (index, project) {
project = project || {};
index
.split('\n')
.forEach((entry) => {
entry = entry.split(' ');
let file = entry[0];
let path = file.split('/');
let hash = entry[1];
if (path.length <= 1) project[path[0]] = hash;
else {
if (project[path[0]])
project[path[0]] = toObject(path.slice(1).join('/') + ' ' + hash, project[path[0]]);
else
project[path[0]] = toObject(path.slice(1).join('/') + ' ' + hash);
}
});
return project;
}
function treeSum (project) {
let treeFileContent = [];
for (let key in project) {
if (typeof project[key] === 'string')
treeFileContent.push(`blob ${project[key]} ${key}`);
else
treeFileContent.push(`tree ${treeSum(project[key])} ${key}`);
}
return createTreeObject(treeFileContent.join('\n'));
}
function createTreeObject (contents) {
let hash = getSha1(contents);
let dir = '.fvs/objects/' + hash.slice(0, 2);
let name = hash.slice(2);
try {
fs.statSync(dir);
} catch (err) {
fs.mkdirSync(dir);
fs.writeFileSync(dir + '/' + name, contents, 'utf8');
}
return hash;
}
module.exports = function createTreesFromIndex (index) {
let project = toObject(index);
return treeSum(project);
};