Skip to content

Commit

Permalink
Fix git root detection in worktrees (#389)
Browse files Browse the repository at this point in the history
* Fix git root detection in worktrees

* Change files

* update naming
  • Loading branch information
ecraig12345 authored Sep 21, 2021
1 parent 74898eb commit c21ba92
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions change/backfill-2021-09-20-22-47-29-worktrees.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "patch",
"comment": "Fix git root detection in worktrees",
"packageName": "backfill",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2021-09-21T05:47:28.208Z"
}
8 changes: 8 additions & 0 deletions change/backfill-cache-2021-09-20-22-47-29-worktrees.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "patch",
"comment": "Fix git root detection in worktrees",
"packageName": "backfill-cache",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2021-09-21T05:47:29.059Z"
}
12 changes: 6 additions & 6 deletions packages/backfill/src/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions packages/cache/src/CacheStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c21ba92

Please sign in to comment.