From 9a0ce832607a4e3fd80c9417b306c4653eeecfee Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 17 Mar 2017 19:12:34 -0400 Subject: [PATCH] Renames git methods to align better with git commands --- src/extension.ts | 2 +- src/git/git.ts | 231 ++++++++++++++++++++++------------------------ src/gitService.ts | 20 ++-- 3 files changed, 119 insertions(+), 134 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index df3b620..4be48f3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -41,7 +41,7 @@ export async function activate(context: ExtensionContext) { let repoPath: string; try { - repoPath = await Git.repoPath(rootPath, gitPath); + repoPath = await Git.getRepoPath(rootPath, gitPath); } catch (ex) { Logger.error(ex); diff --git a/src/git/git.ts b/src/git/git.ts index 30eb9ba..8db0440 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -11,10 +11,8 @@ export * from './enrichers/blameParserEnricher'; export * from './enrichers/logParserEnricher'; let git: IGit; -const UncommittedRegex = /^[0]+$/; -const ShaRegex = /\b[0-9a-f]{40}\b/; -const DefaultLogParams = [`log`, `--name-status`, `--full-history`, `-m`, `--date=iso8601-strict`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparent %P%nsummary %B%nfilename ?`]; +const DefaultLogParams = [`log`, `--name-status`, `--full-history`, `-M`, `--date=iso8601-strict`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparent %P%nsummary %B%nfilename ?`]; async function gitCommand(cwd: string, ...args: any[]) { try { @@ -43,25 +41,14 @@ export const GitBlameFormat = { export class Git { - static normalizePath(fileName: string, repoPath?: string) { - return fileName.replace(/\\/g, '/'); + static ShaRegex = /^[0-9a-f]{40}( -)?$/; + static UncommittedRegex = /^[0]+$/; + + static gitInfo(): IGit { + return git; } - static splitPath(fileName: string, repoPath?: string): [string, string] { - if (repoPath) { - return [ - fileName.replace(repoPath.endsWith('/') ? repoPath : `${repoPath}/`, ''), - repoPath - ]; - } - - return [ - path.basename(fileName).replace(/\\/g, '/'), - path.dirname(fileName).replace(/\\/g, '/') - ]; - } - - static async repoPath(cwd: string, gitPath?: string) { + static async getRepoPath(cwd: string, gitPath?: string) { git = await findGitPath(gitPath); Logger.log(`Git found: ${git.version} @ ${git.path === 'git' ? 'PATH' : git.path}`); @@ -70,51 +57,8 @@ export class Git { return data; } - static blame(format: GitBlameFormat, fileName: string, sha?: string, repoPath?: string) { - const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); - - const params = [`blame`, `--root`, format]; - if (sha) { - params.push(sha); - } - - return gitCommand(root, ...params, `--`, file); - } - - static blameLines(format: GitBlameFormat, fileName: string, startLine: number, endLine: number, sha?: string, repoPath?: string) { - const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); - - const params = [`blame`, `--root`, format, `-L ${startLine},${endLine}`]; - if (sha) { - params.push(sha); - } - - return gitCommand(root, ...params, `--`, file); - } - - static diffDir(repoPath: string, sha1: string, sha2?: string) { - const params = [`difftool`, `--dir-diff`, sha1]; - if (sha2) { - params.push(sha2); - } - - return gitCommand(repoPath, ...params); - } - - static diffStatus(repoPath: string, sha1?: string, sha2?: string) { - const params = [`diff`, `--name-status`, `-M`]; - if (sha1) { - params.push(sha1); - } - if (sha2) { - params.push(sha2); - } - - return gitCommand(repoPath, ...params); - } - static async getVersionedFile(fileName: string, repoPath: string, branchOrSha: string) { - const data = await Git.getVersionedFileText(fileName, repoPath, branchOrSha); + const data = await Git.show(repoPath, fileName, branchOrSha); const suffix = Git.isSha(branchOrSha) ? branchOrSha.substring(0, 8) : branchOrSha; const ext = path.extname(fileName); @@ -139,7 +83,72 @@ export class Git { }); } - static getVersionedFileText(fileName: string, repoPath: string, branchOrSha: string) { + static isSha(sha: string) { + return Git.ShaRegex.test(sha); + } + + static isUncommitted(sha: string) { + return Git.UncommittedRegex.test(sha); + } + + static normalizePath(fileName: string, repoPath?: string) { + return fileName.replace(/\\/g, '/'); + } + + static splitPath(fileName: string, repoPath?: string): [string, string] { + if (repoPath) { + return [ + fileName.replace(repoPath.endsWith('/') ? repoPath : `${repoPath}/`, ''), + repoPath + ]; + } + + return [ + path.basename(fileName).replace(/\\/g, '/'), + path.dirname(fileName).replace(/\\/g, '/') + ]; + } + + // Git commands + + static blame(repoPath: string, fileName: string, format: GitBlameFormat, sha?: string, startLine?: number, endLine?: number) { + const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); + + const params = [`blame`, `--root`, format]; + + if (startLine != null && endLine != null) { + params.push(`-L ${startLine},${endLine}`); + } + + if (sha) { + params.push(sha); + } + + return gitCommand(root, ...params, `--`, file); + } + + static diff_nameStatus(repoPath: string, sha1?: string, sha2?: string) { + const params = [`diff`, `--name-status`, `-M`]; + if (sha1) { + params.push(sha1); + } + if (sha2) { + params.push(sha2); + } + + return gitCommand(repoPath, ...params); + } + + static difftool_dirDiff(repoPath: string, sha1: string, sha2?: string) { + const params = [`difftool`, `--dir-diff`, sha1]; + if (sha2) { + params.push(sha2); + } + + return gitCommand(repoPath, ...params); + } + + static show(repoPath: string, fileName: string, branchOrSha: string) { const [file, root] = Git.splitPath(Git.normalizePath(fileName), repoPath); branchOrSha = branchOrSha.replace('^', ''); @@ -147,49 +156,7 @@ export class Git { return gitCommand(root, 'show', `${branchOrSha}:./${file}`); } - static gitInfo(): IGit { - return git; - } - - static log(fileName: string, sha?: string, repoPath?: string, maxCount?: number, reverse: boolean = false) { - const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); - - const params = [...DefaultLogParams, `--follow`, `--no-merges`]; - if (maxCount && !reverse) { - params.push(`-n${maxCount}`); - } - if (sha) { - if (reverse) { - params.push(`--reverse`); - params.push(`--ancestry-path`); - params.push(`${sha}..HEAD`); - } - else { - params.push(sha); - } - params.push(`--`); - } - - return gitCommand(root, ...params, file); - } - - static logRange(fileName: string, start: number, end: number, sha?: string, repoPath?: string, maxCount?: number) { - const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); - - const params = [...DefaultLogParams, `--no-merges`]; - if (maxCount) { - params.push(`-n${maxCount}`); - } - if (sha) { - params.push(`--follow`); - params.push(sha); - } - params.push(`-L ${start},${end}:${file}`); - - return gitCommand(root, ...params); - } - - static logRepo(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) { + static log(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) { const params = [...DefaultLogParams]; if (maxCount && !reverse) { params.push(`-n${maxCount}`); @@ -207,23 +174,43 @@ export class Git { return gitCommand(repoPath, ...params); } - static statusFile(fileName: string, repoPath: string): Promise { + static log_file(repoPath: string, fileName: string, sha?: string, maxCount?: number, reverse: boolean = false, startLine?: number, endLine?: number) { + const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); + + const params = [...DefaultLogParams, `--no-merges`, `--follow`]; + if (maxCount && !reverse) { + params.push(`-n${maxCount}`); + } + if (sha) { + if (reverse) { + params.push(`--reverse`); + params.push(`--ancestry-path`); + params.push(`${sha}..HEAD`); + } + else { + params.push(sha); + } + } + + if (startLine != null && endLine != null) { + params.push(`-L ${startLine},${endLine}:${file}`); + } + + params.push(`--`); + params.push(file); + + return gitCommand(root, ...params); + } + + static status(repoPath: string): Promise { + const params = ['status', '--short']; + return gitCommand(repoPath, ...params); + } + + static status_file(repoPath: string, fileName: string): Promise { const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath); const params = ['status', file, '--short']; return gitCommand(root, ...params); } - - static statusRepo(repoPath: string): Promise { - const params = ['status', '--short']; - return gitCommand(repoPath, ...params); - } - - static isSha(sha: string) { - return ShaRegex.test(sha); - } - - static isUncommitted(sha: string) { - return UncommittedRegex.test(sha); - } } \ No newline at end of file diff --git a/src/gitService.ts b/src/gitService.ts index 50ac4b1..9a78198 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -294,7 +294,7 @@ export class GitService extends Disposable { } getRepoPath(cwd: string): Promise { - return Git.repoPath(cwd); + return Git.getRepoPath(cwd); } async getRepoPathFromUri(uri?: Uri, fallbackRepoPath?: string): Promise { @@ -338,7 +338,7 @@ export class GitService extends Disposable { return GitService.EmptyPromise as Promise; } - return Git.blame(GitService.BlameFormat, fileName, sha, repoPath) + return Git.blame(repoPath, fileName, GitService.BlameFormat, sha) .then(data => new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName)) .catch(ex => { // Trap and cache expected blame errors @@ -393,7 +393,7 @@ export class GitService extends Disposable { fileName = Git.normalizePath(fileName); try { - const data = await Git.blameLines(GitService.BlameFormat, fileName, line + 1, line + 1, sha, repoPath); + const data = await Git.blame(repoPath, fileName, GitService.BlameFormat, sha, line + 1, line + 1); const blame = new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName); if (!blame) return undefined; @@ -498,7 +498,7 @@ export class GitService extends Disposable { } try { - const data = await Git.logRepo(repoPath, sha, maxCount, reverse); + const data = await Git.log(repoPath, sha, maxCount, reverse); return new GitLogParserEnricher().enrich(data, 'repo', repoPath, maxCount, true, reverse); } catch (ex) { @@ -530,9 +530,7 @@ export class GitService extends Disposable { return GitService.EmptyPromise as Promise; } - return (range - ? Git.logRange(fileName, range.start.line + 1, range.end.line + 1, sha, repoPath, maxCount) - : Git.log(fileName, sha, repoPath, maxCount, reverse)) + return Git.log_file(repoPath, fileName, sha, maxCount, reverse, range && range.start.line + 1, range && range.end.line + 1) .then(data => new GitLogParserEnricher().enrich(data, 'file', repoPath || fileName, maxCount, !!repoPath, reverse)) .catch(ex => { // Trap and cache expected log errors @@ -592,13 +590,13 @@ export class GitService extends Disposable { async getStatusForFile(fileName: string, repoPath: string): Promise { Logger.log(`getStatusForFile('${fileName}', '${repoPath}')`); - const status = await Git.statusFile(fileName, repoPath); + const status = await Git.status_file(repoPath, fileName); return status && status.trim().length && new GitFileStatusItem(repoPath, status); } async getStatusesForRepo(repoPath: string): Promise { Logger.log(`getStatusForRepo('${repoPath}')`); - const statuses = (await Git.statusRepo(repoPath)).split('\n').filter(_ => !!_); + const statuses = (await Git.status(repoPath)).split('\n').filter(_ => !!_); return statuses.map(_ => new GitFileStatusItem(repoPath, _)); } @@ -622,7 +620,7 @@ export class GitService extends Disposable { getVersionedFileText(fileName: string, repoPath: string, sha: string) { Logger.log(`getVersionedFileText('${fileName}', '${repoPath}', ${sha})`); - return Git.getVersionedFileText(fileName, repoPath, sha); + return Git.show(repoPath, fileName, sha); } isEditorBlameable(editor: TextEditor): boolean { @@ -634,7 +632,7 @@ export class GitService extends Disposable { openDirectoryDiff(repoPath: string, sha1: string, sha2?: string) { Logger.log(`openDirectoryDiff('${repoPath}', ${sha1}, ${sha2})`); - return Git.diffDir(repoPath, sha1, sha2); + return Git.difftool_dirDiff(repoPath, sha1, sha2); } toggleCodeLens(editor: TextEditor) {