Skip to content

Commit ae525b2

Browse files
authored
fix issue checking detached when git less than 2.22 (#128)
1 parent f466b96 commit ae525b2

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

dist/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,9 +4799,11 @@ class GitCommandManager {
47994799
branchList(remote) {
48004800
return __awaiter(this, void 0, void 0, function* () {
48014801
const result = [];
4802-
// Note, this implementation uses "rev-parse --symbolic" because the output from
4802+
// Note, this implementation uses "rev-parse --symbolic-full-name" because the output from
48034803
// "branch --list" is more difficult when in a detached HEAD state.
4804-
const args = ['rev-parse', '--symbolic'];
4804+
// Note, this implementation uses "rev-parse --symbolic-full-name" because there is a bug
4805+
// in Git 2.18 that causes "rev-parse --symbolic" to output symbolic full names.
4806+
const args = ['rev-parse', '--symbolic-full-name'];
48054807
if (remote) {
48064808
args.push('--remotes=origin');
48074809
}
@@ -4812,6 +4814,12 @@ class GitCommandManager {
48124814
for (let branch of output.stdout.trim().split('\n')) {
48134815
branch = branch.trim();
48144816
if (branch) {
4817+
if (branch.startsWith('refs/heads/')) {
4818+
branch = branch.substr('refs/heads/'.length);
4819+
}
4820+
else if (branch.startsWith('refs/remotes/')) {
4821+
branch = branch.substr('refs/remotes/'.length);
4822+
}
48154823
result.push(branch);
48164824
}
48174825
}
@@ -4887,11 +4895,9 @@ class GitCommandManager {
48874895
}
48884896
isDetached() {
48894897
return __awaiter(this, void 0, void 0, function* () {
4890-
// Note, this implementation uses "branch --show-current" because
4891-
// "rev-parse --symbolic-full-name HEAD" can fail on a new repo
4892-
// with nothing checked out.
4893-
const output = yield this.execGit(['branch', '--show-current']);
4894-
return output.stdout.trim() === '';
4898+
// Note, "branch --show-current" would be simpler but isn't available until Git 2.22
4899+
const output = yield this.execGit(['rev-parse', '--symbolic-full-name', '--verify', '--quiet', 'HEAD'], true);
4900+
return !output.stdout.trim().startsWith('refs/heads/');
48954901
});
48964902
}
48974903
lfsFetch(ref) {

src/git-command-manager.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ class GitCommandManager {
7777
async branchList(remote: boolean): Promise<string[]> {
7878
const result: string[] = []
7979

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

83-
const args = ['rev-parse', '--symbolic']
85+
const args = ['rev-parse', '--symbolic-full-name']
8486
if (remote) {
8587
args.push('--remotes=origin')
8688
} else {
@@ -92,6 +94,12 @@ class GitCommandManager {
9294
for (let branch of output.stdout.trim().split('\n')) {
9395
branch = branch.trim()
9496
if (branch) {
97+
if (branch.startsWith('refs/heads/')) {
98+
branch = branch.substr('refs/heads/'.length)
99+
} else if (branch.startsWith('refs/remotes/')) {
100+
branch = branch.substr('refs/remotes/'.length)
101+
}
102+
95103
result.push(branch)
96104
}
97105
}
@@ -170,12 +178,12 @@ class GitCommandManager {
170178
}
171179

172180
async isDetached(): Promise<boolean> {
173-
// Note, this implementation uses "branch --show-current" because
174-
// "rev-parse --symbolic-full-name HEAD" can fail on a new repo
175-
// with nothing checked out.
176-
177-
const output = await this.execGit(['branch', '--show-current'])
178-
return output.stdout.trim() === ''
181+
// Note, "branch --show-current" would be simpler but isn't available until Git 2.22
182+
const output = await this.execGit(
183+
['rev-parse', '--symbolic-full-name', '--verify', '--quiet', 'HEAD'],
184+
true
185+
)
186+
return !output.stdout.trim().startsWith('refs/heads/')
179187
}
180188

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

0 commit comments

Comments
 (0)