From d959a2bca5d0a2e6edc32f3f970a6ada180a0ce1 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 1 Nov 2019 19:46:34 -0400 Subject: [PATCH] Removes ignore-revs-file flag if blame will fail --- src/git/git.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/git/git.ts b/src/git/git.ts index e8be374..25517f8 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -1,6 +1,7 @@ 'use strict'; /* eslint-disable @typescript-eslint/camelcase */ import * as paths from 'path'; +import * as fs from 'fs'; import * as iconv from 'iconv-lite'; import { GlyphChars } from '../constants'; import { Container } from '../container'; @@ -329,6 +330,8 @@ export namespace Git { return git({ cwd: repoPath, stdin: patch }, ...params); } + const ignoreRevsFileMap = new Map(); + export async function blame( repoPath: string | undefined, fileName: string, @@ -347,6 +350,38 @@ export namespace Git { } if (options.args != null) { params.push(...options.args); + + const index = params.indexOf('--ignore-revs-file'); + if (index !== -1) { + // Ensure the version of Git supports the --ignore-revs-file flag, otherwise the blame will fail + let supported = Git.validateVersion(2, 23); + if (supported) { + let ignoreRevsFile = params[index + 1]; + if (!paths.isAbsolute(ignoreRevsFile)) { + ignoreRevsFile = paths.join(repoPath || '', ignoreRevsFile); + } + + const exists = ignoreRevsFileMap.get(ignoreRevsFile); + if (exists !== undefined) { + supported = exists; + } else { + // Ensure the specified --ignore-revs-file exists, otherwise the blame will fail + try { + supported = await new Promise(resolve => + fs.exists(ignoreRevsFile, exists => resolve(exists)) + ); + } catch { + supported = false; + } + + ignoreRevsFileMap.set(ignoreRevsFile, supported); + } + } + + if (!supported) { + params.splice(index, 2); + } + } } let stdin;