1 | import HostedGitInfo from 'hosted-git-info';
|
2 | import * as path from 'path';
|
3 | import { TaggedGitRepository } from '../types/tagged-git-repository';
|
4 | import { toForwardSlashes } from '../utils/to-forward-slashes';
|
5 |
|
6 | export type RepositoryFileURLProvider = ({
|
7 | filename,
|
8 | line,
|
9 | }: {
|
10 | filename: string;
|
11 | line?: number;
|
12 | }) => string | undefined;
|
13 |
|
14 | export function getRepositoryFileURLProvider({
|
15 | repository,
|
16 | }: {
|
17 | repository?: TaggedGitRepository;
|
18 | }): RepositoryFileURLProvider {
|
19 | if (!repository) {
|
20 | return () => undefined;
|
21 | }
|
22 |
|
23 | const { url, tag = '', dir = '' } = repository;
|
24 | const info = HostedGitInfo.fromUrl(url, { noGitPlus: true });
|
25 | if (!info) {
|
26 | return () => undefined;
|
27 | }
|
28 |
|
29 | const linePrefix = getLinePrefix({ info });
|
30 |
|
31 | return ({ filename, line }) => {
|
32 | const filepath = toForwardSlashes(path.join(dir, filename));
|
33 | const fileURL = info.browse(filepath, { committish: tag });
|
34 | const lineFragment = line ? `${linePrefix}${line}` : '';
|
35 | return `${fileURL}${lineFragment}`;
|
36 | };
|
37 | }
|
38 |
|
39 | function getLinePrefix({ info }: { info: HostedGitInfo }): string {
|
40 | const { type: provider } = info;
|
41 | switch (provider) {
|
42 | case 'bitbucket':
|
43 | return '#lines-';
|
44 | default:
|
45 | return '#L';
|
46 | }
|
47 | }
|