Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbou committed Nov 23, 2023
1 parent 2cea436 commit 90e889e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/client/opamArg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ let apply_global_options cli o =
{ pelem = String cygcheck; _}::_ ->
let cygbin = Filename.dirname cygcheck in
OpamCoreConfig.update ~cygbin ()
| Some { pelem = String "gitbinpath"; _},
{ pelem = String gitbinpath; _}::_ ->
| Some { pelem = String "gitbinfield"; _},
{ pelem = String gitbin; _}::_ ->
let gitbinpath = Filename.dirname gitbin in
OpamCoreConfig.update ~gitbinpath ()
| _, element::elements -> aux (Some element) elements
in
Expand Down
138 changes: 100 additions & 38 deletions src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -636,43 +636,99 @@ let init_checks ?(hard_fail_exn=true) init_config =
else not (soft_fail || hard_fail)

let git_for_windows_check =
if Sys.win32 || Sys.cygwin then
fun cygbin ->
OpamConsole.menu
"Which cygwin should we use?"
~options: [
`Default, "default found";
`Select, "select from known";
`Specify, "specify git path";
]
let gits =
OpamStd.Env.get "PATH"
|> OpamStd.Sys.split_path_variable
|> OpamStd.List.filter_map (fun p ->
match OpamSystem.resolve_command ~env:[||] (Filename.concat p "git.exe") with
| Some git ->
Some (git,
OpamSystem.resolve_command ~env:[||] (Filename.concat p "bash.exe") <> None)
| None -> None)
in
if gits = [] then () else
OpamConsole.msg "Found those git binaries in path:\n%s\n"
(OpamStd.Format.itemize (fun (git, has_bash) ->
Printf.sprintf "%s%s"
git
(if hash_bash && not (OpamStd.String.starts_with ~prefix:cygbin git) then " but contains bash in it, which is dangerous" else "");
let first = List.hd git in
let options = [
`Default git, ("default found: "^git);
`Specify, "specify git path";
]
in
OpamConsole.menu
"Which cygwin should we use?"
~options


if not Sys.win32 && not Sys.cygwin then fun _ -> None else
fun cygbin ->
let gits =
OpamStd.Env.get "PATH"
|> OpamStd.Sys.split_path_variable
|> OpamStd.List.filter_map (fun p ->
match OpamSystem.resolve_command ~env:[||] (Filename.concat p "git.exe") with
| Some git ->
Some (git,
OpamSystem.resolve_command ~env:[||] (Filename.concat p "bash.exe") <> None)
| None -> None)
in
(if gits = [] then
OpamConsole.warning
"No preinstalled git software found, we recommend using Git for \
Windows https://gitforwindows.org or winget. You can install it \
and relaunch opam initialisation. Otherwise, opam will install \
directly git from Cygwin."
else
let is_in_cygwin p =
match cygbin with
| None -> false
| Some cygbin -> OpamStd.String.starts_with ~prefix:cygbin p
in
OpamConsole.msg "Found those git binaries in path:\n%s\n"
(OpamStd.Format.itemize (fun (git, has_bash) ->
Printf.sprintf "%s%s"
git
(if has_bash
&& not (is_in_cygwin git) then
" but contains bash in it, which is dangerous" else ""))
gits));
let rec get_gitbin () =
match OpamConsole.read "Enter Git for Windows or winget binary path:" with
| None -> get_gitbin ()
| Some gitbin ->
let resolve exe =
OpamSystem.resolve_command ~env:[||] (Filename.concat gitbin exe)
in
match resolve "git.exe", resolve "bash.exe" with
| Some _, None -> gitbin
| Some _, Some _ ->
OpamConsole.error
"A bash executable was found in %s, which will override \
usual bash. Please check you binary path"
gitbin;
get_gitbin ()
| None, _ ->
OpamConsole.error "No git executable found in %s" gitbin;
get_gitbin ()
in
if OpamConsole.confirm ~default:false
"Do you want to specify which git to use (non cygwin)?" then
Some (get_gitbin ())
else None

(*
let _ =
OpamConsole.menu
"Which cygwin should we use?"
~options: [
`Default, "default found";
`Select, "select from known";
`Specify, "specify git path";
]
in
let gits =
OpamStd.Env.get "PATH"
|> OpamStd.Sys.split_path_variable
|> OpamStd.List.filter_map (fun p ->
match OpamSystem.resolve_command ~env:[||] (Filename.concat p "git.exe") with
| Some git ->
Some (git,
OpamSystem.resolve_command ~env:[||] (Filename.concat p "bash.exe") <> None)
| None -> None)
in
if gits = [] then () else
OpamConsole.msg "Found those git binaries in path:\n%s\n"
(OpamStd.Format.itemize (fun (git, has_bash) ->
Printf.sprintf "%s%s"
git
(if hash_bash && not (OpamStd.String.starts_with ~prefix:cygbin git) then " but contains bash in it, which is dangerous" else "");
let first = List.hd git in
let options = [
`Default git, ("default found: "^git);
`Specify, "specify git path";
]
in
OpamConsole.menu
"Which cygwin should we use?"
~options
*)
(*
let gitcmd = OpamSystem.resolve_command "git" in
match gitcmd with
| None -> (* git *)
Expand Down Expand Up @@ -719,7 +775,7 @@ in
Consider installing Git for Windows: https://gitforwindows.org \
and cleaning your Path"
bindir
else fun _ -> ()
*)

let windows_checks ?cygwin_setup config =
let vars = OpamFile.Config.global_variables config in
Expand Down Expand Up @@ -957,7 +1013,13 @@ let windows_checks ?cygwin_setup config =
>>| OpamFilename.Dir.to_string)
in
OpamCoreConfig.update ?cygbin ();
let config = git_for_windows_check cygbin in
let gitbinpath = git_for_windows_check cygbin in
OpamCoreConfig.update ?gitbinpath ();
let config =
match gitbinpath with
| Some gitbin -> OpamFile.Config.with_gitbinfield (OpamFilename.Dir.of_string gitbin) config
| None -> config
in
assert false;
config

