async function getActiveFile()

in src/desktop/commands/openers.ts [72:106]


async function getActiveFile({
  projectInRepository,
  activeEditor,
}: ProjectInRepositoryAndFile): Promise<string> {
  const { repository } = projectInRepository.pointer;

  const repoPath = repository.rootFsPath;
  const editorPath = activeEditor.document.uri.fsPath;
  if (!editorPath.startsWith(repoPath)) {
    throw new WarningError('The current file is not in the project repository.');
  }
  const filePath = path.relative(repoPath, editorPath).replace(/\\/g, '/');

  const log = await repository.rawRepository.log({ maxEntries: 1, path: filePath });

  if (log.length === 0) {
    throw new WarningError(
      'No link exists for the current file. Commit the current file to the repository.',
    );
  }
  const { project } = projectInRepository;
  const fileUrl = `${project.webUrl}/-/blob/${encodeURIComponent(log[0].hash)}/${filePath}`;
  let anchor = '';

  if (activeEditor.selection) {
    const { start, end } = activeEditor.selection;
    anchor = `#L${start.line + 1}`;

    if (end.line > start.line) {
      anchor += `-${end.line + 1}`;
    }
  }

  return `${fileUrl}${anchor}`;
}