Browse Source

Handles detached head in getBranch call

main
Eric Amodio 4 years ago
parent
commit
da4fefbc1c
2 changed files with 23 additions and 2 deletions
  1. +21
    -1
      src/git/gitService.ts
  2. +2
    -1
      src/git/models/branch.ts

+ 21
- 1
src/git/gitService.ts View File

@ -1036,7 +1036,27 @@ export class GitService implements Disposable {
async getBranch(repoPath: string | undefined): Promise<GitBranch | undefined> {
if (repoPath == null) return undefined;
const [branch] = await this.getBranches(repoPath, { filter: b => b.current });
let [branch] = await this.getBranches(repoPath, { filter: b => b.current });
if (branch != null) return branch;
const data = await Git.rev_parse__currentBranch(repoPath);
if (data == null) return undefined;
const [name, tracking] = data[0].split('\n');
if (GitBranch.isDetached(name)) {
const committerDate = await Git.log__recent_committerdate(repoPath);
branch = new GitBranch(
repoPath,
name,
false,
true,
committerDate == null ? undefined : new Date(Number(committerDate) * 1000),
data[1],
tracking,
);
}
return branch;
}

+ 2
- 1
src/git/models/branch.ts View File

@ -8,6 +8,7 @@ import { GitBranchReference, GitReference } from './models';
import { BranchSorting, configuration, DateStyle } from '../../configuration';
const whitespaceRegex = /\s/;
const detachedHEADRegex = /^(?=.*\bHEAD\b)(?=.*\bdetached\b).*$/;
export const BranchDateFormatting = {
dateFormat: undefined! as string | null,
@ -220,6 +221,6 @@ export class GitBranch implements GitBranchReference {
static isDetached(name: string): boolean {
// If there is whitespace in the name assume this is not a valid branch name
// Deals with detached HEAD states
return whitespaceRegex.test(name) || name.includes('(detached)');
return whitespaceRegex.test(name) || detachedHEADRegex.test(name);
}
}

Loading…
Cancel
Save