Skip to content

Commit 2fe3f85

Browse files
test: pull file tree using git
1 parent a8213b0 commit 2fe3f85

1 file changed

Lines changed: 63 additions & 19 deletions

File tree

scripts/test-pull.js

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,61 +63,105 @@ async function pullRepo() {
6363

6464
const owner = process.argv[2] ?? (await ask.input(`owner`));
6565
const name = process.argv[3] ?? (await ask.input(`name`));
66+
const mode =
67+
process.argv[4] ?? (await ask.list(`mode?`, [`commits`, `tree`]));
68+
if (![`commits`, `tree`].includes(mode)) {
69+
console.error(`Invalid mode`);
70+
return process.exit(1);
71+
}
72+
const headCommit = process.argv[5];
73+
6674
const repo = {owner, name};
6775

6876
const http = await getHttpHandler(repo, secrets);
6977
const repoURL = new URL(`https://github.com/${repo.owner}/${repo.name}.git`);
7078

79+
const start = Date.now();
7180
console.warn(`git_init`, `Git init request ${repoURL.href}`);
7281
const {capabilities: serverCapabilities} = await git.initialRequest(repoURL, {
7382
http,
7483
agent: 'rollingversions.com',
7584
});
7685

7786
console.warn(`git_lsrefs`, `Git ls refs request ${repoURL.href}`);
78-
const remoteRefs = await git.lsRefs(
79-
repoURL,
80-
{
81-
// TODO: what do we need here?
82-
// symrefs: true,
83-
refPrefix: ['refs/heads/', 'refs/tags/', 'refs/pull/'],
84-
},
85-
{
86-
http,
87-
agent: 'rollingversions.com',
88-
serverCapabilities,
89-
},
90-
);
87+
const remoteRefs = headCommit
88+
? [{objectID: headCommit}]
89+
: await git.lsRefs(
90+
repoURL,
91+
{
92+
// TODO: what do we need here?
93+
// symrefs: true,
94+
refPrefix:
95+
mode === 'tree'
96+
? ['refs/heads/master', 'refs/heads/main']
97+
: ['refs/heads/', 'refs/tags/', 'refs/pull/'],
98+
},
99+
{
100+
http,
101+
agent: 'rollingversions.com',
102+
serverCapabilities,
103+
},
104+
);
91105
for (const ref of remoteRefs) {
92106
console.warn(`ref:`, ref);
93107
}
94108

95109
console.warn(`git_fetch_objects`, `Git fetch request ${repoURL.href}`);
96110
const fetchResponse = await git.fetchObjects(
97111
repoURL,
98-
{
99-
want: [...new Set(remoteRefs.map((ref) => ref.objectID))],
100-
have: [],
101-
filter: [git.treeDepth(0)],
102-
},
112+
113+
mode === 'tree'
114+
? {
115+
want: [...new Set(remoteRefs.map((ref) => ref.objectID))],
116+
have: [],
117+
filter: [git.blobNone(0)],
118+
deepen: 1,
119+
}
120+
: {
121+
want: [...new Set(remoteRefs.map((ref) => ref.objectID))],
122+
have: [],
123+
filter: [git.treeDepth(0)],
124+
},
103125
{
104126
http,
105127
agent: 'rollingversions.com',
106128
serverCapabilities,
107129
},
108130
);
131+
const trees = new Map();
132+
let rootHash = ``;
109133
await new Promise((resolve, reject) => {
110134
fetchResponse
111135
.on(`data`, (entry) => {
112136
if (gitObj.objectIsCommit(entry.body)) {
113137
const commit = gitObj.decodeObject(entry.body);
114138
console.warn(`${entry.hash} ${commit.body.message}`);
139+
headTreeSha = commit.body.tree;
140+
rootHash = commit.body.tree;
141+
}
142+
if (gitObj.objectIsTree(entry.body)) {
143+
const tree = gitObj.decodeObject(entry.body);
144+
trees.set(entry.hash, tree.body);
115145
}
116146
})
117147
.on(`error`, reject)
118148
.on(`end`, () => resolve());
119149
});
120-
console.log(`!done!`);
150+
if (mode === `tree`) {
151+
const printTree = (hash, path) => {
152+
const tree = trees.get(hash);
153+
for (const [name, {mode, hash}] of Object.entries(tree)) {
154+
console.log(`${path}/${name} ${gitObj.Mode[mode]} ${hash}`);
155+
156+
if (mode === gitObj.Mode.tree) {
157+
printTree(hash, `${path}/${name}`);
158+
}
159+
}
160+
};
161+
printTree(rootHash, ``);
162+
}
163+
const end = Date.now();
164+
console.log(`Pull duration: ${end - start}`);
121165
}
122166

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

0 commit comments

Comments
 (0)