Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added --sort, -s options to ometastore and find-git-files, find-git-r…

…epos.

The overhead in fold_directory is fairly small.
  • Loading branch information...
commit cdd8a381e95e49791ce3807d5b283999dcb3b703 1 parent 8a1ea8b
@mfp mfp authored
Showing with 50 additions and 33 deletions.
  1. +5 −3 find-git-files.ml
  2. +4 −1 find-git-repos.ml
  3. +23 −20 folddir.ml
  4. +18 −9 ometastore.ml
View
8 find-git-files.ml
@@ -10,7 +10,7 @@ let debug = ref false
module Find(F : Folddir.S) =
struct
- let find ?(debug=false) path =
+ let find ?(debug=false) ?(sorted=false) path =
let aux l name stat =
let dir = join path name in
match stat.st_kind with
@@ -19,7 +19,7 @@ struct
with Unix_error _ -> Continue (name :: l)
end
| _ -> Continue (name :: l)
- in List.rev (F.fold_directory ~debug aux [] path "")
+ in List.rev (F.fold_directory ~debug ~sorted aux [] path "")
end
module Gitignored = Find(Folddir.Make(Folddir.Gitignore))
@@ -28,14 +28,16 @@ let main () =
let usage = "Usage: find-git-files <options>" in
let path = ref "." in
let zerosep = ref false in
+ let sorted = ref false in
let specs = [
"--path", Arg.Set_string path, "Set base path (default: .)";
"-z", Arg.Set zerosep, "Use \\0 to separate filenames.";
+ "-s", Arg.Set sorted, "Sort output.";
"--debug", Arg.Set debug, "Debug mode"
]
in
Arg.parse specs ignore usage;
let print = if !zerosep then printf "%s\000" else printf "%s\n" in
- List.iter print (Gitignored.find ~debug:!debug !path)
+ List.iter print (Gitignored.find ~debug:!debug ~sorted:!sorted !path)
let () = main ()
View
5 find-git-repos.ml
@@ -30,15 +30,18 @@ let main () =
let path = ref "." in
let find_repos = ref All.find_repositories in
let zerosep = ref false in
+ let sorted = ref false in
let specs = [
"--path", Arg.Set_string path, "Set base path (default: .)";
"-i", Arg.Unit (fun () -> find_repos := Gitignored.find_repositories),
"Mimic git semantics (honor .gitignore, don't scan git submodules)";
"-z", Arg.Set zerosep, "Use \\0 to separate filenames.";
+ "-s", Arg.Set sorted, "Sort output.";
"--debug", Arg.Set debug, "Debug mode"
]
in Arg.parse specs ignore usage;
let print = if !zerosep then printf "%s\000" else printf "%s\n" in
- List.iter print (!find_repos ~debug:!debug !path)
+ let l = !find_repos ~debug:!debug !path in
+ List.iter print (if !sorted then List.sort compare l else l)
let () = main ()
View
43 folddir.ml
@@ -20,7 +20,8 @@ module type S =
sig
type ignore_info
val fold_directory :
- ?debug:bool -> ('a -> string -> Unix.stats -> 'a fold_acc) -> 'a ->
+ ?debug:bool -> ?sorted:bool ->
+ ('a -> string -> Unix.stats -> 'a fold_acc) -> 'a ->
string -> ?ign_info:ignore_info -> string -> 'a
end
@@ -28,31 +29,33 @@ module Make(M : IGNORE) : S with type ignore_info = M.t =
struct
type ignore_info = M.t
- let rec fold_directory ?(debug=false) f acc base ?(ign_info = M.init base) path =
+ let rec fold_directory ?(debug=false) ?(sorted=false) f acc base
+ ?(ign_info = M.init base) path =
+ let readd d =
+ let l = ref [] in
+ try while true do l := readdir d :: !l done; assert false
+ with End_of_file -> !l in
let acc = ref acc in
let ign_info = M.update ign_info ~base ~path in
let dir = join base path in
try
do_finally (opendir dir) closedir
(fun d ->
- try
- while true do
- match readdir d with
- "." | ".." -> ()
- | n when M.is_ignored ~debug ign_info n -> ()
- | n ->
- let n = join path n in
- let stat = lstat (join base n) in
- match f !acc n stat with
- | Continue x ->
- acc := x;
- if stat.st_kind = S_DIR then
- acc := fold_directory ~debug f ~ign_info
- !acc base n
- | Prune x -> acc := x
- done;
- assert false
- with End_of_file -> ());
+ List.iter
+ (function
+ "." | ".." -> ()
+ | n when M.is_ignored ~debug ign_info n -> ()
+ | n ->
+ let n = join path n in
+ let stat = lstat (join base n) in
+ match f !acc n stat with
+ | Continue x ->
+ acc := x;
+ if stat.st_kind = S_DIR then
+ acc := fold_directory ~debug ~sorted f ~ign_info
+ !acc base n
+ | Prune x -> acc := x)
+ (let l = readd d in if sorted then List.sort compare l else l));
!acc
with Unix.Unix_error _ -> !acc
end
View
27 ometastore.ml
@@ -48,7 +48,7 @@ let entry_of_path path =
module Entries(F : Folddir.S) =
struct
- let get_entries ?(debug=false) path =
+ let get_entries ?(debug=false) ?(sorted=false) path =
let aux l name stat =
let fullname = join path name in
let entry = { (entry_of_path fullname) with path = name } in
@@ -58,7 +58,7 @@ struct
with Unix_error _ -> Continue (entry :: l)
end
| _ -> Continue (entry :: l)
- in List.rev (F.fold_directory ~debug aux [] path "")
+ in List.rev (F.fold_directory ~debug ~sorted aux [] path "")
end
let write_int os bytes n =
@@ -92,7 +92,7 @@ let common_prefix_chars s1 s2 =
if String.length s1 = 0 || String.length s2 = 0 then 0
else loop s1 s2 0 (min (String.length s1 - 1) (String.length s2 -1))
-let dump_entries ?(verbose=false) l fname =
+let dump_entries ?(verbose=false) ?(sorted=false) l fname =
let dump_entry os prev e =
if verbose then printf "%s\n" e.path;
let pref = common_prefix_chars prev e.path in
@@ -111,7 +111,8 @@ let dump_entries ?(verbose=false) l fname =
in do_finally (open_out_bin fname) close_out begin fun os ->
output_string os (magic ^ "\n");
output_string os (version ^ "\n");
- ignore (List.fold_left (dump_entry os) "" l)
+ let l = if sorted then List.sort compare l else l in
+ ignore (List.fold_left (dump_entry os) "" l)
end
let read_entries fname =
@@ -166,7 +167,7 @@ let compare_entries l1 l2 =
[] l1
in List.rev (List.rev_append deletions changes)
-let print_changes =
+let print_changes ?(sorted=false) l =
List.iter
(function
Added e -> printf "Added: %s\n" e.path
@@ -185,11 +186,14 @@ let print_changes =
in match List.rev diffs with
[] -> ()
| l -> printf "Changed %s: %s\n" e1.path (String.concat " " l))
+ (if sorted then List.sort compare l else l)
-let print_deleted separator =
+let print_deleted ?(sorted=false) separator l =
List.iter
(function Deleted e -> printf "%s%s" e.path separator
| Added _ | Diff _ -> ())
+ (if sorted then List.sort compare l else l)
+
let out s = if !verbose then Printf.fprintf Pervasives.stdout s
else Printf.ifprintf Pervasives.stdout s
@@ -235,6 +239,7 @@ let main () =
let path = ref "." in
let get_entries = ref Allentries.get_entries in
let sep = ref "\n" in
+ let sorted = ref false in
let specs = [
"-c", Arg.Unit (fun () -> mode := `Compare),
"Show all differences between stored and real metadata";
@@ -246,19 +251,23 @@ let main () =
"Mimic git semantics (honor .gitignore, don't scan git submodules)";
"-m", Arg.Set use_mtime, "Consider mtime for diff and apply";
"-z", Arg.Unit (fun () -> sep := "\000"), "Use \\0 to separate filenames.";
+ "--sort", Arg.Set sorted, "Sort output by filename.";
"-v", Arg.Set verbose, "Verbose mode";
"--debug", Arg.Set debug, "Debug mode"
]
in Arg.parse specs ignore usage;
match !mode with
| `Unset -> Arg.usage specs usage
- | `Save -> dump_entries ~verbose:!verbose (!get_entries !path) !file
+ | `Save ->
+ dump_entries ~sorted:!sorted ~verbose:!verbose (!get_entries !path) !file
| `Show_deleted | `Compare | `Apply as mode ->
let stored = read_entries !file in
let actual = !get_entries ~debug:!debug !path in
match mode with
- `Compare -> print_changes (compare_entries stored actual)
+ `Compare ->
+ print_changes ~sorted:!sorted (compare_entries stored actual)
| `Apply -> apply_changes !path (compare_entries actual stored)
- | `Show_deleted -> print_deleted !sep (compare_entries stored actual)
+ | `Show_deleted ->
+ print_deleted ~sorted:!sorted !sep (compare_entries stored actual)
let () = main ()
Please sign in to comment.
Something went wrong with that request. Please try again.