From db1bc26fa5c52f4f78016018d30b21aab09a8ded Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Sat, 15 Sep 2018 21:23:23 -0400 Subject: [PATCH 1/2] refactor: git logic to separate file --- src/extension.ts | 90 +++--------------------------------------------- src/git.ts | 77 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 85 deletions(-) create mode 100644 src/git.ts diff --git a/src/extension.ts b/src/extension.ts index 9759df9c..244fcb6a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,92 +1,12 @@ 'use strict' -import execa from 'execa' + import opn from 'opn' -import * as path from 'path' import * as vscode from 'vscode' +import { getSourcegraphUrl } from './config' +import { repoInfo } from './git' const VERSION = require('../package.json').version -/** - * Returns the names of all git remotes, e.g. ["origin", "foobar"] - */ -async function gitRemotes(repoDir: string): Promise { - const { stdout } = await execa('git', ['remote'], { cwd: repoDir }) - return stdout.split('\n') -} - -/** - * Returns the remote URL for the given remote name. - * e.g. `origin` -> `git@github.com:foo/bar` - */ -async function gitRemoteURL(repoDir: string, remoteName: string): Promise { - const { stdout } = await execa('git', ['remote', 'get-url', remoteName], { cwd: repoDir }) - return stdout -} - -/** - * Returns the remote URL of the first Git remote found. - */ -async function gitDefaultRemoteURL(repoDir: string): Promise { - const remotes = await gitRemotes(repoDir) - if (remotes.length === 0) { - throw new Error('no configured git remotes') - } - if (remotes.length > 1) { - console.log('using first git remote:', remotes[0]) - } - return await gitRemoteURL(repoDir, remotes[0]) -} - -/** - * Returns the repository root directory for any directory within the - * repository. - */ -async function gitRootDir(repoDir: string): Promise { - const { stdout } = await execa('git', ['rev-parse', '--show-toplevel'], { cwd: repoDir }) - return stdout -} - -/** - * Returns either the current branch name of the repository OR in all - * other cases (e.g. detached HEAD state), it returns "HEAD". - */ -async function gitBranch(repoDir: string): Promise { - const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repoDir }) - return stdout -} - -function sourcegraphURL(): string { - const url = vscode.workspace.getConfiguration('sourcegraph').get('url')! // has default value - if (!url.endsWith('/')) { - return url + '/' - } - return url -} - -/** - * Returns the Sourcegraph repository URI, and the file path relative - * to the repository root. If the repository URI cannot be determined, empty - * strings are returned. - */ -async function repoInfo(fileName: string): Promise<[string, string, string]> { - let remoteURL = '' - let branch = '' - let fileRel = '' - try { - // Determine repository root directory. - const fileDir = path.dirname(fileName) - const repoRoot = await gitRootDir(fileDir) - - // Determine file path, relative to repository root. - fileRel = fileName.slice(repoRoot.length + 1) - remoteURL = await gitDefaultRemoteURL(repoRoot) - branch = await gitBranch(repoRoot) - } catch (e) { - console.log('repoInfo:', e) - } - return [remoteURL, branch, fileRel] -} - /** * Displays an error message to the user. */ @@ -117,7 +37,7 @@ async function openCommand(): Promise { // Open in browser. await opn( - `${sourcegraphURL()}-/editor` + + `${getSourcegraphUrl()}-/editor` + `?remote_url=${encodeURIComponent(remoteURL)}` + `&branch=${encodeURIComponent(branch)}` + `&file=${encodeURIComponent(fileRel)}` + @@ -147,7 +67,7 @@ async function searchCommand(): Promise { // Search in browser. await opn( - `${sourcegraphURL()}-/editor` + + `${getSourcegraphUrl()}-/editor` + `?remote_url=${encodeURIComponent(remoteURL)}` + `&branch=${encodeURIComponent(branch)}` + `&file=${encodeURIComponent(fileRel)}` + diff --git a/src/git.ts b/src/git.ts new file mode 100644 index 00000000..b16f8376 --- /dev/null +++ b/src/git.ts @@ -0,0 +1,77 @@ +'use strict' + +import execa from 'execa' +import * as path from 'path' + +/** + * Returns the names of all git remotes, e.g. ["origin", "foobar"] + */ +async function gitRemotes(repoDir: string): Promise { + const { stdout } = await execa('git', ['remote'], { cwd: repoDir }) + return stdout.split('\n') +} + +/** + * Returns the remote URL for the given remote name. + * e.g. `origin` -> `git@github.com:foo/bar` + */ +async function gitRemoteURL(repoDir: string, remoteName: string): Promise { + const { stdout } = await execa('git', ['remote', 'get-url', remoteName], { cwd: repoDir }) + return stdout +} + +/** + * Returns the remote URL of the first Git remote found. + */ +async function gitDefaultRemoteURL(repoDir: string): Promise { + const remotes = await gitRemotes(repoDir) + if (remotes.length === 0) { + throw new Error('no configured git remotes') + } + if (remotes.length > 1) { + console.log('using first git remote:', remotes[0]) + } + return await gitRemoteURL(repoDir, remotes[0]) +} + +/** + * Returns the repository root directory for any directory within the + * repository. + */ +async function gitRootDir(repoDir: string): Promise { + const { stdout } = await execa('git', ['rev-parse', '--show-toplevel'], { cwd: repoDir }) + return stdout +} + +/** + * Returns either the current branch name of the repository OR in all + * other cases (e.g. detached HEAD state), it returns "HEAD". + */ +async function gitBranch(repoDir: string): Promise { + const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repoDir }) + return stdout +} + +/** + * Returns the Sourcegraph repository URI, and the file path relative + * to the repository root. If the repository URI cannot be determined, empty + * strings are returned. + */ +export async function repoInfo(fileName: string): Promise<[string, string, string]> { + let remoteURL = '' + let branch = '' + let fileRel = '' + try { + // Determine repository root directory. + const fileDir = path.dirname(fileName) + const repoRoot = await gitRootDir(fileDir) + + // Determine file path, relative to repository root. + fileRel = fileName.slice(repoRoot.length + 1) + remoteURL = await gitDefaultRemoteURL(repoRoot) + branch = await gitBranch(repoRoot) + } catch (e) { + console.log('repoInfo:', e) + } + return [remoteURL, branch, fileRel] +} From 0f88c440fc4315a105752560098543c26d41186e Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Sat, 15 Sep 2018 21:26:46 -0400 Subject: [PATCH 2/2] refactor: add config --- src/config.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/config.ts diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 00000000..af98c606 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,11 @@ +'use strict' + +import vscode from 'vscode' + +export function getSourcegraphUrl(): string { + const url = vscode.workspace.getConfiguration('sourcegraph').get('url')! // has default value + if (url.endsWith('/')) { + return url.slice(0, -1) + } + return url +}