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