Skip to content

Commit 92a9626

Browse files
authored
fix switch from a detached head to a branch not working (#1218)
* fix switch from a detached head to a branch not working by transforming the fake branch name (HEAD detached at #####) to ##### * prettify and format correctly * move detached head fix to refreshBranch() * prettify and format correctly * fix default mocked response for branch * remove change to the current branch tag attribute as the detached head case can occur when the git repository is checked out at a specific commit that is not a branch head * add detached flag to IBranch, and tune the branch display according to the flag * add optional chaining to short circuit and remove error that is thrown * relocate title translate
1 parent 35f712f commit 92a9626

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/components/Toolbar.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,14 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
260260
* @returns React element
261261
*/
262262
private _renderBranchMenu(): React.ReactElement | null {
263+
let branchTitle = this.props.trans.__('Current Branch');
263264
if (this.props.model.pathRepository === null) {
264265
return null;
265266
}
267+
if (this.props.model.currentBranch?.detached) {
268+
branchTitle = this.props.trans.__('Detached Head at');
269+
}
270+
266271
return (
267272
<div className={toolbarMenuWrapperClass}>
268273
<button
@@ -275,9 +280,7 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
275280
>
276281
<branchIcon.react className={toolbarMenuButtonIconClass} />
277282
<div className={toolbarMenuButtonTitleWrapperClass}>
278-
<p className={toolbarMenuButtonTitleClass}>
279-
{this.props.trans.__('Current Branch')}
280-
</p>
283+
<p className={toolbarMenuButtonTitleClass}>{branchTitle}</p>
281284
<p className={toolbarMenuButtonSubtitleClass}>
282285
{this.props.currentBranch || ''}
283286
</p>

src/model.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,15 @@ export class GitExtension implements IGitExtension {
10461046
);
10471047

10481048
this._branches = data.branches ?? [];
1049+
1050+
const detachedHeadRegex = /\(HEAD detached at (.+)\)/;
1051+
const result = data.current_branch.name.match(detachedHeadRegex);
1052+
1053+
if (result && result.length > 1) {
1054+
data.current_branch.name = result[1];
1055+
data.current_branch.detached = true;
1056+
}
1057+
10491058
this._currentBranch = data.current_branch;
10501059
if (this._currentBranch) {
10511060
// Set up the marker obj for the current (valid) repo/branch combination

src/tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ export namespace Git {
820820
upstream: string | null;
821821
top_commit: string;
822822
tag: string | null;
823+
detached?: boolean;
823824
}
824825

825826
/** Interface for GitBranch request result,

tests/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const defaultMockedResponses: {
2828
return {
2929
code: 0,
3030
branches: [],
31-
current_branch: null
31+
current_branch: { name: "" }
3232
};
3333
}
3434
},

0 commit comments

Comments
 (0)