Expand Down
4 changes: 2 additions & 2 deletions src/format/opamFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ module ConfigSyntax = struct
depext_cannot_install : bool;
depext_bypass: OpamSysPkg.Set.t;
sys_pkg_manager_cmd: filename OpamStd.String.Map.t;
gitbinfield: filename option;
gitbinfield: dirname option;
swh_fallback: bool;
}

Expand Down Expand Up @@ -1507,7 +1507,7 @@ module ConfigSyntax = struct
-| Pp.of_pair "Distribution Map" OpamStd.String.Map.(of_list, bindings));
"gitbinfield", Pp.ppacc_opt
with_gitbinfield gitbinfield
(Pp.V.string -| Pp.of_module "filename" (module OpamFilename));
(Pp.V.string -| Pp.of_module "dirname" (module OpamFilename.Dir));
"swh-fallback", Pp.ppacc
with_swh_fallback swh_fallback
Pp.V.bool;
Expand Down
3 changes: 3 additions & 0 deletions src/format/opamFile.mli
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ module Config: sig
val with_sys_pkg_manager_cmd: filename OpamStd.String.Map.t -> t -> t

val with_swh_fallback: bool -> t -> t
val with_gitbinfield: dirname -> t -> t

(** Return the opam version *)
val opam_version: t -> opam_version
Expand Down Expand Up @@ -233,6 +234,8 @@ module Config: sig
sources *)
val swh_fallback: t -> bool

val gitbinfield: t -> dirname option

val fields: (string * (t, value) OpamPp.field_parser) list

(** All file fields as print-AST, Fields within sections are
Expand Down

0 comments on commit 90e889e

Please sign in to comment.