-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerateObjects.js
More file actions
204 lines (160 loc) · 4.79 KB
/
generateObjects.js
File metadata and controls
204 lines (160 loc) · 4.79 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
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
import {
getObjectType,
getHeadCommit,
getDecompressedFileBuffer
} from "./commonFns";
let FILE_ARR = [];
let OBJECT_ARR = [];
let RECURSED_DATA = {};
async function getObjects(fileObjects, branchInfo) {
FILE_ARR = fileObjects;
const head = await getHeadCommit(FILE_ARR, branchInfo.currentBranch.name);
let commit = head;
let parentCommit = "";
let tree = "";
// await recurseCommits(head);
do {
const commitFilePath =
".git/objects/" + commit.slice(0, 2) + "/" + commit.slice(2);
let commitContent = await getDecompressedFileBuffer(
FILE_ARR,
commitFilePath
);
if (commitContent === undefined) {
FILE_ARR = [];
OBJECT_ARR = [];
RECURSED_DATA = {};
throw new Error("File not found.");
}
commitContent = commitContent.toString("utf-8").split("\n");
let commitMsg = getCommitMsg(commitContent);
const treeArr = commitContent[0].split(" ");
tree = treeArr[2];
const blobs = await getBlobs(tree);
const parentCommitArr = commitContent[1].split(" ");
if (parentCommitArr[0] === "parent") parentCommit = parentCommitArr[1];
else parentCommit = "";
let branchHead = "";
for (let i = 0; i < branchInfo.allBranches.length; i++) {
if (branchInfo.allBranches[i].branchHeadHash === commit) {
if (branchHead === "")
branchHead = branchInfo.allBranches[i].branchName;
else
branchHead =
branchHead +
", " +
branchInfo.allBranches[i].branchName;
}
}
OBJECT_ARR.push({
commit,
branchHead,
commitMsg,
parentCommit,
tree,
blobs
});
commit = parentCommit;
} while (parentCommit !== "");
const objectData = { objects: OBJECT_ARR, recursiveTrees: RECURSED_DATA };
FILE_ARR = [];
OBJECT_ARR = [];
RECURSED_DATA = {};
return objectData;
}
// async function recurseCommits(startCommit = "") {
// let commit = startCommit;
// let parentCommits = [];
// let tree = "";
// do {
// const commitFilePath =
// ".git/objects/" + commit.slice(0, 2) + "/" + commit.slice(2);
// let commitContent = await getDecompressedFileBuffer(commitFilePath);
// commitContent = commitContent.toString("utf-8").split("\n");
// const treeArr = commitContent[0].split(" ");
// tree = treeArr[2];
// const blobs = await getBlobs(tree);
// parentCommits = [];
// let parentCommitArr = commitContent[1].split(" ");
// if (parentCommitArr[0] === "parent")
// parentCommits.push(parentCommitArr[1]);
// else parentCommits[0] = "";
// parentCommitArr = commitContent[2].split(" ");
// if (parentCommitArr[0] === "parent") {
// parentCommits.push(parentCommitArr[1]);
// OBJECT_ARR.push({
// commit,
// commitMsg: "",
// parentCommits,
// tree,
// blobs
// });
// await recurseCommits(parentCommitArr[1]);
// } else
// OBJECT_ARR.push({
// commit,
// commitMsg: "",
// parentCommits,
// tree,
// blobs
// });
// commit = parentCommits[0];
// } while (parentCommits[0] !== "" && !isCommitInObjArr(commit));
// }
async function getBlobs(treeHash = "") {
if (treeHash === "") throw new Error("Tree hash parameter missing.");
const treeFilePath =
".git/objects/" + treeHash.slice(0, 2) + "/" + treeHash.slice(2);
const treeContent = await getDecompressedFileBuffer(FILE_ARR, treeFilePath);
if (treeContent === undefined) {
FILE_ARR = [];
OBJECT_ARR = [];
RECURSED_DATA = {};
throw new Error("File not found.");
}
let index = treeContent.indexOf("\0");
let [type, length] = treeContent.toString("utf-8", 0, index).split(" ");
length = parseInt(length);
let blobArr = [];
for (let nullIndex = index; nullIndex < length; index = nullIndex) {
nullIndex = treeContent.indexOf("\0", index + 1);
let [mode, name] = treeContent
.toString("utf-8", index, nullIndex)
.split(" ");
nullIndex++;
const hash = treeContent.toString("hex", nullIndex, (nullIndex += 20)); // '20' since the SHA1 hash is 20 bytes (40 hexadecimal characters per SHA1 hash)
const objectType = await getObjectType(FILE_ARR, hash);
blobArr.push({ type: objectType, name, hash });
if (objectType === "tree") {
const newData = await getBlobs(hash);
RECURSED_DATA[hash] = newData;
}
}
return blobArr;
}
function getCommitMsg(commitObjContent = []) {
let i = 0;
let firstParent = commitObjContent[1].split(" ");
let secondParent = commitObjContent[2].split(" ");
if (firstParent[0] === "parent") {
i++;
if (secondParent[0] === "parent") i++;
}
i += 3;
let temp = commitObjContent[i].split(" ");
if (temp[0] === "gpgsig") {
let j = 0;
for (j = i; j < commitObjContent.length; j++) {
if (commitObjContent[j] === " -----END PGP SIGNATURE-----") {
break;
}
}
i = j + 2;
}
i++;
return commitObjContent[i];
}
// function isCommitInObjArr(commitHash = "") {
// return OBJECT_ARR.some((obj) => obj.commit === commitHash);
// }
export default getObjects;