Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Some repos also get a condition on top of their plan. A repo with no recent comm
| Ruin | a file nobody has touched in over two years |
| Flash on a roof | touched by the commit now playing |

Drag to pan and scroll to zoom. The biggest districts carry signposts with their folder's name, and more appear as you zoom in. Hover a building to see its file and light up its whole district, and click it to light up its imports. The timeline under the city shows how busy each stretch of history was: click or drag it to jump, <kbd>Space</kbd> plays or pauses, <kbd>←</kbd> <kbd>→</kbd> step. The signpost button (or <kbd>L</kbd>) hides the names for a clean view, and <kbd>Esc</kbd> clears the selection.
Drag to pan and scroll to zoom. The biggest districts carry signposts with their folder's name, and more appear as you zoom in. Hover a building to see its file and light up its whole district, and click it to light up its imports. The timeline under the city shows how busy each stretch of history was: click or drag it to jump, <kbd>Space</kbd> plays or pauses, <kbd>←</kbd> <kbd>→</kbd> step. The signpost button (or <kbd>L</kbd>) hides the names for a clean view. The builder button (or <kbd>B</kbd>) lists who built the city, each with the number of buildings they put up (a file's builder is whoever added it), and picking one lights up their buildings; the address then carries `?by=` with their name, so the link shows them too. <kbd>Esc</kbd> clears the selection.

## Share your city

