@@ -6,30 +6,30 @@ import {
66} from '@jupyterlab/apputils' ;
77import { TranslationBundle } from '@jupyterlab/translation' ;
88import { CommandRegistry } from '@lumino/commands' ;
9+ import ClearIcon from '@mui/icons-material/Clear' ;
910import List from '@mui/material/List' ;
1011import ListItem from '@mui/material/ListItem' ;
11- import ClearIcon from '@mui/icons-material/Clear' ;
1212import * as React from 'react' ;
1313import { FixedSizeList , ListChildComponentProps } from 'react-window' ;
1414import { classes } from 'typestyle' ;
15+ import { showError } from '../notifications' ;
1516import { hiddenButtonStyle } from '../style/ActionButtonStyle' ;
1617import {
1718 activeListItemClass ,
18- nameClass ,
1919 filterClass ,
2020 filterClearClass ,
2121 filterInputClass ,
2222 filterWrapperClass ,
2323 listItemClass ,
2424 listItemIconClass ,
25+ nameClass ,
2526 newBranchButtonClass ,
2627 wrapperClass
2728} from '../style/BranchMenu' ;
2829import { branchIcon , mergeIcon , trashIcon } from '../style/icons' ;
2930import { CommandIDs , Git , IGitExtension } from '../tokens' ;
3031import { ActionButton } from './ActionButton' ;
3132import { NewBranchDialog } from './NewBranchDialog' ;
32- import { showError } from '../notifications' ;
3333
3434const ITEM_HEIGHT = 24.8 ; // HTML element height for a single branch
3535const MIN_HEIGHT = 150 ; // Minimal HTML element height for the branches list
@@ -274,7 +274,10 @@ export class BranchMenu extends React.Component<
274274 listItemClass ,
275275 isActive ? activeListItemClass : null
276276 ) }
277- onClick = { this . _onBranchClickFactory ( branch . name ) }
277+ onClick = { this . _onBranchClickFactory (
278+ branch . name ,
279+ branch . is_remote_branch
280+ ) }
278281 role = "listitem"
279282 style = { style }
280283 >
@@ -425,7 +428,7 @@ export class BranchMenu extends React.Component<
425428 * @param branch - branch name
426429 * @returns callback
427430 */
428- private _onBranchClickFactory ( branch : string ) {
431+ private _onBranchClickFactory ( branch : string , isRemote : boolean ) {
429432 /**
430433 * Callback invoked upon clicking a branch name.
431434 *
@@ -441,6 +444,9 @@ export class BranchMenu extends React.Component<
441444 ) ;
442445 return ;
443446 }
447+
448+ let message = 'Switched branch.' ;
449+ let type : Notification . TypeOptions = 'success' ;
444450 const opts = {
445451 branchname : branch
446452 } ;
@@ -452,15 +458,42 @@ export class BranchMenu extends React.Component<
452458 ) ;
453459
454460 try {
455- await this . props . model . checkout ( opts ) ;
461+ let shouldCheckout = true ;
462+ const localBranchname = branch . split ( '/' ) . slice ( - 1 ) [ 0 ] ;
463+ const localBranchExists = this . props . model . branches . find (
464+ branch => branch . name === localBranchname
465+ ) ;
466+
467+ if ( localBranchExists && isRemote ) {
468+ const result = await showDialog ( {
469+ title : this . props . trans . __ ( 'Confirm Checkout' ) ,
470+ body : this . props . trans . __ (
471+ 'Checking out this branch will reset your local copy to the remote. Any changes not pushed to the remote will be lost. Are you sure you want to continue?'
472+ ) ,
473+ buttons : [
474+ Dialog . cancelButton ( { label : this . props . trans . __ ( 'Cancel' ) } ) ,
475+ Dialog . warnButton ( { label : this . props . trans . __ ( 'Continue' ) } )
476+ ]
477+ } ) ;
478+
479+ if ( ! result . button . accept ) {
480+ shouldCheckout = false ;
481+ message = 'Checkout cancelled.' ;
482+ type = 'warning' ;
483+ }
484+ }
485+
486+ if ( shouldCheckout ) {
487+ await this . props . model . checkout ( opts ) ;
488+ }
456489 } catch ( err ) {
457490 return onBranchError ( err , id , this . props . trans ) ;
458491 }
459492
460493 Notification . update ( {
461494 id,
462- message : this . props . trans . __ ( 'Switched branch.' ) ,
463- type : 'success' ,
495+ message : this . props . trans . __ ( message ) ,
496+ type,
464497 autoClose : 5000
465498 } ) ;
466499 } ;
0 commit comments