Coverage Report

Created: 2026-09-28 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/mailmap.c
Line
Count
Source
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
0
{
26
0
  struct mailmap_info *mi = (struct mailmap_info *)p;
27
0
  free(mi->name);
28
0
  free(mi->email);
29
0
  free(mi);
30
0
}
31
32
static void free_mailmap_entry(void *p, const char *s UNUSED)
33
0
{
34
0
  struct mailmap_entry *me = (struct mailmap_entry *)p;
35
36
0
  free(me->name);
37
0
  free(me->email);
38
39
0
  me->namemap.strdup_strings = 1;
40
0
  string_list_clear_func(&me->namemap, free_mailmap_info);
41
0
  free(me);
42
0
}
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
0
{
55
0
  return strcasecmp(a, b);
56
0
}
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
0
{
62
0
  struct mailmap_entry *me;
63
0
  struct string_list_item *item;
64
65
0
  if (!old_email) {
66
0
    old_email = new_email;
67
0
    new_email = NULL;
68
0
  }
69
70
0
  item = string_list_insert(map, old_email);
71
0
  if (item->util) {
72
0
    me = (struct mailmap_entry *)item->util;
73
0
  } else {
74
0
    CALLOC_ARRAY(me, 1);
75
0
    me->namemap.strdup_strings = 1;
76
0
    me->namemap.cmp = namemap_cmp;
77
0
    item->util = me;
78
0
  }
79
80
0
  if (!old_name) {
81
    /* Replace current name and new email for simple entry */
82
0
    if (new_name) {
83
0
      free(me->name);
84
0
      me->name = xstrdup(new_name);
85
0
    }
86
0
    if (new_email) {
87
0
      free(me->email);
88
0
      me->email = xstrdup(new_email);
89
0
    }
90
0
  } else {
91
0
    struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
92
0
    mi->name = xstrdup_or_null(new_name);
93
0
    mi->email = xstrdup_or_null(new_email);
94
0
    string_list_insert(&me->namemap, old_name)->util = mi;
95
0
  }
96
0
}
97
98
static char *parse_name_and_email(char *buffer, char **name,
99
          char **email, int allow_empty_email)
100
0
{
101
0
  char *left, *right, *nstart, *nend;
102
0
  *name = *email = NULL;
103
104
0
  if (!(left = strchr(buffer, '<')))
105
0
    return NULL;
106
0
  if (!(right = strchr(left + 1, '>')))
107
0
    return NULL;
108
0
  if (!allow_empty_email && (left+1 == right))
109
0
    return NULL;
110
111
  /* remove whitespace from beginning and end of name */
112
0
  nstart = buffer;
113
0
  while (isspace(*nstart) && nstart < left)
114
0
    ++nstart;
115
0
  nend = left-1;
116
0
  while (nend > nstart && isspace(*nend))
117
0
    --nend;
118
119
0
  *name = (nstart <= nend ? nstart : NULL);
120
0
  *email = left+1;
121
0
  *(nend+1) = '\0';
122
0
  *right++ = '\0';
123
124
0
  return (*right == '\0' ? NULL : right);
125
0
}
126
127
static void read_mailmap_line(struct string_list *map, char *buffer)
128
0
{
129
0
  char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
130
131
0
  if (buffer[0] == '#')
132
0
    return;
133
134
0
  if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
135
0
    parse_name_and_email(name2, &name2, &email2, 1);
136
137
0
  if (email1)
138
0
    add_mapping(map, name1, email1, name2, email2);
139
0
}
140
141
int read_mailmap_file(struct string_list *map, const char *filename,
142
          unsigned flags)
143
0
{
144
0
  char buffer[1024];
145
0
  FILE *f;
146
0
  int fd;
147
148
0
  if (!filename)
149
0
    return 0;
150
151
0
  if (flags & MAILMAP_NOFOLLOW)
152
0
    fd = open_nofollow(filename, O_RDONLY);
153
0
  else
154
0
    fd = open(filename, O_RDONLY);
155
156
0
  if (fd < 0) {
157
0
    if (errno == ENOENT)
158
0
      return 0;
159
0
    return error_errno("unable to open mailmap at %s", filename);
160
0
  }
161
0
  f = xfdopen(fd, "r");
162
163
0
  while (fgets(buffer, sizeof(buffer), f) != NULL)
164
0
    read_mailmap_line(map, buffer);
165
0
  fclose(f);
166
0
  return 0;
167
0
}
168
169
static void read_mailmap_string(struct string_list *map, char *buf)
170
0
{
171
0
  while (*buf) {
172
0
    char *end = strchrnul(buf, '\n');
173
174
0
    if (*end)
175
0
      *end++ = '\0';
176
177
0
    read_mailmap_line(map, buf);
178
0
    buf = end;
179
0
  }
180
0
}
181
182
int read_mailmap_blob(struct repository *repo, struct string_list *map,
183
          const char *name)
