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.
27 (* adjust as convenient *)
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 (@) *)
35 (* example: gitgrep -grouped -maxlen 25 - "[A-Z][A-Z]+" + "[A-Z][A-Z]+"
38 type dir
= Minus
| Plus
| Context
| ChangeLog
40 type res
= Git
of string | Block
of int * string
42 let grouped = ref false
45 let space = Str.regexp
" "
47 let matches pattern line
=
48 try let _ = Str.search_forward pattern line
0 in true
49 with Not_found
-> false
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
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 = '
+'
) &&
67 then (res := Git
(git
)::!res; loop true cl git
)
68 else loop skipping cl git
in
71 (* for Minus and Plus directions only *)
72 let scan_grouped dir pattern i
=
74 (* mp = true in minus-plus region *)
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
81 if String.length
line > 0
83 let first_char = String.get
line 0 in
86 '
-'
| '
+'
-> (if not mp
then block := !block + 1; true)
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);
94 | _ -> loop new_mp git
99 let rec loop skipping num git
=
100 let line = input_line i
in
101 match Str.split
space line with
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
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
114 let dot = Str.regexp
"\\."
117 let tmp = prefix^file
in
118 if Sys.file_exists
tmp
121 match List.rev
(Str.split
dot file
) with
123 let last_int = int_of_string last
in
127 "can't go back one version from 0; make the log file by hand";
129 String.concat
"." (List.rev
((string_of_int
(last_int-1))::rest
)) in
132 (Printf.sprintf
"git log -p v%s..v%s > %s" prev file
tmp) in
136 let rec split_args = function
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"
146 let process_one (dir
,pattern
) version
=
148 let i = open_git version
in
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)
155 let process_len max version
=
157 let i = open_git version
in
159 with End_of_file
-> (close_in
i; List.rev
!res)
167 let rec loop = function
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
173 | (Block
(block,git
)) as x
->
174 let rec loop = function
176 | Git
(git1
)::rest
when git
= git1
-> x
::prev
177 | Block
(b1
,git1
)::rest
when block = b1
&& git
= git1
->
179 | _::rest
-> loop rest
in
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
191 List.map
(function Git x
-> x
| Block
(_,x
) -> x
)
195 inter (process_one pattern
version) all
)
196 (process_one (List.hd
requirements) version)
197 (List.tl
requirements)) in
204 function x
-> if List.mem x
prev then prev else x
::prev)
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