+let mutex = Mutex.create ();;
+
+let to_be_committed = ref [];;
+
+let html_of_matita s =
+ let patt1 = Str.regexp "\005" in
+ let patt2 = Str.regexp "\006" in
+ let patt3 = Str.regexp "<" in
+ let patt4 = Str.regexp ">" in
+ let res = Str.global_replace patt4 ">" s in
+ let res = Str.global_replace patt3 "<" res in
+ let res = Str.global_replace patt2 ">" res in
+ let res = Str.global_replace patt1 "<" res in
+ res
+;;
+
+(* adds a user to the commit queue; concurrent instances possible, so we
+ * enclose the update in a CS
+ *)
+let add_user_for_commit uid =
+ Mutex.lock mutex;
+ to_be_committed := uid::List.filter (fun x -> x <> uid) !to_be_committed;
+ Mutex.unlock mutex;
+;;
+
+let do_global_commit () =
+ prerr_endline ("to be committed: " ^ String.concat " " !to_be_committed);
+ List.fold_left
+ (fun out u ->
+ let ft = MatitaAuthentication.read_ft u in
+
+ (* first we add new files/dirs to the repository *)
+ (* must take the reverse because svn requires the add to be performed in
+ the correct order
+ (otherwise run with --parents option) *)
+ let to_be_added = List.rev (List.map fst
+ (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MAdd) ft))
+ in
+ prerr_endline ("@@@ ADDING files: " ^ String.concat ", " to_be_added);
+ let out =
+ try
+ let newout = MatitaFilesystem.add_files u to_be_added in
+ out ^ "\n" ^ newout
+ with
+ | MatitaFilesystem.SvnError outstr ->
+ prerr_endline ("ADD OF " ^ u ^ "FAILED:" ^ outstr);
+ out
+ in
+
+ (* now we update the local copy (to merge updates from other users) *)
+ let out = try
+ let files,anomalies,(added,conflict,del,upd,merged) =
+ MatitaFilesystem.update_user u
+ in
+ let anomalies = String.concat "\n" anomalies in
+ let details = Printf.sprintf
+ ("%d new files\n"^^
+ "%d deleted files\n"^^
+ "%d updated files\n"^^
+ "%d merged files\n"^^
+ "%d conflicting files\n\n" ^^
+ "Anomalies:\n%s") added del upd merged conflict anomalies
+ in
+ prerr_endline ("update details:\n" ^ details);
+ MatitaAuthentication.set_file_flag u files;
+ out ^ "\n" ^ details
+ with
+ | MatitaFilesystem.SvnError outstr ->
+ prerr_endline ("UPDATE OF " ^ u ^ "FAILED:" ^ outstr);
+ out
+ in
+
+ (* we re-read the file table after updating *)
+ let ft = MatitaAuthentication.read_ft u in
+
+ (* finally we perform the real commit *)
+ let modified = (List.map fst
+ (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MModified) ft))
+ in
+ let to_be_committed = to_be_added @ modified
+ in
+ let out = try
+ let newout = MatitaFilesystem.commit u to_be_committed in
+ out ^ "\n" ^ newout
+ with
+ | MatitaFilesystem.SvnError outstr ->
+ prerr_endline ("COMMIT OF " ^ u ^ "FAILED:" ^ outstr);
+ out
+ in
+
+ (* call stat to get the final status *)
+ let files, anomalies = MatitaFilesystem.stat_user u in
+ let added,not_added = List.fold_left
+ (fun (a_acc, na_acc) fname ->
+ if List.mem fname (List.map fst files) then
+ a_acc, fname::na_acc
+ else
+ fname::a_acc, na_acc)
+ ([],[]) to_be_added
+ in
+ let committed,not_committed = List.fold_left
+ (fun (c_acc, nc_acc) fname ->
+ if List.mem fname (List.map fst files) then
+ c_acc, fname::nc_acc
+ else
+ fname::c_acc, nc_acc)
+ ([],[]) modified
+ in
+ let conflicts = List.map fst (List.filter
+ (fun (_,f) -> f = Some MatitaFilesystem.MConflict) files)
+ in
+ MatitaAuthentication.set_file_flag u
+ (List.map (fun x -> x, Some MatitaFilesystem.MSynchronized) (added@committed));
+ MatitaAuthentication.set_file_flag u files;
+ out ^ "\n\n" ^ (Printf.sprintf
+ ("COMMIT RESULTS for %s\n" ^^
+ "==============\n" ^^
+ "added and committed (%d of %d): %s\n" ^^
+ "modified and committed (%d of %d): %s\n" ^^
+ "not added: %s\n" ^^
+ "not committed: %s\n" ^^
+ "conflicts: %s\n")
+ u (List.length added) (List.length to_be_added) (String.concat ", " added)
+ (List.length committed) (List.length modified) (String.concat ", " committed)
+ (String.concat ", " not_added)
+ (String.concat ", " not_committed) (String.concat ", " conflicts)))
+
+ (* XXX: at the moment, we don't keep track of the order in which users have
+ scheduled their commits, but we should, otherwise we will get a
+ "first come, random served" policy *)
+ "" (* (List.rev !to_be_committed) *) (MatitaAuthentication.get_users ())
+;;