Skip to content

Commit ea73ea2

Browse files
committed
Add basic examples
1 parent 2265519 commit ea73ea2

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

examples/commits.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createGitReader } from '@discoveryjs/scan-git';
2+
import { gitDir } from './get-git-dir.js';
3+
4+
const repo = await createGitReader(gitDir);
5+
6+
console.log('createGitReader:', repo.initTime, 'ms');
7+
8+
async function collectCommits() {
9+
const startTime = Date.now();
10+
// read all commits reachable from HEAD
11+
const commits = await repo.log({
12+
ref: 'master',
13+
depth: Infinity
14+
});
15+
16+
console.log(commits.length, 'commits read');
17+
console.log('time:', Date.now() - startTime, 'ms');
18+
console.log('mem:', process.memoryUsage());
19+
20+
// prepare commits data for analysis
21+
// const commitIdxByOid = commits.reduce((map, commit, idx) => map.set(commit.oid, idx), new Map());
22+
// for (const commit of commits) {
23+
// for (let i = 0; i < commit.parent.length; i++) {
24+
// commit.parent[i] = commitIdxByOid.get(commit.parent[i]);
25+
// }
26+
// }
27+
28+
// writeFileSync(
29+
// 'commits.json',
30+
// JSON.stringify(commits, (key, value) =>
31+
// key === 'timezone' || key === 'tree'
32+
// ? undefined
33+
// : key === 'message' && value.includes('\n')
34+
// ? value.slice(0, value.indexOf('\n'))
35+
// : value
36+
// )
37+
// );
38+
}
39+
40+
collectCommits();

examples/get-git-dir.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { resolve } from 'path';
2+
3+
export const gitDir = resolve(process.argv[2] || '.git');

examples/list-files.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { gitDir } from './get-git-dir.js';
2+
import { createGitReader } from '../lib/index.js';
3+
4+
const repo = await createGitReader(gitDir);
5+
6+
console.log('createGitReader:', repo.initTime, 'ms');
7+
8+
async function example(oneMoreRun) {
9+
const startTime = Date.now();
10+
const files = await repo.listFiles();
11+
12+
console.log();
13+
console.log(oneMoreRun ? '[COLD RUN]' : '[HOT RUN]');
14+
console.log(
15+
files.length,
16+
'files in',
17+
Date.now() - startTime,
18+
'ms, mem:',
19+
process.memoryUsage()
20+
);
21+
22+
if (oneMoreRun) {
23+
example();
24+
}
25+
}
26+
27+
example(true);

examples/stat.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createGitReader } from '../lib/index.js';
2+
import { gitDir } from './get-git-dir.js';
3+
4+
const repo = await createGitReader(gitDir);
5+
6+
console.log('createGitReader:', repo.initTime, 'ms');
7+
8+
async function example() {
9+
const startTime = Date.now();
10+
const stats = await repo.stat();
11+
12+
console.log('Done in', Date.now() - startTime, 'ms');
13+
console.log(process.memoryUsage());
14+
15+
console.log('loose objects:', stats.objects.loose.objects.count);
16+
console.log('packed objects:', stats.objects.packed.objects.count);
17+
18+
// writeFileSync('./stat.json', JSON.stringify(stats), 'utf8');
19+
}
20+
21+
example();

0 commit comments

Comments
 (0)