-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch_diff.escript
executable file
·147 lines (130 loc) · 4.11 KB
/
patch_diff.escript
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env escript
%% -*- erlang -*-
%% Copyright 2011-13 Feuerlabs Inc.
%% License: "BSD New" (http://www.opensource.org/licenses/BSD-3-Clause)
%% Author: Ulf Wiger <[email protected]>
-record(state, {capture = [beam],
diff_prev = " HEAD^",
diff_cur = "",
diff_tail = [],
compress = false,
output = tty}).
main(Args) ->
try begin
St = args(Args, #state{}),
run_diff(St)
end
catch
error:E ->
rpt_error(E, erlang:get_stacktrace()),
usage(),
halt(1)
end.
run_diff(#state{diff_prev = Prev, diff_cur = Cur, diff_tail = Tail,
capture = Capture, output = _Target} = St) ->
Cmd = "git diff" ++ Prev ++ Cur ++ " --name-only" ++ Tail,
io:fwrite("~s~n", [Cmd]),
DiffResult = re:split(os:cmd(Cmd), "\\v", [{return, list}]),
Classified = [{file_type(F), F} || F <- DiffResult, F=/=[]],
case [F || {unknown, F} <- Classified] of
[] ->
Grouped = group(Classified),
Keep = [{Type, [actual_file(Type, F) || F <- Fs]}
|| {Type, Fs} <- Grouped, lists:member(Type, Capture)],
output(Keep, St);
[_|_] = Unknown ->
io:fwrite("Unknown = ~p~n", [Unknown]),
io:fwrite("Can't recognize:~n", []), [io:fwrite(" ~s~n", [F])
|| F <- Unknown],
halt(1)
end.
group(List) ->
lists:foldl(fun({T,F},D) -> orddict:append(T,F,D) end, orddict:new(), List).
file_type(F) when F=="Makefile"; F=="rebar.config"; F=="rebar.config.script";
F=="patch_diff.escript"; F=="rebar"; F=="devrun";
F=="README.md"; F=="rel/sys.config" ->
make;
file_type(F) when F=="rel/vm.args"; F=="ctl"; F=="make_node" ->
setup;
file_type(F) ->
%% use longest match to find extension, rather than filename:extension/1
case re:run(F,"\\..+$",[{capture,all,list}]) of
{match, [".erl"]} ->
beam;
{match, [".app.src"]} ->
app;
_ ->
unknown
end.
actual_file(beam, F) ->
[Base, "src" | RevTail] = lists:reverse(filename:split(F)),
NewBase = filename:basename(Base,".erl") ++ ".beam",
Beam = filename:join(
lists:reverse([NewBase, "ebin" | RevTail])),
case filelib:is_regular(Beam) of
true -> Beam;
false -> error({cannot_find, Beam})
end;
actual_file(_, F) ->
F.
output(L, #state{output = tty}) ->
io:fwrite("Files = ~p~n", [L]);
output(L, #state{output = {tar, F}, compress = Compress}) ->
make_tar(L, F, Compress).
make_tar(L, Tar, Compress) ->
Patches = "rel/patches",
Setup = "rel/patches/setup_files",
FileList =
lists:flatmap(
fun({beam,Files}) ->
[{filename:join(Patches, filename:basename(F)), F}
|| F <- Files];
({setup, Files}) ->
[{filename:join(Setup, filename:basename(F)), F}
|| F <- Files]
end, L),
erl_tar:create(Tar, FileList, [compressed || Compress]).
args(["-cur", V|T], St) ->
args(T, St#state{diff_cur = " " ++ V});
args(["-prev", V|T], St) ->
args(T, St#state{diff_prev = " " ++ V});
args(["-tar", F|T], St) ->
args(T, St#state{output = {tar, F}});
args(["-z" | T], St) ->
args(T, St#state{compress = true});
args(["-capture" | T], St) ->
{Types, Rest} = capture(T),
io:fwrite("Types = ~p; Rest = ~p~nT = ~p~n", [Types,Rest,T]),
args(Rest, St#state{capture = Types});
args(["--" | T], St) ->
St#state{diff_tail = [" --"| [[" ",X] || X <- T]]};
args([H|_], _St) ->
error({?MODULE, unknown_argument, H});
args([], St) ->
St.
types() ->
[beam, app, setup, all].
capture(Args) ->
capture(Args, types(), []).
capture([H|T] = L, Ts, Acc) ->
case lists:member(A = list_to_atom(H), Ts) of
true ->
capture(T, Ts, [A|Acc]);
false ->
{lists:reverse(Acc), L}
end;
capture([], _, Acc) ->
{lists:reverse(Acc), []}.
usage() ->
Script = escript:script_name(),
Pad = lists:duplicate(length(Script) +7, $\s), % "Usage: "
io:fwrite("You're doing it wrong!~n"
"Usage: ~s" ++
" [-prev PrevVsn] [-z] [-tar TarFile]~n" ++ Pad ++
" [-- Path]~n",
[escript:script_name()]).
rpt_error({?MODULE, What, Info}, _) ->
io:fwrite("ERROR: ~p~n", [{What, Info}]);
rpt_error(E, Stack) ->
io:fwrite("ERROR: ~p~n"
"Stack: ~p~n", [E, Stack]).