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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
28 (* adjust as convenient *)
32 (* The -grouped option means that all - and + code must appear in a
33 single contiguous block of - + code. This option has no effect on the
34 other kinds of patterns, ie Changelog (C) or Context (@) *)
36 (* example: gitgrep -grouped -maxlen 25 - "[A-Z][A-Z]+" + "[A-Z][A-Z]+"
39 type dir
= Minus
| Plus
| Context
| ChangeLog
41 type res
= Git
of string | Block
of int * string
43 let grouped = ref false
46 let space = Str.regexp
" "
48 let matches pattern line
=
49 try let _ = Str.search_forward pattern line
0 in true
50 with Not_found
-> false
54 let scan dir pattern i
=
55 let rec loop skipping cl git
=
56 let line = input_line i
in
57 match Str.split
space line with
58 ["commit";git
] -> loop false true git
59 | "diff"::_ -> loop skipping
false git
61 if String.length
line > 0 && not skipping
&&
62 ((String.get
line 0 = '
-'
&& dir
= Minus
) or
63 (String.get
line 0 = '
+'
&& dir
= Plus
) or
64 (cl
&& dir
= ChangeLog
) or
65 (not
(String.get
line 0 = '
-'
) && not
(String.get
line 0 = '
+'
) &&
68 then (res := Git
(git
)::!res; loop true cl git
)
69 else loop skipping cl git
in
72 (* for Minus and Plus directions only *)
73 let scan_grouped dir pattern i
=
75 (* mp = true in minus-plus region *)
77 let line = input_line i
in
78 match Str.split
space line with
79 ["commit";git
] -> loop false git
80 | "diff"::_ -> loop false git
82 if String.length
line > 0
84 let first_char = String.get
line 0 in
87 '
-'
| '
+'
-> (if not mp
then block := !block + 1; true)
89 match (first_char,dir
) with
90 ('
-'
,Minus
) | ('
+'
,Plus
) ->
91 let info = Block
(!block,git
) in
92 (if matches pattern
line && not
(List.mem
info !res)
93 then res := info::!res);
95 | _ -> loop new_mp git
100 let rec loop skipping num git
=
101 let line = input_line i
in
102 match Str.split
space line with
106 if num
> max
&& not skipping
107 then (res:=Git
(git
)::!res;loop true (num
+1) git
)
108 else loop skipping
(if num
= (-1) then 1 else num
+1) git
110 if num
> max
&& not skipping
111 then (res:=Git
(git
)::!res;loop true (num
+1) git
)
112 else loop skipping
(if num
= (-1) then num
else num
+1) git
in
115 let dot = Str.regexp
"\\."
118 let tmp = prefix^file
in
119 if Sys.file_exists
tmp
122 match List.rev
(Str.split
dot file
) with
124 let last_int = int_of_string last
in
128 "can't go back one version from 0; make the log file by hand";
130 String.concat
"." (List.rev
((string_of_int
(last_int-1))::rest
)) in
133 (Printf.sprintf
"git log -p v%s..v%s > %s" prev file
tmp) in
137 let rec split_args = function
139 | "-grouped"::rest
-> grouped := true; split_args rest
140 | "-maxlen"::len
::rest
-> maxlen := Some
(int_of_string len
); split_args rest
141 | "-"::pattern
::rest
-> (Minus
,Str.regexp pattern
) :: split_args rest
142 | "+"::pattern
::rest
-> (Plus
,Str.regexp pattern
) :: split_args rest
143 | "@"::pattern
::rest
-> (Context
,Str.regexp pattern
) :: split_args rest
144 | "C"::pattern
::rest
-> (ChangeLog
,Str.regexp pattern
) :: split_args rest
145 | _ -> failwith
"bad argument list"
147 let process_one (dir
,pattern
) version
=
149 let i = open_git version
in
151 if !grouped && (dir
= Minus
or dir
= Plus
)
152 then scan_grouped dir pattern
i
153 else scan dir pattern
i
154 with End_of_file
-> (close_in
i; List.rev
!res)
156 let process_len max version
=
158 let i = open_git version
in
160 with End_of_file
-> (close_in
i; List.rev
!res)
168 let rec loop = function
170 | Git
(git1
)::rest
when git
= git1
-> x
::prev
171 | Block
(b1
,git1
)::rest
when git
= git1
-> Block
(b1
,git1
)::prev
172 | _::rest
-> loop rest
in
174 | (Block
(block,git
)) as x
->
175 let rec loop = function
177 | Git
(git1
)::rest
when git
= git1
-> x
::prev
178 | Block
(b1
,git1
)::rest
when block = b1
&& git
= git1
->
180 | _::rest
-> loop rest
in
185 if Array.length
Sys.argv
< 4
186 then failwith
"arguments: -/+/@/C pattern -/+/@/C pattern ... version";
187 let args = List.tl
(Array.to_list
Sys.argv
) in
188 let version = List.hd
(List.rev
args) in
189 let pairs = List.rev
(List.tl
(List.rev
args)) in
190 let requirements = split_args pairs in
192 List.map
(function Git x
-> x
| Block
(_,x
) -> x
)
196 inter (process_one pattern
version) all
)
197 (process_one (List.hd
requirements) version)
198 (List.tl
requirements)) in
205 function x
-> if List.mem x
prev then prev else x
::prev)
212 let badgits = process_len max
version in
213 List.filter
(function x
-> not
(List.mem
(Git
(x
)) badgits)) res in
214 List.iter
(function name
-> Printf.printf
"%s\n" name
) res