Expand Down
26 changes: 18 additions & 8 deletions src/scan.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,16 @@ export async function scanRepo(repo, { maxCommits = 20000, name } = {}) {
// renameLimit: pinned to git's own default, so pairMoves knows when git gave up (and an older git,
// or one configured otherwise, pairs the same files)
const proc = spawn('git', ['-C', root, '-c', 'core.quotePath=false', '-c', `diff.renameLimit=${RENAME_LIMIT}`, 'log', `--max-count=${maxCommits}`, '--raw', '--no-abbrev',
'--format=@@%ct %H', '-M', '--reverse'], { stdio: ['ignore', 'pipe', 'ignore'] });
'--format=@@%ct %H %aN', '-M', '--reverse'], { stdio: ['ignore', 'pipe', 'ignore'] });
const rl = readline.createInterface({ input: proc.stdout });
let cur = null, added = [], deleted = [];
rl.on('line', line => {
if (line.startsWith('@@')) {
// "@@<time> <hash> <author>" (%aN: the name .mailmap gives them, so one person's two emails are one builder)
const at = line.indexOf(' '), by = line.indexOf(' ', at + 1);
if (cur) pairMoves(added, deleted);
else boundary = line.slice(line.indexOf(' ') + 1);
cur = { t: parseInt(line.slice(2)), f: [] };
else boundary = line.slice(at + 1, by);
cur = { t: parseInt(line.slice(2)), a: line.slice(by + 1) || undefined, f: [] }; // (a commit can have no name on it)
commits.push(cur);
added = []; deleted = [];
return;
Expand All @@ -372,8 +374,8 @@ export async function scanRepo(repo, { maxCommits = 20000, name } = {}) {
// branches), and stays deleted.
if (!h && (h = dead.get(to)) && arrives) { dead.delete(to); delete h.died; }
// (a file seen for the first time by an edit was there before the commits read: it isn't new.
// was: its path then, to look up when it was born)
if (!h) { h = { born: cur.t, last: 0, churn: 0 }; if (st[0] !== 'A') h.was = from; if (arrives) added.push(to); }
// was: its path then, to look up when it was born. by: who added it, when the walk saw them do it)
if (!h) { h = { born: cur.t, last: 0, churn: 0 }; if (st[0] !== 'A') h.was = from; else h.by = cur.a; if (arrives) added.push(to); }
if (st[0] === 'D') {
alias.delete(from);
dead.set(from, h);
Expand Down Expand Up @@ -472,7 +474,7 @@ export async function scanRepo(repo, { maxCommits = 20000, name } = {}) {
const idx = index.get(p);
if (idx === undefined) continue;
recIdx.set(h, idx);
Object.assign(files[idx], { born: h.born, last: h.last, churn: h.churn });
Object.assign(files[idx], { born: h.born, last: h.last, churn: h.churn, by: h.by });
}
for (const c of commits) c.f = [...new Set(c.f.map(h => recIdx.get(h)).filter(i => i !== undefined))];
const touched = commits.filter(c => c.f.length);
Expand Down Expand Up @@ -509,19 +511,27 @@ export async function scanRepo(repo, { maxCommits = 20000, name } = {}) {
if (!f || kind(f) && kind(f) !== 'code' && !keep[kind(f)]) continue;
if (engine && ENGINE_LANG[engine.name].includes(f.lang) && !SAMPLE_DIR.test(p)) f.g = engine.name;
// last: its last change before it went; churn: its changes, not counting the deletion
gone.push({ p, lang: f.lang, loc: f.loc, a: f.a, born: h.born, last: h.edited, died: h.died, churn: h.churn - 1, ...(f.g && { g: f.g }) });
gone.push({ p, lang: f.lang, loc: f.loc, a: f.a, born: h.born, last: h.edited, died: h.died, churn: h.churn - 1, ...(f.g && { g: f.g }), by: h.by });
}
}
} catch { gone.length = 0; } // the deleted files are extra: without them the city is still whole
gone.sort((x, y) => x.died - y.died || (x.p < y.p ? -1 : 1));
// who built the city, those who built the most first; each building names its builder by place in the
// list (b), and one added before the commits read has none. Names only: the emails stay in the repo
const built = new Map();
for (const f of [...files, ...gone]) if (f.by !== undefined) built.set(f.by, (built.get(f.by) || 0) + 1);
const authors = [...built].sort((x, y) => y[1] - x[1] || (x[0] < y[0] ? -1 : 1)).map(([a]) => a);
const builder = new Map(authors.map((a, i) => [a, i]));
for (const g of gone) { const { by } = g; delete g.by; if (by !== undefined) g.b = builder.get(by); }
const edges = files.reduce((n, f) => n + f.imp.size, 0);
const city = {
name,
scannedAt: Math.floor(Date.now() / 1000),
scanMs: Date.now() - t0,
start, end, ...(truncated && { truncated, firstCommit }),
stats: { files: files.length, loc: files.reduce((n, f) => n + f.loc, 0), edges, commits: touched.length },
files: files.map(f => ({ p: f.p, lang: f.lang, loc: f.loc, imp: [...f.imp], a: f.a, churn: f.churn, born: f.born, last: f.last, ...(status.get(f) && { s: status.get(f) }), ...(f.g && { g: f.g }) })),
files: files.map(f => ({ p: f.p, lang: f.lang, loc: f.loc, imp: [...f.imp], a: f.a, churn: f.churn, born: f.born, last: f.last, ...(status.get(f) && { s: status.get(f) }), ...(f.g && { g: f.g }), ...(f.by !== undefined && { b: builder.get(f.by) }) })),
authors,
// files deleted along the way (none of them is in commits: those name the files above)
gone,
// compact: [time, fileIdx, fileIdx, ...]; skip giant sweeping commits (formatting, renames)
Expand Down
29 changes: 26 additions & 3 deletions test/history.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ function repo() {
git(dir, ['config', 'user.name', 'test']); git(dir, ['config', 'user.email', 'test@example.com']);
return dir;
}
// files: { path: text, or null to delete it }; returns the commit's time
function commit(dir, files, msg = 'change') {
// files: { path: text, or null to delete it }; by: its author, "Name <email>"; returns the commit's time
function commit(dir, files, msg = 'change', by) {
clock += DAY;
for (const [p, text] of Object.entries(files)) {
if (text === null) { rmSync(path.join(dir, p)); continue; }
mkdirSync(path.dirname(path.join(dir, p)), { recursive: true });
writeFileSync(path.join(dir, p), text);
}
git(dir, ['add', '-A']);
git(dir, ['commit', '-q', '-m', msg]);
git(dir, ['commit', '-q', '-m', msg, ...(by ? [`--author=${by}`] : [])]);
return clock;
}
// merge `branch` into the current one; on a conflict, `resolve` settles each path (true keeps it, false deletes it)
Expand All @@ -55,6 +55,7 @@ const cases = [];
const check = (name, fn) => cases.push([name, fn]);
const born = (city, p) => city.files.find(f => f.p === p)?.born;
const gone = (city, p) => city.gone.find(g => g.p === p);
const builder = (city, p) => { const b = (city.files.find(f => f.p === p) ?? gone(city, p))?.b; return b === undefined ? undefined : city.authors[b]; };

check('a file rewritten in another format keeps its birth (same folder and stem)', async () => {
const dir = repo();
Expand Down Expand Up @@ -220,6 +221,28 @@ check('a deleted file drawn from its size alone is drawn without reading it', as
return [[lock.length > 1_000_000, true], [gone(city, 'package-lock.json')?.loc, Math.round(lock.length / 40)]];
});

check('a building is its first author\'s, moved, deleted or edited by others (one name per person)', async () => {
const dir = repo();
const ann = 'Ann <ann@example.com>', bob = 'Bob <bob@example.com>';
commit(dir, { 'a.js': src('a'), 'lib/old.js': src('old'), '.mailmap': 'Ann <ann@example.com> <ann@home.example.com>\n' }, 'start', ann);
commit(dir, { 'a.js': src('a2'), 'lib/old.js': null, 'src/old.js': src('old') }, 'move', bob);
commit(dir, { 'b.js': src('b') }, 'add', bob);
commit(dir, { 'c.js': src('c') }, 'add', 'ann at home <ann@home.example.com>');
commit(dir, { 'b.js': null }, 'remove', ann);
const city = await scanRepo(dir);
return [[builder(city, 'a.js'), 'Ann'], [builder(city, 'src/old.js'), 'Ann'], [builder(city, 'b.js'), 'Bob'], [builder(city, 'c.js'), 'Ann'],
[city.authors.join(), 'Ann,Bob'], [city.authors.includes('ann at home'), false]];
});

check('a file there before the commits read has no builder', async () => {
const dir = repo();
commit(dir, { 'old.js': src('o') }, 'start', 'Ann <ann@example.com>');
commit(dir, { 'old.js': src('o2') }, 'edit', 'Bob <bob@example.com>');
commit(dir, { 'new.js': src('n') }, 'add', 'Bob <bob@example.com>');
const city = await scanRepo(dir, { maxCommits: 2 });
return [[builder(city, 'old.js'), undefined], [builder(city, 'new.js'), 'Bob']];
});

let failed = 0;
try {
for (const [name, fn] of cases) {
Expand Down
7 changes: 4 additions & 3 deletions test/snapshot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ function checkout({ repo, sha }) {

// What's compared: everything the scanner hands the city, minus timings. File and import references
// are by path, not by index, so one added file shows as one added file and not as every file shifting.
// Builders likewise, by name (the list of them is in the order of who built most).
function normalize(city) {
const { scannedAt, scanMs, files, gone, commits, identity, ...rest } = city;
const { scannedAt, scanMs, files, gone, commits, identity, authors, ...rest } = city;
const { idleYears, ...metrics } = identity.metrics; // the frozen clock talking, not the scanner
const paths = files.map(f => f.p);
const commitList = commits.map(([t, ...idx]) => [t, ...idx.map(i => paths[i])]);
Expand All @@ -64,8 +65,8 @@ function normalize(city) {
identity: { ...identity, metrics },
// the timeline is long and every file's born / last / churn already tells its story: keep a fingerprint
commits: { count: commitList.length, digest: createHash('sha256').update(JSON.stringify(commitList)).digest('hex').slice(0, 16) },
files: Object.fromEntries(files.map(({ p, imp, ...f }) => [p, { ...f, imp: imp.map(i => paths[i]) }])),
gone: Object.fromEntries(gone.map(({ p, ...f }) => [p, f])),
files: Object.fromEntries(files.map(({ p, imp, b, ...f }) => [p, { ...f, imp: imp.map(i => paths[i]), ...(b !== undefined && { by: authors[b] }) }])),
gone: Object.fromEntries(gone.map(({ p, b, ...f }) => [p, { ...f, ...(b !== undefined && { by: authors[b] }) }])),
};
}

Expand Down
Loading
Loading