Skip to content

Commit 1d65dde

Browse files
committed
Optimizes get current branch lookup
1 parent d8654c6 commit 1d65dde

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/git/git.ts

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,17 @@ export class Git {
739739
return data.length === 0 ? undefined : data.trim();
740740
}
741741

742+
static async log__recent(repoPath: string) {
743+
const data = await git<string>(
744+
{ cwd: repoPath, errors: GitErrorHandling.Ignore },
745+
'log',
746+
'-n1',
747+
'--format=%H',
748+
'--'
749+
);
750+
return data.length === 0 ? undefined : data.trim();
751+
}
752+
742753
static log__search(repoPath: string, search: string[] = emptyArray, { maxCount }: { maxCount?: number } = {}) {
743754
const params = ['log', '--name-status', `--format=${GitLogParser.defaultFormat}`];
744755
if (maxCount) {
@@ -825,49 +836,32 @@ export class Git {
825836
}
826837

827838
static async rev_parse__currentBranch(repoPath: string): Promise<[string, string | undefined] | undefined> {
828-
const params = ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@', '@{u}'];
829-
830-
const opts: GitCommandOptions = {
831-
cwd: repoPath,
832-
errors: GitErrorHandling.Throw
833-
};
834-
835839
try {
836-
const data = await git<string>(opts, ...params);
840+
const data = await git<string>(
841+
{ cwd: repoPath, errors: GitErrorHandling.Throw },
842+
'rev-parse',
843+
'--abbrev-ref',
844+
'--symbolic-full-name',
845+
'@',
846+
'@{u}',
847+
'--'
848+
);
837849
return [data, undefined];
838850
}
839851
catch (ex) {
840852
const msg = ex && ex.toString();
841-
if (GitWarnings.headNotABranch.test(msg)) {
842-
const data = await git<string>(
843-
{ ...opts, errors: GitErrorHandling.Ignore },
844-
'log',
845-
'-n1',
846-
'--format=%H',
847-
'--'
848-
);
849-
if (data.length === 0) return undefined;
850-
851-
// Matches output of `git branch -vv`
852-
const sha = data.trim();
853-
return [`(HEAD detached at ${this.shortenSha(sha)})`, sha];
853+
if (GitErrors.badRevision.test(msg) || GitWarnings.noUpstream.test(msg)) {
854+
return [ex.stdout, undefined];
854855
}
855856

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

859-
if (GitWarnings.unknownRevision.test(msg)) {
860-
const data = await git<string>(
861-
{ ...opts, errors: GitErrorHandling.Ignore },
862-
'symbolic-ref',
863-
'-q',
864-
'--short',
865-
'HEAD'
866-
);
867-
return data.length === 0 ? undefined : [data.trim(), undefined];
861+
return [`(HEAD detached at ${this.shortenSha(sha)})`, sha];
868862
}
869863

870-
defaultExceptionHandler(ex, opts.cwd);
864+
defaultExceptionHandler(ex, repoPath);
871865
return undefined;
872866
}
873867
}

src/git/shell.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,18 @@ export function run<TOut extends string | Buffer>(
139139
command,
140140
args,
141141
opts,
142-
(error: (Error & { code?: string | number }) | null, stdout, stderr) => {
142+
(error: (Error & { stdout?: TOut | undefined }) | null, stdout, stderr) => {
143143
if (error != null) {
144144
if (bufferExceededRegex.test(error.message)) {
145145
error.message = `Command output exceeded the allocated stdout buffer. Set 'options.maxBuffer' to a larger value than ${
146146
opts.maxBuffer
147147
} bytes`;
148148
}
149149

150+
error.stdout =
151+
encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer'
152+
? (stdout as TOut)
153+
: (iconv.decode(Buffer.from(stdout, 'binary'), encoding) as TOut);
150154
reject(error);
151155

152156
return;

0 commit comments

Comments
 (0)