Skip to content

Commit d83fd0b

Browse files
committed
Optimize + log + fixes (works now)
1 parent 6457294 commit d83fd0b

16 files changed

+191
-98
lines changed

src/client/opamInitDefaults.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ let recommended_tools () =
133133
let required_tools ~sandboxing () =
134134
req_dl_tools () @
135135
[
136-
["diff"], None, None;
137136
["patch"], None, Some patch_filter;
138137
["gpatch"], None, Some gpatch_filter;
139138
["tar"], None, Some tar_filter;
@@ -148,7 +147,7 @@ let required_tools ~sandboxing () =
148147

149148
let required_packages_for_cygwin =
150149
[
151-
"diffutils";
150+
"diffutils"; (* TODO: not used internally anymore but used by many packages *)
152151
"git"; (* XXX hg & mercurial ? *)
153152
"make";
154153
"patch";

src/core/opamFilename.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,12 @@ let link ?(relative=false) ~target ~link =
429429
OpamSystem.link target (to_string link)
430430
[@@ocaml.warning "-16"]
431431

432-
let patch ?preprocess ?internal filename dirname =
433-
OpamSystem.patch ?preprocess ?internal ~dir:(Dir.to_string dirname) (to_string filename)
432+
let patch ?preprocess filename dirname =
433+
OpamSystem.patch ?preprocess ~dir:(Dir.to_string dirname) (to_string filename)
434+
435+
let internal_patch ~patch_filename diffs dirname =
436+
OpamSystem.internal_patch
437+
~patch_filename:(to_string patch_filename) ~dir:(Dir.to_string dirname) diffs
434438

435439
let flock flag ?dontblock file = OpamSystem.flock flag ?dontblock (to_string file)
436440

src/core/opamFilename.mli

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,11 @@ val remove_prefix_dir: Dir.t -> Dir.t -> string
259259
val remove_suffix: Base.t -> t -> string
260260

261261
(** Apply a patch in a directory. If [preprocess] is set to false, there is no
262-
CRLF translation. If [internal] is set to true, a pure OCaml version of patch
263-
will be used instead of calling the "patch" external command.
262+
CRLF translation.
264263
Returns [None] on success, the process error otherwise *)
265-
val patch: ?preprocess:bool -> ?internal:bool -> t -> Dir.t -> exn option OpamProcess.job
264+
val patch: ?preprocess:bool -> t -> Dir.t -> exn option OpamProcess.job
265+
266+
val internal_patch : patch_filename:t -> Patch.t list -> Dir.t -> unit
266267

267268
(** Create an empty file *)
268269
val touch: t -> unit

src/core/opamSystem.ml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,21 +1635,29 @@ let translate_patch ~dir orig corrected =
16351635
16361636
exception Internal_patch_error of string
16371637
1638-
let internal_patch ~dir p =
1638+
let internal_patch ~patch_filename ~dir diffs =
16391639
let fmt = Printf.sprintf in
1640+
let get_filename ~p full =
1641+
(* Taken from my code from ocaml-patch *)
1642+
let rec iter idx = function
1643+
| 0 -> String.sub full idx (String.length full - idx)
1644+
| p -> iter (String.index_from full idx '/') (p - 1)
1645+
in
1646+
try iter 0 p with Not_found -> failwith "Malformed patch"
1647+
in
16401648
let get_path file =
16411649
let dir = real_path dir in
1642-
let file = real_path (Filename.concat dir file) in
1650+
let file = real_path (Filename.concat dir (get_filename ~p:1 file)) in
16431651
if not (OpamStd.String.is_prefix_of ~from:0 ~full:file dir) then
1644-
raise (Internal_patch_error (fmt "Patch %S tried to escape its scope." p));
1652+
raise (Internal_patch_error (fmt "Patch %S tried to escape its scope." patch_filename));
16451653
file
16461654
in
16471655
let patch content diff =
16481656
match Patch.patch content diff with
16491657
| Some x -> x
16501658
| None -> assert false
16511659
| exception _ ->
1652-
raise (Internal_patch_error (fmt "Patch %S does not apply cleanly." p))
1660+
raise (Internal_patch_error (fmt "Patch %S does not apply cleanly." patch_filename))
16531661
in
16541662
let apply diff = match diff.Patch.operation with
16551663
| Patch.Edit file ->
@@ -1684,13 +1692,9 @@ let internal_patch ~dir p =
16841692
let dst = get_path dst in
16851693
Unix.rename src dst
16861694
in
1687-
let content = read p in
1688-
match Patch.to_diffs content with
1689-
| diffs -> List.iter apply diffs
1690-
| exception _ ->
1691-
raise (Internal_patch_error (fmt "Patch %S failed to parse." p))
1695+
List.iter apply diffs
16921696
1693-
let patch ?(preprocess=true) ?(internal=false) ~dir p =
1697+
let patch ?(preprocess=true) ~dir p =
16941698
if not (Sys.file_exists p) then
16951699
(OpamConsole.error "Patch file %S not found." p;
16961700
raise Not_found);
@@ -1706,19 +1710,15 @@ let patch ?(preprocess=true) ?(internal=false) ~dir p =
17061710
if not (OpamConsole.debug ()) then Sys.remove p';
17071711
in
17081712
Fun.protect ~finally:cleanup @@ fun () ->
1709-
if internal then begin
1710-
try internal_patch ~dir p; Done None
1711-
with exn -> Done (Some exn)
1712-
end else
1713-
let patch_cmd =
1714-
match OpamStd.Sys.os () with
1715-
| OpamStd.Sys.OpenBSD
1716-
| OpamStd.Sys.FreeBSD -> "gpatch"
1717-
| _ -> "patch"
1718-
in
1719-
make_command ~name:"patch" ~dir patch_cmd ["-p1"; "-i"; p'] @@> fun r ->
1720-
if OpamProcess.is_success r then Done None
1721-
else Done (Some (Process_error r))
1713+
let patch_cmd =
1714+
match OpamStd.Sys.os () with
1715+
| OpamStd.Sys.OpenBSD
1716+
| OpamStd.Sys.FreeBSD -> "gpatch"
1717+
| _ -> "patch"
1718+
in
1719+
make_command ~name:"patch" ~dir patch_cmd ["-p1"; "-i"; p'] @@> fun r ->
1720+
if OpamProcess.is_success r then Done None
1721+
else Done (Some (Process_error r))
17221722
17231723
let register_printer () =
17241724
Printexc.register_printer (function

src/core/opamSystem.mli

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ val read: string -> string
112112
advisory write lock to prevent concurrent reads or writes) *)
113113
val write: string -> string -> unit
114114

115+
(** [get_files dir] returns the list of files inside the directory [dir]. *)
116+
val get_files : string -> string list
117+
115118
(** [remove filename] removes [filename]. Works whether [filename] is
116119
a file or a directory *)
117120
val remove: string -> unit
@@ -305,10 +308,11 @@ val get_lock_fd: lock -> Unix.file_descr
305308
(** {2 Misc} *)
306309

307310
(** Apply a patch file in the current directory. If [preprocess] is set to
308-
false, there is no CRLF translation. If [internal] is set to true,
309-
a pure OCaml version of patch will be used instead of calling the "patch"
310-
external command. Returns the error if the patch didn't apply. *)
311-
val patch: ?preprocess:bool -> ?internal:bool -> dir:string -> string -> exn option OpamProcess.job
311+
false, there is no CRLF translation.
312+
Returns the error if the patch didn't apply. *)
313+
val patch: ?preprocess:bool -> dir:string -> string -> exn option OpamProcess.job
314+
315+
val internal_patch : patch_filename:string -> dir:string -> Patch.t list -> unit
312316

313317
(** Returns the end-of-line encoding style for the given file. [None] means that
314318
either the encoding of line endings is mixed, or the file contains no line

src/repository/opamDarcs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ module VCS = struct
151151
if OpamSystem.file_is_empty patch_file then
152152
(finalise (); Done None)
153153
else
154-
Done (Some (OpamFilename.of_string patch_file))
154+
Done (Some (OpamFilename.of_string patch_file, Patch.to_diffs ~p:1 (String.concat "\n" r.r_stdout)))
155155

156156
let versioned_files repo_root =
157157
darcs repo_root [ "show" ; "files" ]

src/repository/opamGit.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module VCS : OpamVCS.VCS = struct
205205
else if OpamSystem.file_is_empty patch_file then
206206
(finalise (); Done None)
207207
else
208-
Done (Some (OpamFilename.of_string patch_file))
208+
Done (Some (OpamFilename.of_string patch_file, Patch.to_diffs ~p:1 (String.concat "\n" r.r_stdout)))
209209

210210
let is_up_to_date ?subpath repo_root repo_url =
211211
let rref = remote_ref repo_url in

src/repository/opamHTTP.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ module B = struct
5757
Done (OpamRepositoryBackend.Update_full quarantine)
5858
else
5959
OpamProcess.Job.finally finalise @@ fun () ->
60-
OpamRepositoryBackend.job_text repo_name "diff"
61-
(OpamRepositoryBackend.get_diff
62-
(OpamFilename.dirname_dir repo_root)
63-
(OpamFilename.basename_dir repo_root)
64-
(OpamFilename.basename_dir quarantine))
65-
@@| function
66-
| None -> OpamRepositoryBackend.Update_empty
67-
| Some patch -> OpamRepositoryBackend.Update_patch patch
60+
OpamRepositoryBackend.job_text repo_name "diff" @@
61+
(OpamRepositoryBackend.get_diff
62+
(OpamFilename.dirname_dir repo_root)
63+
(OpamFilename.basename_dir repo_root)
64+
(OpamFilename.basename_dir quarantine)
65+
|> function
66+
| None -> Done (OpamRepositoryBackend.Update_empty)
67+
| Some patch -> Done (OpamRepositoryBackend.Update_patch patch))
6868

6969
let repo_update_complete _ _ = Done ()
7070

src/repository/opamHg.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module VCS = struct
8282
else if OpamSystem.file_is_empty patch_file then
8383
(finalise (); Done None)
8484
else
85-
Done (Some (OpamFilename.of_string patch_file))
85+
Done (Some (OpamFilename.of_string patch_file, Patch.to_diffs ~p:1 (String.concat "\n" r.r_stdout)))
8686

8787
let is_up_to_date ?subpath:_ repo_root repo_url =
8888
let mark = mark_from_url repo_url in

src/repository/opamLocal.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ module B = struct
180180
else
181181
OpamProcess.Job.finally finalise @@ fun () ->
182182
OpamRepositoryBackend.job_text repo_name "diff" @@
183-
OpamRepositoryBackend.get_diff
184-
(OpamFilename.dirname_dir repo_root)
185-
(OpamFilename.basename_dir repo_root)
186-
(OpamFilename.basename_dir quarantine)
187-
@@| function
188-
| None -> OpamRepositoryBackend.Update_empty
189-
| Some p -> OpamRepositoryBackend.Update_patch p
183+
(OpamRepositoryBackend.get_diff
184+
(OpamFilename.dirname_dir repo_root)
185+
(OpamFilename.basename_dir repo_root)
186+
(OpamFilename.basename_dir quarantine)
187+
|> function
188+
| None -> Done (OpamRepositoryBackend.Update_empty)
189+
| Some p -> Done (OpamRepositoryBackend.Update_patch p))
190190

191191
let repo_update_complete _ _ = Done ()
192192

src/repository/opamRepository.ml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ let validate_repo_update repo repo_root update =
458458
| "anchors", _ -> Some (S (String.concat "," ta.fingerprints))
459459
| "quorum", _ -> Some (S (string_of_int ta.quorum))
460460
| "repo", _ -> Some (S (OpamFilename.Dir.to_string repo_root))
461-
| "patch", Update_patch f -> Some (S (OpamFilename.to_string f))
461+
| "patch", Update_patch (f, _diff) -> Some (S (OpamFilename.to_string f))
462462
| "incremental", Update_patch _ -> Some (B true)
463463
| "incremental", _ -> Some (B false)
464464
| "dir", Update_full d -> Some (S (OpamFilename.Dir.to_string d))
@@ -493,24 +493,21 @@ let apply_repo_update repo repo_root = function
493493
(OpamConsole.colorise `green
494494
(OpamRepositoryName.to_string repo.repo_name));
495495
Done ()
496-
| Update_patch f ->
496+
| Update_patch (f, diff) ->
497497
OpamConsole.msg "[%s] synchronised from %s\n"
498498
(OpamConsole.colorise `green
499499
(OpamRepositoryName.to_string repo.repo_name))
500500
(OpamUrl.to_string repo.repo_url);
501501
log "%a: applying patch update at %a"
502502
(slog OpamRepositoryName.to_string) repo.repo_name
503503
(slog OpamFilename.to_string) f;
504-
let preprocess =
505-
match repo.repo_url.OpamUrl.backend with
506-
| `http | `rsync -> false
507-
| _ -> true
508-
in
509-
(OpamFilename.patch ~preprocess ~internal:true f repo_root @@+ function
510-
| Some e ->
511-
if not (OpamConsole.debug ()) then OpamFilename.remove f;
512-
raise e
513-
| None -> OpamFilename.remove f; Done ())
504+
(try
505+
OpamFilename.internal_patch ~patch_filename:f diff repo_root;
506+
OpamFilename.remove f; Done ()
507+
with
508+
| e ->
509+
if not (OpamConsole.debug ()) then OpamFilename.remove f;
510+
raise e)
514511
| Update_empty ->
515512
OpamConsole.msg "[%s] no changes from %s\n"
516513
(OpamConsole.colorise `green
@@ -525,7 +522,7 @@ let cleanup_repo_update upd =
525522
if not (OpamConsole.debug ()) then
526523
match upd with
527524
| Update_full d -> OpamFilename.rmdir d
528-
| Update_patch f -> OpamFilename.remove f
525+
| Update_patch (f, _diff) -> OpamFilename.remove f
529526
| _ -> ()
530527

531528
let update repo repo_root =

0 commit comments

Comments
 (0)