Skip to content

Commit 64ae820

Browse files
committed
Cleans up some duplicate code
1 parent 5624567 commit 64ae820

File tree

6 files changed

+74
-21
lines changed

6 files changed

+74
-21
lines changed

src/commands.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ export * from './commands/openInRemote';
2121
export * from './commands/openRepoInRemote';
2222
export * from './commands/resetSuppressedWarnings';
2323
export * from './commands/showBlameHistory';
24+
export * from './commands/showCommitSearch';
2425
export * from './commands/showFileBlame';
2526
export * from './commands/showFileHistory';
2627
export * from './commands/showLastQuickPick';
2728
export * from './commands/showLineBlame';
29+
export * from './commands/showQuickBranchHistory';
2830
export * from './commands/showQuickCommitDetails';
2931
export * from './commands/showQuickCommitFileDetails';
30-
export * from './commands/showCommitSearch';
31-
export * from './commands/showQuickFileHistory';
32-
export * from './commands/showQuickBranchHistory';
3332
export * from './commands/showQuickCurrentBranchHistory';
33+
export * from './commands/showQuickFileHistory';
3434
export * from './commands/showQuickRepoStatus';
3535
export * from './commands/showQuickStashList';
3636
export * from './commands/stashApply';

src/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import { CodeLensLocations, IConfig, LineHighlightLocations } from './configurat
2121
import { ApplicationInsightsKey, ExtensionKey, QualifiedExtensionId, WorkspaceState } from './constants';
2222
import { CurrentLineController, LineAnnotationType } from './currentLineController';
2323
import { GitContentProvider } from './gitContentProvider';
24-
import { GitContextTracker, GitService } from './gitService';
2524
import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider';
25+
import { GitContextTracker, GitService } from './gitService';
2626
import { Logger } from './logger';
2727
import { Messages, SuppressedKeys } from './messages';
2828
import { Telemetry } from './telemetry';
@@ -270,6 +270,8 @@ async function notifyOnNewGitLensVersion(context: ExtensionContext, version: str
270270
const [major, minor] = version.split('.');
271271
const [prevMajor, prevMinor] = previousVersion.split('.');
272272
if (major === prevMajor && minor === prevMinor) return;
273+
// Don't notify on downgrades
274+
if (major < prevMajor || (major === prevMajor && minor < prevMinor)) return;
273275

274276
await Messages.showUpdateMessage(version);
275277
}

