@@ -4,19 +4,27 @@ import * as path from "path";
44import { getGitExtension } from "./repository" ;
55import { logDebug } from "../log" ;
66
7- async function getGitRemoteForPath ( documentPath : string ) : Promise < string | undefined > {
7+ async function getGitRemoteForPath ( directoryPath : string ) : Promise < string | undefined > {
88 try {
9- const dir = path . dirname ( documentPath ) ;
10- const cp = await import ( "child_process" ) ;
9+ const fs = await import ( "fs/promises" ) ;
10+ const stats = await fs . stat ( directoryPath ) ;
11+ const cwd = stats . isDirectory ( ) ? directoryPath : path . dirname ( directoryPath ) ;
12+
13+ const child_process = await import ( "child_process" ) ;
1114 const util = await import ( "util" ) ;
12- const execPromise = util . promisify ( cp . exec ) ;
13- const { stdout} = await execPromise ( "git config --get remote.origin.url" , { cwd : dir } ) ;
15+ const execFile = util . promisify ( child_process . execFile ) ;
16+ const { stdout} = await execFile ( "git" , [ " config" , " --get" , " remote.origin.url"] , { cwd} ) ;
1417 return stdout . trim ( ) ;
1518 } catch {
1619 return undefined ;
1720 }
1821}
1922
23+ function isPathWithin ( childPath : string , parentPath : string ) : boolean {
24+ const relative = path . relative ( parentPath , childPath ) ;
25+ return ! relative . startsWith ( ".." ) && ! path . isAbsolute ( relative ) ;
26+ }
27+
2028export async function getRepositoryRootForDocumentUri ( documentUri : vscode . Uri ) : Promise < vscode . Uri | undefined > {
2129 const git = await getGitExtension ( ) ;
2230 if ( ! git || git . repositories . length === 0 ) {
@@ -28,14 +36,13 @@ export async function getRepositoryRootForDocumentUri(documentUri: vscode.Uri):
2836 for ( const repository of git . repositories ) {
2937 const repoPath = repository . rootUri . fsPath ;
3038
31- if ( documentPath . startsWith ( repoPath ) ) {
32- await repository . status ( ) ;
39+ if ( isPathWithin ( documentPath , repoPath ) ) {
3340 const state = repository . state ;
3441
3542 if ( state && state . submodules && state . submodules . length > 0 ) {
3643 for ( const submodule of state . submodules ) {
3744 const submodulePath = path . join ( repoPath , submodule . path ) ;
38- if ( documentPath . startsWith ( submodulePath ) ) {
45+ if ( isPathWithin ( documentPath , submodulePath ) ) {
3946 logDebug ( "Found document in submodule:" , submodule . path ) ;
4047 const remoteUrl = await getGitRemoteForPath ( submodulePath ) ;
4148 if ( remoteUrl ) {
0 commit comments