forked from devcontainers/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
33 lines (31 loc) · 1.21 KB
/
Copy pathgit.ts
File metadata and controls
33 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { runCommandNoPty, CLIHost } from './commonUtils';
import { Log } from '../spec-utils/log';
import { FileHost } from '../spec-utils/pfs';
export async function findGitRootFolder(cliHost: FileHost | CLIHost, folderPath: string, output: Log) {
if (!('exec' in cliHost)) {
for (let current = folderPath, previous = ''; current !== previous; previous = current, current = cliHost.path.dirname(current)) {
if (await cliHost.isFile(cliHost.path.join(current, '.git', 'config'))) {
return current;
}
}
return undefined;
}
try {
// Preserves symlinked paths (unlike --show-toplevel).
const { stdout } = await runCommandNoPty({
exec: cliHost.exec,
cmd: 'git',
args: ['rev-parse', '--show-cdup'],
cwd: folderPath,
output,
});
const cdup = stdout.toString().trim();
return cliHost.path.resolve(folderPath, cdup);
} catch {
return undefined;
}
}