src/git/formatters/commit.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ export interface ICommitFormatOptions {
1717

1818
export class CommitFormatter {
1919

20+
private _commit: GitCommit;
2021
private _options: ICommitFormatOptions;
2122

22-
constructor(private commit: GitCommit, options?: ICommitFormatOptions) {
23+
constructor(commit: GitCommit, options?: ICommitFormatOptions) {
24+
this.reset(commit, options);
25+
}
26+
27+
reset(commit: GitCommit, options?: ICommitFormatOptions) {
28+
this._commit = commit;
29+
30+
if (options === undefined && this._options !== undefined) return;
31+
2332
options = options || {};
2433
if (options.tokenOptions == null) {
2534
options.tokenOptions = {};
@@ -33,31 +42,31 @@ export class CommitFormatter {
3342
}
3443

3544
get ago() {
36-
const ago = moment(this.commit.date).fromNow();
45+
const ago = moment(this._commit.date).fromNow();
3746
return this._padOrTruncate(ago, this._options.tokenOptions!.ago);
3847
}
3948

4049
get author() {
41-
const author = this.commit.author;
50+
const author = this._commit.author;
4251
return this._padOrTruncate(author, this._options.tokenOptions!.author);
4352
}
4453

4554
get authorAgo() {
46-
const authorAgo = `${this.commit.author}, ${moment(this.commit.date).fromNow()}`;
55+
const authorAgo = `${this._commit.author}, ${moment(this._commit.date).fromNow()}`;
4756
return this._padOrTruncate(authorAgo, this._options.tokenOptions!.authorAgo);
4857
}
4958

5059
get date() {
51-
const date = moment(this.commit.date).format(this._options.dateFormat!);
60+
const date = moment(this._commit.date).format(this._options.dateFormat!);
5261
return this._padOrTruncate(date, this._options.tokenOptions!.date);
5362
}
5463

5564
get id() {
56-
return this.commit.shortSha;
65+
return this._commit.shortSha;
5766
}
5867

5968
get message() {
60-
const message = this.commit.isUncommitted ? 'Uncommitted change' : this.commit.message;
69+
const message = this._commit.isUncommitted ? 'Uncommitted change' : this._commit.message;
6170
return this._padOrTruncate(message, this._options.tokenOptions!.message);
6271
}
6372

@@ -113,6 +122,18 @@ export class CommitFormatter {
113122
return s;
114123
}
115124

125+
private static _formatter: CommitFormatter | undefined = undefined;
126+
127+
static fromCommit(commit: GitCommit, options?: ICommitFormatOptions): CommitFormatter {
128+
if (CommitFormatter._formatter === undefined) {
129+
CommitFormatter._formatter = new CommitFormatter(commit, options);
130+
}
131+
else {
132+
CommitFormatter._formatter.reset(commit, options);
133+
}
134+
return CommitFormatter._formatter;
135+
}
136+
116137
static fromTemplate(template: string, commit: GitCommit, dateFormat: string | null): string;
117138
static fromTemplate(template: string, commit: GitCommit, options?: ICommitFormatOptions): string;
118139
static fromTemplate(template: string, commit: GitCommit, dateFormatOrOptions?: string | null | ICommitFormatOptions): string;

src/git/gitUri.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ export class GitUri extends Uri {
102102
const uri = Uri.file(path.resolve(repoPath, original ? status.originalFileName || status.fileName : status.fileName));
103103
return new GitUri(uri, repoPathOrCommit);
104104
}
105+
106+
static getDirectory(fileName: string): string {
107+
const directory: string | undefined = GitService.normalizePath(path.dirname(fileName));
108+
return (!directory || directory === '.') ? '' : directory;
109+
}
110+
111+
static getFormattedPath(fileNameOrUri: string | Uri, separator: string = ' \u00a0\u2022\u00a0 '): string {
112+
let fileName: string;
113+
if (fileNameOrUri instanceof Uri) {
114+
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(separator);
115+
116+
fileName = fileNameOrUri.fsPath;
117+
}
118+
else {
119+
fileName = fileNameOrUri;
120+
}
121+
122+
const directory = GitUri.getDirectory(fileName);
123+
return !directory
124+
? path.basename(fileName)
125+
: `${path.basename(fileName)}${separator}${directory}`;
126+
}
105127
}
106128

107129
export interface IGitCommitInfo {

src/git/models/status.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
import { Uri } from 'vscode';
3+
import { GitUri } from '../gitUri';
34
import * as path from 'path';
45

56
export interface GitStatus {
@@ -32,13 +33,28 @@ export class GitStatusFile implements IGitStatusFile {
3233
this.originalFileName = originalFileName;
3334
}
3435

36+
getFormattedDirectory(includeOriginal: boolean = false): string {
37+
return GitStatusFile.getFormattedDirectory(this, includeOriginal);
38+
}
39+
40+
getFormattedPath(separator: string = ' \u00a0\u2022\u00a0 '): string {
41+
return GitUri.getFormattedPath(this.fileName, separator);
42+
}
43+
3544
getIcon() {
3645
return getGitStatusIcon(this.status);
3746
}
3847

3948
get Uri(): Uri {
4049
return Uri.file(path.resolve(this.repoPath, this.fileName));
4150
}
51+
52+
static getFormattedDirectory(status: IGitStatusFile, includeOriginal: boolean = false): string {
53+
const directory = GitUri.getDirectory(status.fileName);
54+
return (includeOriginal && status.status === 'R' && status.originalFileName)
55+
? `${directory} \u00a0\u2190\u00a0 ${status.originalFileName}`
56+
: directory;
57+
}
4258
}
4359

4460
const statusOcticonsMap = {

src/quickPicks/commitDetails.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Arrays, Iterables } from '../system';
33
import { commands, QuickPickOptions, TextDocumentShowOptions, Uri, window } from 'vscode';
44
import { Commands, CopyMessageToClipboardCommandArgs, CopyShaToClipboardCommandArgs, DiffDirectoryCommandCommandArgs, DiffWithPreviousCommandArgs, Keyboard, KeyNoopCommand, Keys, ShowQuickCommitDetailsCommandArgs, StashApplyCommandArgs, StashDeleteCommandArgs } from '../commands';
55
import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, KeyCommandQuickPickItem, OpenFileCommandQuickPickItem, OpenFilesCommandQuickPickItem, QuickPickItem } from './common';
6-
import { getGitStatusIcon, GitCommit, GitLog, GitLogCommit, GitService, GitStashCommit, GitStatusFileStatus, GitUri, IGitCommitInfo, IGitStatusFile, RemoteResource } from '../gitService';
6+
import { getGitStatusIcon, GitCommit, GitLog, GitLogCommit, GitService, GitStashCommit, GitStatusFile, GitStatusFileStatus, GitUri, IGitCommitInfo, IGitStatusFile, RemoteResource } from '../gitService';
77
import { OpenRemotesCommandQuickPickItem } from './remotes';
88
import * as moment from 'moment';
99
import * as path from 'path';
@@ -19,15 +19,7 @@ export class CommitWithFileStatusQuickPickItem extends OpenFileCommandQuickPickI
1919

2020
constructor(commit: GitCommit, status: IGitStatusFile) {
2121
const icon = getGitStatusIcon(status.status);
22-
23-
let directory: string | undefined = GitService.normalizePath(path.dirname(status.fileName));
24-
if (!directory || directory === '.') {
25-
directory = '';
26-
}
27-
28-
const description = (status.status === 'R' && status.originalFileName)
29-
? `${directory} \u00a0\u2190\u00a0 ${status.originalFileName}`
30-
: directory;
22+
const description = GitStatusFile.getFormattedDirectory(status, true);
3123

3224
let sha;
3325
let shortSha;

0 commit comments

Comments
 (0)