184
0
{
185
0
  struct object_id oid;
186
0
  char *buf;
187
0
  size_t size;
188
0
  enum object_type type;
189
190
0
  if (!name)
191
0
    return 0;
192
0
  if (repo_get_oid(repo, name, &oid) < 0)
193
0
    return 0;
194
195
0
  buf = odb_read_object(repo->objects, &oid, &type, &size);
196
0
  if (!buf)
197
0
    return error("unable to read mailmap object at %s", name);
198
0
  if (type != OBJ_BLOB) {
199
0
    free(buf);
200
0
    return error("mailmap is not a blob: %s", name);
201
0
  }
202
203
0
  read_mailmap_string(map, buf);
204
205
0
  free(buf);
206
0
  return 0;
207
0
}
208
209
int read_mailmap(struct repository *repo, struct string_list *map)
210
0
{
211
0
  int err = 0;
212
0
  char *mailmap_file = NULL, *mailmap_blob = NULL;
213
214
0
  repo_config_get_pathname(repo, "mailmap.file", &mailmap_file);
215
0
  repo_config_get_string(repo, "mailmap.blob", &mailmap_blob);
216
217
0
  map->strdup_strings = 1;
218
0
  map->cmp = namemap_cmp;
219
220
0
  if (!mailmap_blob && is_bare_repository(repo))
221
0
    mailmap_blob = xstrdup("HEAD:.mailmap");
222
223
0
  if (!startup_info->have_repository || !is_bare_repository(repo))
224
0
    err |= read_mailmap_file(map, ".mailmap",
225
0
           startup_info->have_repository ?
226
0
           MAILMAP_NOFOLLOW : 0);
227
0
  if (startup_info->have_repository)
228
0
    err |= read_mailmap_blob(repo, map, mailmap_blob);
229
230
0
  err |= read_mailmap_file(map, mailmap_file, 0);
231
232
0
  free(mailmap_file);
233
0
  free(mailmap_blob);
234
235
0
  return err;
236
0
}
237
238
void clear_mailmap(struct string_list *map)
239
0
{
240
0
  map->strdup_strings = 1;
241
0
  string_list_clear_func(map, free_mailmap_entry);
242
0
}
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
0
{
251
0
  bool exact_match;
252
0
  size_t i = string_list_find_insert_index(map, string, &exact_match);
253
0
  if (exact_match) {
254
0
    if (!string[len])
255
0
      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
0
  } 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
0
    return NULL;
267
0
  }
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
0
  while (i-- && i < map->nr) {
275
0
    int cmp = strncasecmp(map->items[i].string, string, len);
276
0
    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
0
      break;
282
0
    else if (!cmp && !map->items[i].string[len])
283
      /* found it */
284
0
      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
0
  }
291
0
  return NULL;
292
0
}
293
294
int map_user(struct string_list *map,
295
       const char **email, size_t *emaillen,
296
       const char **name, size_t *namelen)
297
0
{
298
0
  struct string_list_item *item;
299
0
  struct mailmap_entry *me;
300
301
0
  item = lookup_prefix(map, *email, *emaillen);
302
0
  if (item) {
303
0
    me = (struct mailmap_entry *)item->util;
304
0
    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
0
      struct string_list_item *subitem;
311
0
      subitem = lookup_prefix(&me->namemap, *name, *namelen);
312
0
      if (subitem)
313
0
        item = subitem;
314
0
    }
315
0
  }
316
0
  if (item) {
317
0
    struct mailmap_info *mi = (struct mailmap_info *)item->util;
318
0
    if (mi->name == NULL && mi->email == NULL)
319
0
      return 0;
320
0
    if (mi->email) {
321
0
        *email = mi->email;
322
0
        *emaillen = strlen(*email);
323
0
    }
324
0
    if (mi->name) {
325
0
        *name = mi->name;
326
0
        *namelen = strlen(*name);
327
0
    }
328
0
    return 1;
329
0
  }
330
0
  return 0;
331
0
}