Expose whether filename_to_relative was successful

Summary:
Change Utils.filename_to_relative to return None in case the filename
is not under root, rather than returning the filename unchanged.

Reviewed By: akotulski

Differential Revision: D4391075

fbshipit-source-id: bf753af
master
Josh Berdine 8 years ago committed by Facebook Github Bot
parent 2270b04252
commit 915282105d

@ -33,13 +33,6 @@ module Map = Caml.Map.Make (OrderedSourceFile)
module Set = Caml.Set.Make (OrderedSourceFile)
let rel_path_from_abs_path root fname =
let relative_complemented_fname = Utils.filename_to_relative root fname in
if String.is_prefix ~prefix:root fname &&
Filename.is_relative relative_complemented_fname then
Some relative_complemented_fname
else None (* The project root is not a prefix of the file name *)
let from_abs_path fname =
if Filename.is_relative fname then
(failwithf
@ -49,10 +42,10 @@ let from_abs_path fname =
let fname_real = try Utils.realpath fname with Unix.Unix_error _ -> fname in
let project_root_real = Utils.realpath Config.project_root in
let models_dir_real = Config.models_src_dir in
match rel_path_from_abs_path project_root_real fname_real with
match Utils.filename_to_relative ~root:project_root_real fname_real with
| Some path -> RelativeProjectRoot path
| None -> (
match rel_path_from_abs_path models_dir_real fname_real with
match Utils.filename_to_relative ~root:models_dir_real fname_real with
| Some path -> RelativeInferModel path
| None -> Absolute fname (* fname is absolute already *)
)

@ -120,13 +120,13 @@ let filename_to_absolute ~root fname =
(** Convert an absolute filename to one relative to the given directory. *)
let filename_to_relative root fname =
let filename_to_relative ~root fname =
let rec relativize_if_under origin target =
match origin, target with
| x :: xs, y :: ys when x = y -> relativize_if_under xs ys
| [], [] -> "."
| [], ys -> Filename.of_parts ys
| _ -> fname
| [], [] -> Some "."
| [], ys -> Some (Filename.of_parts ys)
| _ -> None
in
relativize_if_under (Filename.parts root) (Filename.parts fname)

@ -32,8 +32,9 @@ val read_file : string -> string list option
(** Convert a filename to an absolute one if it is relative, and normalize "." and ".." *)
val filename_to_absolute : root:string -> string -> string
(** Convert an absolute filename to one relative to a root directory *)
val filename_to_relative : string -> string -> string
(** Convert an absolute filename to one relative to a root directory. Returns [None] if filename is
not under root. *)
val filename_to_relative : root:string -> string -> string option
(** type for files used for printing *)
type outfile =

Loading…
Cancel
Save