From de438a4c8fb26b6f69c167dded2cfaec25e111f8 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 2 Feb 2022 13:30:28 -0500 Subject: [PATCH] Improves oldest ref parser --- src/env/node/git/git.ts | 25 +++++++++++++++++-------- src/env/node/git/localGitProvider.ts | 21 ++++++++++++++------- src/git/parsers/logParser.ts | 18 ------------------ 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/env/node/git/git.ts b/src/env/node/git/git.ts index bc4e6e0..af6dfb3 100644 --- a/src/env/node/git/git.ts +++ b/src/env/node/git/git.ts @@ -755,9 +755,11 @@ export class Git { ref: string | undefined, { all, + argsOrFormat, + // TODO@eamodio remove this in favor of argsOrFormat + fileMode = 'full', filters, firstParent = false, - format = 'default', limit, ordering, renames = true, @@ -768,9 +770,11 @@ export class Git { endLine, }: { all?: boolean; + argsOrFormat?: string | string[]; + // TODO@eamodio remove this in favor of argsOrFormat + fileMode?: 'full' | 'simple' | 'none'; filters?: GitDiffFilter[]; firstParent?: boolean; - format?: 'default' | 'refs' | 'simple'; limit?: number; ordering?: string | null; renames?: boolean; @@ -783,10 +787,15 @@ export class Git { ) { const [file, root] = splitPath(fileName, repoPath, true); - const params = [ - 'log', - `--format=${format === 'default' ? GitLogParser.defaultFormat : GitLogParser.simpleFormat}`, - ]; + if (argsOrFormat == null) { + argsOrFormat = [`--format=${GitLogParser.defaultFormat}`]; + } + + if (typeof argsOrFormat === 'string') { + argsOrFormat = [`--format=${argsOrFormat}`]; + } + + const params = ['log', ...argsOrFormat]; if (ordering) { params.push(`--${ordering}-order`); @@ -826,10 +835,10 @@ export class Git { params.push(`--diff-filter=${filters.join('')}`); } - if (format !== 'refs') { + if (fileMode !== 'none') { if (startLine == null) { // If this is the log of a folder, use `--name-status` to match non-file logs (for parsing) - if (format === 'simple' || isFolderGlob(file)) { + if (fileMode === 'simple' || isFolderGlob(file)) { params.push('--name-status'); } else { params.push('--numstat', '--summary'); diff --git a/src/env/node/git/localGitProvider.ts b/src/env/node/git/localGitProvider.ts index e0b387e..e833822 100644 --- a/src/env/node/git/localGitProvider.ts +++ b/src/env/node/git/localGitProvider.ts @@ -633,8 +633,9 @@ export class LocalGitProvider implements GitProvider, Disposable { // Now check if that commit had any renames data = await this.git.log__file(repoPath, '.', ref, { + argsOrFormat: GitLogParser.simpleFormat, + fileMode: 'simple', filters: ['R', 'C', 'D'], - format: 'simple', limit: 1, ordering: this.container.config.advanced.commitOrdering, }); @@ -1419,13 +1420,16 @@ export class LocalGitProvider implements GitProvider, Disposable { const [path, root] = splitPath(uri.fsPath, repoPath); const data = await this.git.log__file(root, path, '@{push}..', { - format: 'refs', + argsOrFormat: ['-z', '--format=%H'], + fileMode: 'none', ordering: this.container.config.advanced.commitOrdering, renames: true, }); - if (data == null || data.length === 0) return undefined; + if (!data) return undefined; - return GitLogParser.parseLastRefOnly(data); + // -2 to skip the ending null + const index = data.lastIndexOf('\0', data.length - 2); + return index === -1 ? undefined : data.slice(index + 1, data.length - 2); } @log() @@ -2709,8 +2713,9 @@ export class LocalGitProvider implements GitProvider, Disposable { const fileName = GitUri.relativeTo(uri, repoPath); let data = await this.git.log__file(repoPath, fileName, ref, { + argsOrFormat: GitLogParser.simpleFormat, + fileMode: 'simple', filters: filters, - format: 'simple', limit: skip + 1, ordering: this.container.config.advanced.commitOrdering, reverse: true, @@ -2722,8 +2727,9 @@ export class LocalGitProvider implements GitProvider, Disposable { // If the file was deleted, check for a possible rename if (status === 'D') { data = await this.git.log__file(repoPath, '.', nextRef, { + argsOrFormat: GitLogParser.simpleFormat, + fileMode: 'simple', filters: ['R', 'C'], - format: 'simple', limit: 1, ordering: this.container.config.advanced.commitOrdering, // startLine: editorLine != null ? editorLine + 1 : undefined @@ -2962,8 +2968,9 @@ export class LocalGitProvider implements GitProvider, Disposable { let data; try { data = await this.git.log__file(repoPath, path, ref, { + argsOrFormat: GitLogParser.simpleFormat, + fileMode: 'simple', firstParent: firstParent, - format: 'simple', limit: skip + 2, ordering: this.container.config.advanced.commitOrdering, startLine: editorLine != null ? editorLine + 1 : undefined, diff --git a/src/git/parsers/logParser.ts b/src/git/parsers/logParser.ts index 7467d4b..192d10f 100644 --- a/src/git/parsers/logParser.ts +++ b/src/git/parsers/logParser.ts @@ -23,7 +23,6 @@ const fileStatusAndSummaryRegex = /^(\d+?|-)\s+?(\d+?|-)\s+?(.*)(?:\n\s(delete|r const fileStatusAndSummaryRenamedFileRegex = /(.+)\s=>\s(.+)/; const fileStatusAndSummaryRenamedFilePathRegex = /(.*?){(.+?)\s=>\s(.*?)}(.*)/; -const logRefsRegex = /^ (.*)/gm; const logFileSimpleRegex = /^ (.*)\s*(?:(?:diff --git a\/(.*) b\/(.*))|(?:(\S)\S*\t([^\t\n]+)(?:\t(.+))?))/gm; const logFileSimpleRenamedRegex = /^ (\S+)\s*(.*)$/s; const logFileSimpleRenamedFilesRegex = /^(\S)\S*\t([^\t\n]+)(?:\t(.+)?)?$/gm; @@ -645,23 +644,6 @@ export class GitLogParser { } @debug({ args: false }) - static parseLastRefOnly(data: string): string | undefined { - let ref; - let match; - do { - match = logRefsRegex.exec(data); - if (match == null) break; - - [, ref] = match; - } while (true); - - // Ensure the regex state is reset - logRefsRegex.lastIndex = 0; - - return ref; - } - - @debug({ args: false }) static parseSimple( data: string, skip: number,