From 64ae82075e1cd6581564f58b82ef1670d1efdbed Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 12 Jun 2017 10:33:58 -0400 Subject: [PATCH] Cleans up some duplicate code --- src/commands.ts | 6 +++--- src/extension.ts | 4 +++- src/git/formatters/commit.ts | 35 ++++++++++++++++++++++++++++------- src/git/gitUri.ts | 22 ++++++++++++++++++++++ src/git/models/status.ts | 16 ++++++++++++++++ src/quickPicks/commitDetails.ts | 12 ++---------- 6 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 7fce9d4..3b7731e 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -21,16 +21,16 @@ export * from './commands/openInRemote'; export * from './commands/openRepoInRemote'; export * from './commands/resetSuppressedWarnings'; export * from './commands/showBlameHistory'; +export * from './commands/showCommitSearch'; export * from './commands/showFileBlame'; export * from './commands/showFileHistory'; export * from './commands/showLastQuickPick'; export * from './commands/showLineBlame'; +export * from './commands/showQuickBranchHistory'; export * from './commands/showQuickCommitDetails'; export * from './commands/showQuickCommitFileDetails'; -export * from './commands/showCommitSearch'; -export * from './commands/showQuickFileHistory'; -export * from './commands/showQuickBranchHistory'; export * from './commands/showQuickCurrentBranchHistory'; +export * from './commands/showQuickFileHistory'; export * from './commands/showQuickRepoStatus'; export * from './commands/showQuickStashList'; export * from './commands/stashApply'; diff --git a/src/extension.ts b/src/extension.ts index 9b2bf06..b4847bf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -21,8 +21,8 @@ import { CodeLensLocations, IConfig, LineHighlightLocations } from './configurat import { ApplicationInsightsKey, ExtensionKey, QualifiedExtensionId, WorkspaceState } from './constants'; import { CurrentLineController, LineAnnotationType } from './currentLineController'; import { GitContentProvider } from './gitContentProvider'; -import { GitContextTracker, GitService } from './gitService'; import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider'; +import { GitContextTracker, GitService } from './gitService'; import { Logger } from './logger'; import { Messages, SuppressedKeys } from './messages'; import { Telemetry } from './telemetry'; @@ -270,6 +270,8 @@ async function notifyOnNewGitLensVersion(context: ExtensionContext, version: str const [major, minor] = version.split('.'); const [prevMajor, prevMinor] = previousVersion.split('.'); if (major === prevMajor && minor === prevMinor) return; + // Don't notify on downgrades + if (major < prevMajor || (major === prevMajor && minor < prevMinor)) return; await Messages.showUpdateMessage(version); } diff --git a/src/git/formatters/commit.ts b/src/git/formatters/commit.ts index 0089fef..e3eeae9 100644 --- a/src/git/formatters/commit.ts +++ b/src/git/formatters/commit.ts @@ -17,9 +17,18 @@ export interface ICommitFormatOptions { export class CommitFormatter { + private _commit: GitCommit; private _options: ICommitFormatOptions; - constructor(private commit: GitCommit, options?: ICommitFormatOptions) { + constructor(commit: GitCommit, options?: ICommitFormatOptions) { + this.reset(commit, options); + } + + reset(commit: GitCommit, options?: ICommitFormatOptions) { + this._commit = commit; + + if (options === undefined && this._options !== undefined) return; + options = options || {}; if (options.tokenOptions == null) { options.tokenOptions = {}; @@ -33,31 +42,31 @@ export class CommitFormatter { } get ago() { - const ago = moment(this.commit.date).fromNow(); + const ago = moment(this._commit.date).fromNow(); return this._padOrTruncate(ago, this._options.tokenOptions!.ago); } get author() { - const author = this.commit.author; + const author = this._commit.author; return this._padOrTruncate(author, this._options.tokenOptions!.author); } get authorAgo() { - const authorAgo = `${this.commit.author}, ${moment(this.commit.date).fromNow()}`; + const authorAgo = `${this._commit.author}, ${moment(this._commit.date).fromNow()}`; return this._padOrTruncate(authorAgo, this._options.tokenOptions!.authorAgo); } get date() { - const date = moment(this.commit.date).format(this._options.dateFormat!); + const date = moment(this._commit.date).format(this._options.dateFormat!); return this._padOrTruncate(date, this._options.tokenOptions!.date); } get id() { - return this.commit.shortSha; + return this._commit.shortSha; } get message() { - const message = this.commit.isUncommitted ? 'Uncommitted change' : this.commit.message; + const message = this._commit.isUncommitted ? 'Uncommitted change' : this._commit.message; return this._padOrTruncate(message, this._options.tokenOptions!.message); } @@ -113,6 +122,18 @@ export class CommitFormatter { return s; } + private static _formatter: CommitFormatter | undefined = undefined; + + static fromCommit(commit: GitCommit, options?: ICommitFormatOptions): CommitFormatter { + if (CommitFormatter._formatter === undefined) { + CommitFormatter._formatter = new CommitFormatter(commit, options); + } + else { + CommitFormatter._formatter.reset(commit, options); + } + return CommitFormatter._formatter; + } + static fromTemplate(template: string, commit: GitCommit, dateFormat: string | null): string; static fromTemplate(template: string, commit: GitCommit, options?: ICommitFormatOptions): string; static fromTemplate(template: string, commit: GitCommit, dateFormatOrOptions?: string | null | ICommitFormatOptions): string; diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index 797ba1f..e1aa4f4 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -102,6 +102,28 @@ export class GitUri extends Uri { const uri = Uri.file(path.resolve(repoPath, original ? status.originalFileName || status.fileName : status.fileName)); return new GitUri(uri, repoPathOrCommit); } + + static getDirectory(fileName: string): string { + const directory: string | undefined = GitService.normalizePath(path.dirname(fileName)); + return (!directory || directory === '.') ? '' : directory; + } + + static getFormattedPath(fileNameOrUri: string | Uri, separator: string = ' \u00a0\u2022\u00a0 '): string { + let fileName: string; + if (fileNameOrUri instanceof Uri) { + if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(separator); + + fileName = fileNameOrUri.fsPath; + } + else { + fileName = fileNameOrUri; + } + + const directory = GitUri.getDirectory(fileName); + return !directory + ? path.basename(fileName) + : `${path.basename(fileName)}${separator}${directory}`; + } } export interface IGitCommitInfo { diff --git a/src/git/models/status.ts b/src/git/models/status.ts index 3f3f254..30c30c9 100644 --- a/src/git/models/status.ts +++ b/src/git/models/status.ts @@ -1,5 +1,6 @@ 'use strict'; import { Uri } from 'vscode'; +import { GitUri } from '../gitUri'; import * as path from 'path'; export interface GitStatus { @@ -32,6 +33,14 @@ export class GitStatusFile implements IGitStatusFile { this.originalFileName = originalFileName; } + getFormattedDirectory(includeOriginal: boolean = false): string { + return GitStatusFile.getFormattedDirectory(this, includeOriginal); + } + + getFormattedPath(separator: string = ' \u00a0\u2022\u00a0 '): string { + return GitUri.getFormattedPath(this.fileName, separator); + } + getIcon() { return getGitStatusIcon(this.status); } @@ -39,6 +48,13 @@ export class GitStatusFile implements IGitStatusFile { get Uri(): Uri { return Uri.file(path.resolve(this.repoPath, this.fileName)); } + + static getFormattedDirectory(status: IGitStatusFile, includeOriginal: boolean = false): string { + const directory = GitUri.getDirectory(status.fileName); + return (includeOriginal && status.status === 'R' && status.originalFileName) + ? `${directory} \u00a0\u2190\u00a0 ${status.originalFileName}` + : directory; + } } const statusOcticonsMap = { diff --git a/src/quickPicks/commitDetails.ts b/src/quickPicks/commitDetails.ts index d099cac..c042125 100644 --- a/src/quickPicks/commitDetails.ts +++ b/src/quickPicks/commitDetails.ts @@ -3,7 +3,7 @@ import { Arrays, Iterables } from '../system'; import { commands, QuickPickOptions, TextDocumentShowOptions, Uri, window } from 'vscode'; import { Commands, CopyMessageToClipboardCommandArgs, CopyShaToClipboardCommandArgs, DiffDirectoryCommandCommandArgs, DiffWithPreviousCommandArgs, Keyboard, KeyNoopCommand, Keys, ShowQuickCommitDetailsCommandArgs, StashApplyCommandArgs, StashDeleteCommandArgs } from '../commands'; import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, KeyCommandQuickPickItem, OpenFileCommandQuickPickItem, OpenFilesCommandQuickPickItem, QuickPickItem } from './common'; -import { getGitStatusIcon, GitCommit, GitLog, GitLogCommit, GitService, GitStashCommit, GitStatusFileStatus, GitUri, IGitCommitInfo, IGitStatusFile, RemoteResource } from '../gitService'; +import { getGitStatusIcon, GitCommit, GitLog, GitLogCommit, GitService, GitStashCommit, GitStatusFile, GitStatusFileStatus, GitUri, IGitCommitInfo, IGitStatusFile, RemoteResource } from '../gitService'; import { OpenRemotesCommandQuickPickItem } from './remotes'; import * as moment from 'moment'; import * as path from 'path'; @@ -19,15 +19,7 @@ export class CommitWithFileStatusQuickPickItem extends OpenFileCommandQuickPickI constructor(commit: GitCommit, status: IGitStatusFile) { const icon = getGitStatusIcon(status.status); - - let directory: string | undefined = GitService.normalizePath(path.dirname(status.fileName)); - if (!directory || directory === '.') { - directory = ''; - } - - const description = (status.status === 'R' && status.originalFileName) - ? `${directory} \u00a0\u2190\u00a0 ${status.originalFileName}` - : directory; + const description = GitStatusFile.getFormattedDirectory(status, true); let sha; let shortSha;