Skip to content

Commit b7bf92a

Browse files
committed
Add support for git worktree (resolve #990, close #991)
1 parent fc96595 commit b7bf92a

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/main
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false

packages/knip/src/WorkspaceWorker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
GetInputsFromScriptsPartial,
1111
GetSourceFile,
1212
HandleInput,
13+
Plugin,
1314
WorkspaceConfiguration,
1415
} from './types/config.js';
1516
import type { ConfigurationHint } from './types/issues.js';
@@ -186,13 +187,16 @@ export class WorkspaceWorker {
186187
return [patterns, this.negatedWorkspacePatterns].flat();
187188
}
188189

190+
private getPluginConfig(plugin: Plugin) {
191+
return typeof plugin.config === 'function' ? plugin.config({ cwd: this.dir }) : plugin.config;
192+
}
193+
189194
getPluginConfigPatterns() {
190195
const patterns: string[] = [];
191196
for (const [pluginName, plugin] of PluginEntries) {
192197
const pluginConfig = this.getConfigForPlugin(pluginName);
193198
if (this.enabledPluginsMap[pluginName] && pluginConfig) {
194-
const { config } = pluginConfig;
195-
patterns.push(...(config ?? plugin.config ?? []));
199+
patterns.push(...(pluginConfig.config ?? this.getPluginConfig(plugin) ?? []));
196200
}
197201
}
198202
return patterns;
@@ -234,7 +238,7 @@ export class WorkspaceWorker {
234238
private getConfigurationFilePatterns(pluginName: PluginName) {
235239
const plugin = Plugins[pluginName];
236240
const pluginConfig = this.getConfigForPlugin(pluginName);
237-
return pluginConfig.config ?? plugin.config ?? [];
241+
return pluginConfig.config ?? this.getPluginConfig(plugin) ?? [];
238242
}
239243

240244
public async runPlugins() {

packages/knip/src/plugins/lefthook/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ const enablers = ['lefthook', '@arkweid/lefthook', '@evilmartians/lefthook'];
1313

1414
const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
1515

16-
const gitHookPaths = getGitHookPaths();
17-
18-
const config = ['lefthook.yml', ...gitHookPaths];
19-
2016
type Command = {
2117
run: string;
2218
root: string;
@@ -56,7 +52,7 @@ const plugin: Plugin = {
5652
title,
5753
enablers,
5854
isEnabled,
59-
config,
55+
config: options => ['lefthook.yml', ...getGitHookPaths('.git/hooks', true, options.cwd)],
6056
resolveConfig,
6157
};
6258

packages/knip/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface Plugin {
155155
enablers?: IgnorePatterns | string;
156156
isEnabled?: IsPluginEnabled;
157157
isRootOnly?: boolean;
158-
config?: string[];
158+
config?: string[] | ((options: { cwd: string }) => string[]);
159159
entry?: string[];
160160
production?: string[];
161161
project?: string[];

packages/knip/src/util/git.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ const hookFileNames = [
1212
'post-{checkout,commit,merge,rewrite}',
1313
];
1414

15-
const getGitHooksPath = (defaultPath = '.git/hooks') => {
15+
const getGitHooksPath = (defaultPath = '.git/hooks', cwd: string | undefined) => {
1616
try {
17-
return execSync('git config --get core.hooksPath', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] }).trim();
17+
return execSync('git rev-parse --git-path hooks', {
18+
encoding: 'utf8',
19+
stdio: ['pipe', 'pipe', 'ignore'],
20+
cwd,
21+
}).trim();
1822
} catch (_error) {
1923
return defaultPath;
2024
}
2125
};
2226

23-
export const getGitHookPaths = (defaultPath = '.git/hooks', followGitConfig = true) => {
24-
const gitHooksPath = followGitConfig ? getGitHooksPath(defaultPath) : defaultPath;
27+
export const getGitHookPaths = (defaultPath = '.git/hooks', followGitConfig = true, cwd?: string) => {
28+
const gitHooksPath = followGitConfig ? getGitHooksPath(defaultPath, cwd) : defaultPath;
2529
return hookFileNames.map(fileName => join(gitHooksPath, fileName));
2630
};

0 commit comments

Comments
 (0)