ocaml / opam

opam is a source-based package manager. It supports multiple simultaneous compiler installations, flexible package constraints, and a Git-friendly development workflow.
https://opam.ocaml.org
Other
1.21k stars 348 forks source link

Improvements to `opam init` "Git" menu on Windows #5963

Closed dra27 closed 1 month ago

dra27 commented 1 month ago

This PR extends the detection of Git on Windows during opam init in several ways (which includes addressing the cyclic logic identified in #5835)

The PR is three sections: the first three commits add Windows primitives:

The next part is easy: OpamClient.git_for_windows_check current does stuff when opam is built for Cygwin, which it categorically should not. From our perspective, if a user is using Cygwin's build of opam then they should only use Cygwin's build of git (or have to work very very hard themselves to change that); from the Cygwin opam package maintainer's perspective, a Cygwin tool should not suddenly ignore a Cygwin package in preference for an external tool (imagine we were trying to make the Linux build of opam behave this way in WSL and then imagine the response of the Debian maintainer 🙂)

There are then three areas where opam init's Git for Windows detection is enhanced. We can identify a Git for Windows binary from the Product Version string in the executable which will contain .windows. (the tag format for Git for Windows versions is covered in their security guide). This instantly differentiates it from much older builds of msysgit (from pre-Git-for-Windows days) and also from Cygwin/MSYS2's Git (which isn't built with resource blocks anyway, and would use Git's tag numbers if it were). I've gone with just checking the .windows. format for now because its clearly enough - if it were ever to stop being enough, we could examine other fields (like the author name, etc...!).

If opam init resolves git.exe in PATH, and that git.exe has a Product Version of the format git-major.git-minor.git-revision.windows.gfw-revision, then no menu is displayed - Git for Windows is in use and has been identified, which fixes #5835.

The last two commits extend this further to deal with what I think is one very likely user mistake and one possible user misconfiguration.

I think it is highly likely that we will encounter users who do see that they should go and install Git for Windows, but who do not then open a fresh Command Prompt / PowerShell session before running opam init again. In this instance, git.exe will still not be found in PATH. To counter this, opam init now checks the initial environment which, after running winget install Git.Git or using the Git-for-Windows installer, will now include the location of git.exe. If git.exe can be found in PATH in this environment then the menu is still displayed, but with a warning, thus:

<><> Git ><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
Cygwin Git is functional but can have credentials issues for private repositories, we recommend using:
  - Install via 'winget install Git.Git'
  - Git for Windows can be downloaded and installed from https://gitforwindows.org

[WARNING] It looks as though Git for Windows has been installed but the shell needs to
          be restarted. You may wish to abort and re-run opam init from a fresh
          session.

Which Git should opam use?
> 1. Use default Cygwin Git
  2. Enter the location of installed Git
  3. Abort initialisation to install recommended Git.

The final commit extends this one stage further. The Git for Windows installer includes an option to install, but not update PATH. If git.exe is found in neither the current PATH nor the pristine/initial PATH, then opam now searches the Registry for the two entries which may point to Git for Windows installations (it is possible to install both for the entire machine and for the current user). If one or both of these are found, then opam still displays the menu, but with a different warning and it also adds the \cmd directory of this installation to the list of possible values to use for git-location:

<><> Git ><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
Cygwin Git is functional but can have credentials issues for private repositories, we recommend using:
  - Install via 'winget install Git.Git'
  - Git for Windows can be downloaded and installed from https://gitforwindows.org

[WARNING] It looks as though Git for Windows has been installed, but configured not to
          put the git binary in your PATH. You can either abort and reconfigure your
          environment (or re-run the Git for Windows installer) to enable this, or you
          can use the menu below to have opam use this Git installation internally.

Which Git should opam use?
> 1. Use default Cygwin Git
  2. Use found git in C:\Program Files\Git\cmd
  3. Enter the location of installed Git
  4. Abort initialisation to install recommended Git.
kit-ty-kate commented 1 month ago

LGTM on first read apart from the question above

dra27 commented 1 month ago

I pulled your changes from rjbou/gwf-menu as well, thanks!