THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

366 lines
8.9 KiB

  1. #include "cache.h"
  2. #include "string-list.h"
  3. #include "mailmap.h"
  4. #define DEBUG_MAILMAP 0
  5. #if DEBUG_MAILMAP
  6. #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
  7. #define debug_str(X) ((X) ? (X) : "(none)")
  8. #else
  9. static inline void debug_mm(const char *format, ...) {}
  10. static inline const char *debug_str(const char *s) { return s; }
  11. #endif
  12. const char *git_mailmap_file;
  13. const char *git_mailmap_blob;
  14. struct mailmap_info {
  15. char *name;
  16. char *email;
  17. };
  18. struct mailmap_entry {
  19. /* name and email for the simple mail-only case */
  20. char *name;
  21. char *email;
  22. /* name and email for the complex mail and name matching case */
  23. struct string_list namemap;
  24. };
  25. static void free_mailmap_info(void *p, const char *s)
  26. {
  27. struct mailmap_info *mi = (struct mailmap_info *)p;
  28. debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n",
  29. s, debug_str(mi->name), debug_str(mi->email));
  30. free(mi->name);
  31. free(mi->email);
  32. }
  33. static void free_mailmap_entry(void *p, const char *s)
  34. {
  35. struct mailmap_entry *me = (struct mailmap_entry *)p;
  36. debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n",
  37. s, me->namemap.nr);
  38. debug_mm("mailmap: - simple: '%s' <%s>\n",
  39. debug_str(me->name), debug_str(me->email));
  40. free(me->name);
  41. free(me->email);
  42. me->namemap.strdup_strings = 1;
  43. string_list_clear_func(&me->namemap, free_mailmap_info);
  44. }
  45. /*
  46. * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
  47. * definition of strcasecmp and no non-inline implementation is
  48. * supplied anywhere, which is, eh, "unusual"; we cannot take an
  49. * address of such a function to store it in namemap.cmp. This is
  50. * here as a workaround---do not assign strcasecmp directly to
  51. * namemap.cmp until we know no systems that matter have such an
  52. * "unusual" string.h.
  53. */
  54. static int namemap_cmp(const char *a, const char *b)
  55. {
  56. return strcasecmp(a, b);
  57. }
  58. static void add_mapping(struct string_list *map,
  59. char *new_name, char *new_email,
  60. char *old_name, char *old_email)
  61. {
  62. struct mailmap_entry *me;
  63. struct string_list_item *item;
  64. if (old_email == NULL) {
  65. old_email = new_email;
  66. new_email = NULL;
  67. }
  68. item = string_list_insert(map, old_email);
  69. if (item->util) {
  70. me = (struct mailmap_entry *)item->util;
  71. } else {
  72. me = xcalloc(1, sizeof(struct mailmap_entry));
  73. me->namemap.strdup_strings = 1;
  74. me->namemap.cmp = namemap_cmp;
  75. item->util = me;
  76. }
  77. if (old_name == NULL) {
  78. debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email);
  79. /* Replace current name and new email for simple entry */
  80. if (new_name) {
  81. free(me->name);
  82. me->name = xstrdup(new_name);
  83. }
  84. if (new_email) {
  85. free(me->email);
  86. me->email = xstrdup(new_email);
  87. }
  88. } else {
  89. struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
  90. debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email);
  91. if (new_name)
  92. mi->name = xstrdup(new_name);
  93. if (new_email)
  94. mi->email = xstrdup(new_email);
  95. string_list_insert(&me->namemap, old_name)->util = mi;
  96. }
  97. debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
  98. debug_str(old_name), old_email,
  99. debug_str(new_name), debug_str(new_email));
  100. }
  101. static char *parse_name_and_email(char *buffer, char **name,
  102. char **email, int allow_empty_email)
  103. {
  104. char *left, *right, *nstart, *nend;
  105. *name = *email = NULL;
  106. if ((left = strchr(buffer, '<')) == NULL)
  107. return NULL;
  108. if ((right = strchr(left+1, '>')) == NULL)
  109. return NULL;
  110. if (!allow_empty_email && (left+1 == right))
  111. return NULL;
  112. /* remove whitespace from beginning and end of name */
  113. nstart = buffer;
  114. while (isspace(*nstart) && nstart < left)
  115. ++nstart;
  116. nend = left-1;
  117. while (nend > nstart && isspace(*nend))
  118. --nend;
  119. *name = (nstart <= nend ? nstart : NULL);
  120. *email = left+1;
  121. *(nend+1) = '\0';
  122. *right++ = '\0';
  123. return (*right == '\0' ? NULL : right);
  124. }
  125. static void read_mailmap_line(struct string_list *map, char *buffer,
  126. char **repo_abbrev)
  127. {
  128. char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
  129. if (buffer[0] == '#') {
  130. static const char abbrev[] = "# repo-abbrev:";
  131. int abblen = sizeof(abbrev) - 1;
  132. int len = strlen(buffer);
  133. if (!repo_abbrev)
  134. return;
  135. if (len && buffer[len - 1] == '\n')
  136. buffer[--len] = 0;
  137. if (!strncmp(buffer, abbrev, abblen)) {
  138. char *cp;
  139. free(*repo_abbrev);
  140. for (cp = buffer + abblen; isspace(*cp); cp++)
  141. ; /* nothing */
  142. *repo_abbrev = xstrdup(cp);
  143. }
  144. return;
  145. }
  146. if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
  147. parse_name_and_email(name2, &name2, &email2, 1);
  148. if (email1)
  149. add_mapping(map, name1, email1, name2, email2);
  150. }
  151. static int read_mailmap_file(struct string_list *map, const char *filename,
  152. char **repo_abbrev)
  153. {
  154. char buffer[1024];
  155. FILE *f;
  156. if (!filename)
  157. return 0;
  158. f = fopen(filename, "r");
  159. if (!f) {
  160. if (errno == ENOENT)
  161. return 0;
  162. return error("unable to open mailmap at %s: %s",
  163. filename, strerror(errno));
  164. }
  165. while (fgets(buffer, sizeof(buffer), f) != NULL)
  166. read_mailmap_line(map, buffer, repo_abbrev);
  167. fclose(f);
  168. return 0;
  169. }
  170. static void read_mailmap_string(struct string_list *map, char *buf,
  171. char **repo_abbrev)
  172. {
  173. while (*buf) {
  174. char *end = strchrnul(buf, '\n');
  175. if (*end)
  176. *end++ = '\0';
  177. read_mailmap_line(map, buf, repo_abbrev);
  178. buf = end;
  179. }
  180. }
  181. static int read_mailmap_blob(struct string_list *map,
  182. const char *name,
  183. char **repo_abbrev)
  184. {
  185. unsigned char sha1[20];
  186. char *buf;
  187. unsigned long size;
  188. enum object_type type;
  189. if (!name)
  190. return 0;
  191. if (get_sha1(name, sha1) < 0)
  192. return 0;
  193. buf = read_sha1_file(sha1, &type, &size);
  194. if (!buf)
  195. return error("unable to read mailmap object at %s", name);
  196. if (type != OBJ_BLOB)
  197. return error("mailmap is not a blob: %s", name);
  198. read_mailmap_string(map, buf, repo_abbrev);
  199. free(buf);
  200. return 0;
  201. }
  202. int read_mailmap(struct string_list *map, char **repo_abbrev)
  203. {
  204. int err = 0;
  205. map->strdup_strings = 1;
  206. map->cmp = namemap_cmp;
  207. if (!git_mailmap_blob && is_bare_repository())
  208. git_mailmap_blob = "HEAD:.mailmap";
  209. err |= read_mailmap_file(map, ".mailmap", repo_abbrev);
  210. if (startup_info->have_repository)
  211. err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
  212. err |= read_mailmap_file(map, git_mailmap_file, repo_abbrev);
  213. return err;
  214. }
  215. void clear_mailmap(struct string_list *map)
  216. {
  217. debug_mm("mailmap: clearing %d entries...\n", map->nr);
  218. map->strdup_strings = 1;
  219. string_list_clear_func(map, free_mailmap_entry);
  220. debug_mm("mailmap: cleared\n");
  221. }
  222. /*
  223. * Look for an entry in map that match string[0:len]; string[len]
  224. * does not have to be NUL (but it could be).
  225. */
  226. static struct string_list_item *lookup_prefix(struct string_list *map,
  227. const char *string, size_t len)
  228. {
  229. int i = string_list_find_insert_index(map, string, 1);
  230. if (i < 0) {
  231. /* exact match */
  232. i = -1 - i;
  233. if (!string[len])
  234. return &map->items[i];
  235. /*
  236. * that map entry matches exactly to the string, including
  237. * the cruft at the end beyond "len". That is not a match
  238. * with string[0:len] that we are looking for.
  239. */
  240. } else if (!string[len]) {
  241. /*
  242. * asked with the whole string, and got nothing. No
  243. * matching entry can exist in the map.
  244. */
  245. return NULL;
  246. }
  247. /*
  248. * i is at the exact match to an overlong key, or location the
  249. * overlong key would be inserted, which must come after the
  250. * real location of the key if one exists.
  251. */
  252. while (0 <= --i && i < map->nr) {
  253. int cmp = strncasecmp(map->items[i].string, string, len);
  254. if (cmp < 0)
  255. /*
  256. * "i" points at a key definitely below the prefix;
  257. * the map does not have string[0:len] in it.
  258. */
  259. break;
  260. else if (!cmp && !map->items[i].string[len])
  261. /* found it */
  262. return &map->items[i];
  263. /*
  264. * otherwise, the string at "i" may be string[0:len]
  265. * followed by a string that sorts later than string[len:];
  266. * keep trying.
  267. */
  268. }
  269. return NULL;
  270. }
  271. int map_user(struct string_list *map,
  272. const char **email, size_t *emaillen,
  273. const char **name, size_t *namelen)
  274. {
  275. struct string_list_item *item;
  276. struct mailmap_entry *me;
  277. debug_mm("map_user: map '%.*s' <%.*s>\n",
  278. (int)*namelen, debug_str(*name),
  279. (int)*emaillen, debug_str(*email));
  280. item = lookup_prefix(map, *email, *emaillen);
  281. if (item != NULL) {
  282. me = (struct mailmap_entry *)item->util;
  283. if (me->namemap.nr) {
  284. /*
  285. * The item has multiple items, so we'll look up on
  286. * name too. If the name is not found, we choose the
  287. * simple entry.
  288. */
  289. struct string_list_item *subitem;
  290. subitem = lookup_prefix(&me->namemap, *name, *namelen);
  291. if (subitem)
  292. item = subitem;
  293. }
  294. }
  295. if (item != NULL) {
  296. struct mailmap_info *mi = (struct mailmap_info *)item->util;
  297. if (mi->name == NULL && mi->email == NULL) {
  298. debug_mm("map_user: -- (no simple mapping)\n");
  299. return 0;
  300. }
  301. if (mi->email) {
  302. *email = mi->email;
  303. *emaillen = strlen(*email);
  304. }
  305. if (mi->name) {
  306. *name = mi->name;
  307. *namelen = strlen(*name);
  308. }
  309. debug_mm("map_user: to '%.*s' <%.*s>\n",
  310. (int)*namelen, debug_str(*name),
  311. (int)*emaillen, debug_str(*email));
  312. return 1;
  313. }
  314. debug_mm("map_user: --\n");
  315. return 0;
  316. }