-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
contributors.d
executable file
·159 lines (139 loc) · 4.78 KB
/
contributors.d
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
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env rdmd
/**
Query contributors between two D releases.
Copyright: D Language Foundation 2017.
License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
Example usage:
---
./contributors.d "v2.074.0..v2.075.0"
---
Author: Sebastian Wilzbach
*/
import std.array;
import std.algorithm;
import std.conv;
import std.exception;
import std.file;
import std.format;
import std.process;
import std.path;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
/// Name <my@email.com>
struct GitAuthor
{
string name, email;
string toString()
{
return "%s <%s>".format(name, email);
}
}
/// Options for finding authors
struct FindConfig
{
bool refreshTags; /// will query github.com for new tags
bool noMerges; // will ignore merge commits
bool showAllContributors; // will ignore the revRange and show all contributors
string cwd; // working directory (should be tools)
string mailmapFile; // location to the .mailmap file
}
/**
Search all git commit messages within revRange of all D repositories
Returns: Array that maps each git `Author: ...` line to a GitAuthor
*/
auto findAuthors(string revRange, FindConfig config)
{
Appender!(GitAuthor[]) authors;
int commits;
auto repos = ["dmd", "phobos", "dlang.org", "tools", "installer"];
if (config.showAllContributors)
repos ~= ["dub", "dub-registry", "dconf.org"];
foreach (repo; repos.map!(r => buildPath(config.cwd, "..", r)))
{
if (!repo.exists)
{
stderr.writefln("Warning: %s doesn't exist. " ~
"Consider running: git clone https://github.com/dlang/%s ../%2$s",
repo, repo.baseName);
continue;
}
if (config.refreshTags)
{
auto cmd = ["git", "-C", repo, "fetch", "--tags", "https://github.com/dlang/" ~ repo.baseName,
"+refs/heads/*:refs/remotes/upstream/*"];
auto p = pipeProcess(cmd, Redirect.stdout);
enforce(wait(p.pid) == 0, "Failed to execute '%(%s %)'.".format(cmd));
}
auto cmd = ["git", "-c", "mailmap.file=%s".format(config.mailmapFile), "-C", repo, "log", "--use-mailmap", "--pretty=format:%aN|%aE"];
if (!config.showAllContributors)
cmd ~= revRange;
if (config.noMerges)
cmd ~= "--no-merges";
auto p = pipeProcess(cmd, Redirect.stdout);
scope(exit) enforce(wait(p.pid) == 0, "Failed to execute '%(%s %)'.".format(cmd));
authors ~= p.stdout
.byLineCopy
.tee!(_ => commits++)
.map!((line){
auto ps = line.splitter("|");
return GitAuthor(ps.front, ps.dropOne.front);
})
.filter!(a => a.name != "The Dlang Bot");
}
if (!config.showAllContributors)
stderr.writefln("Looked at %d commits in %s", commits, revRange);
else
stderr.writefln("Looked at %d commits", commits);
return authors.data;
}
/// Sorts the authors and filters for duplicates
auto reduceAuthors(GitAuthors)(GitAuthors authors)
{
import std.uni : sicmp;
return authors
.sort!((a, b) => sicmp(a.name, b.name) < 0)
.uniq!((a, b) => a.name == b.name);
}
version(Contributors_Lib) {} else
int main(string[] args)
{
import std.getopt;
string revRange;
FindConfig config = {
cwd: __FILE_FULL_PATH__.dirName.asNormalizedPath.to!string,
};
config.mailmapFile = config.cwd.buildPath(".mailmap");
enum PrintMode { name, markdown, ddoc, csv, git}
PrintMode printMode;
auto helpInformation = getopt(
args,
std.getopt.config.passThrough,
"f|format", "Result format (name, markdown, ddoc, csv, git)", &printMode,
"a|all", "Show all contributors", &config.showAllContributors,
"refresh-tags", "Refresh tags", &config.refreshTags,
"no-merges", "Ignore merge commits", &config.noMerges,
);
if (helpInformation.helpWanted || (args.length < 2 && !config.showAllContributors))
{
`D contributors extractor.
./contributors.d "v2.075.0..v2.076.0"`.defaultGetoptPrinter(helpInformation.options);
return 1;
}
revRange = args.length > 1 ? args[1] : null;
revRange.findAuthors(config)
.reduceAuthors
.each!((a){
with(PrintMode)
final switch (printMode)
{
case name: a.name.writeln; break;
case markdown: writefln("- %s", a.name); break;
case ddoc: writefln("$(D_CONTRIBUTOR %s)", a.name); break;
case csv: writefln("%s, %s", a.name, a.email); break;
case git: writefln("%s <%s>", a.name, a.email); break;
}
});
return 0;
}