Skip to content

Commit

Permalink
Ignore trailing newlines when doing 'test and set' on reference files
Browse files Browse the repository at this point in the history
This is because `git commit` might add these. Fix #610

Signed-off-by: Thomas Gazagnaire <thomas@gazagnaire.org>
  • Loading branch information
samoht committed Nov 21, 2017
1 parent 8c9ff71 commit 8e9d41b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/datakit-io/datakit_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,24 @@ module FS = struct
)
)

let trim s =
let len = Cstruct.len s in
if len >= 2
&& Cstruct.get_char s (len - 1) = '\n'
&& Cstruct.get_char s (len - 2) = '\r'
then
Cstruct.sub s 0 (len - 2)
else if len >= 1 && Cstruct.get_char s (len - 1) = '\n' then
Cstruct.sub s 0 (len - 1)
else
s

let test_and_set_file ?temp_dir ~lock file ~test ~set =
Lock.with_lock (Some lock) (fun () ->
read_file file >>= fun v ->
let equal = match test, v with
| None , None -> true
| Some x, Some y -> Cstruct.equal x y
| Some x, Some y -> Cstruct.equal (trim x) (trim y)
| _ -> false
in
if not equal then Lwt.return false
Expand Down

0 comments on commit 8e9d41b

Please sign in to comment.