CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | /**
* Time-Travel Code Explorer
*
* GitHub shows you blame (who changed each line).
* gluecron shows you the STORY of your code:
* - How did this function evolve?
* - When was this behavior introduced?
* - What was the context of each change?
*
* This answers the question developers actually ask:
* "WHY is this code like this?"
*/
import { getRepoPath, getDefaultBranch } from "../git/repository";
export interface FileTimeline {
path: string;
totalRevisions: number;
firstSeen: { sha: string; date: string; author: string; message: string };
lastModified: { sha: string; date: string; author: string; message: string };
revisions: FileRevision[];
}
export interface FileRevision {
sha: string;
date: string;
author: string;
message: string;
linesAdded: number;
linesRemoved: number;
sizeAfter: number;
}
export interface FunctionTimeline {
name: string;
file: string;
firstSeen: { sha: string; date: string; author: string };
revisions: FunctionRevision[];
currentSignature: string;
}
export interface FunctionRevision {
sha: string;
date: string;
author: string;
message: string;
changeType: "created" | "modified" | "renamed" | "signature-changed";
snippet: string;
}
async function exec(
cmd: string[],
cwd: string
): Promise<{ stdout: string; exitCode: number }> {
const proc = Bun.spawn(cmd, {
cwd,
stdout: "pipe",
stderr: "pipe",
});
const stdout = await new Response(proc.stdout).text();
const exitCode = await proc.exited;
return { stdout: stdout.trim(), exitCode };
}
/**
* Get the complete evolution history of a file.
* Every commit that touched it, with stats.
*/
export async function getFileTimeline(
owner: string,
repo: string,
ref: string,
filePath: string
): Promise<FileTimeline | null> {
const repoDir = getRepoPath(owner, repo);
// Get all commits that touched this file
const { stdout, exitCode } = await exec(
[
"git",
"log",
"--follow",
"--format=%H%x00%aI%x00%an%x00%s",
"--numstat",
ref,
"--",
filePath,
],
repoDir
);
if (exitCode !== 0 || !stdout) return null;
const revisions: FileRevision[] = [];
const lines = stdout.split("\n");
let i = 0;
while (i < lines.length) {
const line = lines[i];
if (!line) {
i++;
continue;
}
const parts = line.split("\0");
if (parts.length < 4) {
i++;
continue;
}
const [sha, date, author, message] = parts;
let linesAdded = 0;
let linesRemoved = 0;
i++;
// Read numstat line (may be on next non-empty line)
while (i < lines.length && lines[i] === "") i++;
if (i < lines.length) {
const statLine = lines[i];
const statMatch = statLine.match(/^(\d+|-)\t(\d+|-)\t/);
if (statMatch) {
linesAdded = statMatch[1] === "-" ? 0 : parseInt(statMatch[1], 10);
linesRemoved = statMatch[2] === "-" ? 0 : parseInt(statMatch[2], 10);
i++;
}
}
// Get file size at this commit
const { stdout: sizeStr } = await exec(
["git", "cat-file", "-s", `${sha}:${filePath}`],
repoDir
);
const sizeAfter = parseInt(sizeStr, 10) || 0;
revisions.push({
sha,
date,
author,
message,
linesAdded,
linesRemoved,
sizeAfter,
});
}
if (revisions.length === 0) return null;
return {
path: filePath,
totalRevisions: revisions.length,
firstSeen: {
sha: revisions[revisions.length - 1].sha,
date: revisions[revisions.length - 1].date,
author: revisions[revisions.length - 1].author,
message: revisions[revisions.length - 1].message,
},
lastModified: {
sha: revisions[0].sha,
date: revisions[0].date,
author: revisions[0].author,
message: revisions[0].message,
},
revisions,
};
}
/**
* Track the evolution of a specific function/symbol in a file.
* Uses git log -L to trace function history.
*/
export async function getFunctionTimeline(
owner: string,
repo: string,
ref: string,
filePath: string,
functionName: string
): Promise<FunctionTimeline | null> {
const repoDir = getRepoPath(owner, repo);
// Use git log -L to trace function evolution
// -L :functionName:filePath traces the function
const { stdout, exitCode } = await exec(
[
"git",
"log",
`-L:${functionName}:${filePath}`,
"--format=%H%x00%aI%x00%an%x00%s",
"--no-patch",
ref,
],
repoDir
);
if (exitCode !== 0 || !stdout) {
// Fallback: search for the function name in git log
return getFunctionTimelineFallback(
owner,
repo,
ref,
filePath,
functionName
);
}
const revisions: FunctionRevision[] = [];
for (const line of stdout.split("\n").filter(Boolean)) {
const parts = line.split("\0");
if (parts.length < 4) continue;
const [sha, date, author, message] = parts;
// Get the function snippet at this commit
const { stdout: content } = await exec(
["git", "show", `${sha}:${filePath}`],
repoDir
);
const snippet = extractFunctionSnippet(content, functionName);
revisions.push({
sha,
date,
author,
message,
changeType:
revisions.length === 0 ? "created" : "modified",
snippet: snippet.slice(0, 500),
});
}
if (revisions.length === 0) return null;
// Get current signature
const { stdout: currentContent } = await exec(
["git", "show", `${ref}:${filePath}`],
repoDir
);
const currentSignature = extractFunctionSignature(
currentContent,
functionName
);
return {
name: functionName,
file: filePath,
firstSeen: {
sha: revisions[revisions.length - 1].sha,
date: revisions[revisions.length - 1].date,
author: revisions[revisions.length - 1].author,
},
revisions,
currentSignature,
};
}
async function getFunctionTimelineFallback(
owner: string,
repo: string,
ref: string,
filePath: string,
functionName: string
): Promise<FunctionTimeline | null> {
const repoDir = getRepoPath(owner, repo);
// Get commits where this function name appears in the diff
const { stdout } = await exec(
[
"git",
"log",
"--format=%H%x00%aI%x00%an%x00%s",
`-S${functionName}`,
ref,
"--",
filePath,
],
repoDir
);
if (!stdout) return null;
const revisions: FunctionRevision[] = [];
for (const line of stdout.split("\n").filter(Boolean)) {
const parts = line.split("\0");
if (parts.length < 4) continue;
const [sha, date, author, message] = parts;
revisions.push({
sha,
date,
author,
message,
changeType: revisions.length === 0 ? "created" : "modified",
snippet: "",
});
}
if (revisions.length === 0) return null;
const { stdout: currentContent } = await exec(
["git", "show", `${ref}:${filePath}`],
repoDir
);
return {
name: functionName,
file: filePath,
firstSeen: {
sha: revisions[revisions.length - 1].sha,
date: revisions[revisions.length - 1].date,
author: revisions[revisions.length - 1].author,
},
revisions,
currentSignature: extractFunctionSignature(currentContent, functionName),
};
}
/**
* Detect hotspots — files that change together frequently.
* If file A and file B always change in the same commit,
* they're coupled. This catches architectural issues.
*/
export async function detectCoupledFiles(
owner: string,
repo: string,
ref: string,
limit = 20
): Promise<Array<{ files: [string, string]; cochanges: number; percentage: number }>> {
const repoDir = getRepoPath(owner, repo);
// Get recent commits with their changed files
const { stdout } = await exec(
[
"git",
"log",
"--format=%H",
"--name-only",
"-100",
ref,
],
repoDir
);
const commits: string[][] = [];
let current: string[] = [];
for (const line of stdout.split("\n")) {
if (line.match(/^[0-9a-f]{40}$/)) {
if (current.length > 0) commits.push(current);
current = [];
} else if (line.trim()) {
current.push(line.trim());
}
}
if (current.length > 0) commits.push(current);
// Count co-changes
const pairCounts: Record<string, number> = {};
const fileCounts: Record<string, number> = {};
for (const files of commits) {
for (const f of files) {
fileCounts[f] = (fileCounts[f] || 0) + 1;
}
// Count pairs
for (let i = 0; i < files.length; i++) {
for (let j = i + 1; j < files.length; j++) {
const pair = [files[i], files[j]].sort().join("|||");
pairCounts[pair] = (pairCounts[pair] || 0) + 1;
}
}
}
return Object.entries(pairCounts)
.filter(([, count]) => count >= 3) // At least 3 co-changes
.sort((a, b) => b[1] - a[1])
.slice(0, limit)
.map(([pair, count]) => {
const [f1, f2] = pair.split("|||");
const maxChanges = Math.max(fileCounts[f1] || 0, fileCounts[f2] || 0);
return {
files: [f1, f2] as [string, string],
cochanges: count,
percentage: maxChanges > 0 ? Math.round((count / maxChanges) * 100) : 0,
};
});
}
/**
* Get the "story" of a repository — key milestones,
* major changes, turning points.
*/
export async function getRepoStory(
owner: string,
repo: string,
ref: string
): Promise<Array<{
sha: string;
date: string;
author: string;
message: string;
significance: "milestone" | "major" | "normal";
stats: { files: number; additions: number; deletions: number };
}>> {
const repoDir = getRepoPath(owner, repo);
const { stdout } = await exec(
[
"git",
"log",
"--format=%H%x00%aI%x00%an%x00%s",
"--shortstat",
ref,
],
repoDir
);
const entries: Array<{
sha: string;
date: string;
author: string;
message: string;
significance: "milestone" | "major" | "normal";
stats: { files: number; additions: number; deletions: number };
}> = [];
const lines = stdout.split("\n");
let i = 0;
while (i < lines.length) {
const line = lines[i];
if (!line) {
i++;
continue;
}
const parts = line.split("\0");
if (parts.length < 4) {
i++;
continue;
}
const [sha, date, author, message] = parts;
let files = 0;
let additions = 0;
let deletions = 0;
i++;
// Read stat line
while (i < lines.length && lines[i] === "") i++;
if (i < lines.length) {
const statMatch = lines[i].match(
/(\d+) files? changed(?:, (\d+) insertions?)?(?:, (\d+) deletions?)?/
);
if (statMatch) {
files = parseInt(statMatch[1], 10) || 0;
additions = parseInt(statMatch[2], 10) || 0;
deletions = parseInt(statMatch[3], 10) || 0;
i++;
}
}
// Determine significance
let significance: "milestone" | "major" | "normal" = "normal";
const lowerMsg = message.toLowerCase();
if (
lowerMsg.includes("v1") ||
lowerMsg.includes("v2") ||
lowerMsg.includes("release") ||
lowerMsg.includes("launch") ||
lowerMsg.includes("initial commit") ||
lowerMsg.match(/v\d+\.\d+/)
) {
significance = "milestone";
} else if (
files > 20 ||
additions + deletions > 1000 ||
lowerMsg.includes("refactor") ||
lowerMsg.includes("breaking") ||
lowerMsg.includes("migration") ||
lowerMsg.includes("major")
) {
significance = "major";
}
entries.push({
sha,
date,
author,
message,
significance,
stats: { files, additions, deletions },
});
}
return entries;
}
// ─── Helpers ─────────────────────────────────────────────────
function extractFunctionSnippet(
content: string,
functionName: string
): string {
const lines = content.split("\n");
const regex = new RegExp(
`(?:export\\s+)?(?:async\\s+)?(?:function\\s+${functionName}|const\\s+${functionName}\\s*=|${functionName}\\s*[:(])`,
);
for (let i = 0; i < lines.length; i++) {
if (regex.test(lines[i])) {
// Get function body (up to 20 lines)
return lines.slice(i, i + 20).join("\n");
}
}
return "";
}
function extractFunctionSignature(
content: string,
functionName: string
): string {
const lines = content.split("\n");
const regex = new RegExp(
`(?:export\\s+)?(?:async\\s+)?(?:function\\s+${functionName}|const\\s+${functionName}\\s*=)`,
);
for (const line of lines) {
if (regex.test(line)) {
return line.trim();
}
}
return `${functionName}(...)`;
}
|