@matheusbsbs / matheus-bagliano / commits / b8ec59234b
Fork 0

Build in shortlog

[jc: with minimum squelching of compiler warning under "-pedantic" compilation options.] Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Oct 22, 2006 at 13:23 UTC b8ec59234ba2c1833e29eece9ed87f7a471cbae2
6 files changed +307 -235
Documentation/git-shortlog.txt
+1
@@ -8,6 +8,7 @@ git-shortlog - Summarize 'git log' output
8 SYNOPSIS
9 --------
10 git-log --pretty=short | 'git-shortlog' [-h] [-n] [-s]
11 +git-shortlog [-n|--number] [-s|--summary] [<committish>...]
12
13 DESCRIPTION
14 -----------
Makefile
+2 -1
@@ -174,7 +174,7 @@ SCRIPT_SH = \
174
175 SCRIPT_PERL = \
176 git-archimport.perl git-cvsimport.perl git-relink.perl \
177 - git-shortlog.perl git-rerere.perl \
177 + git-rerere.perl \
178 git-cvsserver.perl \
179 git-svnimport.perl git-cvsexportcommit.perl \
180 git-send-email.perl git-svn.perl
@@ -300,6 +300,7 @@ BUILTIN_OBJS = \
300 builtin-rev-parse.o \
301 builtin-rm.o \
302 builtin-runstatus.o \
303 + builtin-shortlog.o \
304 builtin-show-branch.o \
305 builtin-stripspace.o \
306 builtin-symbolic-ref.o \
builtin-shortlog.c new
+302
@@ -0,0 +1,302 @@
1 +#include "builtin.h"
2 +#include "cache.h"
3 +#include "commit.h"
4 +#include "diff.h"
5 +#include "path-list.h"
6 +#include "revision.h"
7 +#include <string.h>
8 +
9 +static const char shortlog_usage[] =
10 +"git-shortlog [-n] [-s] [<commit-id>... ]\n";
11 +
12 +static int compare_by_number(const void *a1, const void *a2)
13 +{
14 + const struct path_list_item *i1 = a1, *i2 = a2;
15 + const struct path_list *l1 = i1->util, *l2 = i2->util;
16 +
17 + if (l1->nr < l2->nr)
18 + return -1;
19 + else if (l1->nr == l2->nr)
20 + return 0;
21 + else
22 + return +1;
23 +}
24 +
25 +static struct path_list_item mailmap_list[] = {
26 + { "R.Marek@sh.cvut.cz", (void*)"Rudolf Marek" },
27 + { "Ralf.Wildenhues@gmx.de", (void*)"Ralf Wildenhues" },
28 + { "aherrman@de.ibm.com", (void*)"Andreas Herrmann" },
29 + { "akpm@osdl.org", (void*)"Andrew Morton" },
30 + { "andrew.vasquez@qlogic.com", (void*)"Andrew Vasquez" },
31 + { "aquynh@gmail.com", (void*)"Nguyen Anh Quynh" },
32 + { "axboe@suse.de", (void*)"Jens Axboe" },
33 + { "blaisorblade@yahoo.it", (void*)"Paolo 'Blaisorblade' Giarrusso" },
34 + { "bunk@stusta.de", (void*)"Adrian Bunk" },
35 + { "domen@coderock.org", (void*)"Domen Puncer" },
36 + { "dougg@torque.net", (void*)"Douglas Gilbert" },
37 + { "dwmw2@shinybook.infradead.org", (void*)"David Woodhouse" },
38 + { "ecashin@coraid.com", (void*)"Ed L Cashin" },
39 + { "felix@derklecks.de", (void*)"Felix Moeller" },
40 + { "fzago@systemfabricworks.com", (void*)"Frank Zago" },
41 + { "gregkh@suse.de", (void*)"Greg Kroah-Hartman" },
42 + { "hch@lst.de", (void*)"Christoph Hellwig" },
43 + { "htejun@gmail.com", (void*)"Tejun Heo" },
44 + { "jejb@mulgrave.(none)", (void*)"James Bottomley" },
45 + { "jejb@titanic.il.steeleye.com", (void*)"James Bottomley" },
46 + { "jgarzik@pretzel.yyz.us", (void*)"Jeff Garzik" },
47 + { "johnpol@2ka.mipt.ru", (void*)"Evgeniy Polyakov" },
48 + { "kay.sievers@vrfy.org", (void*)"Kay Sievers" },
49 + { "minyard@acm.org", (void*)"Corey Minyard" },
50 + { "mshah@teja.com", (void*)"Mitesh shah" },
51 + { "pj@ludd.ltu.se", (void*)"Peter A Jonsson" },
52 + { "rmps@joel.ist.utl.pt", (void*)"Rui Saraiva" },
53 + { "santtu.hyrkko@gmail.com", (void*)"Santtu Hyrkk,Av(B" },
54 + { "simon@thekelleys.org.uk", (void*)"Simon Kelley" },
55 + { "ssant@in.ibm.com", (void*)"Sachin P Sant" },
56 + { "terra@gnome.org", (void*)"Morten Welinder" },
57 + { "tony.luck@intel.com", (void*)"Tony Luck" },
58 + { "welinder@anemone.rentec.com", (void*)"Morten Welinder" },
59 + { "welinder@darter.rentec.com", (void*)"Morten Welinder" },
60 + { "welinder@troll.com", (void*)"Morten Welinder" }
61 +};
62 +
63 +static struct path_list mailmap = {
64 + mailmap_list,
65 + sizeof(mailmap_list) / sizeof(struct path_list_item), 0, 0
66 +};
67 +
68 +static int map_email(char *email, char *name, int maxlen)
69 +{
70 + char *p;
71 + struct path_list_item *item;
72 +
73 + /* autocomplete common developers */
74 + p = strchr(email, '>');
75 + if (!p)
76 + return 0;
77 +
78 + *p = '\0';
79 + item = path_list_lookup(email, &mailmap);
80 + if (item != NULL) {
81 + const char *realname = (const char *)item->util;
82 + strncpy(name, realname, maxlen);
83 + return 1;
84 + }
85 + return 0;
86 +}
87 +
88 +static void insert_author_oneline(struct path_list *list,
89 + const char *author, int authorlen,
90 + const char *oneline, int onelinelen)
91 +{
92 + const char *dot3 = "/pub/scm/linux/kernel/git/";
93 + char *buffer, *p;
94 + struct path_list_item *item;
95 + struct path_list *onelines;
96 +
97 + while (authorlen > 0 && isspace(author[authorlen - 1]))
98 + authorlen--;
99 +
100 + buffer = xmalloc(authorlen + 1);
101 + memcpy(buffer, author, authorlen);
102 + buffer[authorlen] = '\0';
103 +
104 + item = path_list_insert(buffer, list);
105 + if (item->util == NULL)
106 + item->util = xcalloc(1, sizeof(struct path_list));
107 + else
108 + free(buffer);
109 +
110 + if (!strncmp(oneline, "[PATCH", 6)) {
111 + char *eob = strchr(buffer, ']');
112 +
113 + while (isspace(eob[1]) && eob[1] != '\n')
114 + eob++;
115 + if (eob - oneline < onelinelen) {
116 + onelinelen -= eob - oneline;
117 + oneline = eob;
118 + }
119 + }
120 +
121 + while (onelinelen > 0 && isspace(oneline[0])) {
122 + oneline++;
123 + onelinelen--;
124 + }
125 +
126 + while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
127 + onelinelen--;
128 +
129 + buffer = xmalloc(onelinelen + 1);
130 + memcpy(buffer, oneline, onelinelen);
131 + buffer[onelinelen] = '\0';
132 +
133 + while ((p = strstr(buffer, dot3)) != NULL) {
134 + memcpy(p, "...", 3);
135 + strcpy(p + 2, p + sizeof(dot3) - 1);
136 + }
137 +
138 +
139 + onelines = item->util;
140 + if (onelines->nr >= onelines->alloc) {
141 + onelines->alloc = alloc_nr(onelines->nr);
142 + onelines->items = xrealloc(onelines->items,
143 + onelines->alloc
144 + * sizeof(struct path_list_item));
145 + }
146 +
147 + onelines->items[onelines->nr].util = NULL;
148 + onelines->items[onelines->nr++].path = buffer;
149 +}
150 +
151 +static void read_from_stdin(struct path_list *list)
152 +{
153 + char buffer[1024];
154 +
155 + while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
156 + char *bob;
157 + if ((buffer[0] == 'A' || buffer[0] == 'a') &&
158 + !strncmp(buffer + 1, "uthor: ", 7) &&
159 + (bob = strchr(buffer + 7, '<')) != NULL) {
160 + char buffer2[1024], offset = 0;
161 +
162 + if (map_email(bob + 1, buffer, sizeof(buffer)))
163 + bob = buffer + strlen(buffer);
164 + else {
165 + offset = 8;
166 + while (isspace(bob[-1]))
167 + bob--;
168 + }
169 +
170 + while (fgets(buffer2, sizeof(buffer2), stdin) &&
171 + buffer2[0] != '\n')
172 + ; /* chomp input */
173 + if (fgets(buffer2, sizeof(buffer2), stdin))
174 + insert_author_oneline(list,
175 + buffer + offset,
176 + bob - buffer - offset,
177 + buffer2, strlen(buffer2));
178 + }
179 + }
180 +}
181 +
182 +static void get_from_rev(struct rev_info *rev, struct path_list *list)
183 +{
184 + char scratch[1024];
185 + struct commit *commit;
186 +
187 + prepare_revision_walk(rev);
188 + while ((commit = get_revision(rev)) != NULL) {
189 + char *author = NULL, *oneline, *buffer;
190 + int authorlen = authorlen, onelinelen;
191 +
192 + /* get author and oneline */
193 + for (buffer = commit->buffer; buffer && *buffer != '\0' &&
194 + *buffer != '\n'; ) {
195 + char *eol = strchr(buffer, '\n');
196 +
197 + if (eol == NULL)
198 + eol = buffer + strlen(buffer);
199 + else
200 + eol++;
201 +
202 + if (!strncmp(buffer, "author ", 7)) {
203 + char *bracket = strchr(buffer, '<');
204 +
205 + if (bracket == NULL || bracket > eol)
206 + die("Invalid commit buffer: %s",
207 + sha1_to_hex(commit->object.sha1));
208 +
209 + if (map_email(bracket + 1, scratch,
210 + sizeof(scratch))) {
211 + author = scratch;
212 + authorlen = strlen(scratch);
213 + } else {
214 + while (bracket[-1] == ' ')
215 + bracket--;
216 +
217 + author = buffer + 7;
218 + authorlen = bracket - buffer - 7;
219 + }
220 + }
221 + buffer = eol;
222 + }
223 +
224 + if (author == NULL)
225 + die ("Missing author: %s",
226 + sha1_to_hex(commit->object.sha1));
227 +
228 + if (buffer == NULL || *buffer == '\0') {
229 + oneline = "<none>";
230 + onelinelen = sizeof(oneline) + 1;
231 + } else {
232 + char *eol;
233 +
234 + oneline = buffer + 1;
235 + eol = strchr(oneline, '\n');
236 + if (eol == NULL)
237 + onelinelen = strlen(oneline);
238 + else
239 + onelinelen = eol - oneline;
240 + }
241 +
242 + insert_author_oneline(list,
243 + author, authorlen, oneline, onelinelen);
244 + }
245 +
246 +}
247 +
248 +int cmd_shortlog(int argc, const char **argv, const char *prefix)
249 +{
250 + struct rev_info rev;
251 + struct path_list list = { NULL, 0, 0, 1 };
252 + int i, j, sort_by_number = 0, summary = 0;
253 +
254 + init_revisions(&rev, prefix);
255 + argc = setup_revisions(argc, argv, &rev, NULL);
256 + while (argc > 1) {
257 + if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
258 + sort_by_number = 1;
259 + else if (!strcmp(argv[1], "-s") ||
260 + !strcmp(argv[1], "--summary"))
261 + summary = 1;
262 + else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
263 + usage(shortlog_usage);
264 + else
265 + die ("unrecognized argument: %s", argv[1]);
266 + argv++;
267 + argc--;
268 + }
269 +
270 + if (rev.pending.nr == 1)
271 + die ("Need a range!");
272 + else if (rev.pending.nr == 0)
273 + read_from_stdin(&list);
274 + else
275 + get_from_rev(&rev, &list);
276 +
277 + if (sort_by_number)
278 + qsort(list.items, sizeof(struct path_list_item), list.nr,
279 + compare_by_number);
280 +
281 + for (i = 0; i < list.nr; i++) {
282 + struct path_list *onelines = list.items[i].util;
283 +
284 + printf("%s (%d):\n", list.items[i].path, onelines->nr);
285 + if (!summary) {
286 + for (j = onelines->nr - 1; j >= 0; j--)
287 + printf(" %s\n", onelines->items[j].path);
288 + printf("\n");
289 + }
290 +
291 + onelines->strdup_paths = 1;
292 + path_list_clear(onelines, 1);
293 + free(onelines);
294 + list.items[i].util = NULL;
295 + }
296 +
297 + list.strdup_paths = 1;
298 + path_list_clear(&list, 1);
299 +
300 + return 0;
301 +}
302 +
builtin.h
+1
@@ -55,6 +55,7 @@ extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
55 extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
56 extern int cmd_rm(int argc, const char **argv, const char *prefix);
57 extern int cmd_runstatus(int argc, const char **argv, const char *prefix);
58 +extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
59 extern int cmd_show(int argc, const char **argv, const char *prefix);
60 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
61 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
git-shortlog.perl deleted
-234
@@ -1,234 +0,0 @@
1 -#!/usr/bin/perl -w
2 -
3 -use strict;
4 -use Getopt::Std;
5 -use File::Basename qw(basename dirname);
6 -
7 -our ($opt_h, $opt_n, $opt_s);
8 -getopts('hns');
9 -
10 -$opt_h && usage();
11 -
12 -sub usage {
13 - print STDERR "Usage: ${\basename $0} [-h] [-n] [-s] < <log_data>\n";
14 - exit(1);
15 -}
16 -
17 -my (%mailmap);
18 -my (%email);
19 -my (%map);
20 -my $pstate = 1;
21 -my $n_records = 0;
22 -my $n_output = 0;
23 -
24 -sub shortlog_entry($$) {
25 - my ($name, $desc) = @_;
26 - my $key = $name;
27 -
28 - $desc =~ s#/pub/scm/linux/kernel/git/#/.../#g;
29 - $desc =~ s#\[PATCH\] ##g;
30 -
31 - # store description in array, in email->{desc list} map
32 - if (exists $map{$key}) {
33 - # grab ref
34 - my $obj = $map{$key};
35 -
36 - # add desc to array
37 - push(@$obj, $desc);
38 - } else {
39 - # create new array, containing 1 item
40 - my @arr = ($desc);
41 -
42 - # store ref to array
43 - $map{$key} = \@arr;
44 - }
45 -}
46 -
47 -# sort comparison function
48 -sub by_name($$) {
49 - my ($a, $b) = @_;
50 -
51 - uc($a) cmp uc($b);
52 -}
53 -sub by_nbentries($$) {
54 - my ($a, $b) = @_;
55 - my $a_entries = $map{$a};
56 - my $b_entries = $map{$b};
57 -
58 - @$b_entries - @$a_entries || by_name $a, $b;
59 -}
60 -
61 -my $sort_method = $opt_n ? \&by_nbentries : \&by_name;
62 -
63 -sub summary_output {
64 - my ($obj, $num, $key);
65 -
66 - foreach $key (sort $sort_method keys %map) {
67 - $obj = $map{$key};
68 - $num = @$obj;
69 - printf "%s: %u\n", $key, $num;
70 - $n_output += $num;
71 - }
72 -}
73 -
74 -sub shortlog_output {
75 - my ($obj, $num, $key, $desc);
76 -
77 - foreach $key (sort $sort_method keys %map) {
78 - $obj = $map{$key};
79 - $num = @$obj;
80 -
81 - # output author
82 - printf "%s (%u):\n", $key, $num;
83 -
84 - # output author's 1-line summaries
85 - foreach $desc (reverse @$obj) {
86 - print " $desc\n";
87 - $n_output++;
88 - }
89 -
90 - # blank line separating author from next author
91 - print "\n";
92 - }
93 -}
94 -
95 -sub changelog_input {
96 - my ($author, $desc);
97 -
98 - while (<>) {
99 - # get author and email
100 - if ($pstate == 1) {
101 - my ($email);
102 -
103 - next unless /^[Aa]uthor:?\s*(.*?)\s*<(.*)>/;
104 -
105 - $n_records++;
106 -
107 - $author = $1;
108 - $email = $2;
109 - $desc = undef;
110 -
111 - # cset author fixups
112 - if (exists $mailmap{$email}) {
113 - $author = $mailmap{$email};
114 - } elsif (exists $mailmap{$author}) {
115 - $author = $mailmap{$author};
116 - } elsif (!$author) {
117 - $author = $email;
118 - }
119 - $email{$author}{$email}++;
120 - $pstate++;
121 - }
122 -
123 - # skip to blank line
124 - elsif ($pstate == 2) {
125 - next unless /^\s*$/;
126 - $pstate++;
127 - }
128 -
129 - # skip to non-blank line
130 - elsif ($pstate == 3) {
131 - next unless /^\s*?(.*)/;
132 -
133 - # skip lines that are obviously not
134 - # a 1-line cset description
135 - next if /^\s*From: /;
136 -
137 - chomp;
138 - $desc = $1;
139 -
140 - &shortlog_entry($author, $desc);
141 -
142 - $pstate = 1;
143 - }
144 -
145 - else {
146 - die "invalid parse state $pstate";
147 - }
148 - }
149 -}
150 -
151 -sub read_mailmap {
152 - my ($fh, $mailmap) = @_;
153 - while (<$fh>) {
154 - chomp;
155 - if (/^([^#].*?)\s*<(.*)>/) {
156 - $mailmap->{$2} = $1;
157 - }
158 - }
159 -}
160 -
161 -sub setup_mailmap {
162 - read_mailmap(\*DATA, \%mailmap);
163 - if (-f '.mailmap') {
164 - my $fh = undef;
165 - open $fh, '<', '.mailmap';
166 - read_mailmap($fh, \%mailmap);
167 - close $fh;
168 - }
169 -}
170 -
171 -sub finalize {
172 - #print "\n$n_records records parsed.\n";
173 -
174 - if ($n_records != $n_output) {
175 - die "parse error: input records != output records\n";
176 - }
177 - if (0) {
178 - for my $author (sort keys %email) {
179 - my $e = $email{$author};
180 - for my $email (sort keys %$e) {
181 - print STDERR "$author <$email>\n";
182 - }
183 - }
184 - }
185 -}
186 -
187 -&setup_mailmap;
188 -&changelog_input;
189 -$opt_s ? &summary_output : &shortlog_output;
190 -&finalize;
191 -exit(0);
192 -
193 -
194 -__DATA__
195 -#
196 -# Even with git, we don't always have name translations.
197 -# So have an email->real name table to translate the
198 -# (hopefully few) missing names
199 -#
200 -Adrian Bunk <bunk@stusta.de>
201 -Andreas Herrmann <aherrman@de.ibm.com>
202 -Andrew Morton <akpm@osdl.org>
203 -Andrew Vasquez <andrew.vasquez@qlogic.com>
204 -Christoph Hellwig <hch@lst.de>
205 -Corey Minyard <minyard@acm.org>
206 -David Woodhouse <dwmw2@shinybook.infradead.org>
207 -Domen Puncer <domen@coderock.org>
208 -Douglas Gilbert <dougg@torque.net>
209 -Ed L Cashin <ecashin@coraid.com>
210 -Evgeniy Polyakov <johnpol@2ka.mipt.ru>
211 -Felix Moeller <felix@derklecks.de>
212 -Frank Zago <fzago@systemfabricworks.com>
213 -Greg Kroah-Hartman <gregkh@suse.de>
214 -James Bottomley <jejb@mulgrave.(none)>
215 -James Bottomley <jejb@titanic.il.steeleye.com>
216 -Jeff Garzik <jgarzik@pretzel.yyz.us>
217 -Jens Axboe <axboe@suse.de>
218 -Kay Sievers <kay.sievers@vrfy.org>
219 -Mitesh shah <mshah@teja.com>
220 -Morten Welinder <terra@gnome.org>
221 -Morten Welinder <welinder@anemone.rentec.com>
222 -Morten Welinder <welinder@darter.rentec.com>
223 -Morten Welinder <welinder@troll.com>
224 -Nguyen Anh Quynh <aquynh@gmail.com>
225 -Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
226 -Peter A Jonsson <pj@ludd.ltu.se>
227 -Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
228 -Rudolf Marek <R.Marek@sh.cvut.cz>
229 -Rui Saraiva <rmps@joel.ist.utl.pt>
230 -Sachin P Sant <ssant@in.ibm.com>
231 -Santtu Hyrkk,Av(B <santtu.hyrkko@gmail.com>
232 -Simon Kelley <simon@thekelleys.org.uk>
233 -Tejun Heo <htejun@gmail.com>
234 -Tony Luck <tony.luck@intel.com>
git.c
+1
@@ -260,6 +260,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
260 { "rev-parse", cmd_rev_parse, RUN_SETUP },
261 { "rm", cmd_rm, RUN_SETUP },
262 { "runstatus", cmd_runstatus, RUN_SETUP },
263 + { "shortlog", cmd_shortlog, RUN_SETUP },
264 { "show-branch", cmd_show_branch, RUN_SETUP },
265 { "show", cmd_show, RUN_SETUP | USE_PAGER },
266 { "stripspace", cmd_stripspace },