Skip to content

Commit aa803cf

Browse files
authored
Merge pull request #12673 from punchagan/rev-store-show-git-errors
pkg: Show git error output when Rev_store's fetch fails
2 parents c9ee648 + 3d35bd0 commit aa803cf

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

src/dune_pkg/opamUrl0.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ type resolve =
5252
| Resolved of Rev_store.Object.resolved
5353
| Unresolved of Rev_store.Object.t
5454

55-
let not_found t =
55+
let not_found ~loc ~git_output t =
5656
let url = base_url t in
5757
let rev = rev t in
58+
let git_output = List.map ~f:Pp.verbatim git_output in
5859
Error
59-
(User_message.make
60-
[ (match rev with
60+
(User_message.make ~loc
61+
@@ git_output
62+
@ [ (match rev with
6163
| None -> Pp.textf "default branch not found in %s" url
6264
| Some rev -> Pp.textf "revision %S not found in %s" rev url)
6365
])
@@ -89,7 +91,7 @@ let resolve t ~loc rev_store =
8991
| `Ref revision ->
9092
Rev_store.resolve_revision rev_store remote ~revision
9193
>>| (function
92-
| None -> not_found t
94+
| None -> not_found ~loc ~git_output:[] t
9395
| Some o -> Ok (Resolved o))
9496
;;
9597

@@ -101,8 +103,8 @@ let fetch_revision t ~loc resolve rev_store =
101103
| Unresolved o ->
102104
Rev_store.fetch_object rev_store remote o
103105
>>| (function
104-
| None -> not_found t
105-
| Some rev -> Ok rev)
106+
| Error git_output -> not_found ~loc ~git_output t
107+
| Ok rev -> Ok rev)
106108
;;
107109

108110
let set_rev (t : t) rev = { t with hash = Some (Rev_store.Object.to_hex rev) }

src/dune_pkg/rev_store.ml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,7 @@ let run_with_exit_code { dir; _ } ~allow_codes ~display args =
486486
minimum supported version is Git 2.29."
487487
]
488488
~hints:[ User_message.command "Please update your git version." ]
489-
| _ ->
490-
Console.print [ Pp.verbatim stderr ];
491-
Error { Git_error.dir; args; exit_code; output = [] })
489+
| _ -> Error { Git_error.dir; args; exit_code; output = [ stderr ] })
492490
;;
493491

494492
let run t ~display args =
@@ -725,15 +723,16 @@ let fetch_allow_failure repo ~url obj =
725723
| true -> Fiber.return `Fetched
726724
| false ->
727725
run_with_exit_code
728-
~allow_codes:(fun x -> x = 0 || x = 128)
726+
~allow_codes:(Int.equal 0)
729727
repo
730728
~display:!Dune_engine.Clflags.display
731729
[ "fetch"; "--no-write-fetch-head"; url; Object.to_hex obj ]
732730
>>| (function
733-
| Ok 128 -> `Not_found
734731
| Ok 0 ->
735732
Table.set repo.present_objects obj ();
736733
`Fetched
734+
| Error { Git_error.exit_code; output; _ } when exit_code = 128 ->
735+
`Not_found output
737736
| Error git_error -> Git_error.raise_code_error git_error
738737
| _ -> assert false))
739738
;;
@@ -742,8 +741,14 @@ let fetch repo ~url obj =
742741
fetch_allow_failure repo ~url obj
743742
>>| function
744743
| `Fetched -> ()
745-
| `Not_found ->
746-
User_error.raise [ Pp.textf "unable to fetch %S from %S" (Object.to_hex obj) url ]
744+
| `Not_found output ->
745+
User_error.raise
746+
([ Pp.textf
747+
"Dune was unable to fetch %S from %S due to the following git fetch error:"
748+
(Object.to_hex obj)
749+
url
750+
]
751+
@ List.map ~f:Pp.verbatim output)
747752
;;
748753

749754
module Debug = struct
@@ -1156,8 +1161,8 @@ let resolve_revision t (remote : Remote.t) ~revision =
11561161
let fetch_object t (remote : Remote.t) revision =
11571162
fetch_allow_failure t ~url:remote.url revision
11581163
>>= function
1159-
| `Not_found -> Fiber.return None
1160-
| `Fetched -> At_rev.of_rev t ~revision >>| Option.some
1164+
| `Not_found git_output -> Fiber.return (Error git_output)
1165+
| `Fetched -> At_rev.of_rev t ~revision >>| Result.ok
11611166
;;
11621167

11631168
let content_of_files t files =

src/dune_pkg/rev_store.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ end
104104
val resolve_revision : t -> Remote.t -> revision:string -> Object.resolved option Fiber.t
105105

106106
(** [fetch_object t remote object] ensures that an [object] from the [remote]
107-
is present in the revision store [t]. If the reviison is already present,
108-
no network I/O is performed. Returns [None] if the remote reports "not
109-
found". *)
110-
val fetch_object : t -> Remote.t -> Object.t -> At_rev.t option Fiber.t
107+
is present in the revision store [t]. If the revision is already present,
108+
no network I/O is performed. Returns [Error git_error_lines] if the remote
109+
reports "not found". *)
110+
val fetch_object : t -> Remote.t -> Object.t -> (At_rev.t, string list) result Fiber.t
111111

112112
(** Fetch the file contents of the repository at the given revision into the
113113
store and return the repository view. *)

test/blackbox-tests/test-cases/pkg/commit-hash-references.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ Depend on foo from the repo
3232

3333
Which foo will we get?
3434

35-
$ dune pkg lock 2>&1 | head -1 | sed "s/$AMBIGUOUS_REF/\$AMBIGUOUS_REF/g"
35+
$ dune pkg lock 2>&1 | grep "not found" | sed "s/$AMBIGUOUS_REF/\$AMBIGUOUS_REF/g"
3636
revision "$AMBIGUOUS_REF" not found in

test/expect-tests/dune_pkg/rev_store_tests.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ let%expect_test "fetching an object twice from the store" =
117117
Rev_store.Object.of_sha1 remote_revision
118118
|> Option.value_exn
119119
|> Rev_store.fetch_object rev_store remote
120-
>>| Option.value_exn
120+
>>| Result.to_option
121121
in
122+
let at_rev = Option.value_exn at_rev in
122123
Rev_store.At_rev.directory_entries at_rev ~recursive:true Path.Local.root
123124
|> Rev_store.File.Set.find ~f:(fun f ->
124125
Path.Local.equal (Rev_store.File.path f) (Path.Local.of_string file))

0 commit comments

Comments
 (0)