-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathfindProjectRoot.m
More file actions
30 lines (28 loc) · 939 Bytes
/
findProjectRoot.m
File metadata and controls
30 lines (28 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function rootDir = findProjectRoot(marker)
%findProjectRoot Locate the root directory of the project.
% rootDir = findProjectRoot(marker) searches upward from the current
% directory to find a directory containing the file or folder specified
% by marker (e.g., 'startup.m', '.git').
%
% Inputs:
% marker - Name of a file or folder that identifies the project root
%
% Outputs:
% rootDir - Absolute path to the project root directory
if nargin < 1
marker = 'startup.m'; % Default marker file
end
currentDir = pwd;
while true
if exist(fullfile(currentDir, marker), 'file') || ...
exist(fullfile(currentDir, marker), 'dir')
rootDir = currentDir;
return;
end
[parentDir, ~, ~] = fileparts(currentDir);
if strcmp(currentDir, parentDir)
error('Project root not found. Marker "%s" not found in any parent directory.', marker);
end
currentDir = parentDir;
end
end