Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/mailmap.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / mailmap.c` 331 lines ยท 7811 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "environment.h" 3 #include "string-list.h" 4 #include "mailmap.h" 5 #include "object-name.h" 6 #include "odb.h" 7 #include "setup.h" 8 #include "config.h" 9 10 struct mailmap_info { 11 char *name; 12 char *email; 13 }; 14 15 struct mailmap_entry { 16 /* name and email for the simple mail-only case */ 17 char *name; 18 char *email; 19 20 /* name and email for the complex mail and name matching case */ 21 struct string_list namemap; 22 }; 23 24 static void free_mailmap_info(void *p, const char *s UNUSED) 25 { 26 struct mailmap_info *mi = (struct mailmap_info *)p; 27 free(mi->name); 28 free(mi->email); 29 free(mi); 30 } 31 32 static void free_mailmap_entry(void *p, const char *s UNUSED) 33 { 34 struct mailmap_entry *me = (struct mailmap_entry *)p; 35 36 free(me->name); 37 free(me->email); 38 39 me->namemap.strdup_strings = 1; 40 string_list_clear_func(&me->namemap, free_mailmap_info); 41 free(me); 42 } 43 44 /* 45 * On some systems (e.g. MinGW 4.0), string.h has _only_ inline 46 * definition of strcasecmp and no non-inline implementation is 47 * supplied anywhere, which is, eh, "unusual"; we cannot take an 48 * address of such a function to store it in namemap.cmp. This is 49 * here as a workaround---do not assign strcasecmp directly to 50 * namemap.cmp until we know no systems that matter have such an 51 * "unusual" string.h. 52 */ 53 static int namemap_cmp(const char *a, const char *b) 54 { 55 return strcasecmp(a, b); 56 } 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 65 if (!old_email) { 66 old_email = new_email; 67 new_email = NULL; 68 } 69 70 item = string_list_insert(map, old_email); 71 if (item->util) { 72 me = (struct mailmap_entry *)item->util; 73 } else { 74 CALLOC_ARRAY(me, 1); 75 me->namemap.strdup_strings = 1; 76 me->namemap.cmp = namemap_cmp; 77 item->util = me; 78 } 79 80 if (!old_name) { 81 /* Replace current name and new email for simple entry */ 82 if (new_name) { 83 free(me->name); 84 me->name = xstrdup(new_name); 85 } 86 if (new_email) { 87 free(me->email); 88 me->email = xstrdup(new_email); 89 } 90 } else { 91 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info)); 92 mi->name = xstrdup_or_null(new_name); 93 mi->email = xstrdup_or_null(new_email); 94 string_list_insert(&me->namemap, old_name)->util = mi; 95 } 96 } 97 98 static char *parse_name_and_email(char *buffer, char **name, 99 char **email, int allow_empty_email) 100 { 101 char *left, *right, *nstart, *nend; 102 *name = *email = NULL; 103 104 if (!(left = strchr(buffer, '<'))) 105 return NULL; 106 if (!(right = strchr(left + 1, '>'))) 107 return NULL; 108 if (!allow_empty_email && (left+1 == right)) 109 return NULL; 110 111 /* remove whitespace from beginning and end of name */ 112 nstart = buffer; 113 while (isspace(*nstart) && nstart < left) 114 ++nstart; 115 nend = left-1; 116 while (nend > nstart && isspace(*nend)) 117 --nend; 118 119 *name = (nstart <= nend ? nstart : NULL); 120 *email = left+1; 121 *(nend+1) = '\0'; 122 *right++ = '\0'; 123 124 return (*right == '\0' ? NULL : right); 125 } 126 127 static void read_mailmap_line(struct string_list *map, char *buffer) 128 { 129 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL; 130 131 if (buffer[0] == '#') 132 return; 133 134 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0))) 135 parse_name_and_email(name2, &name2, &email2, 1); 136 137 if (email1) 138 add_mapping(map, name1, email1, name2, email2); 139 } 140 141 int read_mailmap_file(struct string_list *map, const char *filename, 142 unsigned flags) 143 { 144 char buffer[1024]; 145 FILE *f; 146 int fd; 147 148 if (!filename) 149 return 0; 150 151 if (flags & MAILMAP_NOFOLLOW) 152 fd = open_nofollow(filename, O_RDONLY); 153 else 154 fd = open(filename, O_RDONLY); 155 156 if (fd < 0) { 157 if (errno == ENOENT) 158 return 0; 159 return error_errno("unable to open mailmap at %s", filename); 160 } 161 f = xfdopen(fd, "r"); 162 163 while (fgets(buffer, sizeof(buffer), f) != NULL) 164 read_mailmap_line(map, buffer); 165 fclose(f); 166 return 0; 167 } 168 169 static void read_mailmap_string(struct string_list *map, char *buf) 170 { 171 while (*buf) { 172 char *end = strchrnul(buf, '\n'); 173 174 if (*end) 175 *end++ = '\0'; 176 177 read_mailmap_line(map, buf); 178 buf = end; 179 } 180 } 181 182 int read_mailmap_blob(struct repository *repo, struct string_list *map, 183 const char *name) 184 { 185 struct object_id oid; 186 char *buf; 187 size_t size; 188 enum object_type type; 189 190 if (!name) 191 return 0; 192 if (repo_get_oid(repo, name, &oid) < 0) 193 return 0; 194 195 buf = odb_read_object(repo->objects, &oid, &type, &size); 196 if (!buf) 197 return error("unable to read mailmap object at %s", name); 198 if (type != OBJ_BLOB) { 199 free(buf); 200 return error("mailmap is not a blob: %s", name); 201 } 202 203 read_mailmap_string(map, buf); 204 205 free(buf); 206 return 0; 207 } 208 209 int read_mailmap(struct repository *repo, struct string_list *map) 210 { 211 int err = 0; 212 char *mailmap_file = NULL, *mailmap_blob = NULL; 213 214 repo_config_get_pathname(repo, "mailmap.file", &mailmap_file); 215 repo_config_get_string(repo, "mailmap.blob", &mailmap_blob); 216 217 map->strdup_strings = 1; 218 map->cmp = namemap_cmp; 219 220 if (!mailmap_blob && is_bare_repository(repo)) 221 mailmap_blob = xstrdup("HEAD:.mailmap"); 222 223 if (!startup_info->have_repository || !is_bare_repository(repo)) 224 err |= read_mailmap_file(map, ".mailmap", 225 startup_info->have_repository ? 226 MAILMAP_NOFOLLOW : 0); 227 if (startup_info->have_repository) 228 err |= read_mailmap_blob(repo, map, mailmap_blob); 229 230 err |= read_mailmap_file(map, mailmap_file, 0); 231 232 free(mailmap_file); 233 free(mailmap_blob); 234 235 return err; 236 } 237 238 void clear_mailmap(struct string_list *map) 239 { 240 map->strdup_strings = 1; 241 string_list_clear_func(map, free_mailmap_entry); 242 } 243 244 /* 245 * Look for an entry in map that match string[0:len]; string[len] 246 * does not have to be NUL (but it could be). 247 */ 248 static struct string_list_item *lookup_prefix(struct string_list *map, 249 const char *string, size_t len) 250 { 251 bool exact_match; 252 size_t i = string_list_find_insert_index(map, string, &exact_match); 253 if (exact_match) { 254 if (!string[len]) 255 return &map->items[i]; 256 /* 257 * that map entry matches exactly to the string, including 258 * the cruft at the end beyond "len". That is not a match 259 * with string[0:len] that we are looking for. 260 */ 261 } else if (!string[len]) { 262 /* 263 * asked with the whole string, and got nothing. No 264 * matching entry can exist in the map. 265 */ 266 return NULL; 267 } 268 269 /* 270 * i is at the exact match to an overlong key, or location the 271 * overlong key would be inserted, which must come after the 272 * real location of the key if one exists. 273 */ 274 while (i-- && i < map->nr) { 275 int cmp = strncasecmp(map->items[i].string, string, len); 276 if (cmp < 0) 277 /* 278 * "i" points at a key definitely below the prefix; 279 * the map does not have string[0:len] in it. 280 */ 281 break; 282 else if (!cmp && !map->items[i].string[len]) 283 /* found it */ 284 return &map->items[i]; 285 /* 286 * otherwise, the string at "i" may be string[0:len] 287 * followed by a string that sorts later than string[len:]; 288 * keep trying. 289 */ 290 } 291 return NULL; 292 } 293 294 int map_user(struct string_list *map, 295 const char **email, size_t *emaillen, 296 const char **name, size_t *namelen) 297 { 298 struct string_list_item *item; 299 struct mailmap_entry *me; 300 301 item = lookup_prefix(map, *email, *emaillen); 302 if (item) { 303 me = (struct mailmap_entry *)item->util; 304 if (me->namemap.nr) { 305 /* 306 * The item has multiple items, so we'll look up on 307 * name too. If the name is not found, we choose the 308 * simple entry. 309 */ 310 struct string_list_item *subitem; 311 subitem = lookup_prefix(&me->namemap, *name, *namelen); 312 if (subitem) 313 item = subitem; 314 } 315 } 316 if (item) { 317 struct mailmap_info *mi = (struct mailmap_info *)item->util; 318 if (mi->name == NULL && mi->email == NULL) 319 return 0; 320 if (mi->email) { 321 *email = mi->email; 322 *emaillen = strlen(*email); 323 } 324 if (mi->name) { 325 *name = mi->name; 326 *namelen = strlen(*name); 327 } 328 return 1; 329 } 330 return 0; 331 }