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

fix issue checking detached when git less than 2.22 #128

Merged
merged 4 commits into from Jan 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions dist/index.js
Expand Up @@ -4799,9 +4799,11 @@ class GitCommandManager {
branchList(remote) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useful script for testing:

      - run: |
          cd ..
          Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.18.0.windows.1/MinGit-2.18.0-64-bit.zip -OutFile mingit.zip
          mkdir mingit
          Expand-Archive -LiteralPath .\mingit.zip -DestinationPath .\mingit
          dir .\mingit
          echo "::add-path::$PWD\mingit\cmd"

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i ran into this bug in git 2.18 when testing. Switching to --symbolic-full-name and trimming refs/heads/ and refs/remotes/ from the output works around the issue.

// 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/');
ericsciple marked this conversation as resolved.
Show resolved Hide resolved
});
}
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