Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 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" |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 7 | |
| 8 | static const char shortlog_usage[] = |
Nicolas Pitre | ac60c94 | 2006-11-21 15:49:45 -0500 | [diff] [blame] | 9 | "git-shortlog [-n] [-s] [<commit-id>... ]"; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 10 | |
Junio C Hamano | 7595e2e | 2006-11-25 00:07:54 -0800 | [diff] [blame] | 11 | static char *common_repo_prefix; |
| 12 | |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 13 | static int compare_by_number(const void *a1, const void *a2) |
| 14 | { |
| 15 | const struct path_list_item *i1 = a1, *i2 = a2; |
| 16 | const struct path_list *l1 = i1->util, *l2 = i2->util; |
| 17 | |
| 18 | if (l1->nr < l2->nr) |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 19 | return 1; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 20 | else if (l1->nr == l2->nr) |
| 21 | return 0; |
| 22 | else |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 23 | return -1; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 26 | static struct path_list mailmap = {NULL, 0, 0, 0}; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 27 | |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 28 | static int read_mailmap(const char *filename) |
| 29 | { |
| 30 | char buffer[1024]; |
| 31 | FILE *f = fopen(filename, "r"); |
| 32 | |
| 33 | if (f == NULL) |
| 34 | return 1; |
| 35 | while (fgets(buffer, sizeof(buffer), f) != NULL) { |
| 36 | char *end_of_name, *left_bracket, *right_bracket; |
| 37 | char *name, *email; |
Johannes Schindelin | 5496523 | 2006-11-19 17:29:14 +0100 | [diff] [blame] | 38 | int i; |
Junio C Hamano | 7595e2e | 2006-11-25 00:07:54 -0800 | [diff] [blame] | 39 | if (buffer[0] == '#') { |
| 40 | static const char abbrev[] = "# repo-abbrev:"; |
| 41 | int abblen = sizeof(abbrev) - 1; |
| 42 | int len = strlen(buffer); |
| 43 | |
| 44 | if (len && buffer[len - 1] == '\n') |
| 45 | buffer[--len] = 0; |
| 46 | if (!strncmp(buffer, abbrev, abblen)) { |
| 47 | char *cp; |
| 48 | |
| 49 | if (common_repo_prefix) |
| 50 | free(common_repo_prefix); |
| 51 | common_repo_prefix = xmalloc(len); |
| 52 | |
| 53 | for (cp = buffer + abblen; isspace(*cp); cp++) |
| 54 | ; /* nothing */ |
| 55 | strcpy(common_repo_prefix, cp); |
| 56 | } |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 57 | continue; |
Junio C Hamano | 7595e2e | 2006-11-25 00:07:54 -0800 | [diff] [blame] | 58 | } |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 59 | if ((left_bracket = strchr(buffer, '<')) == NULL) |
| 60 | continue; |
| 61 | if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL) |
| 62 | continue; |
| 63 | if (right_bracket == left_bracket + 1) |
| 64 | continue; |
| 65 | for (end_of_name = left_bracket; end_of_name != buffer |
| 66 | && isspace(end_of_name[-1]); end_of_name--) |
| 67 | /* keep on looking */ |
| 68 | if (end_of_name == buffer) |
| 69 | continue; |
| 70 | name = xmalloc(end_of_name - buffer + 1); |
| 71 | strlcpy(name, buffer, end_of_name - buffer + 1); |
| 72 | email = xmalloc(right_bracket - left_bracket); |
Johannes Schindelin | 5496523 | 2006-11-19 17:29:14 +0100 | [diff] [blame] | 73 | for (i = 0; i < right_bracket - left_bracket - 1; i++) |
| 74 | email[i] = tolower(left_bracket[i + 1]); |
| 75 | email[right_bracket - left_bracket - 1] = '\0'; |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 76 | path_list_insert(email, &mailmap)->util = name; |
| 77 | } |
| 78 | fclose(f); |
| 79 | return 0; |
| 80 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 81 | |
| 82 | static int map_email(char *email, char *name, int maxlen) |
| 83 | { |
| 84 | char *p; |
| 85 | struct path_list_item *item; |
| 86 | |
| 87 | /* autocomplete common developers */ |
| 88 | p = strchr(email, '>'); |
| 89 | if (!p) |
| 90 | return 0; |
| 91 | |
| 92 | *p = '\0'; |
Johannes Schindelin | 5496523 | 2006-11-19 17:29:14 +0100 | [diff] [blame] | 93 | /* downcase the email address */ |
| 94 | for (p = email; *p; p++) |
| 95 | *p = tolower(*p); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 96 | item = path_list_lookup(email, &mailmap); |
| 97 | if (item != NULL) { |
| 98 | const char *realname = (const char *)item->util; |
| 99 | strncpy(name, realname, maxlen); |
| 100 | return 1; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static void insert_author_oneline(struct path_list *list, |
| 106 | const char *author, int authorlen, |
| 107 | const char *oneline, int onelinelen) |
| 108 | { |
Junio C Hamano | 7595e2e | 2006-11-25 00:07:54 -0800 | [diff] [blame] | 109 | const char *dot3 = common_repo_prefix; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 110 | char *buffer, *p; |
| 111 | struct path_list_item *item; |
| 112 | struct path_list *onelines; |
| 113 | |
| 114 | while (authorlen > 0 && isspace(author[authorlen - 1])) |
| 115 | authorlen--; |
| 116 | |
| 117 | buffer = xmalloc(authorlen + 1); |
| 118 | memcpy(buffer, author, authorlen); |
| 119 | buffer[authorlen] = '\0'; |
| 120 | |
| 121 | item = path_list_insert(buffer, list); |
| 122 | if (item->util == NULL) |
| 123 | item->util = xcalloc(1, sizeof(struct path_list)); |
| 124 | else |
| 125 | free(buffer); |
| 126 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 127 | if (!prefixcmp(oneline, "[PATCH")) { |
Johannes Schindelin | 72019cd | 2006-11-19 17:28:25 +0100 | [diff] [blame] | 128 | char *eob = strchr(oneline, ']'); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 129 | |
Johannes Schindelin | 72019cd | 2006-11-19 17:28:25 +0100 | [diff] [blame] | 130 | if (eob) { |
| 131 | while (isspace(eob[1]) && eob[1] != '\n') |
| 132 | eob++; |
| 133 | if (eob - oneline < onelinelen) { |
| 134 | onelinelen -= eob - oneline; |
| 135 | oneline = eob; |
| 136 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | while (onelinelen > 0 && isspace(oneline[0])) { |
| 141 | oneline++; |
| 142 | onelinelen--; |
| 143 | } |
| 144 | |
| 145 | while (onelinelen > 0 && isspace(oneline[onelinelen - 1])) |
| 146 | onelinelen--; |
| 147 | |
| 148 | buffer = xmalloc(onelinelen + 1); |
| 149 | memcpy(buffer, oneline, onelinelen); |
| 150 | buffer[onelinelen] = '\0'; |
| 151 | |
Junio C Hamano | c95044d | 2006-11-25 00:01:27 -0800 | [diff] [blame] | 152 | if (dot3) { |
| 153 | int dot3len = strlen(dot3); |
| 154 | if (dot3len > 5) { |
| 155 | while ((p = strstr(buffer, dot3)) != NULL) { |
| 156 | int taillen = strlen(p) - dot3len; |
| 157 | memcpy(p, "/.../", 5); |
| 158 | memmove(p + 5, p + dot3len, taillen + 1); |
| 159 | } |
| 160 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 163 | onelines = item->util; |
| 164 | if (onelines->nr >= onelines->alloc) { |
| 165 | onelines->alloc = alloc_nr(onelines->nr); |
| 166 | onelines->items = xrealloc(onelines->items, |
| 167 | onelines->alloc |
| 168 | * sizeof(struct path_list_item)); |
| 169 | } |
| 170 | |
| 171 | onelines->items[onelines->nr].util = NULL; |
| 172 | onelines->items[onelines->nr++].path = buffer; |
| 173 | } |
| 174 | |
| 175 | static void read_from_stdin(struct path_list *list) |
| 176 | { |
| 177 | char buffer[1024]; |
| 178 | |
| 179 | while (fgets(buffer, sizeof(buffer), stdin) != NULL) { |
| 180 | char *bob; |
| 181 | if ((buffer[0] == 'A' || buffer[0] == 'a') && |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 182 | !prefixcmp(buffer + 1, "uthor: ") && |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 183 | (bob = strchr(buffer + 7, '<')) != NULL) { |
| 184 | char buffer2[1024], offset = 0; |
| 185 | |
| 186 | if (map_email(bob + 1, buffer, sizeof(buffer))) |
| 187 | bob = buffer + strlen(buffer); |
| 188 | else { |
| 189 | offset = 8; |
Junio C Hamano | 6f98725 | 2006-12-10 15:51:54 -0800 | [diff] [blame] | 190 | while (buffer + offset < bob && |
| 191 | isspace(bob[-1])) |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 192 | bob--; |
| 193 | } |
| 194 | |
| 195 | while (fgets(buffer2, sizeof(buffer2), stdin) && |
| 196 | buffer2[0] != '\n') |
| 197 | ; /* chomp input */ |
Junio C Hamano | bca7325 | 2006-12-10 15:55:07 -0800 | [diff] [blame] | 198 | if (fgets(buffer2, sizeof(buffer2), stdin)) { |
| 199 | int l2 = strlen(buffer2); |
| 200 | int i; |
| 201 | for (i = 0; i < l2; i++) |
| 202 | if (!isspace(buffer2[i])) |
| 203 | break; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 204 | insert_author_oneline(list, |
| 205 | buffer + offset, |
| 206 | bob - buffer - offset, |
Junio C Hamano | bca7325 | 2006-12-10 15:55:07 -0800 | [diff] [blame] | 207 | buffer2 + i, l2 - i); |
| 208 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void get_from_rev(struct rev_info *rev, struct path_list *list) |
| 214 | { |
| 215 | char scratch[1024]; |
| 216 | struct commit *commit; |
| 217 | |
| 218 | prepare_revision_walk(rev); |
| 219 | while ((commit = get_revision(rev)) != NULL) { |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 220 | const char *author = NULL, *oneline, *buffer; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 221 | int authorlen = authorlen, onelinelen; |
| 222 | |
| 223 | /* get author and oneline */ |
| 224 | for (buffer = commit->buffer; buffer && *buffer != '\0' && |
| 225 | *buffer != '\n'; ) { |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 226 | const char *eol = strchr(buffer, '\n'); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 227 | |
| 228 | if (eol == NULL) |
| 229 | eol = buffer + strlen(buffer); |
| 230 | else |
| 231 | eol++; |
| 232 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 233 | if (!prefixcmp(buffer, "author ")) { |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 234 | char *bracket = strchr(buffer, '<'); |
| 235 | |
| 236 | if (bracket == NULL || bracket > eol) |
| 237 | die("Invalid commit buffer: %s", |
| 238 | sha1_to_hex(commit->object.sha1)); |
| 239 | |
| 240 | if (map_email(bracket + 1, scratch, |
| 241 | sizeof(scratch))) { |
| 242 | author = scratch; |
| 243 | authorlen = strlen(scratch); |
| 244 | } else { |
Jeff King | 90ffefe | 2006-12-08 23:04:21 -0500 | [diff] [blame] | 245 | if (bracket[-1] == ' ') |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 246 | bracket--; |
| 247 | |
| 248 | author = buffer + 7; |
| 249 | authorlen = bracket - buffer - 7; |
| 250 | } |
| 251 | } |
| 252 | buffer = eol; |
| 253 | } |
| 254 | |
| 255 | if (author == NULL) |
| 256 | die ("Missing author: %s", |
| 257 | sha1_to_hex(commit->object.sha1)); |
| 258 | |
| 259 | if (buffer == NULL || *buffer == '\0') { |
| 260 | oneline = "<none>"; |
| 261 | onelinelen = sizeof(oneline) + 1; |
| 262 | } else { |
| 263 | char *eol; |
| 264 | |
| 265 | oneline = buffer + 1; |
| 266 | eol = strchr(oneline, '\n'); |
| 267 | if (eol == NULL) |
| 268 | onelinelen = strlen(oneline); |
| 269 | else |
| 270 | onelinelen = eol - oneline; |
| 271 | } |
| 272 | |
| 273 | insert_author_oneline(list, |
| 274 | author, authorlen, oneline, onelinelen); |
| 275 | } |
| 276 | |
| 277 | } |
| 278 | |
| 279 | int cmd_shortlog(int argc, const char **argv, const char *prefix) |
| 280 | { |
| 281 | struct rev_info rev; |
| 282 | struct path_list list = { NULL, 0, 0, 1 }; |
| 283 | int i, j, sort_by_number = 0, summary = 0; |
| 284 | |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 285 | /* since -n is a shadowed rev argument, parse our args first */ |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 286 | while (argc > 1) { |
| 287 | if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered")) |
| 288 | sort_by_number = 1; |
| 289 | else if (!strcmp(argv[1], "-s") || |
| 290 | !strcmp(argv[1], "--summary")) |
| 291 | summary = 1; |
| 292 | else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) |
| 293 | usage(shortlog_usage); |
| 294 | else |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 295 | break; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 296 | argv++; |
| 297 | argc--; |
| 298 | } |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 299 | init_revisions(&rev, prefix); |
| 300 | argc = setup_revisions(argc, argv, &rev, NULL); |
| 301 | if (argc > 1) |
| 302 | die ("unrecognized argument: %s", argv[1]); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 303 | |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 304 | if (!access(".mailmap", R_OK)) |
| 305 | read_mailmap(".mailmap"); |
| 306 | |
Junio C Hamano | 0497c62 | 2007-03-08 02:12:06 -0800 | [diff] [blame] | 307 | if (rev.pending.nr == 0) { |
| 308 | if (isatty(0)) |
| 309 | fprintf(stderr, "(reading log to summarize from standard input)\n"); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 310 | read_from_stdin(&list); |
Junio C Hamano | 0497c62 | 2007-03-08 02:12:06 -0800 | [diff] [blame] | 311 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 312 | else |
| 313 | get_from_rev(&rev, &list); |
| 314 | |
| 315 | if (sort_by_number) |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 316 | qsort(list.items, list.nr, sizeof(struct path_list_item), |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 317 | compare_by_number); |
| 318 | |
| 319 | for (i = 0; i < list.nr; i++) { |
| 320 | struct path_list *onelines = list.items[i].util; |
| 321 | |
Nicolas Pitre | ac60c94 | 2006-11-21 15:49:45 -0500 | [diff] [blame] | 322 | if (summary) { |
| 323 | printf("%s: %d\n", list.items[i].path, onelines->nr); |
| 324 | } else { |
| 325 | printf("%s (%d):\n", list.items[i].path, onelines->nr); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 326 | for (j = onelines->nr - 1; j >= 0; j--) |
| 327 | printf(" %s\n", onelines->items[j].path); |
| 328 | printf("\n"); |
| 329 | } |
| 330 | |
| 331 | onelines->strdup_paths = 1; |
| 332 | path_list_clear(onelines, 1); |
| 333 | free(onelines); |
| 334 | list.items[i].util = NULL; |
| 335 | } |
| 336 | |
| 337 | list.strdup_paths = 1; |
| 338 | path_list_clear(&list, 1); |
Johannes Schindelin | d8e8125 | 2006-11-19 17:28:51 +0100 | [diff] [blame] | 339 | mailmap.strdup_paths = 1; |
| 340 | path_list_clear(&mailmap, 1); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |