Release coccinelle-0.2.0
[bpt/coccinelle.git] / tools / gitsort.ml
CommitLineData
9f8e26f4
C
1(*
2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
34e49164
C
23(* sort a list of git codes such that the most recent comes first *)
24
25let git_home = ref "/home/julia/linux-2.6"
26
27let unwind_protect f cleanup =
28 try f ()
29 with e -> begin cleanup e; raise e end
30
31let (with_open_infile: string -> ((in_channel) -> 'a) -> 'a) = fun file f ->
32 let chan = open_in file in
33 unwind_protect (fun () ->
34 let res = f chan in
35 close_in chan;
36 res)
37 (fun e -> close_in chan)
38
39(* ----------------------------------------------------------------------- *)
40
41let months =
42 [("Jan",1);("Feb",2);("Mar",3);("Apr",4);("May",5);("Jun",6);("Jul",7);
43 ("Aug",8);("Sep",9);("Oct",10);("Nov",11);("Dec",12)]
44
45let antimonths =
46 [(1,31);(2,28);(3,31);(4,30);(5,31); (6,30);(7,31);(8,31);(9,30);(10,31);
47 (11,30);(12,31);(0,31)]
48
49let normalize (year,month,day,hour,minute,second) =
50 if hour < 0
51 then
52 let (day,hour) = (day - 1,hour + 24) in
53 if day = 0
54 then
55 let month = month - 1 in
56 let day = List.assoc month antimonths in
57 let day =
58 if month = 2 && year / 4 * 4 = year && not (year / 100 * 100 = year)
59 then 29
60 else day in
61 if month = 0
62 then (year-1,12,day,hour,minute,second)
63 else (year,month,day,hour,minute,second)
64 else (year,month,day,hour,minute,second)
65 else (year,month,day,hour,minute,second)
66
67exception Fail of string
68
69let read_info code =
70 let _ =
71 Sys.command
72 (Printf.sprintf
73 "pushd %s >& /dev/null ; git log %s^..%s | grep Date: > /tmp/gitsort_info ; popd >& /dev/null"
74 !git_home code code) in
75 with_open_infile "/tmp/gitsort_info" (fun i ->
76 let l =
77 try input_line i
78 with End_of_file -> raise (Fail "bad git file") in
79 match Str.split (Str.regexp " ") l with
80 [date;_;_;weekday;month;day;time;year;offset] ->
81 let day = int_of_string day in
82 let month = List.assoc month months in
83 let year = int_of_string year in
84 (match Str.split (Str.regexp ":") time with
85 [hour;minute;second] ->
86 let hour = int_of_string hour in
87 let minute = int_of_string minute in
88 let second = int_of_string second in
89 let modifier =
90 match String.get offset 0 with
91 '-' -> -1
92 | '+' -> 1
93 | _ -> raise (Fail "bad offset") in
94 (if not (String.sub offset 3 2 = "00")
95 then raise (Fail "require 0 minutes difference"));
96 let hour =
97 hour + (modifier * (int_of_string (String.sub offset 1 2))) in
98 normalize (year,month,day,hour,minute,second)
99 | _ -> raise (Fail "bad date2"))
100 | l -> raise (Fail ("bad date1: "^(String.concat "|" l))))
101
102let rec get_dates = function
103 [] -> []
104 | code::rest ->
105 let date =
106 try Some (read_info code)
107 with
108 Fail s -> Printf.printf "problem in %s: %s\n" code s; None
109 | _ -> Printf.printf "problem in %s\n" code; None in
110 match date with
111 Some date -> (date,code)::(get_dates rest)
112 | None -> get_dates rest
113
114let get_codes file =
115 let gits = ref ([] : string list) in
116 with_open_infile file (fun i ->
117 let rec loop _ =
118 let git = try Some (input_line i) with End_of_file -> None in
119 match git with
120 Some x -> gits := x :: !gits; loop()
121 | None -> () in
122 loop ());
123 List.concat
124 (List.map
125 (function l ->
126 List.filter
127 (* all because I don't know how to make a backslash regexp...*)
128 (function x -> String.length x > 10)
129 (Str.split (Str.regexp "[ \t]+") l))
130 !gits)
131
132let _ =
133 let args = Array.to_list Sys.argv in
134 let file =
135 match args with
136 [_;git_home_info;gits] -> git_home := git_home_info; gits
137 | [_;gits] -> gits
138 | _ -> failwith "args: [git home] git_codes_file" in
139 let codes = get_codes file in
140 let dates = get_dates codes in
141 match List.sort compare dates with
142 (_,last)::prev ->
143 List.iter (function (_,x) -> Printf.printf "%s \\\n" x) (List.rev prev);
144 Printf.printf "%s\n" last
145 | _ -> ()
146
147
148