-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetfiles.ml
102 lines (88 loc) · 3.14 KB
/
getfiles.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(*
* Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
* Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Jesper Andersen
* This file is part of Coccinelle.
*
* Coccinelle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, according to version 2 of the License.
*
* Coccinelle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
*
* The authors reserve the right to distribute this or future versions of
* Coccinelle under other licenses.
*)
(* small script to read a git-commit from stdin and extract files *)
(* read lines from stdin and return list of lines
* preserves ordering of lines in the list *)
let read_commit_stdin () =
let lines = ref [] in
let rec loop () =
lines := read_line () :: !lines;
loop() in
try (loop ()) with End_of_file -> List.rev (!lines)
let indexmatch = Str.regexp "^index"
let indexsplit = Str.regexp "index\\| +\\|\\.\\."
let filesmatch = Str.regexp "---"
let filegroup = Str.regexp "/.*"
exception Notfound of string
let rec find_loc locm err ls =
match ls with
| [] -> raise (Notfound err)
| l :: ls when Str.string_match locm l 0 -> (l, ls)
| _ :: ls -> find_loc locm err ls
let find_index = find_loc indexmatch "index"
let find_files = find_loc filesmatch "file"
exception Noindices
let get_indices l =
let ls = Str.split indexsplit l in
match ls with
| _ :: i1 :: i2 :: _ ->
(*print_endline "indices: ";*)
(*print_endline (i1 ^ ", " ^ i2);*)
(i1, i2)
| _ -> raise Noindices
let get_file l =
let lastindex = String.length l - 1 in
let _ = Str.search_backward filegroup l lastindex in
let fs = Str.matched_string l in
let len = String.length fs in
let name= String.sub fs 1 (len - 1) in
(name ^ ".orig", name ^ ".new")
let next_files ls =
let (il, ls1) = find_index ls in
let (i1, i2) = get_indices il in
let (fl, ls2) = find_files ls1 in
let (oname, nname) = get_file fl in
((i1,i2,oname,nname),ls2)
let main () =
(* output file pairs to dir *)
let dir = Sys.argv.(1) in
print_endline ("[main] outputing to " ^ dir);
let ls = read_commit_stdin () in
print_endline "[ls]";
let rec loop ls =
try
let (i1, i2, oname, nname),ls = next_files ls in
let mkc = function c -> (Sys.command c; ()) in
let cmd1 =
if i1 = "0000000"
then "echo \" \""
else "git show " ^ i1 ^ " > " ^ dir ^ oname in
let cmd2 =
if i2 = "0000000"
then "echo \" \""
else "git show " ^ i2 ^ " > " ^ dir ^ nname in
print_endline ("File: " ^ i1 ^ " -> " ^ i2);
let cmd3 = "echo " ^ oname ^ " " ^ nname ^ ">> " ^ dir ^ "specfile" in
List.iter mkc [cmd1;cmd2;cmd3];
loop ls
with _ -> () in
loop ls
let _ = main();