This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
refactor: git and config logic to separate file #16
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| 'use strict' | ||
|
|
||
| import vscode from 'vscode' | ||
|
|
||
| export function getSourcegraphUrl(): string { | ||
| const url = vscode.workspace.getConfiguration('sourcegraph').get<string>('url')! // has default value | ||
| if (url.endsWith('/')) { | ||
| return url.slice(0, -1) | ||
| } | ||
| return url | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string[]> { | ||
| 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<string> { | ||
| 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<string> { | ||
| 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<string> { | ||
| 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<string> { | ||
| 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] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slimsag @felixfbecker is this useful/necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://stackoverflow.com/questions/31391760/use-strict-needed-in-a-typescript-file
We should enable the always strict TS compiler flag I guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#17