Skip to content

Commit ec121ea

Browse files
committed
Slight cleanup in diff_promotion
Signed-off-by: Ambre Austen Suhamy <[email protected]>
1 parent a970183 commit ec121ea

File tree

5 files changed

+75
-51
lines changed

5 files changed

+75
-51
lines changed

bin/promotion.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module Apply = struct
4949
let missing =
5050
Diff_promotion.promote_files_registered_in_last_run files_to_promote
5151
in
52-
List.iter missing ~f:on_missing)
52+
List.iter ~f:on_missing missing)
5353
| Error lock_held_by ->
5454
Scheduler.go_without_rpc_server ~common ~config (fun () ->
5555
let open Fiber.O in
@@ -75,7 +75,11 @@ module Diff = struct
7575
let common, config = Common.init builder in
7676
let files_to_promote = files_to_promote ~common files in
7777
Scheduler.go_with_rpc_server ~common ~config (fun () ->
78-
Diff_promotion.display_diffs ~on_missing files_to_promote)
78+
let open Fiber.O in
79+
let db = Diff_promotion.load_db () in
80+
let* missing = Diff_promotion.missing ~db files_to_promote in
81+
List.iter ~f:on_missing missing;
82+
Diff_promotion.display_diffs ~db files_to_promote)
7983
;;
8084

8185
let command = Cmd.v info term
@@ -90,7 +94,11 @@ module Files = struct
9094
let common, config = Common.init builder in
9195
let files_to_promote = files_to_promote ~common files in
9296
Scheduler.go_with_rpc_server ~common ~config (fun () ->
93-
Diff_promotion.display_files ~on_missing files_to_promote)
97+
let open Fiber.O in
98+
let db = Diff_promotion.load_db () in
99+
let* missing = Diff_promotion.missing ~db files_to_promote in
100+
List.iter ~f:on_missing missing;
101+
Diff_promotion.display_files ~db files_to_promote)
94102
;;
95103

96104
let command = Cmd.v info term

src/promote/diff_promotion.ml

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,11 @@ let group_by_targets db =
130130
~f:(List.sort ~compare:(fun (x, _) (y, _) -> Path.Build.compare x y))
131131
;;
132132

133-
let do_promote db files_to_promote =
134-
let by_targets = group_by_targets db in
135-
let promote_one dst srcs =
136-
match srcs with
137-
| [] -> assert false
138-
| (src, staging) :: others ->
139-
(* We used to remove promoted files from the digest cache, to force Dune
133+
let promote_one dst srcs =
134+
match srcs with
135+
| [] -> assert false
136+
| (src, staging) :: others ->
137+
(* We used to remove promoted files from the digest cache, to force Dune
140138
to redigest them on the next run. We did this because on OSX [mtime] is
141139
not precise enough and if a file is modified and promoted quickly, it
142140
looked like it hadn't changed even though it might have.
@@ -149,38 +147,48 @@ let do_promote db files_to_promote =
149147
not promote into the build directory anyway), and source digests should
150148
be correctly invalidated via [fs_memo]. If that doesn't happen, we
151149
should fix [fs_memo] instead of manually resetting the caches here. *)
152-
File.promote { src; staging; dst };
153-
List.iter others ~f:(fun (path, _staging) ->
154-
Console.print
155-
[ Pp.textf " -> ignored %s." (Path.to_string_maybe_quoted (Path.build path))
156-
; Pp.space
157-
])
150+
File.promote { src; staging; dst };
151+
List.iter others ~f:(fun (path, _staging) ->
152+
Console.print
153+
[ Pp.textf " -> ignored %s." (Path.to_string_maybe_quoted (Path.build path))
154+
; Pp.space
155+
])
156+
;;
157+
158+
let do_promote_all db = group_by_targets db |> Path.Source.Map.iteri ~f:promote_one
159+
160+
let do_promote_these db files =
161+
let by_targets = group_by_targets db in
162+
let by_targets, missing =
163+
let files = Path.Source.Set.of_list files in
164+
Path.Source.Set.fold files ~init:(by_targets, []) ~f:(fun fn (map, missing) ->
165+
match Path.Source.Map.find map fn with
166+
| None -> map, fn :: missing
167+
| Some srcs ->
168+
promote_one fn srcs;
169+
Path.Source.Map.remove map fn, missing)
158170
in
159-
match files_to_promote with
171+
let remaining =
172+
Path.Source.Map.to_list by_targets
173+
|> List.concat_map ~f:(fun (dst, srcs) ->
174+
List.map srcs ~f:(fun (src, staging) -> { File.src; staging; dst }))
175+
in
176+
let sorted_missing = List.rev missing in
177+
remaining, sorted_missing
178+
;;
179+
180+
let do_promote db = function
160181
| Dune_rpc_private.Files_to_promote.All ->
161-
Path.Source.Map.iteri by_targets ~f:promote_one;
182+
do_promote_all db;
162183
[], []
163-
| These files ->
164-
let by_targets, missing =
165-
let files = Path.Source.Set.of_list files in
166-
Path.Source.Set.fold files ~init:(by_targets, []) ~f:(fun fn (map, missing) ->
167-
match Path.Source.Map.find map fn with
168-
| None -> map, fn :: missing
169-
| Some srcs ->
170-
promote_one fn srcs;
171-
Path.Source.Map.remove map fn, missing)
172-
in
173-
( Path.Source.Map.to_list by_targets
174-
|> List.concat_map ~f:(fun (dst, srcs) ->
175-
List.map srcs ~f:(fun (src, staging) -> { File.src; staging; dst }))
176-
, missing )
184+
| These files -> do_promote_these db files
177185
;;
178186

