Skip to content

Commit

Permalink
Warn if GNU patch is not detected during opam update
Browse files Browse the repository at this point in the history
  • Loading branch information
kit-ty-kate committed Apr 2, 2024
1 parent 4789ed2 commit 25f4f5c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
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
25 changes: 19 additions & 6 deletions src/core/opamSystem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,24 @@ let translate_patch ~dir orig corrected =
end;
close_in ch
let patch_cmd = lazy begin
match OpamStd.Sys.os () with
| OpamStd.Sys.OpenBSD
| OpamStd.Sys.FreeBSD -> "gpatch"
| _ -> "patch"
end
let is_gpatch_available = lazy begin
let patch_cmd = Lazy.force patch_cmd in
let r = OpamProcess.run (make_command ~name:"patch" patch_cmd ["--version"]) in
match OpamProcess.is_success r, r.OpamProcess.r_stdout with
| true, full::_ when
OpamStd.String.is_prefix_of ~from:0 ~full "GNU patch " ->
true
| _ ->
false
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 +1663,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 patch_cmd 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
2 changes: 2 additions & 0 deletions src/core/opamSystem.mli
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ val get_lock_fd: lock -> Unix.file_descr

(** {2 Misc} *)

val is_gpatch_available : bool Lazy.t

(** Apply a patch file in the current directory. If [preprocess] is set to
false, there is no CRLF translation. Returns the error if the patch didn't
apply. *)
Expand Down
2 changes: 2 additions & 0 deletions src/repository/opamRepository.ml
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ let cleanup_repo_update upd =

let update repo repo_root =
log "update %a" (slog OpamRepositoryBackend.to_string) repo;
if not (Lazy.force OpamSystem.is_gpatch_available) then
OpamConsole.warning "Invalid patch utility. Please install GNU patch";
let module B = (val find_backend repo: OpamRepositoryBackend.S) in
B.fetch_repo_update repo.repo_name repo_root repo.repo_url @@+ function
| Update_err e -> raise e
Expand Down
4 changes: 4 additions & 0 deletions tests/reftests/repository.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ some content
### : Internal repository storage as archive or plain directory :
### opam switch create tarring --empty
### opam update -vv | grep '^\+' | sed-cmd diff | sed-cmd patch | 'patch-[^"]+' -> 'patch'
+ patch "--version"
+ diff "-ruaN" "default" "default.new" (CWD=${BASEDIR}/OPAM/repo)
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${BASEDIR}/OPAM/repo/default)
### ls $OPAMROOT/repo | grep -v "cache"
Expand All @@ -25,6 +26,7 @@ build: ["test" "-f" "baz"]
### <REPO/packages/foo/foo.2/files/baz>
some content
### opam update default -vv | grep '^\+' | sed-cmd tar | sed-cmd diff | sed-cmd patch | 'patch-[^"]+' -> 'patch'
+ patch "--version"
+ diff "-ruaN" "default" "default.new" (CWD=${BASEDIR}/OPAM/repo)
+ 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"
Expand Down Expand Up @@ -63,6 +65,7 @@ build: ["test" "-f" "baz"]
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}"
+ patch "--version"
+ diff "-ruaN" "tarred" "tarred.new" (CWD=${OPAMTMP})
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${OPAMTMP}/tarred)
+ tar "cfz" "${BASEDIR}/OPAM/repo/tarred.tar.gz.tmp" "-C" "${OPAMTMP}" "tarred"
Expand All @@ -85,6 +88,7 @@ build: ["test" "-f" "quux"]
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}"
+ patch "--version"
+ diff "-ruaN" "tarred" "tarred.new" (CWD=${OPAMTMP})
+ patch "-p1" "-i" "${BASEDIR}/OPAM/log/patch" (CWD=${OPAMTMP}/tarred)
### opam install foo.5 -vv | grep '^\+' | sed-cmd test
Expand Down

0 comments on commit 25f4f5c

Please sign in to comment.