@@ -6,20 +6,20 @@ import {
6
6
showDialog ,
7
7
showErrorMessage
8
8
} from '@jupyterlab/apputils' ;
9
+ import { PathExt } from '@jupyterlab/coreutils' ;
9
10
import { FileBrowser } from '@jupyterlab/filebrowser' ;
10
11
import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
11
12
import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
12
13
import { ITerminal } from '@jupyterlab/terminal' ;
13
14
import { CommandRegistry } from '@lumino/commands' ;
14
15
import { Menu } from '@lumino/widgets' ;
16
+ import * as React from 'react' ;
17
+ import { openDiffView } from './components/diff/DiffWidget' ;
18
+ import { GitExtension } from './model' ;
15
19
import { Git } from './tokens' ;
16
20
import { GitCredentialsForm } from './widgets/CredentialsBox' ;
17
21
import { doGitClone } from './widgets/gitClone' ;
18
22
import { GitPullPushDialog , Operation } from './widgets/gitPushPull' ;
19
- import { openListedFile , discardChanges } from './utils' ;
20
- import { GitExtension } from './model' ;
21
- import { openDiffView } from './components/diff/DiffWidget' ;
22
- import { PathExt } from '@jupyterlab/coreutils' ;
23
23
24
24
const RESOURCES = [
25
25
{
@@ -254,10 +254,28 @@ export function addCommands(
254
254
label : 'Open' ,
255
255
caption : 'Open selected file' ,
256
256
execute : async args => {
257
- const selectedFile : Git . IStatusFileResult = args as any ;
258
- await openListedFile ( selectedFile , model ) ;
259
- } ,
260
- isEnabled : args => args && args . to !== undefined
257
+ const file : Git . IStatusFileResult = args as any ;
258
+
259
+ const { x, y, to } = file ;
260
+ if ( x === 'D' || y === 'D' ) {
261
+ await showErrorMessage (
262
+ 'Open File Failed' ,
263
+ 'This file has been deleted!'
264
+ ) ;
265
+ return ;
266
+ }
267
+ try {
268
+ if ( to [ to . length - 1 ] !== '/' ) {
269
+ commands . execute ( 'docmanager:open' , {
270
+ path : model . getRelativeFilePath ( to )
271
+ } ) ;
272
+ } else {
273
+ console . log ( 'Cannot open a folder here' ) ;
274
+ }
275
+ } catch ( err ) {
276
+ console . error ( `Fail to open ${ to } .` ) ;
277
+ }
278
+ }
261
279
} ) ;
262
280
263
281
commands . addCommand ( CommandIDs . gitFileDiffWorking , {
@@ -275,8 +293,7 @@ export function addCommands(
275
293
renderMime ,
276
294
! selectedFile . is_binary
277
295
) ;
278
- } ,
279
- isEnabled : args => args && args . to !== undefined
296
+ }
280
297
} ) ;
281
298
282
299
commands . addCommand ( CommandIDs . gitFileDiffIndex , {
@@ -294,8 +311,7 @@ export function addCommands(
294
311
renderMime ,
295
312
! selectedFile . is_binary
296
313
) ;
297
- } ,
298
- isEnabled : args => args && args . to !== undefined
314
+ }
299
315
} ) ;
300
316
301
317
commands . addCommand ( CommandIDs . gitFileStage , {
@@ -304,8 +320,7 @@ export function addCommands(
304
320
execute : async args => {
305
321
const selectedFile : Git . IStatusFile = args as any ;
306
322
await model . add ( selectedFile . to ) ;
307
- } ,
308
- isEnabled : args => args && args . to !== undefined
323
+ }
309
324
} ) ;
310
325
311
326
commands . addCommand ( CommandIDs . gitFileTrack , {
@@ -314,8 +329,7 @@ export function addCommands(
314
329
execute : async args => {
315
330
const selectedFile : Git . IStatusFile = args as any ;
316
331
await model . add ( selectedFile . to ) ;
317
- } ,
318
- isEnabled : args => args && args . to !== undefined
332
+ }
319
333
} ) ;
320
334
321
335
commands . addCommand ( CommandIDs . gitFileUnstage , {
@@ -326,18 +340,47 @@ export function addCommands(
326
340
if ( selectedFile . x !== 'D' ) {
327
341
await model . reset ( selectedFile . to ) ;
328
342
}
329
- } ,
330
- isEnabled : args => args && args . to !== undefined
343
+ }
331
344
} ) ;
332
345
333
346
commands . addCommand ( CommandIDs . gitFileDiscard , {
334
347
label : 'Discard' ,
335
348
caption : 'Discard recent changes of selected file' ,
336
349
execute : async args => {
337
- const selectedFile : Git . IStatusFile = args as any ;
338
- await discardChanges ( selectedFile , model ) ;
339
- } ,
340
- isEnabled : args => args && args . to !== undefined
350
+ const file : Git . IStatusFile = args as any ;
351
+
352
+ const result = await showDialog ( {
353
+ title : 'Discard changes' ,
354
+ body : (
355
+ < span >
356
+ Are you sure you want to permanently discard changes to{ ' ' }
357
+ < b > { file . to } </ b > ? This action cannot be undone.
358
+ </ span >
359
+ ) ,
360
+ buttons : [
361
+ Dialog . cancelButton ( ) ,
362
+ Dialog . warnButton ( { label : 'Discard' } )
363
+ ]
364
+ } ) ;
365
+ if ( result . button . accept ) {
366
+ try {
367
+ if ( file . status === 'staged' || file . status === 'partially-staged' ) {
368
+ await model . reset ( file . to ) ;
369
+ }
370
+ if (
371
+ file . status === 'unstaged' ||
372
+ ( file . status === 'partially-staged' && file . x !== 'A' )
373
+ ) {
374
+ // resetting an added file moves it to untracked category => checkout will fail
375
+ await model . checkout ( { filename : file . to } ) ;
376
+ }
377
+ } catch ( reason ) {
378
+ showErrorMessage ( `Discard changes for ${ file . to } failed.` , reason , [
379
+ Dialog . warnButton ( { label : 'DISMISS' } )
380
+ ] ) ;
381
+ }
382
+ }
383
+ }
341
384
} ) ;
342
385
343
386
commands . addCommand ( CommandIDs . gitIgnore , {
@@ -348,8 +391,7 @@ export function addCommands(
348
391
if ( selectedFile ) {
349
392
await model . ignore ( selectedFile . to , false ) ;
350
393
}
351
- } ,
352
- isEnabled : args => args && args . to !== undefined
394
+ }
353
395
} ) ;
354
396
355
397
commands . addCommand ( CommandIDs . gitIgnoreExtension , {
@@ -379,7 +421,6 @@ export function addCommands(
379
421
}
380
422
}
381
423
} ,
382
- isEnabled : args => args && args . to !== undefined ,
383
424
isVisible : args => {
384
425
const selectedFile : Git . IStatusFile = args as any ;
385
426
const extension = PathExt . extname ( selectedFile . to ) ;
0 commit comments