@altano/repository-tools
Version:
Misc tools for dealing with repositories of multiple version control systems (git, svn, etc)
47 lines (45 loc) • 1.19 kB
JavaScript
import { repositoryExecSync } from "./repositoryCommand.js";
//#region src/findRootSync.ts
function findGitRoot(cwd) {
return repositoryExecSync(cwd, "git", [
"rev-parse",
"--quiet",
"--show-toplevel"
]);
}
function findSaplingRoot(cwd) {
return repositoryExecSync(cwd, "sl", ["--quiet", "root"]);
}
function findMercurialRoot(cwd) {
return repositoryExecSync(cwd, "hg", ["--quiet", "root"]);
}
function findSubversionRoot(cwd) {
return repositoryExecSync(cwd, "svn", [
"info",
"--show-item",
"wc-root"
]);
}
const findMethods = [
findGitRoot,
findSubversionRoot,
findSaplingRoot,
findMercurialRoot
];
/**
* Find the root path of the repository containing the given directory.
*
* @returns null if not found or the path of the repository root as a string
*/
function findRootSync(directory) {
for (const method of findMethods) try {
const result = method(directory);
if (result != null && result !== "") return result;
} catch (e) {
if (e instanceof Error && "code" in e && e.code === "ENOTDIR") throw new Error(`Given path isn't a directory: ${directory}`);
}
return null;
}
//#endregion
export { findRootSync };
//# sourceMappingURL=findRootSync.js.map