1 (* adjust as convenient *)
5 (* The -grouped option means that all - and + code must appear in a
6 single contiguous block of - + code. This option has no effect on the
7 other kinds of patterns, ie Changelog (C) or Context (@) *)
9 (* example: gitgrep -grouped -maxlen 25 - "[A-Z][A-Z]+" + "[A-Z][A-Z]+"
12 type dir
= Minus
| Plus
| Context
| ChangeLog
14 type res
= Git
of string | Block
of int * string
16 let grouped = ref false
19 let space = Str.regexp
" "
21 let matches pattern line
=
22 try let _ = Str.search_forward pattern line
0 in true
23 with Not_found
-> false
27 let scan dir pattern i
=
28 let rec loop skipping cl git
=
29 let line = input_line i
in
30 match Str.split
space line with
31 ["commit";git
] -> loop false true git
32 | "diff"::_ -> loop skipping
false git
34 if String.length
line > 0 && not skipping
&&
35 ((String.get
line 0 = '
-'
&& dir
= Minus
) or
36 (String.get
line 0 = '
+'
&& dir
= Plus
) or
37 (cl
&& dir
= ChangeLog
) or
38 (not
(String.get
line 0 = '
-'
) && not
(String.get
line 0 = '
+'
) &&
41 then (res := Git
(git
)::!res; loop true cl git
)
42 else loop skipping cl git
in
45 (* for Minus and Plus directions only *)
46 let scan_grouped dir pattern i
=
48 (* mp = true in minus-plus region *)
50 let line = input_line i
in
51 match Str.split
space line with
52 ["commit";git
] -> loop false git
53 | "diff"::_ -> loop false git
55 if String.length
line > 0
57 let first_char = String.get
line 0 in
60 '
-'
| '
+'
-> (if not mp
then block := !block + 1; true)
62 match (first_char,dir
) with
63 ('
-'
,Minus
) | ('
+'
,Plus
) ->
64 let info = Block
(!block,git
) in
65 (if matches pattern
line && not
(List.mem
info !res)
66 then res := info::!res);
68 | _ -> loop new_mp git
73 let rec loop skipping num git
=
74 let line = input_line i
in
75 match Str.split
space line with
79 if num
> max
&& not skipping
80 then (res:=Git
(git
)::!res;loop true (num
+1) git
)
81 else loop skipping
(if num
= (-1) then 1 else num
+1) git
83 if num
> max
&& not skipping
84 then (res:=Git
(git
)::!res;loop true (num
+1) git
)
85 else loop skipping
(if num
= (-1) then num
else num
+1) git
in
88 let dot = Str.regexp
"\\."
91 let tmp = prefix^file
in
92 if Sys.file_exists
tmp
95 match List.rev
(Str.split
dot file
) with
97 let last_int = int_of_string last
in
101 "can't go back one version from 0; make the log file by hand";
103 String.concat
"." (List.rev
((string_of_int
(last_int-1))::rest
)) in
106 (Printf.sprintf
"git log -p v%s..v%s > %s" prev file
tmp) in
110 let rec split_args = function
112 | "-grouped"::rest
-> grouped := true; split_args rest
113 | "-maxlen"::len
::rest
-> maxlen := Some
(int_of_string len
); split_args rest
114 | "-"::pattern
::rest
-> (Minus
,Str.regexp pattern
) :: split_args rest
115 | "+"::pattern
::rest
-> (Plus
,Str.regexp pattern
) :: split_args rest
116 | "@"::pattern
::rest
-> (Context
,Str.regexp pattern
) :: split_args rest
117 | "C"::pattern
::rest
-> (ChangeLog
,Str.regexp pattern
) :: split_args rest
118 | _ -> failwith
"bad argument list"
120 let process_one (dir
,pattern
) version
=
122 let i = open_git version
in
124 if !grouped && (dir
= Minus
or dir
= Plus
)
125 then scan_grouped dir pattern
i
126 else scan dir pattern
i
127 with End_of_file
-> (close_in
i; List.rev
!res)
129 let process_len max version
=
131 let i = open_git version
in
133 with End_of_file
-> (close_in
i; List.rev
!res)
141 let rec loop = function
143 | Git
(git1
)::rest
when git
= git1
-> x
::prev
144 | Block
(b1
,git1
)::rest
when git
= git1
-> Block
(b1
,git1
)::prev
145 | _::rest
-> loop rest
in
147 | (Block
(block,git
)) as x
->
148 let rec loop = function
150 | Git
(git1
)::rest
when git
= git1
-> x
::prev
151 | Block
(b1
,git1
)::rest
when block = b1
&& git
= git1
->
153 | _::rest
-> loop rest
in
158 if Array.length
Sys.argv
< 4
159 then failwith
"arguments: -/+/@/C pattern -/+/@/C pattern ... version";
160 let args = List.tl
(Array.to_list
Sys.argv
) in
161 let version = List.hd
(List.rev
args) in
162 let pairs = List.rev
(List.tl
(List.rev
args)) in
163 let requirements = split_args pairs in
165 List.map
(function Git x
-> x
| Block
(_,x
) -> x
)
169 inter (process_one pattern
version) all
)
170 (process_one (List.hd
requirements) version)
171 (List.tl
requirements)) in
178 function x
-> if List.mem x
prev then prev else x
::prev)
185 let badgits = process_len max
version in
186 List.filter
(function x
-> not
(List.mem
(Git
(x
)) badgits)) res in
187 List.iter
(function name
-> Printf.printf
"%s\n" name
) res