1 (* split patch per file *)
3 (* ------------------------------------------------------------------------ *)
4 (* The following are a reminder of what this information should look like.
5 These values are not used. See the README file for information on how to
6 create a .splitpatch file in your home directory. *)
8 let from = ref "email@xyz.org"
9 let git_tree = ref "/var/linuxes/linux-next"
10 let git_options = ref "--cc=kernel-janitors@vger.kernel.org --suppress-cc=self"
11 let prefix_before = ref (Some
"/var/linuxes/linux-next")
12 let prefix_after = ref (Some
"/var/julia/linuxcopy")
14 (* ------------------------------------------------------------------------ *)
17 let process_output_to_list2 = fun command
->
18 let chan = Unix.open_process_in command
in
19 let res = ref ([] : string list
) in
20 let rec process_otl_aux () =
21 let e = input_line
chan in
24 try process_otl_aux ()
26 let stat = Unix.close_process_in
chan in (List.rev
!res,stat)
27 let cmd_to_list command
=
28 let (l
,_
) = process_output_to_list2 command
in l
29 let process_output_to_list = cmd_to_list
30 let cmd_to_list_and_status = process_output_to_list2
32 let safe_chop_extension s
= try Filename.chop_extension s
with _
-> s
34 let safe_get_extension s
=
35 match List.rev
(Str.split
(Str.regexp_string
".") s
) with
36 ext
::_
::rest
-> Some
(String.concat
"." (List.rev rest
))
39 (* ------------------------------------------------------------------------ *)
40 (* set configuration variables *)
42 let from_from_template template
=
44 cmd_to_list (Printf.sprintf
"grep Signed-off-by: %s" template
) in
45 match signed_offs with
46 x
::xs
-> String.concat
" " (Str.split
(Str.regexp
"[ \t]+") x
)
47 | _
-> failwith
"No Signed-off-by in template file"
49 let from_from_gitconfig path
=
50 let config = path^
"/.git/config" in
51 if Sys.file_exists
config
53 let i = open_in
config in
54 let rec inner_loop _
=
55 let l = input_line
i in
56 match Str.split
(Str.regexp
"[ \t]+") l with
57 "from"::"="::f
-> from := String.concat
" " f
59 if String.length
l >= 1 && String.get
l 0 = '
['
62 let rec outer_loop _
=
63 let l = input_line
i in
67 (try outer_loop() with Not_found
-> ());
70 let read_configs template
=
71 let temporary_git_tree = ref None
in
73 prefix_before := None
;
75 (* get information in message template, lowest priority *)
76 from := from_from_template template
;
77 (* get information in git config *)
78 let rec loop = function
81 if Sys.file_exists
".git"
83 begin temporary_git_tree := Some path
; from_from_gitconfig path
end
84 else loop (Filename.dirname path
) in
86 (* get information from .splitpatch *)
87 let home = List.hd
(cmd_to_list "ls -d ~") in
88 let config = home^
"/.splitpatch" in
89 (if Sys.file_exists
config
91 let i = open_in
config in
93 let l = input_line
i in
94 (* bounded split doesn't split at = in value part *)
95 (match Str.bounded_split
(Str.regexp
"[ \t]*=[ \t]*") l 2 with
96 ["from";s
] -> from := s
97 | ["git_tree";s
] -> temporary_git_tree := Some s
98 | ["git_options";s
] -> git_options := s
99 | ["prefix_before";s
] -> prefix_before := Some s
100 | ["prefix_after";s
] -> prefix_after := Some s
101 | _
-> Printf.fprintf stderr
"unknown line: %s\n" l);
103 try loop() with End_of_file
-> close_in
i);
104 match !temporary_git_tree with
105 None
-> failwith
"Unable to find Linux source tree"
106 | Some g
-> git_tree := g
108 (* ------------------------------------------------------------------------ *)
110 let maintainer_command file
=
112 "cd %s; scripts/get_maintainer.pl --separator , --nogit --norolestats -f %s"
115 let subsystem_command file
=
117 "cd %s; scripts/get_maintainer.pl --nogit --subsystem --norolestats -f %s | grep -v @"
120 let checkpatch_command file
=
121 Printf.sprintf
"cd %s; scripts/checkpatch.pl %s" !git_tree file
123 let default_string = "THE REST" (* split by file *)
125 (* ------------------------------------------------------------------------ *)
126 (* ------------------------------------------------------------------------ *)
127 (* Template file processing *)
129 let read_up_to_dashes i =
130 let lines = ref [] in
132 let l = input_line
i in
135 else begin lines := l :: !lines; loop() end in
136 (try loop() with End_of_file
-> ());
139 ""::lines -> List.rev
lines (* drop last line if blank *)
140 | lines -> List.rev
lines in
142 ""::lines -> lines (* drop first line if blank *)
145 let get_template_information file
=
146 let i = open_in file
in
148 let subject = read_up_to_dashes i in
151 let cover = read_up_to_dashes i in
152 let message = read_up_to_dashes i in
154 then (subject,None
,cover)
155 else (subject,Some
cover,message)
158 ("Subject must be exactly one line "^
159 (string_of_int
(List.length
subject)))
161 (* ------------------------------------------------------------------------ *)
162 (* ------------------------------------------------------------------------ *)
163 (* Patch processing *)
165 let spaces = Str.regexp
"[ \t]+"
167 let fix_before_after l prefix
= function
169 (match Str.split
spaces l with
170 ("diff"|"+++"|"---")::_
->
171 (match Str.split
(Str.regexp old_prefix
) l with
173 (match Str.split_delim
(Str.regexp
("[ \t]"^prefix
)) a
with
174 [_
;""] -> a^b
(* prefix is already there *)
181 match Str.split
spaces l with
182 (("+++"|"---") as a
)::path
::rest
-> Printf.sprintf
"%s %s" a path
185 (* ------------------------------------------------------------------------ *)
187 let is_diff = Str.regexp
"diff "
189 let patches = ref [] in
192 match Str.split_delim
(Str.regexp
",") l with
193 [_
;size
] -> int_of_string size
195 | _
-> failwith
("bad size: "^
l) in
196 let rec read_diff_or_atat _
=
197 let l = input_line
i in
198 let l = fix_date(fix_before_after l "a" !prefix_before) in
199 let l = fix_date(fix_before_after l "b" !prefix_after) in
200 match Str.split
spaces l with
202 (if List.length
!cur > 0
203 then patches := List.rev
!cur :: !patches);
206 | "@@"::min
::pl
::"@@"::rest
->
207 let msize = get_size min
in
208 let psize = get_size pl
in
210 read_hunk
msize psize
211 | "\\"::_
-> cur := l :: !cur; read_diff_or_atat()
214 "expected diff or @@ (diffstat information should not be present)"
216 let l = input_line
i in
217 let l = fix_date(fix_before_after l "a" !prefix_before) in
218 let l = fix_date(fix_before_after l "b" !prefix_after) in
220 match Str.split
spaces l with
221 "+++"::_
-> read_diff_or_atat()
223 and read_hunk
msize psize =
224 if msize = 0 && psize = 0
225 then read_diff_or_atat()
227 let l = input_line
i in
229 match String.get
l 0 with
230 '
-'
-> read_hunk
(msize - 1) psize
231 | '
+'
-> read_hunk
msize (psize - 1)
232 | _
-> read_hunk
(msize - 1) (psize - 1) in
233 try read_diff_or_atat()
234 with End_of_file
-> List.rev
((List.rev
!cur)::!patches)
236 (* ------------------------------------------------------------------------ *)
238 let resolve_maintainers patches =
239 let maintainer_table = Hashtbl.create
(List.length
patches) in
243 (match Str.split
(Str.regexp
" a/") diff_line
with
245 (match Str.split
spaces after
with
248 List.hd
(cmd_to_list (maintainer_command file
)) in
250 cmd_to_list (subsystem_command file
) in
251 let info = (subsystems,maintainers) in
253 try Hashtbl.find
maintainer_table info
256 Hashtbl.add
maintainer_table info cell;
258 cell := (file
,(diff_line
:: rest
)) :: !cell
259 | _
-> failwith
"filename not found")
261 failwith
(Printf.sprintf
"prefix a/ not found in %s" diff_line
))
262 | _
-> failwith
"bad diff line")
266 (* ------------------------------------------------------------------------ *)
268 let common_prefix l1 l2
=
269 let rec loop = function
270 ([],_
) | (_
,[]) -> []
271 | (x
::xs
,y
::ys
) when x
= y
-> x
:: (loop (xs
,ys
))
273 match loop (l1
,l2
) with
276 (Printf.sprintf
"found nothing in common for %s and %s"
277 (String.concat
"/" l1
) (String.concat
"/" l2
))
280 let merge_files the_rest
= function
283 let butlast l = if the_rest
then l else List.rev
(List.tl
(List.rev
l)) in
284 match List.map
(function s
-> Str.split
(Str.regexp
"/") s
) files
with
286 let rec loop res = function
287 [] -> String.concat
"/" res
288 | x
::rest
-> loop (common_prefix res x
) rest
in
289 loop (butlast first
) rest
290 | _
-> failwith
"not possible"
292 (* ------------------------------------------------------------------------ *)
295 List.iter
(function x
-> Printf.fprintf o
"%s\n" x
) l
297 let make_mail_header o date
maintainers ctr number
subject =
298 Printf.fprintf o
"From nobody %s\n" date
;
299 Printf.fprintf o
"From: %s\n" !from;
300 (match Str.split
(Str.regexp_string
",") maintainers with
301 [x
] -> Printf.fprintf o
"To: %s\n" x
303 Printf.fprintf o
"To: %s\n" x
;
304 Printf.fprintf o
"Cc: %s\n" (String.concat
"," xs
)
305 | _
-> failwith
"no maintainers");
307 then Printf.fprintf o
"Subject: [PATCH] %s\n\n" subject
308 else Printf.fprintf o
"Subject: [PATCH %d/%d] %s\n\n" ctr number
subject
310 let make_message_files subject cover message date
maintainer_table
311 patch front add_ext
=
315 (function (services
,maintainers) ->
318 if services
=[default_string]
320 (* if no maintainer, then one file per diff *)
322 (function (file
,diff
) ->
324 (!ctr,true,maintainers,[file
],[diff
]))
330 let (files
,diffs
) = List.split
(List.rev
!diffs
) in
331 (!ctr,false,maintainers,files
,diffs
)::rest
333 maintainer_table [] in
334 let number = List.length
elements in
337 (function (ctr,the_rest
,maintainers,files
,diffs
) ->
338 let output_file = add_ext
(Printf.sprintf
"%s%d" front
ctr) in
339 let o = open_out
output_file in
340 make_mail_header o date
maintainers ctr number
341 (Printf.sprintf
"%s: %s" (merge_files the_rest files
) subject);
343 Printf.fprintf
o "\n---\n";
344 let (nm
,o1
) = Filename.open_temp_file
"patch" "patch" in
345 List.iter
(print_all o1
) (List.rev diffs
);
349 (Printf.sprintf
"diffstat -p1 < %s ; /bin/rm %s" nm nm
) in
350 List.iter
(print_all o) [diffstat];
351 Printf.fprintf
o "\n";
352 List.iter
(print_all o) diffs
;
353 Printf.fprintf
o "\n";
356 cmd_to_list_and_status
357 (checkpatch_command ((Sys.getcwd
())^
"/"^
output_file)) in
358 (if not
(stat = Unix.WEXITED
0)
359 then (print_all stderr
info; Printf.fprintf stderr
"\n"));
361 (List.rev
elements) in
362 let later = add_ext
(Printf.sprintf
"%s%d" front
(number+1)) in
363 if Sys.file_exists
later
364 then Printf.fprintf stderr
"Warning: %s and other files may be left over from a previous run\n" later;
367 let make_cover_file n
subject cover front date
maintainer_table =
371 let common_maintainers =
372 let intersect l1 l2
=
375 (function i -> function cur ->
376 if List.mem
cur l2
then cur :: i else i)
378 let start = ref true in
381 (function (services
,maintainers) ->
384 let cur = Str.split
(Str.regexp_string
",") maintainers in
386 then begin start := false; cur end
387 else intersect cur rest
)
388 maintainer_table []) in
389 let output_file = Printf.sprintf
"%s.cover" front
in
390 let o = open_out
output_file in
391 make_mail_header o date
common_maintainers 0 n
subject;
393 Printf.fprintf
o "\n";
396 let mail_sender = "git send-email" (* use this when it works *)
397 let mail_sender = "cocci-send-email.perl"
399 let generate_command front
cover generated =
400 let output_file = front^
".cmd" in
401 let o = open_out
output_file in
405 "%s --auto-to --no-thread --from=\"%s\" %s $* %s\n"
406 mail_sender !from !git_options
407 (String.concat
" " generated)
410 "%s --auto-to --thread --from=\"%s\" %s $* %s\n"
411 mail_sender !from !git_options
412 (String.concat
" " ((front^
".cover") :: generated)));
415 let make_output_files subject cover message maintainer_table patch
=
416 let date = List.hd
(cmd_to_list "date") in
417 let front = safe_chop_extension patch
in
419 match safe_get_extension patch
with
420 Some ext
-> (function s
-> s ^
"." ^ ext
)
421 | None
-> (function s
-> s
) in
423 make_message_files subject cover message date maintainer_table
424 patch
front add_ext in
425 make_cover_file (List.length
generated) subject cover front date
427 generate_command front cover generated
429 (* ------------------------------------------------------------------------ *)
432 let (other_args
,files
) =
434 (function a
-> String.length a
> 1 && String.get a
0 = '
-'
)
437 [file
] -> (file
,String.concat
" " other_args
)
438 | _
-> failwith
"Only one file allowed"
441 let (file
,git_args
) = parse_args (List.tl
(Array.to_list
Sys.argv
)) in
442 let message_file = (safe_chop_extension file
)^
".msg" in
443 (* set up environment *)
444 read_configs message_file;
445 (if not
(git_args
= "") then git_options := !git_options^
" "^git_args
);
446 (* get message information *)
447 let (subject,cover,message) = get_template_information message_file in
449 let i = open_in file
in
450 let patches = split_patch i in
452 let maintainer_table = resolve_maintainers patches in
453 make_output_files subject cover message maintainer_table file