Skip to content

Commit 1e931cb

Browse files
committed
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>
1 parent fd99b36 commit 1e931cb

File tree

1 file changed

+62
-103
lines changed

1 file changed

+62
-103
lines changed

builtin-shortlog.c

Lines changed: 62 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,60 @@ static int compare_by_number(const void *a1, const void *a2)
2727

2828
static struct path_list mailmap = {NULL, 0, 0, 0};
2929

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)
3333
{
3434
const char *dot3 = common_repo_prefix;
3535
char *buffer, *p;
3636
struct path_list_item *item;
3737
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+
}
3860

39-
while (authorlen > 0 && isspace(author[authorlen - 1]))
40-
authorlen--;
41-
42-
buffer = xmemdupz(author, authorlen);
61+
buffer = xstrdup(namebuf);
4362
item = path_list_insert(buffer, list);
4463
if (item->util == NULL)
4564
item->util = xcalloc(1, sizeof(struct path_list));
4665
else
4766
free(buffer);
4867

68+
eol = strchr(oneline, '\n');
69+
if (!eol)
70+
eol = oneline + strlen(oneline);
71+
while (*oneline && isspace(*oneline) && *oneline != '\n')
72+
oneline++;
4973
if (!prefixcmp(oneline, "[PATCH")) {
5074
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;
6077
}
61-
62-
while (onelinelen > 0 && isspace(oneline[0])) {
78+
while (*oneline && isspace(*oneline) && *oneline != '\n')
6379
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);
6984

7085
if (dot3) {
7186
int dot3len = strlen(dot3);
@@ -92,106 +107,50 @@ static void insert_author_oneline(struct path_list *list,
92107

93108
static void read_from_stdin(struct path_list *list)
94109
{
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);
128123
}
129124
}
130125

131126
static void get_from_rev(struct rev_info *rev, struct path_list *list)
132127
{
133-
char scratch[1024];
134128
struct commit *commit;
135129

136130
prepare_revision_walk(rev);
137131
while ((commit = get_revision(rev)) != NULL) {
138-
const char *author = NULL, *oneline, *buffer;
139-
int authorlen = authorlen, onelinelen;
132+
const char *author = NULL, *buffer;
140133

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') {
144136
const char *eol = strchr(buffer, '\n');
145137

146138
if (eol == NULL)
147139
eol = buffer + strlen(buffer);
148140
else
149141
eol++;
150142

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;
170145
buffer = eol;
171146
}
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);
193153
}
194-
195154
}
196155

197156
static int parse_uint(char const **arg, int comma)

0 commit comments

Comments
 (0)