+let fix_sgrep_diffs l =
+ let l =
+ List.filter (function s -> (s =~ "^\\+\\+\\+") || not (s =~ "^\\+")) l in
+ let l = List.rev l in
+ (* adjust second number for + code *)
+ let rec loop1 n = function
+ [] -> []
+ | s::ss ->
+ if s =~ "^-" && not(s =~ "^---")
+ then s :: loop1 (n+1) ss
+ else if s =~ "^@@"
+ then
+ (match Str.split (Str.regexp " ") s with
+ bef::min::pl::aft ->
+ let (n1,n2) =
+ match Str.split (Str.regexp ",") pl with
+ [n1;n2] -> (n1,n2)
+ | [n1] -> (n1,"1")
+ | _ -> failwith "bad + line information" in
+ let n2 = int_of_string n2 in
+ (Printf.sprintf "%s %s %s,%d %s" bef min n1 (n2-n)
+ (String.concat " " aft))
+ :: loop1 0 ss
+ | _ -> failwith "bad @@ information")
+ else s :: loop1 n ss in
+ let rec loop2 n = function
+ [] -> []
+ | s::ss ->
+ if s =~ "^---"
+ then s :: loop2 0 ss
+ else if s =~ "^@@"
+ then
+ (match Str.split (Str.regexp " ") s with
+ bef::min::pl::aft ->
+ let (m2,n1,n2) =
+ match (Str.split (Str.regexp ",") min,
+ Str.split (Str.regexp ",") pl) with
+ ([_;m2],[n1;n2]) -> (m2,n1,n2)
+ | ([_],[n1;n2]) -> ("1",n1,n2)
+ | ([_;m2],[n1]) -> (m2,n1,"1")
+ | ([_],[n1]) -> ("1",n1,"1")
+ | _ -> failwith "bad -/+ line information" in
+ let n1 =
+ int_of_string (String.sub n1 1 ((String.length n1)-1)) in
+ let m2 = int_of_string m2 in
+ let n2 = int_of_string n2 in
+ (Printf.sprintf "%s %s +%d,%d %s" bef min (n1-n) n2
+ (String.concat " " aft))
+ :: loop2 (n+(m2-n2)) ss
+ | _ -> failwith "bad @@ information")
+ else s :: loop2 n ss in
+ loop2 0 (List.rev (loop1 0 l))
+
+let normalize_path file =
+ let fullpath =
+ if String.get file 0 = '/' then file else (Sys.getcwd()) ^ "/" ^ file in
+ let elements = Str.split_delim (Str.regexp "/") fullpath in
+ let rec loop prev = function
+ [] -> String.concat "/" (List.rev prev)
+ | "." :: rest -> loop prev rest
+ | ".." :: rest ->
+ (match prev with
+ x::xs -> loop xs rest
+ | _ -> failwith "bad path")
+ | x::rest -> loop (x::prev) rest in
+ loop [] elements
+
+let show_or_not_diff2 cfile outfile =