Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if GNU patch is not detected when using patch #5893

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.ml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ let main_test_job ~analyse_job ~build_linux_job ~build_windows_job:_ ~build_macO
let host = host_of_platform platform in
let ocamlv = "${{ matrix.ocamlv }}" in
job ~oc ~workflow ?section ~runs_on:(Runner [runner]) ~env:[("OPAM_TEST", "1")] ~matrix ~needs ("Test-" ^ name_of_platform platform)
++ only_on MacOS (install_sys_packages ["coreutils"] ~descr:"Install gnu coreutils" [MacOS])
++ only_on MacOS (install_sys_packages ["coreutils"; "gpatch"] ~descr:"Install gnu coreutils" [MacOS])
++ checkout ()
++ only_on Linux (run "Install bubblewrap" ["sudo apt install bubblewrap"])
++ cache Archives
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ jobs:
OPAM_TEST: 1
steps:
- name: Install gnu coreutils
run: brew install coreutils
run: brew install coreutils gpatch
- name: Checkout tree
uses: actions/checkout@v4
- name: src_ext/archives and opam-repository Cache
Expand Down
6 changes: 6 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ users)
* Simplify computation of OCaml init default `sys-ocaml-*` eval variables on Unix [#5829 @dra27]
* Add a init OCaml `sys-ocaml-system` eval variable [#5829 @dra27]
* Mark the internal cygwin installation as recommended [#5903 @kit-ty-kate]
* Check for gpatch instead of patch on NetBSD and DragonFlyBSD [#5893 @kit-ty-kate]

## Config report

Expand Down Expand Up @@ -65,6 +66,7 @@ users)
* Add warning 69: Warn for new syntax when package name in variable in string interpolation contains several '+' [#5840 @rjbou]

## Repository
* Warn if `GNU patch` is not detected during a repository update [#5893 @kit-ty-kate]

## Lock

kit-ty-kate marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -126,6 +128,9 @@ users)
* Quote all the paths to OPAMROOT when creating the init scripts on Unix in case OPAMROOT contains spaces, backslashes or special characters [#5841 @kit-ty-kate - fix #5804]

## Internal
* Warn if `GNU patch` is not detected when a patch is applied [#5893 @kit-ty-kate]
* Use `gpatch` by default instead of `patch` on NetBSD and DragonFlyBSD [#5893 @kit-ty-kate]
* Use `gpatch` if it exists and is detected as GNU patch when `patch` is not `GNU patch` [#5893 @kit-ty-kate]

## Internal: Windows
* Ensure that the system critical error dialog is disabled when opam starts [#5828 @dra27]
Expand Down Expand Up @@ -191,3 +196,4 @@ users)
## opam-core
* `OpamStd.Sys`: add `is_cygwin_variant_cygcheck` that returns true if in path `cygcheck` is from a Cygwin or MSYS2 installation [#5843 @rjbou]
* `OpamStd.Env.cyg_env`: takes the environment to cygify, usually `OpamStd.Env.raw_env` [#5829 @dra27]
* `OpamSystem.patch` now displays a warning when GNU patch is not detected and looks for both patch and gpatch as a backup option depending on the OS [#5893 @kit-ty-kate]
6 changes: 5 additions & 1 deletion src/client/opamInitDefaults.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ let linux_filter = os_filter "linux"
let macos_filter = os_filter "macos"
let openbsd_filter = os_filter "openbsd"
let freebsd_filter = os_filter "freebsd"
let netbsd_filter = os_filter "netbsd"
let dragonflybsd_filter = os_filter "dragonfly"
let not_open_free_bsd_filter =
FNot (FOr (openbsd_filter, freebsd_filter))
let win32_filter = os_filter "win32"
let not_win32_filter =
FOp (FIdent ([], OpamVariable.of_string "os", None), `Neq, FString "win32")
let sandbox_filter = FOr (linux_filter, macos_filter)

let gpatch_filter = FOr (openbsd_filter, freebsd_filter)
let gpatch_filter =
FOr (FOr (openbsd_filter, netbsd_filter),
FOr (freebsd_filter, dragonflybsd_filter))
let patch_filter = FNot gpatch_filter

let gtar_filter = openbsd_filter
Expand Down
41 changes: 35 additions & 6 deletions src/core/opamSystem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,40 @@ let translate_patch ~dir orig corrected =
end;
close_in ch

let gpatch = lazy begin
let rec search_gpatch = function
| [] -> None
| patch_cmd::patch_cmds ->
match OpamProcess.run (make_command ~name:"patch" patch_cmd ["--version"]) with
| r ->
(match OpamProcess.is_success r, r.OpamProcess.r_stdout with
| true, full::_ when
OpamStd.String.is_prefix_of ~from:0 ~full "GNU patch " ->
Some patch_cmd
| _ ->
search_gpatch patch_cmds)
| exception _ -> search_gpatch patch_cmds
in
let default_cmd, other_cmds =
kit-ty-kate marked this conversation as resolved.
Show resolved Hide resolved
match OpamStd.Sys.os () with
| DragonFly
| FreeBSD
| NetBSD
| OpenBSD -> ("gpatch", ["patch"])
| Cygwin
| Darwin
| Linux
| Unix
| Win32
| Other _ -> ("patch", ["gpatch"])
Comment on lines +1652 to +1661
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hannesm like this?

Suggested change
| DragonFly
| FreeBSD
| NetBSD
| OpenBSD -> ("gpatch", ["patch"])
| Cygwin
| Darwin
| Linux
| Unix
| Win32
| Other _ -> ("patch", ["gpatch"])
| Darwin
| DragonFly
| FreeBSD
| NetBSD
| OpenBSD -> ("gpatch", ["patch"])
| Cygwin
| Linux
| Unix
| Win32
| Other _ -> ("patch", [])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem with that is that it changes the behaviour compared to 2.1.5

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, well, or even:

let default_cmd, other_cmds = "gpatch", [ "patch" ]

avoiding the match on the os type at all. But I hear your "changes the behaviour". With that in mind, I like your PR very much.

The only suggestion left is:

  let default_cmd, other_cmds =
    match OpamStd.Sys.os () with
    | DragonFly
    | FreeBSD
    | NetBSD
    | OpenBSD
    | Darwin -> ("gpatch", ["patch"])
    | Cygwin
    | Linux
    | Unix
    | Win32
    | Other _ -> ("patch", [])

so, put Darwin into the pot where gpatch is tried first, and don't try gpatch on Linux & Windows.

But as said, maybe the current state is best for avoiding too much trouble / regressions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually considering the code and platforms, maybe

let default_cmd, other_cmds = "patch", [ "gpatch" ]

is suitable? This would on OpenBSD and FreeBSD call patch --version, and then go to using gpatch...

in
match search_gpatch (default_cmd :: other_cmds) with
| Some gpatch -> gpatch
| None ->
OpamConsole.warning "Invalid patch utility. Please install GNU patch";
default_cmd
end

let patch ?(preprocess=true) ~dir p =
if not (Sys.file_exists p) then
(OpamConsole.error "Patch file %S not found." p;
Expand All @@ -1645,12 +1679,7 @@ let patch ?(preprocess=true) ~dir p =
else
p
in
let patch_cmd =
match OpamStd.Sys.os () with
| OpamStd.Sys.OpenBSD
| OpamStd.Sys.FreeBSD -> "gpatch"
| _ -> "patch"
in
let patch_cmd = Lazy.force gpatch in
make_command ~name:"patch" ~dir patch_cmd ["-p1"; "-i"; p'] @@> fun r ->
if not (OpamConsole.debug ()) then Sys.remove p';
if OpamProcess.is_success r then Done None
Expand Down
4 changes: 4 additions & 0 deletions tests/reftests/repository.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ some content
### opam switch create tarring --empty
### opam update -vv | grep '^\+' | sed-cmd diff | sed-cmd patch | 'patch-[^"]+' -> 'patch'
+ diff "-ruaN" "default" "default.new" (CWD=${BASEDIR}/OPAM/repo)
+ patch "--version"
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${BASEDIR}/OPAM/repo/default)
### ls $OPAMROOT/repo | grep -v "cache"
default
Expand All @@ -26,6 +27,7 @@ build: ["test" "-f" "baz"]
some content
### opam update default -vv | grep '^\+' | sed-cmd tar | sed-cmd diff | sed-cmd patch | 'patch-[^"]+' -> 'patch'
+ diff "-ruaN" "default" "default.new" (CWD=${BASEDIR}/OPAM/repo)
+ patch "--version"
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${BASEDIR}/OPAM/repo/default)
+ tar "cfz" "${BASEDIR}/OPAM/repo/default.tar.gz.tmp" "-C" "${BASEDIR}/OPAM/repo" "default"
### ls $OPAMROOT/repo | grep -v "cache"
Expand Down Expand Up @@ -64,6 +66,7 @@ some content
### opam update -vv | grep '^\+' | sed-cmd tar | sed-cmd diff | sed-cmd patch | 'patch-[^"]+' -> 'patch'
+ tar "xfz" "${BASEDIR}/OPAM/repo/tarred.tar.gz" "-C" "${OPAMTMP}"
+ diff "-ruaN" "tarred" "tarred.new" (CWD=${OPAMTMP})
+ patch "--version"
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${OPAMTMP}/tarred)
+ tar "cfz" "${BASEDIR}/OPAM/repo/tarred.tar.gz.tmp" "-C" "${OPAMTMP}" "tarred"
### opam install foo.4 -vv | grep '^\+' | sed-cmd test | sed-cmd tar
Expand All @@ -86,6 +89,7 @@ some content
### opam update -vv | grep '^\+' | sed-cmd tar | sed-cmd diff | sed-cmd patch | 'patch-[^"]+' -> 'patch'
+ tar "xfz" "${BASEDIR}/OPAM/repo/tarred.tar.gz" "-C" "${OPAMTMP}"
+ diff "-ruaN" "tarred" "tarred.new" (CWD=${OPAMTMP})
+ patch "--version"
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${OPAMTMP}/tarred)
### opam install foo.5 -vv | grep '^\+' | sed-cmd test
+ test "-f" "quux" (CWD=${BASEDIR}/OPAM/tarring/.opam-switch/build/foo.5)
Expand Down
Loading