Skip to content

Commit

Permalink
File: add cwd option to find_in_parents
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-botbol committed Feb 14, 2025
1 parent cdd328f commit 81f32f1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions compiler/catala_utils/file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ let reverse_path ?(from_dir = Sys.getcwd ()) ~to_dir f =
String.concat Filename.dir_sep
(aux (path_to_list f) rbase (path_to_list to_dir))

let find_in_parents predicate =
let find_in_parents ?cwd predicate =
let cwd = match cwd with None -> Sys.getcwd () | Some cwd -> cwd in
let home = try Sys.getenv "HOME" with Not_found -> "" in
let rec lookup dir rel =
if predicate dir then Some dir, rel
Expand All @@ -121,7 +122,7 @@ let find_in_parents predicate =
if parent = dir then None, Filename.current_dir_name
else lookup parent (rel / Filename.parent_dir_name)
in
match lookup (Sys.getcwd ()) Filename.current_dir_name with
match lookup cwd Filename.current_dir_name with
| Some dir, rel -> Some (dir, rel)
| None, _ -> None

Expand Down
11 changes: 6 additions & 5 deletions compiler/catala_utils/file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ val reverse_path : ?from_dir:t -> to_dir:t -> t -> t
leading to [f] from [to_dir]. The results attempts to be relative to
[to_dir]. *)

val find_in_parents : (t -> bool) -> (t * t) option
(** Checks for the first directory matching the given predicate from the current
directory upwards. Recursion stops at home. Returns a pair [dir, rel_path],
where [dir] is the ancestor directory matching the predicate, and [rel_path]
is a path pointing to it from the current dir. *)
val find_in_parents : ?cwd:t -> (t -> bool) -> (t * t) option
(** Checks for the first directory matching the given predicate from [cwd]
upwards (defaults to the current directory). Recursion stops at home.
Returns a pair [dir, rel_path], where [dir] is the ancestor directory
matching the predicate, and [rel_path] is a path pointing to it from the
current dir. *)

val ( /../ ) : t -> t -> t
(** Sugar for [parent a / b] *)
Expand Down

0 comments on commit 81f32f1

Please sign in to comment.