Skip to content

Commit

Permalink
Optimizes get current branch lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed May 15, 2019
1 parent d8654c6 commit 1d65dde
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
60 changes: 27 additions & 33 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,17 @@ export class Git {
return data.length === 0 ? undefined : data.trim();
}

static async log__recent(repoPath: string) {
const data = await git<string>(
{ cwd: repoPath, errors: GitErrorHandling.Ignore },
'log',
'-n1',
'--format=%H',
'--'
);
return data.length === 0 ? undefined : data.trim();
}

static log__search(repoPath: string, search: string[] = emptyArray, { maxCount }: { maxCount?: number } = {}) {
const params = ['log', '--name-status', `--format=${GitLogParser.defaultFormat}`];
if (maxCount) {
Expand Down Expand Up @@ -825,49 +836,32 @@ export class Git {
}

static async rev_parse__currentBranch(repoPath: string): Promise<[string, string | undefined] | undefined> {
const params = ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@', '@{u}'];

const opts: GitCommandOptions = {
cwd: repoPath,
errors: GitErrorHandling.Throw
};

try {
const data = await git<string>(opts, ...params);
const data = await git<string>(
{ cwd: repoPath, errors: GitErrorHandling.Throw },
'rev-parse',
'--abbrev-ref',
'--symbolic-full-name',
'@',
'@{u}',
'--'
);
return [data, undefined];
}
catch (ex) {
const msg = ex && ex.toString();
if (GitWarnings.headNotABranch.test(msg)) {
const data = await git<string>(
{ ...opts, errors: GitErrorHandling.Ignore },
'log',
'-n1',
'--format=%H',
'--'
);
if (data.length === 0) return undefined;

// Matches output of `git branch -vv`
const sha = data.trim();
return [`(HEAD detached at ${this.shortenSha(sha)})`, sha];
if (GitErrors.badRevision.test(msg) || GitWarnings.noUpstream.test(msg)) {
return [ex.stdout, undefined];
}

const result = GitWarnings.noUpstream.exec(msg);
if (result !== null) return [result[1], undefined];
if (GitWarnings.headNotABranch.test(msg)) {
const sha = await this.log__recent(repoPath);
if (sha === undefined) return undefined;

if (GitWarnings.unknownRevision.test(msg)) {
const data = await git<string>(
{ ...opts, errors: GitErrorHandling.Ignore },
'symbolic-ref',
'-q',
'--short',
'HEAD'
);
return data.length === 0 ? undefined : [data.trim(), undefined];
return [`(HEAD detached at ${this.shortenSha(sha)})`, sha];
}

defaultExceptionHandler(ex, opts.cwd);
defaultExceptionHandler(ex, repoPath);
return undefined;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/git/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ export function run<TOut extends string | Buffer>(
command,
args,
opts,
(error: (Error & { code?: string | number }) | null, stdout, stderr) => {
(error: (Error & { stdout?: TOut | undefined }) | null, stdout, stderr) => {
if (error != null) {
if (bufferExceededRegex.test(error.message)) {
error.message = `Command output exceeded the allocated stdout buffer. Set 'options.maxBuffer' to a larger value than ${
opts.maxBuffer
} bytes`;
}

error.stdout =
encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer'
? (stdout as TOut)
: (iconv.decode(Buffer.from(stdout, 'binary'), encoding) as TOut);
reject(error);

return;
Expand Down

0 comments on commit 1d65dde

Please sign in to comment.