diff --git a/docs/index.md b/docs/index.md index 440500a..902f8aa 100644 --- a/docs/index.md +++ b/docs/index.md @@ -25,7 +25,8 @@ Currently the allowed parameters are: - `repo`: (**required**) the github repository to fetch. - `branch`: the branch of the repository to fetch (default to _main_). - `urlpath`: the path to a notebook file to open (relative to the root of the repository). -- `provider`: The provider of the API. Currently it supports _Github_ and _Gitlab_ API. +- `provider`: the provider of the API. Currently it supports _Github_ and _Gitlab_ API. +- `uploadpath`: the path to the directory in which the repository directory is created. ## Limitations diff --git a/src/gitpuller.ts b/src/gitpuller.ts index 4f7c042..dfc38e9 100644 --- a/src/gitpuller.ts +++ b/src/gitpuller.ts @@ -23,7 +23,14 @@ export abstract class GitPuller { * @returns the path of the created directory. */ async clone(url: string, branch: string, basePath: string): Promise { - await this.createTree([basePath]); + const basePathComponents = basePath.split('/'); + const basePathPrefixes = []; + for (let i = 0; i < basePathComponents.length; i++) { + basePathPrefixes.push(basePathComponents.slice(0, i + 1).join('/')); + } + + // For a basePath 'a/b/c', create ['a', 'a/b', 'a/b/c'] + await this.createTree(basePathPrefixes); const fileList = await this.getFileList(url, branch); @@ -96,11 +103,12 @@ export abstract class GitPuller { path: PathExt.dirname(directory) }; // Create directory if it does not exist. - await this._contents.get(directory, { content: false }).catch(() => { - this._contents.newUntitled(options).then(async newDirectory => { + await this._contents + .get(directory, { content: false }) + .catch(async () => { + const newDirectory = await this._contents.newUntitled(options); await this._contents.rename(newDirectory.path, directory); }); - }); } } diff --git a/src/index.ts b/src/index.ts index d08f508..ded0559 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,10 +61,12 @@ const gitPullerExtension: JupyterFrontEndPlugin = { let puller: GitPuller | null = null; - const basePath = PathExt.basename(repo); const branch = urlParams.get('branch') || 'main'; const provider = urlParams.get('provider') || 'github'; const filePath = urlParams.get('urlpath'); + const uploadPath = urlParams.get('uploadpath') || '/'; + + const basePath = PathExt.join(uploadPath, PathExt.basename(repo)); const repoUrl = new URL(repo); if (provider === 'github') { @@ -95,10 +97,10 @@ const gitPullerExtension: JupyterFrontEndPlugin = { return; } - puller.clone(repoUrl.href, branch, basePath).then(async basePath => { + puller.clone(repoUrl.href, branch, basePath).then(repoPath => { if (filePath) { app.commands.execute('filebrowser:open-path', { - path: PathExt.join(basePath, filePath) + path: PathExt.join(repoPath, filePath) }); } });