From 6d9a666d12313373b5f521fb9ff817f8752e7ce2 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 4 May 2019 04:28:21 -0400 Subject: [PATCH] Renames some members for consistency --- src/annotations/annotations.ts | 4 +- src/commands/diffLineWithWorking.ts | 2 +- src/commands/diffWithWorking.ts | 4 +- src/git/git.ts | 42 +++++++++++---------- src/git/gitService.ts | 71 ++++++++++++++++++----------------- src/git/gitUri.ts | 8 ++-- src/git/models/commit.ts | 2 +- src/quickpicks/repoStatusQuickPick.ts | 4 +- src/views/nodes/fileHistoryNode.ts | 4 +- src/views/nodes/statusFilesNode.ts | 6 +-- 10 files changed, 75 insertions(+), 72 deletions(-) diff --git a/src/annotations/annotations.ts b/src/annotations/annotations.ts index 6b96096..8c671fe 100644 --- a/src/annotations/annotations.ts +++ b/src/annotations/annotations.ts @@ -121,7 +121,7 @@ export class Annotations { let message: string; if (commit.isUncommitted) { - if (uri.sha !== undefined && GitService.isStagedUncommitted(uri.sha)) { + if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) { message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs( commit, editorLine @@ -173,7 +173,7 @@ export class Annotations { ): Promise> { let ref; if (commit.isUncommitted) { - if (uri.sha !== undefined && GitService.isStagedUncommitted(uri.sha)) { + if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) { ref = uri.sha; } } diff --git a/src/commands/diffLineWithWorking.ts b/src/commands/diffLineWithWorking.ts index 49a1e56..f902ec9 100644 --- a/src/commands/diffLineWithWorking.ts +++ b/src/commands/diffLineWithWorking.ts @@ -52,7 +52,7 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand { args.commit = args.commit.with({ sha: status !== undefined && status.indexStatus !== undefined - ? GitService.stagedUncommittedSha + ? GitService.uncommittedStagedSha : args.commit.previousSha!, fileName: args.commit.previousFileName!, originalFileName: null, diff --git a/src/commands/diffWithWorking.ts b/src/commands/diffWithWorking.ts index 7b73ab4..bccac92 100644 --- a/src/commands/diffWithWorking.ts +++ b/src/commands/diffWithWorking.ts @@ -61,12 +61,12 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand { } // If we are a fake "staged" sha, check the status - if (GitService.isStagedUncommitted(gitUri.sha!)) { + if (GitService.isUncommittedStaged(gitUri.sha!)) { gitUri.sha = undefined; const status = await Container.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath); if (status !== undefined && status.indexStatus !== undefined) { - let sha = GitService.stagedUncommittedSha; + let sha = GitService.uncommittedStagedSha; if (args.inDiffEditor) { const commit = await Container.git.getCommitForFile(gitUri.repoPath!, gitUri.fsPath); if (commit === undefined) return Messages.showCommitHasNoPreviousCommitWarningMessage(); diff --git a/src/git/git.ts b/src/git/git.ts index fd54913..3ed2de4 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -206,10 +206,10 @@ export class Git { static shaRegex = /(^[0-9a-f]{40}$)|(^[0]{40}(:|-)$)/; static shaParentRegex = /^[0-9a-f]{40}\^[0-3]?$/; static shaShortenRegex = /^(.*?)([\^@~:].*)?$/; - static stagedUncommittedRegex = /^[0]{40}([\^@~]\S*)?:$/; - static stagedUncommittedSha = '0000000000000000000000000000000000000000:'; static uncommittedRegex = /^[0]{40}(?:[\^@~:]\S*)?:?$/; static uncommittedSha = '0000000000000000000000000000000000000000'; + static uncommittedStagedRegex = /^[0]{40}([\^@~]\S*)?:$/; + static uncommittedStagedSha = '0000000000000000000000000000000000000000:'; static getEncoding(encoding: string | undefined) { return encoding !== undefined && iconv.encodingExists(encoding) ? encoding : 'utf8'; @@ -247,27 +247,29 @@ export class Git { return Git.shaParentRegex.test(ref); } - static isStagedUncommitted(ref: string | undefined): boolean { - return ref ? Git.stagedUncommittedRegex.test(ref) : false; - } - static isUncommitted(ref: string | undefined) { return ref ? Git.uncommittedRegex.test(ref) : false; } + static isUncommittedStaged(ref: string | undefined): boolean { + return ref ? Git.uncommittedStagedRegex.test(ref) : false; + } + static shortenSha( - ref: string, - strings: { stagedUncommitted?: string; uncommitted?: string; working?: string } = {} + ref: string | undefined, + { + stagedUncommitted = 'Index', + uncommitted = 'Working Tree', + working = emptyStr + }: { stagedUncommitted?: string; uncommitted?: string; working?: string } = {} ) { - strings = { stagedUncommitted: 'Index', uncommitted: 'Working Tree', working: emptyStr, ...strings }; - - if (ref == null || ref.length === 0) return strings.working; + if (ref == null || ref.length === 0) return working; if (Git.isUncommitted(ref)) { - if (Git.isStagedUncommitted(ref)) return strings.stagedUncommitted; - - return strings.uncommitted; + return Git.isUncommittedStaged(ref) ? stagedUncommitted : uncommitted; } + if (!Git.isShaLike(ref)) return ref; + // Don't allow shas to be shortened to less than 5 characters const len = Math.max(5, Container.config.advanced.abbreviatedShaLength); @@ -343,7 +345,7 @@ export class Git { let stdin; if (ref) { - if (Git.isStagedUncommitted(ref)) { + if (Git.isUncommittedStaged(ref)) { // Pipe the blame contents to stdin params.push('--contents', '-'); @@ -496,10 +498,10 @@ export class Git { if (ref1.endsWith('^3^')) { ref1 = rootSha; } - params.push(Git.isStagedUncommitted(ref1) ? '--staged' : ref1); + params.push(Git.isUncommittedStaged(ref1) ? '--staged' : ref1); } if (ref2) { - params.push(Git.isStagedUncommitted(ref2) ? '--staged' : ref2); + params.push(Git.isUncommittedStaged(ref2) ? '--staged' : ref2); } const encoding: BufferEncoding = options.encoding === 'utf8' ? 'utf8' : 'binary'; @@ -637,7 +639,7 @@ export class Git { params.push('--use-mailmap', ...authors.map(a => `--author=${a}`)); } - if (ref && !Git.isStagedUncommitted(ref)) { + if (ref && !Git.isUncommittedStaged(ref)) { // If we are reversing, we must add a range (with HEAD) because we are using --ancestry-path for better reverse walking if (reverse) { params.push('--reverse', '--ancestry-path', `${ref}..HEAD`); @@ -697,7 +699,7 @@ export class Git { params.push(`-L ${startLine},${endLine == null ? startLine : endLine}:${file}`); } - if (ref && !Git.isStagedUncommitted(ref)) { + if (ref && !Git.isUncommittedStaged(ref)) { // If we are reversing, we must add a range (with HEAD) because we are using --ancestry-path for better reverse walking if (reverse) { params.push('--reverse', '--ancestry-path', `${ref}..HEAD`); @@ -868,7 +870,7 @@ export class Git { ): Promise { const [file, root] = Git.splitPath(fileName, repoPath); - if (Git.isStagedUncommitted(ref)) { + if (Git.isUncommittedStaged(ref)) { ref = ':'; } if (Git.isUncommitted(ref)) throw new Error(`ref=${ref} is uncommitted`); diff --git a/src/git/gitService.ts b/src/git/gitService.ts index 2836bd5..b0c25cd 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -93,12 +93,9 @@ export enum GitRepoSearchBy { Sha = 'sha' } -export class GitService implements Disposable { - static emptyPromise: Promise = Promise.resolve(undefined); - static deletedOrMissingSha = Git.deletedOrMissingSha; - static stagedUncommittedSha = Git.stagedUncommittedSha; - static uncommittedSha = Git.uncommittedSha; +const emptyPromise: Promise = Promise.resolve(undefined); +export class GitService implements Disposable { private _onDidChangeRepositories = new EventEmitter(); get onDidChangeRepositories(): Event { return this._onDidChangeRepositories.event; @@ -672,7 +669,7 @@ export class GitService implements Disposable { ): Promise { if (!(await this.isTracked(uri))) { Logger.log(cc, `Skipping blame; '${uri.fsPath}' is not tracked`); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } const [file, root] = Git.splitPath(uri.fsPath, uri.repoPath, false); @@ -692,14 +689,14 @@ export class GitService implements Disposable { Logger.debug(cc, `Cache replace (with empty promise): '${key}'`); const value: CachedBlame = { - item: GitService.emptyPromise as Promise, + item: emptyPromise as Promise, errorMessage: msg }; document.state.set(key, value); document.setBlameFailure(); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } return undefined; @@ -756,7 +753,7 @@ export class GitService implements Disposable { ): Promise { if (!(await this.isTracked(uri))) { Logger.log(cc, `Skipping blame; '${uri.fsPath}' is not tracked`); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } const [file, root] = Git.splitPath(uri.fsPath, uri.repoPath, false); @@ -777,13 +774,13 @@ export class GitService implements Disposable { Logger.debug(cc, `Cache replace (with empty promise): '${key}'`); const value: CachedBlame = { - item: GitService.emptyPromise as Promise, + item: emptyPromise as Promise, errorMessage: msg }; document.state.set(key, value); document.setBlameFailure(); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } return undefined; @@ -1169,7 +1166,7 @@ export class GitService implements Disposable { try { let data; - if (ref1 !== undefined && ref2 === undefined && !GitService.isStagedUncommitted(ref1)) { + if (ref1 !== undefined && ref2 === undefined && !GitService.isUncommittedStaged(ref1)) { data = await Git.show__diff(root, file, ref1, originalFileName, { similarityThreshold: Container.config.advanced.similarityThreshold }); @@ -1192,12 +1189,12 @@ export class GitService implements Disposable { Logger.debug(cc, `Cache replace (with empty promise): '${key}'`); const value: CachedDiff = { - item: GitService.emptyPromise as Promise, + item: emptyPromise as Promise, errorMessage: msg }; document.state.set(key, value); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } return undefined; @@ -1216,7 +1213,7 @@ export class GitService implements Disposable { let diff = await this.getDiffForFile(uri, ref1, ref2, originalFileName); // If we didn't find a diff & ref1 is undefined (meaning uncommitted), check for a staged diff if (diff === undefined && ref1 === undefined) { - diff = await this.getDiffForFile(uri, Git.stagedUncommittedSha, ref2, originalFileName); + diff = await this.getDiffForFile(uri, Git.uncommittedStagedSha, ref2, originalFileName); } if (diff === undefined) return undefined; @@ -1537,7 +1534,7 @@ export class GitService implements Disposable { ): Promise { if (!(await this.isTracked(fileName, repoPath, { ref: ref }))) { Logger.log(cc, `Skipping log; '${fileName}' is not tracked`); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } const [file, root] = Git.splitPath(fileName, repoPath, false); @@ -1582,12 +1579,12 @@ export class GitService implements Disposable { Logger.debug(cc, `Cache replace (with empty promise): '${key}'`); const value: CachedLog = { - item: GitService.emptyPromise as Promise, + item: emptyPromise as Promise, errorMessage: msg }; document.state.set(key, value); - return GitService.emptyPromise as Promise; + return emptyPromise as Promise; } return undefined; @@ -1641,7 +1638,7 @@ export class GitService implements Disposable { const fileName = GitUri.getRelativePath(uri, repoPath); - if (Git.isStagedUncommitted(ref)) { + if (Git.isUncommittedStaged(ref)) { return { current: GitUri.fromFile(fileName, repoPath, ref), next: GitUri.fromFile(fileName, repoPath, undefined) @@ -1656,7 +1653,7 @@ export class GitService implements Disposable { if (status.indexStatus !== undefined) { return { current: GitUri.fromFile(fileName, repoPath, ref), - next: GitUri.fromFile(fileName, repoPath, GitService.stagedUncommittedSha) + next: GitUri.fromFile(fileName, repoPath, GitService.uncommittedStagedSha) }; } } @@ -1682,7 +1679,7 @@ export class GitService implements Disposable { // editorLine?: number ): Promise { // If we have no ref (or staged ref) there is no next commit - if (ref === undefined || ref.length === 0 || Git.isStagedUncommitted(ref)) return undefined; + if (ref === undefined || ref.length === 0 || Git.isUncommittedStaged(ref)) return undefined; let filters: GitLogDiffFilter[] | undefined; if (ref === GitService.deletedOrMissingSha) { @@ -1746,18 +1743,18 @@ export class GitService implements Disposable { if (skip === 0) { return { current: GitUri.fromFile(fileName, repoPath, ref), - previous: GitUri.fromFile(fileName, repoPath, GitService.stagedUncommittedSha) + previous: GitUri.fromFile(fileName, repoPath, GitService.uncommittedStagedSha) }; } return { - current: GitUri.fromFile(fileName, repoPath, GitService.stagedUncommittedSha), + current: GitUri.fromFile(fileName, repoPath, GitService.uncommittedStagedSha), previous: await this.getPreviousUri(repoPath, uri, ref, skip - 1, editorLine) }; } } } - else if (GitService.isStagedUncommitted(ref)) { + else if (GitService.isUncommittedStaged(ref)) { const current = skip === 0 ? GitUri.fromFile(fileName, repoPath, ref) @@ -1791,6 +1788,9 @@ export class GitService implements Disposable { editorLine?: number ): Promise { if (ref === GitService.deletedOrMissingSha) return undefined; + if (ref === GitService.uncommittedSha) { + ref = undefined; + } if (ref !== undefined) { skip++; @@ -2130,14 +2130,14 @@ export class GitService implements Disposable { ): Promise { if (ref === GitService.deletedOrMissingSha) return undefined; - if (!ref || (Git.isUncommitted(ref) && !Git.isStagedUncommitted(ref))) { + if (!ref || (Git.isUncommitted(ref) && !Git.isUncommittedStaged(ref))) { const data = await Git.ls_files(repoPath!, fileName); if (data !== undefined) return GitUri.file(fileName); return undefined; } - if (Git.isStagedUncommitted(ref)) { + if (Git.isUncommittedStaged(ref)) { return GitUri.git(fileName, repoPath); } @@ -2435,24 +2435,25 @@ export class GitService implements Disposable { return Git.getEncoding(workspace.getConfiguration('files', uri).get('encoding')); } + static deletedOrMissingSha = Git.deletedOrMissingSha; static getGitPath = Git.getGitPath; static getGitVersion = Git.getGitVersion; static isShaLike = Git.isShaLike; static isShaParent = Git.isShaParent; - static isStagedUncommitted = Git.isStagedUncommitted; static isUncommitted = Git.isUncommitted; + static isUncommittedStaged = Git.isUncommittedStaged; + static uncommittedSha = Git.uncommittedSha; + static uncommittedStagedSha = Git.uncommittedStagedSha; static shortenSha( ref: string | undefined, - strings: { deletedOrMissing?: string; stagedUncommitted?: string; uncommitted?: string; working?: string } = {} + { + deletedOrMissing = '(deleted)', + ...strings + }: { deletedOrMissing?: string; stagedUncommitted?: string; uncommitted?: string; working?: string } = {} ) { - if (ref === undefined) return undefined; - - strings = { deletedOrMissing: '(deleted)', working: emptyStr, ...strings }; - - if (ref == null || ref.length === 0) return strings.working; - if (ref === GitService.deletedOrMissingSha) return strings.deletedOrMissing; + if (ref === GitService.deletedOrMissingSha) return deletedOrMissing; - return Git.isShaLike(ref) || Git.isStagedUncommitted(ref) ? Git.shortenSha(ref, strings) : ref; + return Git.shortenSha(ref, strings); } } diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index a0dbac2..6ffd5fc 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -68,7 +68,7 @@ export class GitUri extends ((Uri as any) as UriEx) { }); this.repoPath = data.repoPath; - if (GitService.isStagedUncommitted(data.ref) || !GitService.isUncommitted(data.ref)) { + if (GitService.isUncommittedStaged(data.ref) || !GitService.isUncommitted(data.ref)) { this.sha = data.ref; } @@ -123,7 +123,7 @@ export class GitUri extends ((Uri as any) as UriEx) { }); this.repoPath = commitOrRepoPath.repoPath; this.versionedPath = commitOrRepoPath.versionedPath; - if (GitService.isStagedUncommitted(commitOrRepoPath.sha) || !GitService.isUncommitted(commitOrRepoPath.sha)) { + if (GitService.isUncommittedStaged(commitOrRepoPath.sha) || !GitService.isUncommitted(commitOrRepoPath.sha)) { this.sha = commitOrRepoPath.sha; } } @@ -257,7 +257,7 @@ export class GitUri extends ((Uri as any) as UriEx) { switch (data.ref) { case emptyStr: case '~': - ref = GitService.stagedUncommittedSha; + ref = GitService.uncommittedStagedSha; break; case null: @@ -397,7 +397,7 @@ export class GitUri extends ((Uri as any) as UriEx) { } if (ref === undefined || GitService.isUncommitted(ref)) { - if (GitService.isStagedUncommitted(ref)) { + if (GitService.isUncommittedStaged(ref)) { return GitUri.git(fileName, repoPath); } diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index acbf0d5..90a1180 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -98,7 +98,7 @@ export abstract class GitCommit { private _isStagedUncommitted: boolean | undefined; get isStagedUncommitted(): boolean { if (this._isStagedUncommitted === undefined) { - this._isStagedUncommitted = Git.isStagedUncommitted(this.sha); + this._isStagedUncommitted = Git.isUncommittedStaged(this.sha); } return this._isStagedUncommitted; } diff --git a/src/quickpicks/repoStatusQuickPick.ts b/src/quickpicks/repoStatusQuickPick.ts index 5a34d91..6a99428 100644 --- a/src/quickpicks/repoStatusQuickPick.ts +++ b/src/quickpicks/repoStatusQuickPick.ts @@ -54,7 +54,7 @@ export class OpenStatusFileCommandQuickPickItem extends OpenFileCommandQuickPick this.commit = new GitLogCommit( GitCommitType.LogFile, status.repoPath, - GitService.stagedUncommittedSha, + GitService.uncommittedStagedSha, 'You', undefined, new Date(), @@ -82,7 +82,7 @@ export class OpenStatusFileCommandQuickPickItem extends OpenFileCommandQuickPick [status], status.status, status.originalFileName, - realIndexStatus !== undefined ? GitService.stagedUncommittedSha : 'HEAD', + realIndexStatus !== undefined ? GitService.uncommittedStagedSha : 'HEAD', status.fileName ); } diff --git a/src/views/nodes/fileHistoryNode.ts b/src/views/nodes/fileHistoryNode.ts index 2763c84..173618c 100644 --- a/src/views/nodes/fileHistoryNode.ts +++ b/src/views/nodes/fileHistoryNode.ts @@ -41,14 +41,14 @@ export class FileHistoryNode extends SubscribeableViewNode implements PageableVi if (status.workingTreeStatus !== undefined) { sha = GitService.uncommittedSha; if (status.indexStatus !== undefined) { - previousSha = GitService.stagedUncommittedSha; + previousSha = GitService.uncommittedStagedSha; } else if (status.workingTreeStatus !== '?') { previousSha = 'HEAD'; } } else { - sha = GitService.stagedUncommittedSha; + sha = GitService.uncommittedStagedSha; previousSha = 'HEAD'; } diff --git a/src/views/nodes/statusFilesNode.ts b/src/views/nodes/statusFilesNode.ts index 84e47a9..8a78279 100644 --- a/src/views/nodes/statusFilesNode.ts +++ b/src/views/nodes/statusFilesNode.ts @@ -67,12 +67,12 @@ export class StatusFilesNode extends ViewNode { older.setMilliseconds(older.getMilliseconds() - 1); return [ - this.toStatusFile(s, GitService.uncommittedSha, GitService.stagedUncommittedSha), - this.toStatusFile(s, GitService.stagedUncommittedSha, 'HEAD', older) + this.toStatusFile(s, GitService.uncommittedSha, GitService.uncommittedStagedSha), + this.toStatusFile(s, GitService.uncommittedStagedSha, 'HEAD', older) ]; } else if (s.indexStatus !== undefined) { - return [this.toStatusFile(s, GitService.stagedUncommittedSha, 'HEAD')]; + return [this.toStatusFile(s, GitService.uncommittedStagedSha, 'HEAD')]; } return [this.toStatusFile(s, GitService.uncommittedSha, 'HEAD')];