Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions @commitlint/top-level/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
},
"license": "MIT",
"devDependencies": {
"@commitlint/utils": "^20.0.0"
"@commitlint/test": "^20.0.0"
},
"dependencies": {
"find-up": "^7.0.0"
"escalade": "^3.2.0"
},
"gitHead": "e82f05a737626bb69979d14564f5ff601997f679"
}
47 changes: 47 additions & 0 deletions @commitlint/top-level/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { test, expect } from "vitest";
import fs from "node:fs/promises";
import path from "node:path";

import { fix, git } from "@commitlint/test";
import topLevel from "./index.js";

test("find .git in normal repository as directory", async () => {
const temp = await git.bootstrap();
const cwd = path.join(temp, "src", "shared");
await fs.mkdir(cwd, { recursive: true });

const top = (await topLevel(cwd)) as string;
expect(top).toEqual(temp);
expect((await fs.stat(path.join(top, ".git"))).isDirectory()).toEqual(true);
});

test("find .git in submodule repository as file", async () => {
const temp = await git.bootstrap();
const root = path.join(temp, "parser");
const cwd = path.join(root, "src");
await fs.mkdir(cwd, { recursive: true });
await fs.writeFile(path.join(root, ".git"), "");

const top = (await topLevel(cwd)) as string;
expect(top).toEqual(root);
expect((await fs.stat(path.join(top, ".git"))).isFile()).toEqual(true);
});

test("not found in non-git folder", async () => {
const temp = await fix.bootstrap();
const cwd = path.join(temp, "src", "shared");
await fs.mkdir(cwd, { recursive: true });

const top = (await topLevel(cwd)) as undefined;
expect(top).toEqual(undefined);
});

test("very deep directory from the root", async () => {
const temp = await git.bootstrap();
const cwd = path.join(temp, ...new Array(10).fill("dir"));
await fs.mkdir(cwd, { recursive: true });

const top = (await topLevel(cwd)) as string;
expect(top).toEqual(temp);
expect((await fs.stat(path.join(top, ".git"))).isDirectory()).toEqual(true);
});
28 changes: 8 additions & 20 deletions @commitlint/top-level/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import path from "node:path";
import { findUp } from "find-up";
import process from "node:process";
import escalade from "escalade";

export default toplevel;

/**
* Find the next git root
*/
async function toplevel(cwd?: string) {
const found = await searchDotGit(cwd);

if (typeof found !== "string") {
return found;
}

return path.join(found, "..");
}

/**
* Search .git, the '.git' can be a file(submodule), also can be a directory(normal)
*/
async function searchDotGit(cwd?: string) {
const foundFile = await findUp(".git", { cwd, type: "file" });
const foundDir = await findUp(".git", { cwd, type: "directory" });

return foundFile || foundDir;
function toplevel(cwd = process.cwd()) {
return escalade(cwd, (directory, files) => {
if (files.includes(".git")) {
return directory;
}
});
}
40 changes: 0 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4068,15 +4068,6 @@ find-up@^5.0.0:
locate-path "^6.0.0"
path-exists "^4.0.0"

find-up@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/find-up/-/find-up-7.0.0.tgz#e8dec1455f74f78d888ad65bf7ca13dd2b4e66fb"
integrity sha512-YyZM99iHrqLKjmt4LJDj58KI+fYyufRLBSYcqycxf//KpBk9FoewoGX0450m9nB44qrZnovzC2oeP5hUibxc/g==
dependencies:
locate-path "^7.2.0"
path-exists "^5.0.0"
unicorn-magic "^0.1.0"

findup-sync@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/findup-sync/-/findup-sync-4.0.0.tgz#956c9cdde804052b881b428512905c4a5f2cdef0"
Expand Down Expand Up @@ -5383,13 +5374,6 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

locate-path@^7.2.0:
version "7.2.0"
resolved "https://registry.npmjs.org/locate-path/-/locate-path-7.2.0.tgz#69cb1779bd90b35ab1e771e1f2f89a202c2a8a8a"
integrity sha512-gvVijfZvn7R+2qyPX8mAuKcFGDf6Nc61GdvGafQsHL0sBIxfKzA+usWn4GFC/bk+QdwPUD4kWFJLhElipq+0VA==
dependencies:
p-locate "^6.0.0"

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
Expand Down Expand Up @@ -6201,13 +6185,6 @@ p-limit@^3.0.2:
dependencies:
yocto-queue "^0.1.0"

p-limit@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644"
integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==
dependencies:
yocto-queue "^1.0.0"

p-locate@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
Expand All @@ -6229,13 +6206,6 @@ p-locate@^5.0.0:
dependencies:
p-limit "^3.0.2"

p-locate@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f"
integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==
dependencies:
p-limit "^4.0.0"

[email protected]:
version "2.1.0"
resolved "https://registry.npmjs.org/p-map-series/-/p-map-series-2.1.0.tgz#7560d4c452d9da0c07e692fdbfe6e2c81a2a91f2"
Expand Down Expand Up @@ -6390,11 +6360,6 @@ path-exists@^4.0.0:
resolved "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==

path-exists@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7"
integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==

path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
Expand Down Expand Up @@ -8136,11 +8101,6 @@ yocto-queue@^0.1.0:
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

yocto-queue@^1.0.0:
version "1.1.1"
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.1.1.tgz#fef65ce3ac9f8a32ceac5a634f74e17e5b232110"
integrity sha512-b4JR1PFR10y1mKjhHY9LaGo6tmrgjit7hxVIeAmyMw3jegXR4dhYqLaQF5zMXZxY7tLpMyJeLjr1C4rLmkVe8g==

yoctocolors-cjs@^2.1.2:
version "2.1.3"
resolved "https://registry.npmjs.org/yoctocolors-cjs/-/yoctocolors-cjs-2.1.3.tgz#7e4964ea8ec422b7a40ac917d3a344cfd2304baa"
Expand Down