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 switch from a detached head to a branch not working #1218

Merged
merged 10 commits into from
Mar 3, 2023
9 changes: 6 additions & 3 deletions src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,14 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
* @returns React element
*/
private _renderBranchMenu(): React.ReactElement | null {
let branchTitle = this.props.trans.__('Current Branch');
if (this.props.model.pathRepository === null) {
return null;
}
if (this.props.model.currentBranch?.detached) {
branchTitle = this.props.trans.__('Detached Head at');
}

return (
<div className={toolbarMenuWrapperClass}>
<button
Expand All @@ -275,9 +280,7 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
>
<branchIcon.react className={toolbarMenuButtonIconClass} />
<div className={toolbarMenuButtonTitleWrapperClass}>
<p className={toolbarMenuButtonTitleClass}>
{this.props.trans.__('Current Branch')}
</p>
<p className={toolbarMenuButtonTitleClass}>{branchTitle}</p>
<p className={toolbarMenuButtonSubtitleClass}>
{this.props.currentBranch || ''}
</p>
Expand Down
9 changes: 9 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,15 @@ export class GitExtension implements IGitExtension {
);

this._branches = data.branches ?? [];

const detachedHeadRegex = /\(HEAD detached at (.+)\)/;
const result = data.current_branch.name.match(detachedHeadRegex);

if (result && result.length > 1) {
data.current_branch.name = result[1];
data.current_branch.detached = true;
}

this._currentBranch = data.current_branch;
if (this._currentBranch) {
// Set up the marker obj for the current (valid) repo/branch combination
Expand Down
1 change: 1 addition & 0 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ export namespace Git {
upstream: string | null;
top_commit: string;
tag: string | null;
detached?: boolean;
}

/** Interface for GitBranch request result,
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const defaultMockedResponses: {
return {
code: 0,
branches: [],
current_branch: null
current_branch: { name: "" }
};
}
},
Expand Down