Skip to content

Commit

Permalink
irmin-git: fix spurious warning when writing to a new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
craigfe committed Jan 4, 2022
1 parent a0d1929 commit 9029dd5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ application installed with `irmin-unix`, by running:
$ echo "root: ." > irmin.yml
$ irmin init
$ irmin set foo/bar "testing 123"
irmin: [ERROR] Reference refs/heads/main not found.
$ irmin get foo/bar
testing 123
```
Expand Down
32 changes: 19 additions & 13 deletions src/irmin-git/atomic_write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ module Make (K : Key) (G : Git.S) = struct
let git_of_branch r = Git.Reference.v (Fmt.to_to_string K.pp_ref r)
let pp_key = Irmin.Type.pp Key.t

let ref_read_opt t head =
(* Make a best-effort attempt to check that the reference actually
exists before [read]-ing it, since the [Error `Reference_not_found]
case causes a spurious warning to be logged inside [ocaml-git]. *)
G.Ref.mem t head >>= function
| false -> Lwt.return_none
| true -> (
let* r = G.Ref.read t head in
match r with
| Ok r -> Lwt.return_some r
| Error (`Reference_not_found _ | `Not_found _) ->
(* We may still hit this case due to a race condition, but it's very unlikely. *)
Lwt.return_none
| Error e -> Fmt.kstr Lwt.fail_with "%a" G.pp_error e)

let mem { t; _ } r =
[%log.debug "mem %a" pp_key r];
G.Ref.mem t (git_of_branch r)
Expand Down Expand Up @@ -170,12 +185,9 @@ module Make (K : Key) (G : Git.S) = struct
match head with
| Some h -> write_head h
| None -> (
let* r = G.Ref.read t Git.Reference.head in
match r with
| Error (`Reference_not_found _ | `Not_found _) ->
write_head (git_of_branch K.main)
| Error e -> Fmt.kstr Lwt.fail_with "%a" G.pp_error e
| Ok r -> Lwt.return r)
ref_read_opt t Git.Reference.head >>= function
| None -> write_head (git_of_branch K.main)
| Some head -> Lwt.return head)
in
let w =
try Hashtbl.find watches (G.dotgit t)
Expand Down Expand Up @@ -243,13 +255,7 @@ module Make (K : Key) (G : Git.S) = struct
true
in
Lwt_mutex.with_lock t.m (fun () ->
let* x =
let* x = G.Ref.read t.t gr in
match x with
| Error (`Reference_not_found _ | `Not_found _) -> Lwt.return_none
| Ok x -> Lwt.return_some x
| Error e -> Fmt.kstr Lwt.fail_with "%a" G.pp_error e
in
let* x = ref_read_opt t.t gr in
let* b =
if not (eq_head_contents_opt x (c test)) then Lwt.return_false
else
Expand Down

0 comments on commit 9029dd5

Please sign in to comment.