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