From c21ba92bfd37769ee06a2f3269694b6d8dfa14e9 Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Tue, 21 Sep 2021 15:27:27 -0700 Subject: [PATCH] Fix git root detection in worktrees (#389) * Fix git root detection in worktrees * Change files * update naming --- change/backfill-2021-09-20-22-47-29-worktrees.json | 8 ++++++++ ...backfill-cache-2021-09-20-22-47-29-worktrees.json | 8 ++++++++ packages/backfill/src/audit.ts | 12 ++++++------ packages/cache/src/CacheStorage.ts | 9 ++++++--- 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 change/backfill-2021-09-20-22-47-29-worktrees.json create mode 100644 change/backfill-cache-2021-09-20-22-47-29-worktrees.json diff --git a/change/backfill-2021-09-20-22-47-29-worktrees.json b/change/backfill-2021-09-20-22-47-29-worktrees.json new file mode 100644 index 00000000..a3eb4135 --- /dev/null +++ b/change/backfill-2021-09-20-22-47-29-worktrees.json @@ -0,0 +1,8 @@ +{ + "type": "patch", + "comment": "Fix git root detection in worktrees", + "packageName": "backfill", + "email": "elcraig@microsoft.com", + "dependentChangeType": "patch", + "date": "2021-09-21T05:47:28.208Z" +} diff --git a/change/backfill-cache-2021-09-20-22-47-29-worktrees.json b/change/backfill-cache-2021-09-20-22-47-29-worktrees.json new file mode 100644 index 00000000..f8945ed7 --- /dev/null +++ b/change/backfill-cache-2021-09-20-22-47-29-worktrees.json @@ -0,0 +1,8 @@ +{ + "type": "patch", + "comment": "Fix git root detection in worktrees", + "packageName": "backfill-cache", + "email": "elcraig@microsoft.com", + "dependentChangeType": "patch", + "date": "2021-09-21T05:47:29.059Z" +} diff --git a/packages/backfill/src/audit.ts b/packages/backfill/src/audit.ts index 7bb0038b..9654679d 100644 --- a/packages/backfill/src/audit.ts +++ b/packages/backfill/src/audit.ts @@ -11,14 +11,14 @@ let changedFilesInsideScope: string[] = []; let watcher: chokidar.FSWatcher; function getGitRepositoryRoot(packageRoot: string) { - const nearestGitFolder = findUp.sync(".git", { - cwd: packageRoot, - type: "directory", - }); + // .git is typically a folder but will be a file in a worktree + const nearestGitInfo = + findUp.sync(".git", { cwd: packageRoot, type: "directory" }) || + findUp.sync(".git", { cwd: packageRoot, type: "file" }); - if (nearestGitFolder) { + if (nearestGitInfo) { // Return the parent folder of some/path/.git - return path.join(nearestGitFolder, ".."); + return path.join(nearestGitInfo, ".."); } return packageRoot; diff --git a/packages/cache/src/CacheStorage.ts b/packages/cache/src/CacheStorage.ts index b718f485..1707913c 100644 --- a/packages/cache/src/CacheStorage.ts +++ b/packages/cache/src/CacheStorage.ts @@ -8,14 +8,17 @@ import { Logger } from "backfill-logger"; const savedHashOfRepos: { [gitRoot: string]: { [file: string]: string } } = {}; function getRepoRoot(cwd: string): string { - const gitFolder = findUp.sync(".git", { cwd, type: "directory" }); - if (!gitFolder) { + // .git is typically a folder but will be a file in a worktree + const nearestGitInfo = + findUp.sync(".git", { cwd, type: "directory" }) || + findUp.sync(".git", { cwd, type: "file" }); + if (!nearestGitInfo) { throw new Error( "The location that backfill is being run against is not in a git repo" ); } - return dirname(gitFolder); + return dirname(nearestGitInfo); } function fetchHashesFor(cwd: string) {