Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / tools / gitgrep.ml
1 (*
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27 (* adjust as convenient *)
28 let prefix = "/tmp/"
29 let prefix = ""
30
31 (* The -grouped option means that all - and + code must appear in a
32 single contiguous block of - + code. This option has no effect on the
33 other kinds of patterns, ie Changelog (C) or Context (@) *)
34
35 (* example: gitgrep -grouped -maxlen 25 - "[A-Z][A-Z]+" + "[A-Z][A-Z]+"
36 usb_21_22 *)
37
38 type dir = Minus | Plus | Context | ChangeLog
39
40 type res = Git of string | Block of int * string
41
42 let grouped = ref false
43 let maxlen = ref None
44
45 let space = Str.regexp " "
46
47 let matches pattern line =
48 try let _ = Str.search_forward pattern line 0 in true
49 with Not_found -> false
50
51 let res = ref []
52
53 let scan dir pattern i =
54 let rec loop skipping cl git =
55 let line = input_line i in
56 match Str.split space line with
57 ["commit";git] -> loop false true git
58 | "diff"::_ -> loop skipping false git
59 | _ ->
60 if String.length line > 0 && not skipping &&
61 ((String.get line 0 = '-' && dir = Minus) or
62 (String.get line 0 = '+' && dir = Plus) or
63 (cl && dir = ChangeLog) or
64 (not (String.get line 0 = '-') && not (String.get line 0 = '+') &&
65 dir = Context)) &&
66 matches pattern line
67 then (res := Git(git)::!res; loop true cl git)
68 else loop skipping cl git in
69 loop false false ""
70
71 (* for Minus and Plus directions only *)
72 let scan_grouped dir pattern i =
73 let block = ref 0 in
74 (* mp = true in minus-plus region *)
75 let rec loop mp git =
76 let line = input_line i in
77 match Str.split space line with
78 ["commit";git] -> loop false git
79 | "diff"::_ -> loop false git
80 | _ ->
81 if String.length line > 0
82 then
83 let first_char = String.get line 0 in
84 let new_mp =
85 match first_char with
86 '-' | '+' -> (if not mp then block := !block + 1; true)
87 | _ -> false in
88 match (first_char,dir) with
89 ('-',Minus) | ('+',Plus) ->
90 let info = Block(!block,git) in
91 (if matches pattern line && not (List.mem info !res)
92 then res := info::!res);
93 loop new_mp git
94 | _ -> loop new_mp git
95 else loop mp git in
96 loop false ""
97
98 let scan_line max i =
99 let rec loop skipping num git =
100 let line = input_line i in
101 match Str.split space line with
102 ["commit";git1] ->
103 loop false (-1) git1
104 | "diff"::_ ->
105 if num > max && not skipping
106 then (res:=Git(git)::!res;loop true (num+1) git)
107 else loop skipping (if num = (-1) then 1 else num+1) git
108 | _ ->
109 if num > max && not skipping
110 then (res:=Git(git)::!res;loop true (num+1) git)
111 else loop skipping (if num = (-1) then num else num+1) git in
112 loop false (-1) ""
113
114 let dot = Str.regexp "\\."
115
116 let open_git file =
117 let tmp = prefix^file in
118 if Sys.file_exists tmp
119 then open_in tmp
120 else
121 match List.rev (Str.split dot file) with
122 last::rest ->
123 let last_int = int_of_string last in
124 if last_int = 0
125 then
126 failwith
127 "can't go back one version from 0; make the log file by hand";
128 let prev =
129 String.concat "." (List.rev ((string_of_int (last_int-1))::rest)) in
130 let _ =
131 Sys.command
132 (Printf.sprintf "git log -p v%s..v%s > %s" prev file tmp) in
133 open_in tmp
134 | _ -> open_in file
135
136 let rec split_args = function
137 [] -> []
138 | "-grouped"::rest -> grouped := true; split_args rest
139 | "-maxlen"::len::rest -> maxlen := Some (int_of_string len); split_args rest
140 | "-"::pattern::rest -> (Minus,Str.regexp pattern) :: split_args rest
141 | "+"::pattern::rest -> (Plus,Str.regexp pattern) :: split_args rest
142 | "@"::pattern::rest -> (Context,Str.regexp pattern) :: split_args rest
143 | "C"::pattern::rest -> (ChangeLog,Str.regexp pattern) :: split_args rest
144 | _ -> failwith "bad argument list"
145
146 let process_one (dir,pattern) version =
147 res := [];
148 let i = open_git version in
149 try
150 if !grouped && (dir = Minus or dir = Plus)
151 then scan_grouped dir pattern i
152 else scan dir pattern i
153 with End_of_file -> (close_in i; List.rev !res)
154
155 let process_len max version =
156 res := [];
157 let i = open_git version in
158 try scan_line max i
159 with End_of_file -> (close_in i; List.rev !res)
160
161 let inter l1 l2 =
162 List.rev
163 (List.fold_left
164 (function prev ->
165 function
166 (Git(git)) as x ->
167 let rec loop = function
168 [] -> prev
169 | Git(git1)::rest when git = git1 -> x::prev
170 | Block(b1,git1)::rest when git = git1 -> Block(b1,git1)::prev
171 | _::rest -> loop rest in
172 loop l2
173 | (Block(block,git)) as x ->
174 let rec loop = function
175 [] -> prev
176 | Git(git1)::rest when git = git1 -> x::prev
177 | Block(b1,git1)::rest when block = b1 && git = git1 ->
178 Block(b1,git1)::prev
179 | _::rest -> loop rest in
180 loop l2)
181 [] l1)
182
183 let _ =
184 if Array.length Sys.argv < 4
185 then failwith "arguments: -/+/@/C pattern -/+/@/C pattern ... version";
186 let args = List.tl(Array.to_list Sys.argv) in
187 let version = List.hd(List.rev args) in
188 let pairs = List.rev(List.tl(List.rev args)) in
189 let requirements = split_args pairs in
190 let res =
191 List.map (function Git x -> x | Block (_,x) -> x)
192 (List.fold_left
193 (function all ->
194 function pattern ->
195 inter (process_one pattern version) all)
196 (process_one (List.hd requirements) version)
197 (List.tl requirements)) in
198 let res =
199 if !grouped
200 then
201 List.rev
202 (List.fold_left
203 (function prev ->
204 function x -> if List.mem x prev then prev else x::prev)
205 [] res)
206 else res in
207 let res =
208 match !maxlen with
209 None -> res
210 | Some max ->
211 let badgits = process_len max version in
212 List.filter (function x -> not(List.mem (Git(x)) badgits)) res in
213 List.iter (function name -> Printf.printf "%s\n" name) res