repometry 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "repometry",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Track repository metrics without SaaS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -15,51 +15,26 @@ export async function gitChurn(directory, commit, paths) {
15
15
  return summarizeGitHistory(log, commit, paths, shallow.trim() === 'true');
16
16
  }
17
17
  export function summarizeGitHistory(log, commit, paths, shallow) {
18
- const fields = log.split('\0');
19
- if (fields[0] !== '' || fields.at(-1) !== '')
20
- throw new Error('Invalid Git history record');
21
18
  const touches = new Map(paths.map(path => [path, 0]));
22
- let index = 1;
23
19
  let visited = 0;
24
20
  let commits = 0;
25
21
  let endingAt;
26
22
  let capReached = false;
27
- while (index < fields.length - 1) {
28
- const [sha, date, ...parents] = fields[index++].split(' ');
29
- const timestamp = Number(date);
30
- const hasParent = parents.length > 0 && parents.every(parent => oid.test(parent));
31
- const root = parents.length === 1 && parents[0] === '';
32
- if (sha === undefined || !oid.test(sha) || date === undefined || !/^\d+$/.test(date) || !Number.isSafeInteger(timestamp) || (!hasParent && !root)) {
33
- throw new Error('Invalid Git history record');
34
- }
35
- if (endingAt === undefined) {
36
- if (sha !== commit)
37
- throw new Error('Git history did not start at the inspected commit');
38
- endingAt = timestamp;
39
- }
40
- if (visited >= churnSettings.maxCommits) {
23
+ for (const record of gitRecords(log)) {
24
+ if (endingAt === undefined && record.sha !== commit)
25
+ throw new Error('Git history did not start at the inspected commit');
26
+ endingAt ??= record.timestamp;
27
+ if (visited++ >= churnSettings.maxCommits) {
41
28
  capReached = true;
42
29
  break;
43
30
  }
44
- visited++;
45
- const cutoff = endingAt - churnSettings.lookbackDays * 86400;
46
- const overlaps = hasParent && timestamp >= cutoff && timestamp <= endingAt;
47
- const changed = new Set();
48
- let first = true;
49
- while (index < fields.length && fields[index] !== '') {
50
- const path = fields[index++];
51
- if (overlaps) {
52
- const normalized = first ? path.replace(/^\n/, '') : path;
53
- if (touches.has(normalized))
54
- changed.add(normalized);
55
- }
56
- first = false;
57
- }
58
- index++;
59
- if (overlaps)
60
- commits++;
61
- for (const path of changed)
62
- touches.set(path, touches.get(path) + 1);
31
+ // Only commits with a parent, inside the window that ends at the inspected commit, count.
32
+ if (!record.hasParent || record.timestamp < endingAt - churnSettings.lookbackDays * 86400 || record.timestamp > endingAt)
33
+ continue;
34
+ commits++;
35
+ for (const path of new Set(record.paths))
36
+ if (touches.has(path))
37
+ touches.set(path, touches.get(path) + 1);
63
38
  }
64
39
  if (endingAt === undefined)
65
40
  throw new Error('Empty Git history');
@@ -68,4 +43,30 @@ export function summarizeGitHistory(log, commit, paths, shallow) {
68
43
  window: { endingAt: new Date(endingAt * 1000).toISOString(), commits, shallow, capReached },
69
44
  };
70
45
  }
46
+ /** The records of `git log -z --format=%x00%H %ct %P --name-only`: a header field, then path fields up to an empty one. */
47
+ function* gitRecords(log) {
48
+ const fields = log.split('\0');
49
+ if (fields[0] !== '' || fields.at(-1) !== '')
50
+ throw new Error('Invalid Git history record');
51
+ let index = 1;
52
+ while (index < fields.length - 1) {
53
+ const header = recordHeader(fields[index++]);
54
+ const paths = [];
55
+ // Git starts each record's path list on a new line.
56
+ for (; index < fields.length && fields[index] !== ''; index++)
57
+ paths.push(paths.length ? fields[index] : fields[index].replace(/^\n/, ''));
58
+ index++;
59
+ yield { ...header, paths };
60
+ }
61
+ }
62
+ function recordHeader(field) {
63
+ const [sha, date, ...parents] = field.split(' ');
64
+ const timestamp = Number(date);
65
+ const hasParent = parents.length > 0 && parents.every(parent => oid.test(parent));
66
+ const root = parents.length === 1 && parents[0] === '';
67
+ const named = sha !== undefined && oid.test(sha) && date !== undefined && /^\d+$/.test(date);
68
+ if (!named || !Number.isSafeInteger(timestamp) || (!hasParent && !root))
69
+ throw new Error('Invalid Git history record');
70
+ return { sha: sha, timestamp, hasParent };
71
+ }
71
72
  //# sourceMappingURL=git-churn.js.map
@@ -26,7 +26,7 @@ export async function review(request) {
26
26
  try {
27
27
  const history = new HistoryCache(request.settings);
28
28
  const inspectCommit = (id, coverage) => inspect({ directory, output, source: { revision: id }, exclude, ignore, plugins,
29
- ...coverage ? { lcov, goCover } : {} }, undefined, { history, deferSummary: true });
29
+ ...coverage ? { lcov, goCover } : {} }, noChurn, { history, deferSummary: true });
30
30
  const base = await inspectCommit(mergeBase, false);
31
31
  const headReport = await inspectCommit(head, true);
32
32
  const changes = await readLineChanges(directory, mergeBase, head);
@@ -41,6 +41,8 @@ export async function review(request) {
41
41
  await rm(output, { recursive: true, force: true });
42
42
  }
43
43
  }
44
+ /** A review compares findings, not file activity: it skips the walk over Git history that churn needs. */
45
+ const noChurn = { recordChurn: undefined };
44
46
  /** The text of one side's changed files, which scope and matching read line by line. */
45
47
  async function sourceLines(directory, commit, changes, side) {
46
48
  const paths = changes.files.flatMap(file => file[side] === null || file.whole ? [] : [file[side]]);
@@ -2,6 +2,7 @@ import { annotationLimit } from '../../core/model/pull-review.js';
2
2
  /** The first line of every review comment, which finds the comment to update. */
3
3
  export const reviewMarker = '<!-- repometry-review -->';
4
4
  const maxLength = 60_000;
5
+ const assets = 'https://raw.githubusercontent.com/idleware/repometry/master/.github/assets';
5
6
  const groups = [
6
7
  { effect: 'new', title: 'New', folded: false }, { effect: 'worsened', title: 'Worsened', folded: false },
7
8
  { effect: 'improved', title: 'Improved', folded: true }, { effect: 'touched', title: 'Touched', folded: true },
@@ -9,7 +10,7 @@ const groups = [
9
10
  const verdicts = { passed: 'Passed', failed: 'Failed', unmeasured: 'Not measured' };
10
11
  /** The review as Markdown: the run summary and the pull request comment carry the same text. */
11
12
  export function pullReviewMarkdown(review) {
12
- const head = [reviewMarker, `## Repometry${review.number === null ? ' review' : ` · PR #${review.number}`}`, '', headline(review), '',
13
+ const head = [reviewMarker, `## ${review.verdict === 'unmeasured' ? '' : `${flag(review.verdict)} `}repometry`, '', headline(review), '',
13
14
  ...facts(review).map(line => `- ${line}`), ''];
14
15
  const annotated = review.issues.filter(issue => issue.effect === 'new' || issue.effect === 'worsened').length;
15
16
  const ranges = review.coverage.uncovered.length;
@@ -53,6 +54,15 @@ function fitTable({ title, folded, heads, rows }, budget) {
53
54
  const cut = rows.length - kept.length;
54
55
  return kept.length ? { lines: [...header, ...kept, ...closing], length, cut } : { lines: [], length: 0, cut };
55
56
  }
57
+ /**
58
+ * The verdict flag that, with the name, forms the logo. Comments load it from master, so these paths must never change.
59
+ * It is cropped to its ink: at 17 px it stands on the heading's baseline at cap height.
60
+ */
61
+ function flag(verdict) {
62
+ const asset = (theme) => `${assets}/flag-${verdict}-${theme}.svg`;
63
+ return `<picture><source media="(prefers-color-scheme: dark)" srcset="${asset('dark')}">`
64
+ + `<img src="${asset('light')}" alt="" width="17" height="17"></picture>`;
65
+ }
56
66
  function headline({ verdict, issues, base, mergeBase, head, changedFiles }) {
57
67
  const counts = groups.map(({ effect }) => [effect, issues.filter(issue => issue.effect === effect).length])
58
68
  .filter(([, count]) => count > 0).map(([effect, count]) => `${count} ${effect}`);