Skip to content

Commit cf80261

Browse files
committed
Add gitRevision:short option
Resolves #2529
1 parent e4fe39f commit cf80261

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Unreleased
22

3+
### Features
4+
5+
- Added `gitRevision:short` placeholder option to `--sourceLinkTemplate` option, #2529.
6+
Links generated by TypeDoc will now default to using the non-short git revision.
7+
8+
### Thanks!
9+
10+
- @xuhdev
11+
312
## v0.25.12 (2024-03-10)
413

514
### Features

src/lib/converter/utils/repository.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export class AssumedRepository implements Repository {
3232
getURL(fileName: string, line: number): string | undefined {
3333
const replacements = {
3434
gitRevision: this.gitRevision,
35+
"gitRevision:short": this.gitRevision.substring(0, 8),
3536
path: fileName.substring(this.path.length + 1),
3637
line,
3738
};
3839

3940
return this.sourceLinkTemplate.replace(
40-
/\{(gitRevision|path|line)\}/g,
41+
/\{(gitRevision|gitRevision:short|path|line)\}/g,
4142
(_, key) => replacements[key as never],
4243
);
4344
}
@@ -93,12 +94,13 @@ export class GitRepository implements Repository {
9394

9495
const replacements = {
9596
gitRevision: this.gitRevision,
97+
"gitRevision:short": this.gitRevision.substring(0, 8),
9698
path: fileName.substring(this.path.length + 1),
9799
line,
98100
};
99101

100102
return this.urlTemplate.replace(
101-
/\{(gitRevision|path|line)\}/g,
103+
/\{(gitRevision|gitRevision:short|path|line)\}/g,
102104
(_, key) => replacements[key as never],
103105
);
104106
}
@@ -122,13 +124,7 @@ export class GitRepository implements Repository {
122124
const topLevel = git("-C", path, "rev-parse", "--show-toplevel");
123125
if (topLevel.status !== 0) return;
124126

125-
gitRevision ||= git(
126-
"-C",
127-
path,
128-
"rev-parse",
129-
"--short",
130-
"HEAD",
131-
).stdout.trim();
127+
gitRevision ||= git("-C", path, "rev-parse", "HEAD").stdout.trim();
132128
if (!gitRevision) return; // Will only happen in a repo with no commits.
133129

134130
let urlTemplate: string | undefined;

src/test/Repository.test.ts

+61-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { guessSourceUrlTemplate } from "../lib/converter/utils/repository";
2-
import { strictEqual as equal } from "assert";
1+
import {
2+
GitRepository,
3+
guessSourceUrlTemplate,
4+
} from "../lib/converter/utils/repository";
5+
import { strictEqual as equal, ok } from "assert";
6+
import { tempdirProject } from "@typestrong/fs-fixture-builder";
7+
import { spawnSync } from "child_process";
8+
import { TestLogger } from "./TestLogger";
9+
import { join } from "path";
310

411
describe("Repository", function () {
512
describe("guessSourceUrlTemplate helper", () => {
@@ -99,4 +106,56 @@ describe("Repository", function () {
99106
equal(guessSourceUrlTemplate(mockRemotes), undefined);
100107
});
101108
});
109+
110+
describe("getURL", () => {
111+
const project = tempdirProject();
112+
function git(...args: string[]) {
113+
const env = {
114+
GIT_AUTHOR_NAME: "test",
115+
GIT_AUTHOR_EMAIL: "[email protected]",
116+
GIT_AUTHOR_DATE: "2024-03-31T22:04:50.119Z",
117+
GIT_COMMITTER_NAME: "test",
118+
GIT_COMMITTER_EMAIL: "[email protected]",
119+
GIT_COMMITTER_DATE: "2024-03-31T22:04:50.119Z",
120+
};
121+
return spawnSync("git", ["-C", project.cwd, ...args], {
122+
encoding: "utf-8",
123+
windowsHide: true,
124+
env,
125+
});
126+
}
127+
128+
afterEach(() => {
129+
project.rm();
130+
});
131+
132+
it("Handles replacements", function () {
133+
project.addFile("test.js", "console.log('hi!')");
134+
project.write();
135+
136+
git("init", "-b", "test");
137+
git("add", ".");
138+
git("commit", "-m", "Test commit");
139+
git(
140+
"remote",
141+
"add",
142+
"origin",
143+
"[email protected]:TypeStrong/typedoc.git",
144+
);
145+
146+
const repo = GitRepository.tryCreateRepository(
147+
project.cwd,
148+
"{gitRevision}/{gitRevision:short}/{path}/{line}",
149+
"", // revision, empty to get from repo
150+
"origin", // remote
151+
new TestLogger(),
152+
);
153+
154+
ok(repo);
155+
equal(
156+
repo.getURL(join(project.cwd, "test.js"), 1),
157+
"b53cc55bcdd9bc5920787a1d4a4a15fa24123b04/b53cc55b/test.js/1",
158+
);
159+
});
160+
});
102161
});

0 commit comments

Comments
 (0)