Skip to content

Commit 1689cf9

Browse files
test: pull package contents using git
Also adds colours to the test-pull script's output
1 parent 2fe3f85 commit 1689cf9

3 files changed

Lines changed: 78 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@types/jest": "^25.2.1",
1717
"@types/react": "^17.0.0",
1818
"@types/react-dom": "^17.0.0",
19+
"chalk": "^4.1.2",
1920
"copy-dir": "^1.3.0",
2021
"husky": "^4.2.5",
2122
"interrogator": "^1.1.0",

scripts/test-pull.js

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const {URL} = require('url');
33
const {default: GitHubClient, auth} = require('@github-graph/api');
44
const ask = require('interrogator');
5+
const chalk = require('chalk');
56
const git = require('../packages/http');
67
const gitObj = require('../packages/objects');
78

@@ -66,7 +67,7 @@ async function pullRepo() {
6667
const mode =
6768
process.argv[4] ?? (await ask.list(`mode?`, [`commits`, `tree`]));
6869
if (![`commits`, `tree`].includes(mode)) {
69-
console.error(`Invalid mode`);
70+
console.error(chalk.red(`Invalid mode`));
7071
return process.exit(1);
7172
}
7273
const headCommit = process.argv[5];
@@ -77,13 +78,34 @@ async function pullRepo() {
7778
const repoURL = new URL(`https://github.com/${repo.owner}/${repo.name}.git`);
7879

7980
const start = Date.now();
80-
console.warn(`git_init`, `Git init request ${repoURL.href}`);
81+
console.warn(chalk.cyan(`git_init`), `Git init request ${repoURL.href}`);
8182
const {capabilities: serverCapabilities} = await git.initialRequest(repoURL, {
8283
http,
8384
agent: 'rollingversions.com',
8485
});
8586

86-
console.warn(`git_lsrefs`, `Git ls refs request ${repoURL.href}`);
87+
const pullObjects = async (want) => {
88+
const fetchResponse = await git.fetchObjects(
89+
repoURL,
90+
{want: [...new Set(want)]},
91+
{
92+
http,
93+
agent: 'rollingversions.com',
94+
serverCapabilities,
95+
},
96+
);
97+
98+
const entries = new Map();
99+
await new Promise((resolve, reject) => {
100+
fetchResponse
101+
.on(`data`, (entry) => entries.set(entry.hash, entry))
102+
.on(`error`, reject)
103+
.on(`end`, () => resolve());
104+
});
105+
return entries;
106+
};
107+
108+
console.warn(chalk.cyan(`git_lsrefs`), `Git ls refs request ${repoURL.href}`);
87109
const remoteRefs = headCommit
88110
? [{objectID: headCommit}]
89111
: await git.lsRefs(
@@ -106,20 +128,21 @@ async function pullRepo() {
106128
console.warn(`ref:`, ref);
107129
}
108130

109-
console.warn(`git_fetch_objects`, `Git fetch request ${repoURL.href}`);
131+
console.warn(
132+
chalk.cyan(`git_fetch_objects`),
133+
`Git fetch request ${repoURL.href}`,
134+
);
110135
const fetchResponse = await git.fetchObjects(
111136
repoURL,
112137

113138
mode === 'tree'
114139
? {
115140
want: [...new Set(remoteRefs.map((ref) => ref.objectID))],
116-
have: [],
117-
filter: [git.blobNone(0)],
141+
filter: [git.blobNone()],
118142
deepen: 1,
119143
}
120144
: {
121145
want: [...new Set(remoteRefs.map((ref) => ref.objectID))],
122-
have: [],
123146
filter: [git.treeDepth(0)],
124147
},
125148
{
@@ -135,33 +158,64 @@ async function pullRepo() {
135158
.on(`data`, (entry) => {
136159
if (gitObj.objectIsCommit(entry.body)) {
137160
const commit = gitObj.decodeObject(entry.body);
138-
console.warn(`${entry.hash} ${commit.body.message}`);
161+
console.warn(`${chalk.magenta(entry.hash)} ${commit.body.message}`);
139162
headTreeSha = commit.body.tree;
140163
rootHash = commit.body.tree;
141-
}
142-
if (gitObj.objectIsTree(entry.body)) {
164+
} else if (gitObj.objectIsTree(entry.body)) {
143165
const tree = gitObj.decodeObject(entry.body);
144166
trees.set(entry.hash, tree.body);
167+
} else {
168+
const obj = gitObj.decodeObject(entry.body);
169+
console.error(
170+
chalk.red(`Unexpected object ${entry.hash} of type ${obj.type}`),
171+
);
145172
}
146173
})
147174
.on(`error`, reject)
148175
.on(`end`, () => resolve());
149176
});
150177
if (mode === `tree`) {
151-
const printTree = (hash, path) => {
178+
const packages = [];
179+
const walkTree = (hash, parentPath) => {
152180
const tree = trees.get(hash);
153181
for (const [name, {mode, hash}] of Object.entries(tree)) {
154-
console.log(`${path}/${name} ${gitObj.Mode[mode]} ${hash}`);
182+
const path = `${parentPath}/${name}`;
183+
const modeName = gitObj.Mode[mode];
184+
const modeColor =
185+
{file: chalk.yellow, tree: chalk.blue}[modeName] ?? chalk.red;
186+
console.log(`${path} ${modeColor(gitObj.Mode[mode])} ${hash}`);
155187

156188
if (mode === gitObj.Mode.tree) {
157-
printTree(hash, `${path}/${name}`);
189+
walkTree(hash, path);
190+
} else if (
191+
mode === gitObj.Mode.file &&
192+
(name === `rolling-package.toml` || name === 'package.json')
193+
) {
194+
packages.push({path, hash});
158195
}
159196
}
160197
};
161-
printTree(rootHash, ``);
198+
walkTree(rootHash, ``);
199+
if (packages.length) {
200+
const packageObjects = await pullObjects(packages.map((p) => p.hash));
201+
for (const {path, hash} of packages) {
202+
const entry = packageObjects.get(hash);
203+
if (entry) {
204+
const obj = gitObj.decodeObject(entry.body);
205+
console.log(`${chalk.magenta(path)} ${hash} ${obj.type}`);
206+
console.log(``);
207+
console.log(Buffer.from(obj.body).toString(`utf8`));
208+
console.log(``);
209+
console.log(``);
210+
} else {
211+
console.error(chalk.red(`${path} ${hash} NOT FOUND!!!`));
212+
}
213+
}
214+
}
162215
}
216+
163217
const end = Date.now();
164-
console.log(`Pull duration: ${end - start}`);
218+
console.log(chalk.green(`Pull duration: ${end - start}`));
165219
}
166220

167221
pullRepo().catch((ex) => {

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,14 @@ chalk@^4.0.0, chalk@^4.1.0:
13631363
ansi-styles "^4.1.0"
13641364
supports-color "^7.1.0"
13651365

1366+
chalk@^4.1.2:
1367+
version "4.1.2"
1368+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
1369+
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
1370+
dependencies:
1371+
ansi-styles "^4.1.0"
1372+
supports-color "^7.1.0"
1373+
13661374
char-regex@^1.0.2:
13671375
version "1.0.2"
13681376
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"

0 commit comments

Comments
 (0)