1#include <string>
2#include <sstream>
3#include <iostream>
4#include <fstream>
5#include <iomanip>
6#include <map>
7#include <list>
8
9using namespace std;
10
11// this function takes a line that may contain a name and/or email address,
12// and returns just the name, while fixing the "bad cases".
13std::string contributor_name(const std::string& line)
14{
15  string result;
16
17  // let's first take care of the case of isolated email addresses, like
18  // "user@localhost.localdomain" entries
19  if(line.find("markb@localhost.localdomain") != string::npos)
20  {
21    return "Mark Borgerding";
22  }
23
24  if(line.find("kayhman@contact.intra.cea.fr") != string::npos)
25  {
26    return "Guillaume Saupin";
27  }
28
29  // from there on we assume that we have a entry of the form
30  // either:
31  //   Bla bli Blurp
32  // or:
33  //   Bla bli Blurp <bblurp@email.com>
34
35  size_t position_of_email_address = line.find_first_of('<');
36  if(position_of_email_address != string::npos)
37  {
38    // there is an e-mail address in <...>.
39
40    // Hauke once committed as "John Smith", fix that.
41    if(line.find("hauke.heibel") != string::npos)
42      result = "Hauke Heibel";
43    else
44    {
45      // just remove the e-mail address
46      result = line.substr(0, position_of_email_address);
47    }
48  }
49  else
50  {
51    // there is no e-mail address in <...>.
52
53    if(line.find("convert-repo") != string::npos)
54      result = "";
55    else
56      result = line;
57  }
58
59  // remove trailing spaces
60  size_t length = result.length();
61  while(length >= 1 && result[length-1] == ' ') result.erase(--length);
62
63  return result;
64}
65
66// parses hg churn output to generate a contributors map.
67map<string,int> contributors_map_from_churn_output(const char *filename)
68{
69  map<string,int> contributors_map;
70
71  string line;
72  ifstream churn_out;
73  churn_out.open(filename, ios::in);
74  while(!getline(churn_out,line).eof())
75  {
76    // remove the histograms "******" that hg churn may draw at the end of some lines
77    size_t first_star = line.find_first_of('*');
78    if(first_star != string::npos) line.erase(first_star);
79
80    // remove trailing spaces
81    size_t length = line.length();
82    while(length >= 1 && line[length-1] == ' ') line.erase(--length);
83
84    // now the last space indicates where the number starts
85    size_t last_space = line.find_last_of(' ');
86
87    // get the number (of changesets or of modified lines for each contributor)
88    int number;
89    istringstream(line.substr(last_space+1)) >> number;
90
91    // get the name of the contributor
92    line.erase(last_space);
93    string name = contributor_name(line);
94
95    map<string,int>::iterator it = contributors_map.find(name);
96    // if new contributor, insert
97    if(it == contributors_map.end())
98      contributors_map.insert(pair<string,int>(name, number));
99    // if duplicate, just add the number
100    else
101      it->second += number;
102  }
103  churn_out.close();
104
105  return contributors_map;
106}
107
108// find the last name, i.e. the last word.
109// for "van den Schbling" types of last names, that's not a problem, that's actually what we want.
110string lastname(const string& name)
111{
112  size_t last_space = name.find_last_of(' ');
113  if(last_space >= name.length()-1) return name;
114  else return name.substr(last_space+1);
115}
116
117struct contributor
118{
119  string name;
120  int changedlines;
121  int changesets;
122  string url;
123  string misc;
124
125  contributor() : changedlines(0), changesets(0) {}
126
127  bool operator < (const contributor& other)
128  {
129    return lastname(name).compare(lastname(other.name)) < 0;
130  }
131};
132
133void add_online_info_into_contributors_list(list<contributor>& contributors_list, const char *filename)
134{
135  string line;
136  ifstream online_info;
137  online_info.open(filename, ios::in);
138  while(!getline(online_info,line).eof())
139  {
140    string hgname, realname, url, misc;
141
142    size_t last_bar = line.find_last_of('|');
143    if(last_bar == string::npos) continue;
144    if(last_bar < line.length())
145      misc = line.substr(last_bar+1);
146    line.erase(last_bar);
147
148    last_bar = line.find_last_of('|');
149    if(last_bar == string::npos) continue;
150    if(last_bar < line.length())
151      url = line.substr(last_bar+1);
152    line.erase(last_bar);
153
154    last_bar = line.find_last_of('|');
155    if(last_bar == string::npos) continue;
156    if(last_bar < line.length())
157      realname = line.substr(last_bar+1);
158    line.erase(last_bar);
159
160    hgname = line;
161
162    // remove the example line
163    if(hgname.find("MercurialName") != string::npos) continue;
164
165    list<contributor>::iterator it;
166    for(it=contributors_list.begin(); it != contributors_list.end() && it->name != hgname; ++it)
167    {}
168
169    if(it == contributors_list.end())
170    {
171      contributor c;
172      c.name = realname;
173      c.url = url;
174      c.misc = misc;
175      contributors_list.push_back(c);
176    }
177    else
178    {
179      it->name = realname;
180      it->url = url;
181      it->misc = misc;
182    }
183  }
184}
185
186int main()
187{
188  // parse the hg churn output files
189  map<string,int> contributors_map_for_changedlines = contributors_map_from_churn_output("churn-changedlines.out");
190  //map<string,int> contributors_map_for_changesets = contributors_map_from_churn_output("churn-changesets.out");
191
192  // merge into the contributors list
193  list<contributor> contributors_list;
194  map<string,int>::iterator it;
195  for(it=contributors_map_for_changedlines.begin(); it != contributors_map_for_changedlines.end(); ++it)
196  {
197    contributor c;
198    c.name = it->first;
199    c.changedlines = it->second;
200    c.changesets = 0; //contributors_map_for_changesets.find(it->first)->second;
201    contributors_list.push_back(c);
202  }
203
204  add_online_info_into_contributors_list(contributors_list, "online-info.out");
205
206  contributors_list.sort();
207
208  cout << "{| cellpadding=\"5\"\n";
209  cout << "!\n";
210  cout << "! Lines changed\n";
211  cout << "!\n";
212
213  list<contributor>::iterator itc;
214  int i = 0;
215  for(itc=contributors_list.begin(); itc != contributors_list.end(); ++itc)
216  {
217    if(itc->name.length() == 0) continue;
218    if(i%2) cout << "|-\n";
219    else cout << "|- style=\"background:#FFFFD0\"\n";
220    if(itc->url.length())
221      cout << "| [" << itc->url << " " << itc->name << "]\n";
222    else
223      cout << "| " << itc->name << "\n";
224    if(itc->changedlines)
225      cout << "| " << itc->changedlines << "\n";
226    else
227      cout << "| (no information)\n";
228    cout << "| " << itc->misc << "\n";
229    i++;
230  }
231  cout << "|}" << endl;
232}
233