Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 2 | #include "environment.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 3 | #include "string-list.h" |
Alex Riesen | fe5d30b | 2007-05-01 00:31:52 +0200 | [diff] [blame] | 4 | #include "mailmap.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame^] | 5 | #include "object-name.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 6 | #include "object-store.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 7 | #include "setup.h" |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 8 | |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 9 | #define DEBUG_MAILMAP 0 |
| 10 | #if DEBUG_MAILMAP |
| 11 | #define debug_mm(...) fprintf(stderr, __VA_ARGS__) |
Eric Sunshine | fbfba7a | 2013-07-15 02:54:12 -0400 | [diff] [blame] | 12 | #define debug_str(X) ((X) ? (X) : "(none)") |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 13 | #else |
Ævar Arnfjörð Bjarmason | 48ca53c | 2021-07-13 10:05:18 +0200 | [diff] [blame] | 14 | __attribute__((format (printf, 1, 2))) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 15 | static inline void debug_mm(const char *format, ...) {} |
Eric Sunshine | fbfba7a | 2013-07-15 02:54:12 -0400 | [diff] [blame] | 16 | static inline const char *debug_str(const char *s) { return s; } |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 17 | #endif |
| 18 | |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 19 | const char *git_mailmap_file; |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 20 | const char *git_mailmap_blob; |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 21 | |
| 22 | struct mailmap_info { |
| 23 | char *name; |
| 24 | char *email; |
| 25 | }; |
| 26 | |
| 27 | struct mailmap_entry { |
| 28 | /* name and email for the simple mail-only case */ |
| 29 | char *name; |
| 30 | char *email; |
| 31 | |
| 32 | /* name and email for the complex mail and name matching case */ |
| 33 | struct string_list namemap; |
| 34 | }; |
| 35 | |
| 36 | static void free_mailmap_info(void *p, const char *s) |
| 37 | { |
| 38 | struct mailmap_info *mi = (struct mailmap_info *)p; |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 39 | debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", |
| 40 | s, debug_str(mi->name), debug_str(mi->email)); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 41 | free(mi->name); |
| 42 | free(mi->email); |
Ævar Arnfjörð Bjarmason | ccdd5d1 | 2021-08-31 15:42:52 +0200 | [diff] [blame] | 43 | free(mi); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static void free_mailmap_entry(void *p, const char *s) |
| 47 | { |
| 48 | struct mailmap_entry *me = (struct mailmap_entry *)p; |
Ævar Arnfjörð Bjarmason | 99d6054 | 2022-03-07 16:27:08 +0100 | [diff] [blame] | 49 | debug_mm("mailmap: removing entries for <%s>, with %"PRIuMAX" sub-entries\n", |
| 50 | s, (uintmax_t)me->namemap.nr); |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 51 | debug_mm("mailmap: - simple: '%s' <%s>\n", |
| 52 | debug_str(me->name), debug_str(me->email)); |
Eric Sunshine | fbfba7a | 2013-07-15 02:54:12 -0400 | [diff] [blame] | 53 | |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 54 | free(me->name); |
| 55 | free(me->email); |
| 56 | |
| 57 | me->namemap.strdup_strings = 1; |
| 58 | string_list_clear_func(&me->namemap, free_mailmap_info); |
Ævar Arnfjörð Bjarmason | ccdd5d1 | 2021-08-31 15:42:52 +0200 | [diff] [blame] | 59 | free(me); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Junio C Hamano | de2f95e | 2013-09-12 08:37:08 -0700 | [diff] [blame] | 62 | /* |
| 63 | * On some systems (e.g. MinGW 4.0), string.h has _only_ inline |
| 64 | * definition of strcasecmp and no non-inline implementation is |
| 65 | * supplied anywhere, which is, eh, "unusual"; we cannot take an |
| 66 | * address of such a function to store it in namemap.cmp. This is |
| 67 | * here as a workaround---do not assign strcasecmp directly to |
| 68 | * namemap.cmp until we know no systems that matter have such an |
| 69 | * "unusual" string.h. |
| 70 | */ |
| 71 | static int namemap_cmp(const char *a, const char *b) |
| 72 | { |
| 73 | return strcasecmp(a, b); |
| 74 | } |
| 75 | |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 76 | static void add_mapping(struct string_list *map, |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 77 | char *new_name, char *new_email, |
| 78 | char *old_name, char *old_email) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 79 | { |
| 80 | struct mailmap_entry *me; |
Stefan Beller | 6322621 | 2014-11-24 19:44:14 -0800 | [diff] [blame] | 81 | struct string_list_item *item; |
Johannes Schindelin | bf63780 | 2009-03-31 02:18:36 +0200 | [diff] [blame] | 82 | |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 83 | if (!old_email) { |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 84 | old_email = new_email; |
| 85 | new_email = NULL; |
| 86 | } |
| 87 | |
Stefan Beller | 6322621 | 2014-11-24 19:44:14 -0800 | [diff] [blame] | 88 | item = string_list_insert(map, old_email); |
| 89 | if (item->util) { |
| 90 | me = (struct mailmap_entry *)item->util; |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 91 | } else { |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 92 | CALLOC_ARRAY(me, 1); |
Junio C Hamano | 97e751b | 2013-07-15 02:54:08 -0400 | [diff] [blame] | 93 | me->namemap.strdup_strings = 1; |
Junio C Hamano | de2f95e | 2013-09-12 08:37:08 -0700 | [diff] [blame] | 94 | me->namemap.cmp = namemap_cmp; |
Junio C Hamano | 97e751b | 2013-07-15 02:54:08 -0400 | [diff] [blame] | 95 | item->util = me; |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 96 | } |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 97 | |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 98 | if (!old_name) { |
Stefan Beller | 6322621 | 2014-11-24 19:44:14 -0800 | [diff] [blame] | 99 | debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email); |
| 100 | |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 101 | /* Replace current name and new email for simple entry */ |
Jim Meyering | d8d2eb7 | 2010-10-11 17:41:16 +0200 | [diff] [blame] | 102 | if (new_name) { |
| 103 | free(me->name); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 104 | me->name = xstrdup(new_name); |
Jim Meyering | d8d2eb7 | 2010-10-11 17:41:16 +0200 | [diff] [blame] | 105 | } |
| 106 | if (new_email) { |
| 107 | free(me->email); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 108 | me->email = xstrdup(new_email); |
Jim Meyering | d8d2eb7 | 2010-10-11 17:41:16 +0200 | [diff] [blame] | 109 | } |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 110 | } else { |
Marc-André Lureau | 74b531f | 2011-11-17 02:25:06 +0100 | [diff] [blame] | 111 | struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info)); |
Stefan Beller | 6322621 | 2014-11-24 19:44:14 -0800 | [diff] [blame] | 112 | debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email); |
Junio C Hamano | 13092a9 | 2016-10-12 11:20:23 -0700 | [diff] [blame] | 113 | mi->name = xstrdup_or_null(new_name); |
| 114 | mi->email = xstrdup_or_null(new_email); |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 115 | string_list_insert(&me->namemap, old_name)->util = mi; |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n", |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 119 | debug_str(old_name), old_email, |
| 120 | debug_str(new_name), debug_str(new_email)); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Björn Steinbrink | 5288dd5 | 2009-03-31 17:30:39 +0200 | [diff] [blame] | 123 | static char *parse_name_and_email(char *buffer, char **name, |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 124 | char **email, int allow_empty_email) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 125 | { |
| 126 | char *left, *right, *nstart, *nend; |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 127 | *name = *email = NULL; |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 128 | |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 129 | if (!(left = strchr(buffer, '<'))) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 130 | return NULL; |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 131 | if (!(right = strchr(left + 1, '>'))) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 132 | return NULL; |
Björn Steinbrink | 5288dd5 | 2009-03-31 17:30:39 +0200 | [diff] [blame] | 133 | if (!allow_empty_email && (left+1 == right)) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 134 | return NULL; |
| 135 | |
| 136 | /* remove whitespace from beginning and end of name */ |
| 137 | nstart = buffer; |
| 138 | while (isspace(*nstart) && nstart < left) |
| 139 | ++nstart; |
| 140 | nend = left-1; |
Romain Francoise | 3174bc5 | 2012-10-28 00:49:55 +0200 | [diff] [blame] | 141 | while (nend > nstart && isspace(*nend)) |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 142 | --nend; |
| 143 | |
Junio C Hamano | 8c38115 | 2013-07-15 02:54:06 -0400 | [diff] [blame] | 144 | *name = (nstart <= nend ? nstart : NULL); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 145 | *email = left+1; |
| 146 | *(nend+1) = '\0'; |
| 147 | *right++ = '\0'; |
| 148 | |
| 149 | return (*right == '\0' ? NULL : right); |
| 150 | } |
| 151 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 152 | static void read_mailmap_line(struct string_list *map, char *buffer) |
Jeff King | 7c8ce30 | 2012-12-12 05:59:45 -0500 | [diff] [blame] | 153 | { |
| 154 | char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL; |
Jeff King | 7c8ce30 | 2012-12-12 05:59:45 -0500 | [diff] [blame] | 155 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 156 | if (buffer[0] == '#') |
Jeff King | 7c8ce30 | 2012-12-12 05:59:45 -0500 | [diff] [blame] | 157 | return; |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 158 | |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 159 | if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0))) |
Jeff King | 7c8ce30 | 2012-12-12 05:59:45 -0500 | [diff] [blame] | 160 | parse_name_and_email(name2, &name2, &email2, 1); |
| 161 | |
| 162 | if (email1) |
| 163 | add_mapping(map, name1, email1, name2, email2); |
| 164 | } |
| 165 | |
Jeff King | adcd9f5 | 2021-02-16 09:44:37 -0500 | [diff] [blame] | 166 | /* Flags for read_mailmap_file() */ |
| 167 | #define MAILMAP_NOFOLLOW (1<<0) |
| 168 | |
| 169 | static int read_mailmap_file(struct string_list *map, const char *filename, |
| 170 | unsigned flags) |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 171 | { |
| 172 | char buffer[1024]; |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 173 | FILE *f; |
Jeff King | adcd9f5 | 2021-02-16 09:44:37 -0500 | [diff] [blame] | 174 | int fd; |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 175 | |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 176 | if (!filename) |
| 177 | return 0; |
| 178 | |
Jeff King | adcd9f5 | 2021-02-16 09:44:37 -0500 | [diff] [blame] | 179 | if (flags & MAILMAP_NOFOLLOW) |
| 180 | fd = open_nofollow(filename, O_RDONLY); |
| 181 | else |
| 182 | fd = open(filename, O_RDONLY); |
| 183 | |
| 184 | if (fd < 0) { |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 185 | if (errno == ENOENT) |
| 186 | return 0; |
Nguyễn Thái Ngọc Duy | 60901e4 | 2016-05-08 16:47:50 +0700 | [diff] [blame] | 187 | return error_errno("unable to open mailmap at %s", filename); |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 188 | } |
Jeff King | adcd9f5 | 2021-02-16 09:44:37 -0500 | [diff] [blame] | 189 | f = xfdopen(fd, "r"); |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 190 | |
Jeff King | 7c8ce30 | 2012-12-12 05:59:45 -0500 | [diff] [blame] | 191 | while (fgets(buffer, sizeof(buffer), f) != NULL) |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 192 | read_mailmap_line(map, buffer); |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 193 | fclose(f); |
| 194 | return 0; |
| 195 | } |
| 196 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 197 | static void read_mailmap_string(struct string_list *map, char *buf) |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 198 | { |
Jeff King | f972a16 | 2013-08-27 21:41:39 -0400 | [diff] [blame] | 199 | while (*buf) { |
| 200 | char *end = strchrnul(buf, '\n'); |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 201 | |
Jeff King | f972a16 | 2013-08-27 21:41:39 -0400 | [diff] [blame] | 202 | if (*end) |
| 203 | *end++ = '\0'; |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 204 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 205 | read_mailmap_line(map, buf); |
Jeff King | f972a16 | 2013-08-27 21:41:39 -0400 | [diff] [blame] | 206 | buf = end; |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 210 | static int read_mailmap_blob(struct string_list *map, const char *name) |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 211 | { |
brian m. carlson | 15be4a5 | 2017-07-13 23:49:27 +0000 | [diff] [blame] | 212 | struct object_id oid; |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 213 | char *buf; |
| 214 | unsigned long size; |
| 215 | enum object_type type; |
| 216 | |
| 217 | if (!name) |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 218 | return 0; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 219 | if (repo_get_oid(the_repository, name, &oid) < 0) |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 220 | return 0; |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 221 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 222 | buf = repo_read_object_file(the_repository, &oid, &type, &size); |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 223 | if (!buf) |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 224 | return error("unable to read mailmap object at %s", name); |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 225 | if (type != OBJ_BLOB) |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 226 | return error("mailmap is not a blob: %s", name); |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 227 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 228 | read_mailmap_string(map, buf); |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 229 | |
| 230 | free(buf); |
| 231 | return 0; |
| 232 | } |
| 233 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 234 | int read_mailmap(struct string_list *map) |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 235 | { |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 236 | int err = 0; |
Jeff King | 8c473ce | 2012-12-13 08:04:47 -0500 | [diff] [blame] | 237 | |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 238 | map->strdup_strings = 1; |
Junio C Hamano | de2f95e | 2013-09-12 08:37:08 -0700 | [diff] [blame] | 239 | map->cmp = namemap_cmp; |
Jeff King | 8c473ce | 2012-12-13 08:04:47 -0500 | [diff] [blame] | 240 | |
| 241 | if (!git_mailmap_blob && is_bare_repository()) |
| 242 | git_mailmap_blob = "HEAD:.mailmap"; |
| 243 | |
Jeff King | a38cb98 | 2021-02-10 15:34:33 -0500 | [diff] [blame] | 244 | if (!startup_info->have_repository || !is_bare_repository()) |
Junio C Hamano | 204333b | 2021-03-22 14:00:22 -0700 | [diff] [blame] | 245 | err |= read_mailmap_file(map, ".mailmap", |
| 246 | startup_info->have_repository ? |
| 247 | MAILMAP_NOFOLLOW : 0); |
Jeff King | 5735dc5 | 2016-03-05 17:13:29 -0500 | [diff] [blame] | 248 | if (startup_info->have_repository) |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 249 | err |= read_mailmap_blob(map, git_mailmap_blob); |
Jeff King | adcd9f5 | 2021-02-16 09:44:37 -0500 | [diff] [blame] | 250 | err |= read_mailmap_file(map, git_mailmap_file, 0); |
Jeff King | 938a60d | 2012-12-12 06:18:02 -0500 | [diff] [blame] | 251 | return err; |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 254 | void clear_mailmap(struct string_list *map) |
| 255 | { |
Ævar Arnfjörð Bjarmason | 99d6054 | 2022-03-07 16:27:08 +0100 | [diff] [blame] | 256 | debug_mm("mailmap: clearing %"PRIuMAX" entries...\n", |
| 257 | (uintmax_t)map->nr); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 258 | map->strdup_strings = 1; |
| 259 | string_list_clear_func(map, free_mailmap_entry); |
| 260 | debug_mm("mailmap: cleared\n"); |
| 261 | } |
| 262 | |
Junio C Hamano | 388c7f8 | 2013-01-05 22:26:39 +0100 | [diff] [blame] | 263 | /* |
| 264 | * Look for an entry in map that match string[0:len]; string[len] |
| 265 | * does not have to be NUL (but it could be). |
| 266 | */ |
| 267 | static struct string_list_item *lookup_prefix(struct string_list *map, |
| 268 | const char *string, size_t len) |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 269 | { |
Junio C Hamano | 388c7f8 | 2013-01-05 22:26:39 +0100 | [diff] [blame] | 270 | int i = string_list_find_insert_index(map, string, 1); |
| 271 | if (i < 0) { |
| 272 | /* exact match */ |
| 273 | i = -1 - i; |
| 274 | if (!string[len]) |
| 275 | return &map->items[i]; |
| 276 | /* |
| 277 | * that map entry matches exactly to the string, including |
| 278 | * the cruft at the end beyond "len". That is not a match |
| 279 | * with string[0:len] that we are looking for. |
| 280 | */ |
| 281 | } else if (!string[len]) { |
| 282 | /* |
| 283 | * asked with the whole string, and got nothing. No |
| 284 | * matching entry can exist in the map. |
| 285 | */ |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * i is at the exact match to an overlong key, or location the |
| 291 | * overlong key would be inserted, which must come after the |
| 292 | * real location of the key if one exists. |
| 293 | */ |
| 294 | while (0 <= --i && i < map->nr) { |
| 295 | int cmp = strncasecmp(map->items[i].string, string, len); |
| 296 | if (cmp < 0) |
| 297 | /* |
| 298 | * "i" points at a key definitely below the prefix; |
| 299 | * the map does not have string[0:len] in it. |
| 300 | */ |
| 301 | break; |
| 302 | else if (!cmp && !map->items[i].string[len]) |
| 303 | /* found it */ |
| 304 | return &map->items[i]; |
| 305 | /* |
| 306 | * otherwise, the string at "i" may be string[0:len] |
| 307 | * followed by a string that sorts later than string[len:]; |
| 308 | * keep trying. |
| 309 | */ |
| 310 | } |
| 311 | return NULL; |
| 312 | } |
| 313 | |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 314 | int map_user(struct string_list *map, |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 315 | const char **email, size_t *emaillen, |
| 316 | const char **name, size_t *namelen) |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 317 | { |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 318 | struct string_list_item *item; |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 319 | struct mailmap_entry *me; |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 320 | |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 321 | debug_mm("map_user: map '%.*s' <%.*s>\n", |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 322 | (int)*namelen, debug_str(*name), |
| 323 | (int)*emaillen, debug_str(*email)); |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 324 | |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 325 | item = lookup_prefix(map, *email, *emaillen); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 326 | if (item) { |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 327 | me = (struct mailmap_entry *)item->util; |
| 328 | if (me->namemap.nr) { |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 329 | /* |
| 330 | * The item has multiple items, so we'll look up on |
| 331 | * name too. If the name is not found, we choose the |
| 332 | * simple entry. |
| 333 | */ |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 334 | struct string_list_item *subitem; |
| 335 | subitem = lookup_prefix(&me->namemap, *name, *namelen); |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 336 | if (subitem) |
| 337 | item = subitem; |
| 338 | } |
| 339 | } |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 340 | if (item) { |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 341 | struct mailmap_info *mi = (struct mailmap_info *)item->util; |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 342 | if (mi->name == NULL && mi->email == NULL) { |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 343 | debug_mm("map_user: -- (no simple mapping)\n"); |
| 344 | return 0; |
| 345 | } |
Antoine Pelisse | ea02ffa | 2013-01-05 22:26:40 +0100 | [diff] [blame] | 346 | if (mi->email) { |
| 347 | *email = mi->email; |
| 348 | *emaillen = strlen(*email); |
| 349 | } |
| 350 | if (mi->name) { |
| 351 | *name = mi->name; |
| 352 | *namelen = strlen(*name); |
| 353 | } |
Junio C Hamano | bd23794 | 2013-07-15 02:54:13 -0400 | [diff] [blame] | 354 | debug_mm("map_user: to '%.*s' <%.*s>\n", |
| 355 | (int)*namelen, debug_str(*name), |
| 356 | (int)*emaillen, debug_str(*email)); |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 357 | return 1; |
| 358 | } |
Marius Storm-Olsen | 0925ce4 | 2009-02-08 15:34:29 +0100 | [diff] [blame] | 359 | debug_mm("map_user: --\n"); |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | } |