Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git - Attempt to parse HEAD file before invoking git.exe #162572

Merged
merged 5 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions extensions/git/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { detectEncoding } from './encoding';
import { Ref, RefType, Branch, Remote, ForcePushMode, GitErrorCodes, LogOptions, Change, Status, CommitOptions, BranchQuery } from './api/git';
import * as byline from 'byline';
import { StringDecoder } from 'string_decoder';
import { OutputChannelLogger } from './log';
import TelemetryReporter from '@vscode/extension-telemetry';

// https://github.com/microsoft/vscode/issues/65693
Expand Down Expand Up @@ -400,8 +401,8 @@ export class Git {
return Versions.compare(Versions.fromString(this.version), Versions.fromString(version));
}

open(repository: string, dotGit: { path: string; commonPath?: string }): Repository {
return new Repository(this, repository, dotGit);
open(repository: string, dotGit: { path: string; commonPath?: string }, outputChannelLogger: OutputChannelLogger): Repository {
return new Repository(this, repository, dotGit, outputChannelLogger);
}

async init(repository: string): Promise<void> {
Expand Down Expand Up @@ -913,7 +914,8 @@ export class Repository {
constructor(
private _git: Git,
private repositoryRoot: string,
readonly dotGit: { path: string; commonPath?: string }
readonly dotGit: { path: string; commonPath?: string },
private outputChannelLogger: OutputChannelLogger
) { }

get git(): Git {
Expand Down Expand Up @@ -1996,22 +1998,52 @@ export class Repository {

async getHEAD(): Promise<Ref> {
try {
// Attempt to parse the HEAD file
const result = await this.getHEADFS();
return result;
}
catch (err) {
this.outputChannelLogger.logWarning(err.message);
}

try {
// Fallback to using git to determine HEAD
const result = await this.exec(['symbolic-ref', '--short', 'HEAD']);

if (!result.stdout) {
throw new Error('Not in a branch');
}

return { name: result.stdout.trim(), commit: undefined, type: RefType.Head };
} catch (err) {
const result = await this.exec(['rev-parse', 'HEAD']);
}
catch (err) { }

if (!result.stdout) {
throw new Error('Error parsing HEAD');
}
// Detached HEAD
const result = await this.exec(['rev-parse', 'HEAD']);

return { name: undefined, commit: result.stdout.trim(), type: RefType.Head };
if (!result.stdout) {
throw new Error('Error parsing HEAD');
}

return { name: undefined, commit: result.stdout.trim(), type: RefType.Head };
}

async getHEADFS(): Promise<Ref> {
const raw = await fs.readFile(path.join(this.dotGit.commonPath ?? this.dotGit.path, 'HEAD'), 'utf8');

// Branch
const branchMatch = raw.match(/^ref: refs\/heads\/(?<name>.*)$/m);
if (branchMatch?.groups?.name) {
return { name: branchMatch.groups.name, commit: undefined, type: RefType.Head };
}

// Detached
const commitMatch = raw.match(/^(?<commit>[0-9a-f]{40})$/m);
if (commitMatch?.groups?.commit) {
return { name: undefined, commit: commitMatch.groups.commit, type: RefType.Head };
}

throw new Error(`Unable to parse HEAD file. HEAD file contents: ${raw}.`);
}

async findTrackingBranches(upstreamBranch: string): Promise<Branch[]> {
Expand Down
2 changes: 1 addition & 1 deletion extensions/git/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export class Model implements IRemoteSourcePublisherRegistry, IPostCommitCommand
}

const dotGit = await this.git.getRepositoryDotGit(repositoryRoot);
const repository = new Repository(this.git.open(repositoryRoot, dotGit), this, this, this, this.globalState, this.outputChannelLogger, this.telemetryReporter);
const repository = new Repository(this.git.open(repositoryRoot, dotGit, this.outputChannelLogger), this, this, this, this.globalState, this.outputChannelLogger, this.telemetryReporter);

this.open(repository);
repository.status(); // do not await this, we want SCM to know about the repo asap
Expand Down