@@ -7,24 +7,27 @@ import { JupyterFrontEnd } from '@jupyterlab/application';
77import { ReactWidget , showDialog } from '@jupyterlab/apputils' ;
88import { PathExt } from '@jupyterlab/coreutils' ;
99import { style } from 'typestyle' ;
10+ import { httpGitRequest } from '../../git' ;
1011
1112/**
1213 * Method to open a main menu panel to show the diff of a given Notebook file.
1314 * If one already exists, just activates the existing one.
1415 *
15- * @param path the path relative to the Jupyter server root.
16+ * @param filePath the path relative to the Git repo root.
1617 * @param app The JupyterLab application instance
1718 * @param diffContext the context in which the diff is being requested
1819 * @param renderMime the renderMime registry instance from the
20+ * @param topRepoPath the Git repo root path.
1921 */
20- export function openDiffView (
21- path : string ,
22+ export async function openDiffView (
23+ filePath : string ,
2224 app : JupyterFrontEnd ,
2325 diffContext : IDiffContext ,
24- renderMime : IRenderMimeRegistry
26+ renderMime : IRenderMimeRegistry ,
27+ topRepoPath : string
2528) {
26- if ( isDiffSupported ( path ) ) {
27- const id = `nbdiff-${ path } -${ getRefValue ( diffContext . currentRef ) } ` ;
29+ if ( isDiffSupported ( filePath ) ) {
30+ const id = `nbdiff-${ filePath } -${ getRefValue ( diffContext . currentRef ) } ` ;
2831 let mainAreaItems = app . shell . widgets ( 'main' ) ;
2932 let mainAreaItem = mainAreaItems . next ( ) ;
3033 while ( mainAreaItem ) {
@@ -35,13 +38,17 @@ export function openDiffView(
3538 mainAreaItem = mainAreaItems . next ( ) ;
3639 }
3740 if ( ! mainAreaItem ) {
41+ const relativeFilePath : string = await getRelativeFilePath (
42+ filePath ,
43+ topRepoPath
44+ ) ;
3845 const nbDiffWidget = ReactWidget . create (
3946 < RenderMimeProvider value = { renderMime } >
40- < Diff path = { path } diffContext = { diffContext } />
47+ < Diff path = { relativeFilePath } diffContext = { diffContext } />
4148 </ RenderMimeProvider >
4249 ) ;
4350 nbDiffWidget . id = id ;
44- nbDiffWidget . title . label = PathExt . basename ( path ) ;
51+ nbDiffWidget . title . label = PathExt . basename ( filePath ) ;
4552 nbDiffWidget . title . iconClass = style ( {
4653 backgroundImage : 'var(--jp-icon-diff)'
4754 } ) ;
@@ -55,8 +62,40 @@ export function openDiffView(
5562 showDialog ( {
5663 title : 'Diff Not Supported' ,
5764 body : `Diff is not supported for ${ PathExt . extname (
58- path
65+ filePath
5966 ) . toLocaleLowerCase ( ) } files.`
6067 } ) ;
6168 }
6269}
70+
71+ let serverRootResultCache = null ;
72+ /**
73+ * Gets the path of the file relative to the Jupyter server root. This resolves the server root path after calling
74+ * the `/git/server_root` REST API, and uses the Git repo root and the file path relative to the repo root to resolve
75+ * the rest.
76+ * @param path the file path relative to Git repo root
77+ * @param topRepoPath the Git repo root path
78+ */
79+ export async function getRelativeFilePath (
80+ path : string ,
81+ topRepoPath : string
82+ ) : Promise < string > {
83+ if ( serverRootResultCache !== null ) {
84+ return serverRootResultCache ;
85+ } else {
86+ const response = await httpGitRequest ( '/git/server_root' , 'GET' , null ) ;
87+ const responseData = await response . json ( ) ;
88+ serverRootResultCache = PathExt . join (
89+ PathExt . relative ( responseData [ 'server_root' ] , topRepoPath ) ,
90+ path
91+ ) ;
92+ return serverRootResultCache ;
93+ }
94+ }
95+
96+ /**
97+ * Visible for testing.
98+ */
99+ export function clearCache ( ) {
100+ serverRootResultCache = null ;
101+ }
0 commit comments