diff --git a/src/commands/openInRemote.ts b/src/commands/openInRemote.ts index 4b78cbb..d99ffc0 100644 --- a/src/commands/openInRemote.ts +++ b/src/commands/openInRemote.ts @@ -3,7 +3,7 @@ import { Strings } from '../system'; import { TextEditor, Uri, window } from 'vscode'; import { ActiveEditorCommand, Commands, getCommandUri } from './common'; import { GlyphChars } from '../constants'; -import { GitLogCommit, GitRemote, RemoteResource } from '../gitService'; +import { GitLogCommit, GitRemote, GitService, RemoteResource } from '../gitService'; import { Logger } from '../logger'; import { CommandQuickPickItem, OpenRemoteCommandQuickPickItem, RemotesQuickPick } from '../quickPicks'; @@ -50,7 +50,7 @@ export class OpenInRemoteCommand extends ActiveEditorCommand { break; case 'commit': - const shortSha = args.resource.sha.substring(0, 8); + const shortSha = GitService.shortenSha(args.resource.sha); placeHolder = `open commit ${shortSha} in${GlyphChars.Ellipsis}`; break; @@ -70,7 +70,7 @@ export class OpenInRemoteCommand extends ActiveEditorCommand { } } else { - const shortFileSha = args.resource.sha === undefined ? '' : args.resource.sha.substring(0, 8); + const shortFileSha = args.resource.sha === undefined ? '' : GitService.shortenSha(args.resource.sha); const shaSuffix = shortFileSha ? ` ${Strings.pad(GlyphChars.Dot, 1, 1)} ${shortFileSha}` : ''; placeHolder = `open ${args.resource.fileName}${shaSuffix} in${GlyphChars.Ellipsis}`; diff --git a/src/commands/showQuickCommitFileDetails.ts b/src/commands/showQuickCommitFileDetails.ts index cd05583..f55aa9b 100644 --- a/src/commands/showQuickCommitFileDetails.ts +++ b/src/commands/showQuickCommitFileDetails.ts @@ -95,7 +95,7 @@ export class ShowQuickCommitFileDetailsCommand extends ActiveEditorCachedCommand args.commit.workingFileName = workingFileName; args.commit.workingFileName = await this.git.findWorkingFileName(args.commit); - const shortSha = args.sha!.substring(0, 8); + const shortSha = GitService.shortenSha(args.sha!); if (args.goBackCommand === undefined) { // Create a command to get back to the commit details diff --git a/src/git/git.ts b/src/git/git.ts index a79fa9d..e699ef9 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -99,7 +99,7 @@ export class Git { static async getVersionedFile(repoPath: string | undefined, fileName: string, branchOrSha: string) { const data = await Git.show(repoPath, fileName, branchOrSha, 'binary'); - const suffix = Git.isSha(branchOrSha) ? branchOrSha.substring(0, 8) : branchOrSha; + const suffix = Git.isSha(branchOrSha) ? Git.shortenSha(branchOrSha) : branchOrSha; const ext = path.extname(fileName); return new Promise((resolve, reject) => { tmp.file({ prefix: `${path.basename(fileName, ext)}-${suffix}__`, postfix: ext }, @@ -134,6 +134,10 @@ export class Git { return fileName && fileName.replace(/\\/g, '/'); } + static shortenSha(sha: string) { + return sha.substring(0, 8); + } + static splitPath(fileName: string, repoPath: string | undefined, extract: boolean = true): [string, string] { if (repoPath) { fileName = this.normalizePath(fileName); diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index 80e23b6..4cd964c 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -56,7 +56,7 @@ export class GitUri extends Uri { } get shortSha() { - return this.sha && this.sha.substring(0, 8); + return this.sha && GitService.shortenSha(this.sha); } fileUri() { diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index cbd106b..5c4b93b 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -51,7 +51,7 @@ export class GitCommit { } get shortSha() { - return this.sha.substring(0, 8); + return Git.shortenSha(this.sha); } get isUncommitted(): boolean { @@ -62,7 +62,7 @@ export class GitCommit { } get previousShortSha() { - return this.previousSha && this.previousSha.substring(0, 8); + return this.previousSha && Git.shortenSha(this.previousSha); } get previousUri(): Uri { diff --git a/src/git/models/logCommit.ts b/src/git/models/logCommit.ts index 86779e7..a141012 100644 --- a/src/git/models/logCommit.ts +++ b/src/git/models/logCommit.ts @@ -1,6 +1,7 @@ 'use strict'; import { Uri } from 'vscode'; import { GitCommit, GitCommitType } from './commit'; +import { Git } from '../git'; import { GitStatusFileStatus, IGitStatusFile } from './status'; import * as path from 'path'; @@ -49,7 +50,7 @@ export class GitLogCommit extends GitCommit { } get nextShortSha() { - return this.nextSha && this.nextSha.substring(0, 8); + return this.nextSha && Git.shortenSha(this.nextSha); } get nextUri(): Uri { diff --git a/src/gitContentProvider.ts b/src/gitContentProvider.ts index 1853f87..4683b8b 100644 --- a/src/gitContentProvider.ts +++ b/src/gitContentProvider.ts @@ -23,7 +23,7 @@ export class GitContentProvider implements TextDocumentContentProvider { } catch (ex) { Logger.error(ex, 'GitContentProvider', 'getVersionedFileText'); - window.showErrorMessage(`Unable to show Git revision ${data.sha.substring(0, 8)} of '${path.relative(data.repoPath, fileName)}'`); + window.showErrorMessage(`Unable to show Git revision ${GitService.shortenSha(data.sha)} of '${path.relative(data.repoPath, fileName)}'`); return undefined; } } diff --git a/src/gitService.ts b/src/gitService.ts index be8ec58..8d77d81 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -1061,6 +1061,10 @@ export class GitService extends Disposable { return Git.normalizePath(fileName, repoPath); } + static shortenSha(sha: string) { + return Git.shortenSha(sha); + } + static toGitContentUri(sha: string, shortSha: string, fileName: string, repoPath: string, originalFileName?: string): Uri; static toGitContentUri(commit: GitCommit): Uri; static toGitContentUri(uri: GitUri): Uri; diff --git a/src/quickPicks/remotes.ts b/src/quickPicks/remotes.ts index a405bd1..73f7f9a 100644 --- a/src/quickPicks/remotes.ts +++ b/src/quickPicks/remotes.ts @@ -4,7 +4,7 @@ import { QuickPickOptions, window } from 'vscode'; import { Commands, OpenInRemoteCommandArgs } from '../commands'; import { CommandQuickPickItem, getQuickPickIgnoreFocusOut } from './common'; import { GlyphChars } from '../constants'; -import { getNameFromRemoteResource, GitLogCommit, GitRemote, RemoteResource } from '../gitService'; +import { getNameFromRemoteResource, GitLogCommit, GitRemote, GitService, RemoteResource } from '../gitService'; import * as path from 'path'; export class OpenRemoteCommandQuickPickItem extends CommandQuickPickItem { @@ -43,7 +43,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { break; case 'commit': - const shortSha = resource.sha.substring(0, 8); + const shortSha = GitService.shortenSha(resource.sha); description = `$(git-commit) ${shortSha}`; break; @@ -67,7 +67,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { } } else { - const shortFileSha = resource.sha === undefined ? '' : resource.sha.substring(0, 8); + const shortFileSha = resource.sha === undefined ? '' : GitService.shortenSha(resource.sha); description = `$(file-text) ${path.basename(resource.fileName)}${shortFileSha ? ` in ${GlyphChars.Space}$(git-commit) ${shortFileSha}` : ''}`; } break; diff --git a/src/quickPicks/repoStatus.ts b/src/quickPicks/repoStatus.ts index 33e0e35..b856bf5 100644 --- a/src/quickPicks/repoStatus.ts +++ b/src/quickPicks/repoStatus.ts @@ -4,7 +4,7 @@ import { commands, QuickPickOptions, TextDocumentShowOptions, Uri, window } from import { Commands, DiffWithWorkingCommandArgs, OpenChangedFilesCommandArgs, ShowQuickBranchHistoryCommandArgs, ShowQuickRepoStatusCommandArgs, ShowQuickStashListCommandArgs } from '../commands'; import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, OpenFileCommandQuickPickItem, QuickPickItem } from './common'; import { GlyphChars } from '../constants'; -import { GitStatus, GitStatusFile, GitUri } from '../gitService'; +import { GitService, GitStatus, GitStatusFile, GitUri } from '../gitService'; import { Keyboard, Keys } from '../keyboard'; import * as path from 'path'; @@ -182,7 +182,7 @@ export class RepoStatusQuickPick { if (status.upstream && status.state.behind) { items.splice(0, 0, new CommandQuickPickItem({ label: `$(cloud-download)${GlyphChars.Space} ${status.state.behind} Commit${status.state.behind > 1 ? 's' : ''} behind ${GlyphChars.Space}$(git-branch) ${status.upstream}`, - description: `${Strings.pad(GlyphChars.Dash, 2, 3)} shows commits in ${GlyphChars.Space}$(git-branch) ${status.upstream} but not ${GlyphChars.Space}$(git-branch) ${status.branch}${status.sha ? ` (since ${GlyphChars.Space}$(git-commit) ${status.sha.substring(0, 8)})` : ''}` + description: `${Strings.pad(GlyphChars.Dash, 2, 3)} shows commits in ${GlyphChars.Space}$(git-branch) ${status.upstream} but not ${GlyphChars.Space}$(git-branch) ${status.branch}${status.sha ? ` (since ${GlyphChars.Space}$(git-commit) ${GitService.shortenSha(status.sha)})` : ''}` }, Commands.ShowQuickBranchHistory, [ new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.branch}..${status.upstream}` }), {