Commit | Line | Data |
---|---|---|
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 | (* adjust as convenient *) |
24 | let prefix = "/tmp/" | |
25 | let prefix = "" | |
26 | ||
27 | (* The -grouped option means that all - and + code must appear in a | |
28 | single contiguous block of - + code. This option has no effect on the | |
29 | other kinds of patterns, ie Changelog (C) or Context (@) *) | |
30 | ||
31 | (* example: gitgrep -grouped -maxlen 25 - "[A-Z][A-Z]+" + "[A-Z][A-Z]+" | |
32 | usb_21_22 *) | |
33 | ||
34 | type dir = Minus | Plus | Context | ChangeLog | |
35 | ||
36 | type res = Git of string | Block of int * string | |
37 | ||
38 | let grouped = ref false | |
39 | let maxlen = ref None | |
40 | ||
41 | let space = Str.regexp " " | |
42 | ||
43 | let matches pattern line = | |
44 | try let _ = Str.search_forward pattern line 0 in true | |
45 | with Not_found -> false | |
46 | ||
47 | let res = ref [] | |
48 | ||
49 | let scan dir pattern i = | |
50 | let rec loop skipping cl git = | |
51 | let line = input_line i in | |
52 | match Str.split space line with | |
53 | ["commit";git] -> loop false true git | |
54 | | "diff"::_ -> loop skipping false git | |
55 | | _ -> | |
56 | if String.length line > 0 && not skipping && | |
57 | ((String.get line 0 = '-' && dir = Minus) or | |
58 | (String.get line 0 = '+' && dir = Plus) or | |
59 | (cl && dir = ChangeLog) or | |
60 | (not (String.get line 0 = '-') && not (String.get line 0 = '+') && | |
61 | dir = Context)) && | |
62 | matches pattern line | |
63 | then (res := Git(git)::!res; loop true cl git) | |
64 | else loop skipping cl git in | |
65 | loop false false "" | |
66 | ||
67 | (* for Minus and Plus directions only *) | |
68 | let scan_grouped dir pattern i = | |
69 | let block = ref 0 in | |
70 | (* mp = true in minus-plus region *) | |
71 | let rec loop mp git = | |
72 | let line = input_line i in | |
73 | match Str.split space line with | |
74 | ["commit";git] -> loop false git | |
75 | | "diff"::_ -> loop false git | |
76 | | _ -> | |
77 | if String.length line > 0 | |
78 | then | |
79 | let first_char = String.get line 0 in | |
80 | let new_mp = | |
81 | match first_char with | |
82 | '-' | '+' -> (if not mp then block := !block + 1; true) | |
83 | | _ -> false in | |
84 | match (first_char,dir) with | |
85 | ('-',Minus) | ('+',Plus) -> | |
86 | let info = Block(!block,git) in | |
87 | (if matches pattern line && not (List.mem info !res) | |
88 | then res := info::!res); | |
89 | loop new_mp git | |
90 | | _ -> loop new_mp git | |
91 | else loop mp git in | |
92 | loop false "" | |
93 | ||
94 | let scan_line max i = | |
95 | let rec loop skipping num git = | |
96 | let line = input_line i in | |
97 | match Str.split space line with | |
98 | ["commit";git1] -> | |
99 | loop false (-1) git1 | |
100 | | "diff"::_ -> | |
101 | if num > max && not skipping | |
102 | then (res:=Git(git)::!res;loop true (num+1) git) | |
103 | else loop skipping (if num = (-1) then 1 else num+1) git | |
104 | | _ -> | |
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 num else num+1) git in | |
108 | loop false (-1) "" | |
109 | ||
110 | let dot = Str.regexp "\\." | |
111 | ||
112 | let open_git file = | |
113 | let tmp = prefix^file in | |
114 | if Sys.file_exists tmp | |
115 | then open_in tmp | |
116 | else | |
117 | match List.rev (Str.split dot file) with | |
118 | last::rest -> | |
119 | let last_int = int_of_string last in | |
120 | if last_int = 0 | |
121 | then | |
122 | failwith | |
123 | "can't go back one version from 0; make the log file by hand"; | |
124 | let prev = | |
125 | String.concat "." (List.rev ((string_of_int (last_int-1))::rest)) in | |
126 | let _ = | |
127 | Sys.command | |
128 | (Printf.sprintf "git log -p v%s..v%s > %s" prev file tmp) in | |
129 | open_in tmp | |
130 | | _ -> open_in file | |
131 | ||
132 | let rec split_args = function | |
133 | [] -> [] | |
134 | | "-grouped"::rest -> grouped := true; split_args rest | |
135 | | "-maxlen"::len::rest -> maxlen := Some (int_of_string len); split_args rest | |
136 | | "-"::pattern::rest -> (Minus,Str.regexp pattern) :: split_args rest | |
137 | | "+"::pattern::rest -> (Plus,Str.regexp pattern) :: split_args rest | |
138 | | "@"::pattern::rest -> (Context,Str.regexp pattern) :: split_args rest | |
139 | | "C"::pattern::rest -> (ChangeLog,Str.regexp pattern) :: split_args rest | |
140 | | _ -> failwith "bad argument list" | |
141 | ||
142 | let process_one (dir,pattern) version = | |
143 | res := []; | |
144 | let i = open_git version in | |
145 | try | |
146 | if !grouped && (dir = Minus or dir = Plus) | |
147 | then scan_grouped dir pattern i | |
148 | else scan dir pattern i | |
149 | with End_of_file -> (close_in i; List.rev !res) | |
150 | ||
151 | let process_len max version = | |
152 | res := []; | |
153 | let i = open_git version in | |
154 | try scan_line max i | |
155 | with End_of_file -> (close_in i; List.rev !res) | |
156 | ||
157 | let inter l1 l2 = | |
158 | List.rev | |
159 | (List.fold_left | |
160 | (function prev -> | |
161 | function | |
162 | (Git(git)) as x -> | |
163 | let rec loop = function | |
164 | [] -> prev | |
165 | | Git(git1)::rest when git = git1 -> x::prev | |
166 | | Block(b1,git1)::rest when git = git1 -> Block(b1,git1)::prev | |
167 | | _::rest -> loop rest in | |
168 | loop l2 | |
169 | | (Block(block,git)) as x -> | |
170 | let rec loop = function | |
171 | [] -> prev | |
172 | | Git(git1)::rest when git = git1 -> x::prev | |
173 | | Block(b1,git1)::rest when block = b1 && git = git1 -> | |
174 | Block(b1,git1)::prev | |
175 | | _::rest -> loop rest in | |
176 | loop l2) | |
177 | [] l1) | |
178 | ||
179 | let _ = | |
180 | if Array.length Sys.argv < 4 | |
181 | then failwith "arguments: -/+/@/C pattern -/+/@/C pattern ... version"; | |
182 | let args = List.tl(Array.to_list Sys.argv) in | |
183 | let version = List.hd(List.rev args) in | |
184 | let pairs = List.rev(List.tl(List.rev args)) in | |
185 | let requirements = split_args pairs in | |
186 | let res = | |
187 | List.map (function Git x -> x | Block (_,x) -> x) | |
188 | (List.fold_left | |
189 | (function all -> | |
190 | function pattern -> | |
191 | inter (process_one pattern version) all) | |
192 | (process_one (List.hd requirements) version) | |
193 | (List.tl requirements)) in | |
194 | let res = | |
195 | if !grouped | |
196 | then | |
197 | List.rev | |
198 | (List.fold_left | |
199 | (function prev -> | |
200 | function x -> if List.mem x prev then prev else x::prev) | |
201 | [] res) | |
202 | else res in | |
203 | let res = | |
204 | match !maxlen with | |
205 | None -> res | |
206 | | Some max -> | |
207 | let badgits = process_len max version in | |
208 | List.filter (function x -> not(List.mem (Git(x)) badgits)) res in | |
209 | List.iter (function name -> Printf.printf "%s\n" name) res |