Skip to content

Commit c0ca28f

Browse files
committed
Keep remote comparisons current
1 parent 1fc9bcc commit c0ca28f

2 files changed

Lines changed: 348 additions & 13 deletions

File tree

‎scripts/build-diff-data.mjs‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ const cacheRoot = cacheOption
8080
const remoteMode = Boolean(prOption || branchOption);
8181
const watching = has('--watch');
8282
const ignoreSummaryWatch = has('--ignore-summary-watch');
83+
const interval = (name, fallback) => {
84+
const value = Number(process.env[name]);
85+
return Number.isFinite(value) && value > 0 ? value : fallback;
86+
};
87+
const watchInterval = interval('DIFFSPLAIN_WATCH_INTERVAL_MS', 2_000);
88+
const remoteRefreshInterval = interval(
89+
'DIFFSPLAIN_REMOTE_REFRESH_INTERVAL_MS',
90+
30_000,
91+
);
8392

8493
if (prOption && branchOption) fail('--pr and --branch cannot be used together');
8594
if (prOption && (baseOption || headOption)) fail('--pr cannot be used with --base or --head');
@@ -311,12 +320,24 @@ function normalizeBranch(value, remoteName) {
311320
}
312321

313322
function resolveRemote() {
314-
const configured = tryRepo(['remote', 'get-url', remoteOption]);
315-
if (configured) return { name: remoteOption, url: configured };
323+
const configured = tryRepo([
324+
'config',
325+
'--get-all',
326+
`remote.${remoteOption}.url`,
327+
])
328+
.split('\n')
329+
.find(Boolean);
330+
if (configured) {
331+
return {
332+
name: remoteOption,
333+
url: configured,
334+
fetchUrl: tryRepo(['remote', 'get-url', remoteOption]) || configured,
335+
};
336+
}
316337
if (remoteOption === 'origin') {
317338
throw new Error(`Git remote "origin" was not found in ${repo}`);
318339
}
319-
return { name: remoteOption, url: remoteOption };
340+
return { name: remoteOption, url: remoteOption, fetchUrl: remoteOption };
320341
}
321342

322343
function bareCache(remoteUrl) {
@@ -506,7 +527,7 @@ function resolveBranchTarget() {
506527
const remote = resolveRemote();
507528
const branch = normalizeBranch(branchOption, remote.name);
508529
const baseBranch = normalizeBranch(
509-
baseOption || remoteDefaultBranch(remote.url),
530+
baseOption || remoteDefaultBranch(remote.fetchUrl),
510531
remote.name,
511532
);
512533
const cache = bareCache(remote.url);
@@ -516,7 +537,7 @@ function resolveBranchTarget() {
516537
.slice(0, 16);
517538
const baseRef = `refs/diffsplain/branch/${key}/base`;
518539
const headRef = `refs/diffsplain/branch/${key}/head`;
519-
fetchInto(cache, remote.url, [
540+
fetchInto(cache, remote.fetchUrl, [
520541
`+refs/heads/${baseBranch}:${baseRef}`,
521542
`+refs/heads/${branch}:${headRef}`,
522543
]);
@@ -562,7 +583,7 @@ function resolvePullRequestTarget() {
562583
.slice(0, 16);
563584
const baseRef = `refs/diffsplain/pr/${key}/base`;
564585
const headRef = `refs/diffsplain/pr/${key}/head`;
565-
fetchInto(cache, remote.url, [
586+
fetchInto(cache, remote.fetchUrl, [
566587
`+refs/heads/${pr.baseRefName}:${baseRef}`,
567588
`+refs/pull/${pr.number}/head:${headRef}`,
568589
]);
@@ -1026,9 +1047,29 @@ function fingerprint() {
10261047
summariesTime,
10271048
].join('|');
10281049
}
1050+
const content = createHash('sha256');
1051+
content.update(
1052+
tryRepo(['diff', '--no-ext-diff', '--binary', 'HEAD', '--']),
1053+
);
1054+
const untracked = tryRepo([
1055+
'ls-files',
1056+
'--others',
1057+
'--exclude-standard',
1058+
'-z',
1059+
])
1060+
.split('\0')
1061+
.filter((path) => path && !excludedPaths.has(path))
1062+
.sort();
1063+
for (const path of untracked) {
1064+
content.update('\0');
1065+
content.update(path);
1066+
content.update('\0');
1067+
content.update(readFileSync(resolve(repo, path)));
1068+
}
10291069
return [
10301070
tryRepo(['rev-parse', 'HEAD']),
10311071
tryRepo(['status', '--porcelain=v1', '--untracked-files=all']),
1072+
content.digest('hex'),
10321073
summariesTime,
10331074
].join('|');
10341075
}
@@ -1040,7 +1081,7 @@ const refresh = () => {
10401081
return true;
10411082
} catch (error) {
10421083
console.error(error.message);
1043-
if (!watching) process.exitCode = 1;
1084+
process.exitCode = 1;
10441085
return false;
10451086
}
10461087
};
@@ -1049,16 +1090,16 @@ const started = refresh();
10491090
if (watching && started) {
10501091
let last = fingerprint();
10511092
let remoteWait = 0;
1052-
setInterval(() => {
1093+
const watcher = setInterval(() => {
10531094
const next = fingerprint();
1054-
remoteWait += 2_000;
1055-
const remoteDue = remoteMode && remoteWait >= 30_000;
1095+
remoteWait += watchInterval;
1096+
const remoteDue = remoteMode && remoteWait >= remoteRefreshInterval;
10561097
if (next !== last || remoteDue) {
10571098
last = next;
10581099
remoteWait = 0;
1059-
refresh();
1100+
if (!refresh()) clearInterval(watcher);
10601101
}
1061-
}, 2_000);
1102+
}, watchInterval);
10621103
} else if (watching) {
10631104
process.exitCode = 1;
10641105
}

0 commit comments

Comments
 (0)