179187
let finalize () =
180188
let db =
181189
match !Dune_engine.Clflags.promote with
182190
| Some Automatically ->
183-
let _is_necessarily_empty = do_promote !File.db All in
191+
do_promote_all !File.db;
184192
[]
185193
| Some Never | None -> !File.db
186194
in
@@ -206,7 +214,7 @@ let diff_for_file (file : File.t) =
206214
(** [partition_db files_to_promote db] splits [files_to_promote] into two lists
207215
- The files present in [db] as actual [File.t]s.
208216
- The files absent from [db] as [Path]s. *)
209-
let partition_db files_to_promote db =
217+
let partition_db db files_to_promote =
210218
match files_to_promote with
211219
| Dune_rpc_private.Files_to_promote.All -> db, []
212220
| These paths ->
@@ -217,9 +225,9 @@ let partition_db files_to_promote db =
217225
| None -> Right path)
218226
;;
219227

220-
let sort_for_display files_to_promote =
228+
let sort_for_display db files_to_promote =
221229
let open Fiber.O in
222-
let files, missing = load_db () |> partition_db files_to_promote in
230+
let files, missing = partition_db db files_to_promote in
223231
let+ diff_opts =
224232
Fiber.parallel_map files ~f:(fun file ->
225233
let+ diff_opt = diff_for_file file in
@@ -236,17 +244,21 @@ let sort_for_display files_to_promote =
236244
sorted_diffs, sorted_missing
237245
;;
238246

239-
let display_diffs ~on_missing files_to_promote =
247+
let missing ~db files_to_promote =
248+
let open Fiber.O in
249+
let+ _diffs, missing = sort_for_display db files_to_promote in
250+
missing
251+
;;
252+
253+
let display_diffs ~db files_to_promote =
240254
let open Fiber.O in
241-
let+ diffs, missing = sort_for_display files_to_promote in
242-
List.iter missing ~f:on_missing;
255+
let+ diffs, _missing = sort_for_display db files_to_promote in
243256
List.iter diffs ~f:(fun (_file, diff) -> Print_diff.Diff.print diff)
244257
;;
245258

246-
let display_files ~on_missing files_to_promote =
259+
let display_files ~db files_to_promote =
247260
let open Fiber.O in
248-
let+ diffs, missing = sort_for_display files_to_promote in
249-
List.iter missing ~f:on_missing;
261+
let+ diffs, _missing = sort_for_display db files_to_promote in
250262
List.iter diffs ~f:(fun (file, _diff) ->
251263
Console.printf "%s" (File.source file |> Path.Source.to_string))
252264
;;

src/promote/diff_promotion.mli

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ val promote_files_registered_in_last_run
3232
: Dune_rpc_private.Files_to_promote.t
3333
-> Path.Source.t list
3434

35-
val display_diffs
36-
: on_missing:(Path.Source.t -> unit)
35+
(** [missing ~db files] returns the list of files in [files] but not in [db]. *)
36+
val missing
37+
: db:File.t list
3738
-> Dune_rpc_private.Files_to_promote.t
38-
-> unit Fiber.t
39+
-> Path.Source.t list Fiber.t
3940

40-
val display_files
41-
: on_missing:(Path.Source.t -> unit)
42-
-> Dune_rpc_private.Files_to_promote.t
43-
-> unit Fiber.t
41+
(** [display_diffs ~db files] will only print the diffs of files that are both
42+
in [files] and in [db]. *)
43+
val display_diffs : db:File.t list -> Dune_rpc_private.Files_to_promote.t -> unit Fiber.t
44+
45+
(** [display_files ~db files] will only print the filenames of files that are
46+
both in [files] and in [db]. *)
47+
val display_files : db:File.t list -> Dune_rpc_private.Files_to_promote.t -> unit Fiber.t

test/blackbox-tests/test-cases/promote/old-tests.t/run.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Test single file promotion
7878
$ cat y
7979
titi
8080
$ dune promote x y
81-
Warning: Nothing to promote for y.
8281
Warning: Nothing to promote for x.
82+
Warning: Nothing to promote for y.
8383

8484
Reproduction case for #1772
8585
---------------------------

test/blackbox-tests/test-cases/watching/promotion-db.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Promoting A at this point does nothing, and we print a warning.
2626

2727
This should be a warning for both A and B.
2828
$ dune promote a.t b.t
29-
Nothing to promote for b.t.
3029
Nothing to promote for a.t.
30+
Nothing to promote for b.t.
3131
Warning: Build completed with 2 warnings.
3232

3333

0 commit comments

Comments
 (0)