@esneiderbravo/speclaw 0.3.9 → 0.3.11

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.
@@ -144,34 +144,114 @@ export function churn(projectPath, opts = {}) {
144
144
  }
145
145
  return { shallow, byPath };
146
146
  }
147
+ /**
148
+ * Richer per-file activity (commits, lines added/deleted, distinct authors).
149
+ *
150
+ * Fail-soft and shallow-aware like {@link churn}. Does not follow renames.
151
+ * Suitable for hotspot ranking; existing {@link churn} callers stay on commit counts.
152
+ *
153
+ * @param projectPath - Project root to query.
154
+ * @param opts - Optional `since` window and `pathspec` filter.
155
+ */
156
+ export function fileActivity(projectPath, opts = {}) {
157
+ const shallow = isShallowRepo(projectPath);
158
+ // Per commit: \0<author>\0 then numstat lines until the next leading NUL.
159
+ const args = ["log", "--numstat", "--format=%x00%an%x00"];
160
+ if (opts.since)
161
+ args.push(`--since=${opts.since}`);
162
+ if (opts.pathspec && opts.pathspec.length > 0)
163
+ args.push("--", ...opts.pathspec);
164
+ const out = git(projectPath, args);
165
+ const byPath = new Map();
166
+ const authorsByPath = new Map();
167
+ if (out === null)
168
+ return { shallow, byPath };
169
+ const records = out.split(NUL);
170
+ for (let i = 1; i + 1 < records.length; i += 2) {
171
+ const author = (records[i] ?? "").trim();
172
+ const tail = records[i + 1] ?? "";
173
+ if (!author && !tail.trim())
174
+ continue;
175
+ for (const line of tail.split("\n")) {
176
+ const cols = line.split("\t");
177
+ if (cols.length < 3)
178
+ continue;
179
+ const path = cols[2].replace(/^\0+/, "").trim();
180
+ if (!path)
181
+ continue;
182
+ const added = numstat(cols[0]);
183
+ const deleted = numstat(cols[1]);
184
+ const cur = byPath.get(path) ?? { commits: 0, linesAdded: 0, linesDeleted: 0, authors: 0 };
185
+ cur.commits += 1;
186
+ cur.linesAdded += added;
187
+ cur.linesDeleted += deleted;
188
+ byPath.set(path, cur);
189
+ if (author) {
190
+ let set = authorsByPath.get(path);
191
+ if (!set) {
192
+ set = new Set();
193
+ authorsByPath.set(path, set);
194
+ }
195
+ set.add(author);
196
+ }
197
+ }
198
+ }
199
+ for (const [path, act] of byPath) {
200
+ act.authors = authorsByPath.get(path)?.size ?? 0;
201
+ }
202
+ return { shallow, byPath };
203
+ }
204
+ /**
205
+ * Jaccard-style coupling strength: `both / (commitsA + commitsB - both)`.
206
+ * Returns `0` when the denominator is zero.
207
+ */
208
+ export function jaccardStrength(both, commitsA, commitsB) {
209
+ const denom = commitsA + commitsB - both;
210
+ if (denom <= 0)
211
+ return 0;
212
+ return both / denom;
213
+ }
147
214
  /**
148
215
  * For every pair of files that changed together, how many commits touched both.
149
216
  *
150
217
  * Groups each commit's changed files and emits a count per unordered pair. Pairs
151
- * with fewer than `minSupport` shared commits are omitted. Fail-soft (empty on
152
- * git failure) and does not follow renames. Carries the shallow marker.
218
+ * with fewer than `minSupport` shared commits are omitted. Commits that touch
219
+ * more than `maxFilesPerCommit` files (when set) are skipped and counted in
220
+ * `skippedTooLarge`. Fail-soft (empty on git failure) and does not follow
221
+ * renames. Carries the shallow marker.
153
222
  *
154
223
  * @param projectPath - Project root to query.
155
- * @param opts - Optional `since` window and `minSupport` threshold (default `1`).
224
+ * @param opts - Optional `since` window, `minSupport` (default `1`), and
225
+ * `maxFilesPerCommit` (omit to keep all commits).
156
226
  * @returns The qualifying co-change pairs and the shallow marker.
157
227
  */
158
228
  export function coChanges(projectPath, opts = {}) {
159
229
  const shallow = isShallowRepo(projectPath);
160
230
  const minSupport = opts.minSupport ?? 1;
231
+ const maxFiles = opts.maxFilesPerCommit;
161
232
  const args = ["log", "--name-only", "--format=%x00"];
162
233
  if (opts.since)
163
234
  args.push(`--since=${opts.since}`);
164
235
  const out = git(projectPath, args);
165
236
  if (out === null)
166
- return { shallow, pairs: [] };
237
+ return { shallow, pairs: [], skippedTooLarge: 0, commitsScanned: 0 };
167
238
  const counts = new Map();
239
+ let skippedTooLarge = 0;
240
+ let commitsScanned = 0;
168
241
  // Each commit's file list is the run of lines between two %x00 markers.
169
242
  for (const commitBlock of out.split(NUL)) {
170
243
  const files = commitBlock
171
244
  .split("\n")
172
245
  .map((l) => l.trim())
173
246
  .filter((l) => l.length > 0);
247
+ if (files.length === 0)
248
+ continue;
174
249
  const unique = [...new Set(files)].sort();
250
+ if (maxFiles !== undefined && unique.length > maxFiles) {
251
+ skippedTooLarge++;
252
+ continue;
253
+ }
254
+ commitsScanned++;
175
255
  for (let i = 0; i < unique.length; i++) {
176
256
  for (let j = i + 1; j < unique.length; j++) {
177
257
  const key = `${unique[i]}\t${unique[j]}`;
@@ -187,7 +267,7 @@ export function coChanges(projectPath, opts = {}) {
187
267
  pairs.push({ a: a, b: b, count });
188
268
  }
189
269
  pairs.sort((x, y) => y.count - x.count || x.a.localeCompare(y.a) || x.b.localeCompare(y.b));
190
- return { shallow, pairs };
270
+ return { shallow, pairs, skippedTooLarge, commitsScanned };
191
271
  }
192
272
  /**
193
273
  * The SHA of the most recent commit that touched `relPath`, or `null` when the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esneiderbravo/speclaw",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },