diff --git a/src/git/git.ts b/src/git/git.ts index f2b8514..aa83ceb 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -34,7 +34,8 @@ const GitWarnings = [ /does not have any commits/, /Path \'.*?\' does not exist in/, /Path \'.*?\' exists on disk, but not in/, - /no upstream configured for branch/ + /no upstream configured for branch/, + /ambiguous argument '.*?': unknown revision or path not in the working tree/ ]; interface GitCommandOptions { @@ -436,10 +437,19 @@ export class Git { return data; } catch (ex) { - if (/HEAD does not point to a branch/.test(ex && ex.toString())) return undefined; - - if (/no upstream configured for branch/.test(ex && ex.toString())) { - return ex.message.split('\n')[0]; + const msg = ex && ex.toString(); + if (/HEAD does not point to a branch/.test(msg)) return undefined; + if (/no upstream configured for branch/.test(msg)) return ex.message.split('\n')[0]; + + if (/ambiguous argument '.*?': unknown revision or path not in the working tree/.test(msg)) { + try { + const params = [`symbolic-ref`, `-q`, `--short`, `HEAD`]; + const data = await gitCommand(opts, ...params); + return data; + } + catch { + return undefined; + } } return gitCommandDefaultErrorHandler(ex, opts, ...params); diff --git a/src/gitService.ts b/src/gitService.ts index 59dac11..226ed0d 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -692,6 +692,12 @@ export class GitService extends Disposable { Logger.log(`getBranches('${repoPath}')`); const data = await Git.branch(repoPath, { all: true }); + // If we don't get any data, assume the repo doesn't have any commits yet so check if we have a current branch + if (data === '') { + const current = await this.getBranch(repoPath); + return current !== undefined ? [current] : []; + } + return GitBranchParser.parse(data, repoPath) || []; }