@matheusbsbs / matheus-bagliano / commits / 1e931cb4d1
Fork 0

shortlog: code restructuring and clean-up

The code tried to parse and clean-up the author name and the one line information in three places (two callers of insert_author_oneline() and the called function itself), which was a mess. This renames the callee to insert_one_record() and make it responsible for cleaning up the author name and one line information. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 7, 2007 at 17:07 UTC 1e931cb4d197e30504b6d2cc77b0b045b0167df6
1 file changed +62 -103
builtin-shortlog.c
+62 -103
@@ -27,45 +27,60 @@ static int compare_by_number(const void *a1, const void *a2)
27
28 static struct path_list mailmap = {NULL, 0, 0, 0};
29
30 -static void insert_author_oneline(struct path_list *list,
31 - const char *author, int authorlen,
32 - const char *oneline, int onelinelen)
30 +static void insert_one_record(struct path_list *list,
31 + const char *author,
32 + const char *oneline)
33 {
34 const char *dot3 = common_repo_prefix;
35 char *buffer, *p;
36 struct path_list_item *item;
37 struct path_list *onelines;
38 + char namebuf[1024];
39 + size_t len;
40 + const char *eol;
41 + const char *boemail, *eoemail;
42 +
43 + boemail = strchr(author, '<');
44 + if (!boemail)
45 + return;
46 + eoemail = strchr(boemail, '>');
47 + if (!eoemail)
48 + return;
49 + if (!map_email(&mailmap, boemail+1, namebuf, sizeof(namebuf))) {
50 + while (author < boemail && isspace(*author))
51 + author++;
52 + for (len = 0;
53 + len < sizeof(namebuf) - 1 && author + len < boemail;
54 + len++)
55 + namebuf[len] = author[len];
56 + while (0 < len && isspace(namebuf[len-1]))
57 + len--;
58 + namebuf[len] = '\0';
59 + }
60
39 - while (authorlen > 0 && isspace(author[authorlen - 1]))
40 - authorlen--;
41 -
42 - buffer = xmemdupz(author, authorlen);
61 + buffer = xstrdup(namebuf);
62 item = path_list_insert(buffer, list);
63 if (item->util == NULL)
64 item->util = xcalloc(1, sizeof(struct path_list));
65 else
66 free(buffer);
67
68 + eol = strchr(oneline, '\n');
69 + if (!eol)
70 + eol = oneline + strlen(oneline);
71 + while (*oneline && isspace(*oneline) && *oneline != '\n')
72 + oneline++;
73 if (!prefixcmp(oneline, "[PATCH")) {
74 char *eob = strchr(oneline, ']');
51 -
52 - if (eob) {
53 - while (isspace(eob[1]) && eob[1] != '\n')
54 - eob++;
55 - if (eob - oneline < onelinelen) {
56 - onelinelen -= eob - oneline;
57 - oneline = eob;
58 - }
59 - }
75 + if (eob && (!eol || eob < eol))
76 + oneline = eob + 1;
77 }
61 -
62 - while (onelinelen > 0 && isspace(oneline[0])) {
78 + while (*oneline && isspace(*oneline) && *oneline != '\n')
79 oneline++;
64 - onelinelen--;
65 - }
66 - while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
67 - onelinelen--;
68 - buffer = xmemdupz(oneline, onelinelen);
80 + len = eol - oneline;
81 + while (len && isspace(oneline[len-1]))
82 + len--;
83 + buffer = xmemdupz(oneline, len);
84
85 if (dot3) {
86 int dot3len = strlen(dot3);
@@ -92,55 +107,32 @@ static void insert_author_oneline(struct path_list *list,
107
108 static void read_from_stdin(struct path_list *list)
109 {
95 - char buffer[1024];
96 -
97 - while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
98 - char *bob;
99 - if ((buffer[0] == 'A' || buffer[0] == 'a') &&
100 - !prefixcmp(buffer + 1, "uthor: ") &&
101 - (bob = strchr(buffer + 7, '<')) != NULL) {
102 - char buffer2[1024], offset = 0;
103 -
104 - if (map_email(&mailmap, bob + 1, buffer, sizeof(buffer)))
105 - bob = buffer + strlen(buffer);
106 - else {
107 - offset = 8;
108 - while (buffer + offset < bob &&
109 - isspace(bob[-1]))
110 - bob--;
111 - }
112 -
113 - while (fgets(buffer2, sizeof(buffer2), stdin) &&
114 - buffer2[0] != '\n')
115 - ; /* chomp input */
116 - if (fgets(buffer2, sizeof(buffer2), stdin)) {
117 - int l2 = strlen(buffer2);
118 - int i;
119 - for (i = 0; i < l2; i++)
120 - if (!isspace(buffer2[i]))
121 - break;
122 - insert_author_oneline(list,
123 - buffer + offset,
124 - bob - buffer - offset,
125 - buffer2 + i, l2 - i);
126 - }
127 - }
110 + char author[1024], oneline[1024];
111 +
112 + while (fgets(author, sizeof(author), stdin) != NULL) {
113 + if (!(author[0] == 'A' || author[0] == 'a') ||
114 + prefixcmp(author + 1, "uthor: "))
115 + continue;
116 + while (fgets(oneline, sizeof(oneline), stdin) &&
117 + oneline[0] != '\n')
118 + ; /* discard headers */
119 + while (fgets(oneline, sizeof(oneline), stdin) &&
120 + oneline[0] == '\n')
121 + ; /* discard blanks */
122 + insert_one_record(list, author + 8, oneline);
123 }
124 }
125
126 static void get_from_rev(struct rev_info *rev, struct path_list *list)
127 {
133 - char scratch[1024];
128 struct commit *commit;
129
130 prepare_revision_walk(rev);
131 while ((commit = get_revision(rev)) != NULL) {
138 - const char *author = NULL, *oneline, *buffer;
139 - int authorlen = authorlen, onelinelen;
132 + const char *author = NULL, *buffer;
133
141 - /* get author and oneline */
142 - for (buffer = commit->buffer; buffer && *buffer != '\0' &&
143 - *buffer != '\n'; ) {
134 + buffer = commit->buffer;
135 + while (*buffer && *buffer != '\n') {
136 const char *eol = strchr(buffer, '\n');
137
138 if (eol == NULL)
@@ -148,50 +140,17 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
140 else
141 eol++;
142
151 - if (!prefixcmp(buffer, "author ")) {
152 - char *bracket = strchr(buffer, '<');
153 -
154 - if (bracket == NULL || bracket > eol)
155 - die("Invalid commit buffer: %s",
156 - sha1_to_hex(commit->object.sha1));
157 -
158 - if (map_email(&mailmap, bracket + 1, scratch,
159 - sizeof(scratch))) {
160 - author = scratch;
161 - authorlen = strlen(scratch);
162 - } else {
163 - if (bracket[-1] == ' ')
164 - bracket--;
165 -
166 - author = buffer + 7;
167 - authorlen = bracket - buffer - 7;
168 - }
169 - }
143 + if (!prefixcmp(buffer, "author "))
144 + author = buffer + 7;
145 buffer = eol;
146 }
172 -
173 - if (author == NULL)
174 - die ("Missing author: %s",
175 - sha1_to_hex(commit->object.sha1));
176 -
177 - if (buffer == NULL || *buffer == '\0') {
178 - oneline = "<none>";
179 - onelinelen = sizeof(oneline) + 1;
180 - } else {
181 - char *eol;
182 -
183 - oneline = buffer + 1;
184 - eol = strchr(oneline, '\n');
185 - if (eol == NULL)
186 - onelinelen = strlen(oneline);
187 - else
188 - onelinelen = eol - oneline;
189 - }
190 -
191 - insert_author_oneline(list,
192 - author, authorlen, oneline, onelinelen);
147 + if (!author)
148 + die("Missing author: %s",
149 + sha1_to_hex(commit->object.sha1));
150 + if (*buffer)
151 + buffer++;
152 + insert_one_record(list, author, !*buffer ? "<none>" : buffer);
153 }
194 -
154 }
155
156 static int parse_uint(char const **arg, int comma)