File: extract_notes

package info (click to toggle)
wings3d 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,404 kB
  • sloc: erlang: 132,169; ansic: 3,779; lisp: 1,434; sh: 805; makefile: 794; cpp: 244; xml: 50
file content (140 lines) | stat: -rwxr-xr-x 3,543 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env escript
%% -*- erlang -*-
%%
%%  extract_notes --
%%
%%     Extract release notes from commit comments.
%%
%%  Copyright (c) 2009 Bjorn Gustavsson
%%
%%  See the file "license.terms" for information on usage and redistribution
%%  of this file, and for a DISCLAIMER OF ALL WARRANTIES.
%%
-mode(compile).

main(_) ->
    ok = io:setopts(standard_io, [{encoding, unicode}]),
    PrevRel = binary_to_list(chomp(git(["describe","--abbrev=0"]))),
    Log = git(["log","--no-color","--reverse",PrevRel++".."]),
    extract_notes(Log),
    init:stop().

author_to_nick(<<"Bjorn Gustavsson",_/binary>>) -> <<"bjorng">>;
author_to_nick(<<"Richard Jones",_/binary>>) -> <<"optigon">>;
author_to_nick(<<"Dan Gudmundsson",_/binary>>) -> <<"dgud">>;
author_to_nick(<<"Andrzej Giniewicz",_/binary>>) -> <<"giniu">>;
author_to_nick(<<"Anthony D'Agostino",_/binary>>) -> <<"scorpius">>;
author_to_nick(Author) ->
    re:replace(Author, "\s*<.*$", "", [{return,binary}]).

extract_notes(Log) ->
    Commits = break_into_commits(Log),
    {ok,Re} = re:compile("Author:\s*([^\n]*).*?\n\\s*N[Oo][Tt][Ee]:(.*?)$",
     			 [dotall]),

    Notes = filter_commits(Commits, Re),
    [format_note(Au, N) || [Au,N] <- Notes],
    ok.

break_into_commits(Bin) ->
    bic_1(Bin, <<>>).

bic_1(<<"\ncommit ",T/binary>>, Commit) ->
    [<<Commit/binary,$\n>>|bic_1(T, <<"commit ">>)];
bic_1(<<H,T/binary>>, Commit) ->
    bic_1(T, <<Commit/binary,H>>);
bic_1(<<>>, Commit) ->
    %% Ensure that each commit ends with exactly two newlines.
    [<<Commit/binary,$\n>>].

filter_commits([C|Cs], Re) ->
    Res = re:run(C, Re, [{capture,all_but_first,binary}]),
    case Res of
	nomatch ->
	    filter_commits(Cs, Re);
	{match,Note} ->
	    [Note|filter_commits(Cs, Re)]
    end;
filter_commits([], _) -> [].

format_note(Author, Note0) ->
    Note1 = maybe_add_nick(Author, Note0),
    {Note,T} = split_after_nl(skip_ws(Note1)),
    io:put_chars(["- ",Note]),
    format_note_1(skip_ws(T)),
    io:nl().

format_note_1(<<>>) -> ok;
format_note_1(Note0) ->
    {Note,T} = split_after_nl(Note0),
    io:put_chars(["  ",Note]),
    format_note_1(skip_blanks(T)).

split_after_nl(Bin) ->
    Pos = split_after_nl_1(Bin, 0),
    split_binary(Bin, Pos).

split_after_nl_1(<<$\n,_/binary>>, N) ->
    N+1;
split_after_nl_1(<<_,T/binary>>, N) ->
    split_after_nl_1(T, N+1);
split_after_nl_1(<<>>, N) ->
    N.

maybe_add_nick(Author, Note) ->
    case re:run(Note, "\\[.*?\\]\s*$") of
	{match,_} -> Note;
	nomatch ->
	    Nick0 = author_to_nick(Author),
	    Nick = <<$\s,$[,Nick0/binary,$],$\n>>,
	    <<(chomp(Note))/binary,Nick/binary>>
	end.

skip_ws(<<S,T/binary>>) when S =< $\s -> skip_ws(T);
skip_ws(T) -> T.

skip_blanks(<<$\s,T/binary>>) -> skip_blanks(T);
skip_blanks(T) -> T.

chomp(Bin) ->
    N = byte_size(Bin) - 1,
    case Bin of
	<<Prefix:N/binary,$\n>> ->
	    Prefix;
	_ ->
	    Bin
    end.

git(Args) ->
    Git = case get(git) of
	      undefined ->
		  case os:find_executable(git) of
		      false ->
			  fatal("git not found");
		      Path ->
			  put(git, Path),
			  Path
		  end;
	      Path -> Path
	  end,
    P = open_port({spawn_executable,Git},
		  [{args,Args},binary,in,eof]),
    get_data(P).

get_data(Port) ->
    get_data(Port, <<>>).

get_data(Port, Sofar) ->
    receive
	{Port,eof} ->
	    erlang:port_close(Port),
	    Sofar;
	{Port,{data,Bytes}} ->
	    get_data(Port, <<Sofar/binary,Bytes/binary>>);
	{'EXIT',Port, _} ->
	    Sofar
    end.

fatal(Str) ->
    io:format(err, "extract_notes: ~s\n", [Str]),
    halt(1).