Skip to content

Commit

Permalink
fix issue checking detached when git less than 2.22 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple committed Jan 3, 2020
1 parent f466b96 commit ae525b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
20 changes: 13 additions & 7 deletions dist/index.js
Expand Up @@ -4799,9 +4799,11 @@ class GitCommandManager {
branchList(remote) {
return __awaiter(this, void 0, void 0, function* () {
const result = [];
// Note, this implementation uses "rev-parse --symbolic" because the output from
// Note, this implementation uses "rev-parse --symbolic-full-name" because the output from
// "branch --list" is more difficult when in a detached HEAD state.
const args = ['rev-parse', '--symbolic'];
// Note, this implementation uses "rev-parse --symbolic-full-name" because there is a bug
// in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names.
const args = ['rev-parse', '--symbolic-full-name'];
if (remote) {
args.push('--remotes=origin');
}
Expand All @@ -4812,6 +4814,12 @@ class GitCommandManager {
for (let branch of output.stdout.trim().split('\n')) {
branch = branch.trim();
if (branch) {
if (branch.startsWith('refs/heads/')) {
branch = branch.substr('refs/heads/'.length);
}
else if (branch.startsWith('refs/remotes/')) {
branch = branch.substr('refs/remotes/'.length);
}
result.push(branch);
}
}
Expand Down Expand Up @@ -4887,11 +4895,9 @@ class GitCommandManager {
}
isDetached() {
return __awaiter(this, void 0, void 0, function* () {
// Note, this implementation uses "branch --show-current" because
// "rev-parse --symbolic-full-name HEAD" can fail on a new repo
// with nothing checked out.
const output = yield this.execGit(['branch', '--show-current']);
return output.stdout.trim() === '';
// Note, "branch --show-current" would be simpler but isn't available until Git 2.22
const output = yield this.execGit(['rev-parse', '--symbolic-full-name', '--verify', '--quiet', 'HEAD'], true);
return !output.stdout.trim().startsWith('refs/heads/');
});
}
lfsFetch(ref) {
Expand Down
24 changes: 16 additions & 8 deletions src/git-command-manager.ts
Expand Up @@ -77,10 +77,12 @@ class GitCommandManager {
async branchList(remote: boolean): Promise<string[]> {
const result: string[] = []

// Note, this implementation uses "rev-parse --symbolic" because the output from
// Note, this implementation uses "rev-parse --symbolic-full-name" because the output from
// "branch --list" is more difficult when in a detached HEAD state.
// Note, this implementation uses "rev-parse --symbolic-full-name" because there is a bug
// in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names.

const args = ['rev-parse', '--symbolic']
const args = ['rev-parse', '--symbolic-full-name']
if (remote) {
args.push('--remotes=origin')
} else {
Expand All @@ -92,6 +94,12 @@ class GitCommandManager {
for (let branch of output.stdout.trim().split('\n')) {
branch = branch.trim()
if (branch) {
if (branch.startsWith('refs/heads/')) {
branch = branch.substr('refs/heads/'.length)
} else if (branch.startsWith('refs/remotes/')) {
branch = branch.substr('refs/remotes/'.length)
}

result.push(branch)
}
}
Expand Down Expand Up @@ -170,12 +178,12 @@ class GitCommandManager {
}

async isDetached(): Promise<boolean> {
// Note, this implementation uses "branch --show-current" because
// "rev-parse --symbolic-full-name HEAD" can fail on a new repo
// with nothing checked out.

const output = await this.execGit(['branch', '--show-current'])
return output.stdout.trim() === ''
// Note, "branch --show-current" would be simpler but isn't available until Git 2.22
const output = await this.execGit(
['rev-parse', '--symbolic-full-name', '--verify', '--quiet', 'HEAD'],
true
)
return !output.stdout.trim().startsWith('refs/heads/')
}

async lfsFetch(ref: string): Promise<void> {
Expand Down

0 comments on commit ae525b2

Please sign in to comment.