Coverage Report

Created: 2026-03-21 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/mailmap.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "environment.h"
5
#include "string-list.h"
6
#include "mailmap.h"
7
#include "object-name.h"
8
#include "odb.h"
9
#include "setup.h"
10
#include "config.h"
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
26
static void free_mailmap_info(void *p, const char *s UNUSED)
27
0
{
28
0
  struct mailmap_info *mi = (struct mailmap_info *)p;
29
0
  free(mi->name);
30
0
  free(mi->email);
31
0
  free(mi);
32
0
}
33
34
static void free_mailmap_entry(void *p, const char *s UNUSED)
35
0
{
36
0
  struct mailmap_entry *me = (struct mailmap_entry *)p;
37
38
0
  free(me->name);
39
0
  free(me->email);
40
41
0
  me->namemap.strdup_strings = 1;
42
0
  string_list_clear_func(&me->namemap, free_mailmap_info);
43
0
  free(me);
44
0
}
45
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
0
{
57
0
  return strcasecmp(a, b);
58
0
}
59
60
static void add_mapping(struct string_list *map,
61
      char *new_name, char *new_email,
62
      char *old_name, char *old_email)
63
0
{
64
0
  struct mailmap_entry *me;
65
0
  struct string_list_item *item;
66
67
0
  if (!old_email) {
68
0
    old_email = new_email;
69
0
    new_email = NULL;
70
0
  }
71
72
0
  item = string_list_insert(map, old_email);
73
0
  if (item->util) {
74
0
    me = (struct mailmap_entry *)item->util;
75
0
  } else {
76
0
    CALLOC_ARRAY(me, 1);
77
0
    me->namemap.strdup_strings = 1;
78
0
    me->namemap.cmp = namemap_cmp;
79
0
    item->util = me;
80
0
  }
81
82
0
  if (!old_name) {
83
    /* Replace current name and new email for simple entry */
84
0
    if (new_name) {
85
0
      free(me->name);
86
0
      me->name = xstrdup(new_name);
87
0
    }
88
0
    if (new_email) {
89
0
      free(me->email);
90
0
      me->email = xstrdup(new_email);
91
0
    }
92
0
  } else {
93
0
    struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
94
0
    mi->name = xstrdup_or_null(new_name);
95
0
    mi->email = xstrdup_or_null(new_email);
96
0
    string_list_insert(&me->namemap, old_name)->util = mi;
97
0
  }
98
0
}
99
100
static char *parse_name_and_email(char *buffer, char **name,
101
          char **email, int allow_empty_email)
102
0
{
103
0
  char *left, *right, *nstart, *nend;
104
0
  *name = *email = NULL;
105
106
0
  if (!(left = strchr(buffer, '<')))
107
0
    return NULL;
108
0
  if (!(right = strchr(left + 1, '>')))
109
0
    return NULL;
110
0
  if (!allow_empty_email && (left+1 == right))
111
0
    return NULL;
112
113
  /* remove whitespace from beginning and end of name */
114
0
  nstart = buffer;
115
0
  while (isspace(*nstart) && nstart < left)
116
0
    ++nstart;
117
0
  nend = left-1;
118
0
  while (nend > nstart && isspace(*nend))
119
0
    --nend;
120
121
0
  *name = (nstart <= nend ? nstart : NULL);
122
0
  *email = left+1;
123
0
  *(nend+1) = '\0';
124
0
  *right++ = '\0';
125
126
0
  return (*right == '\0' ? NULL : right);
127
0
}
128
129
static void read_mailmap_line(struct string_list *map, char *buffer)
130
0
{
131
0
  char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
132
133
0
  if (buffer[0] == '#')
134
0
    return;
135
136
0
  if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
137
0
    parse_name_and_email(name2, &name2, &email2, 1);
138
139
0
  if (email1)
140
0
    add_mapping(map, name1, email1, name2, email2);
141
0
}
142
143
int read_mailmap_file(struct string_list *map, const char *filename,
144
          unsigned flags)
145
0
{
146
0
  char buffer[1024];
147
0
  FILE *f;
148
0
  int fd;
149
150
0
  if (!filename)
151
0
    return 0;
152
153
0
  if (flags & MAILMAP_NOFOLLOW)
154
0
    fd = open_nofollow(filename, O_RDONLY);
155
0
  else
156
0
    fd = open(filename, O_RDONLY);
157
158
0
  if (fd < 0) {
159
0
    if (errno == ENOENT)
160
0
      return 0;
161
0
    return error_errno("unable to open mailmap at %s", filename);
162
0
  }
163
0
  f = xfdopen(fd, "r");
164
165
0
  while (fgets(buffer, sizeof(buffer), f) != NULL)
166
0
    read_mailmap_line(map, buffer);
167
0
  fclose(f);
168
0
  return 0;
169
0
}
170
171
static void read_mailmap_string(struct string_list *map, char *buf)
172
0
{
173
0
  while (*buf) {
174
0
    char *end = strchrnul(buf, '\n');
175
176
0
    if (*end)
177
0
      *end++ = '\0';
178
179
0
    read_mailmap_line(map, buf);
180
0
    buf = end;
181
0
  }
182
0
}
183
184
int read_mailmap_blob(struct repository *repo, struct string_list *map,
185
          const char *name)
186
0
{
187
0
  struct object_id oid;
188
0
  char *buf;
189
0
  unsigned long size;
190
0
  enum object_type type;
191
192
0
  if (!name)
193
0
    return 0;
194
0
  if (repo_get_oid(repo, name, &oid) < 0)
195
0
    return 0;
196
197
0
  buf = odb_read_object(repo->objects, &oid, &type, &size);
198
0
  if (!buf)
199
0
    return error("unable to read mailmap object at %s", name);
200
0
  if (type != OBJ_BLOB) {
201
0
    free(buf);
202
0
    return error("mailmap is not a blob: %s", name);
203
0
  }
204
205
0
  read_mailmap_string(map, buf);
206
207
0
  free(buf);
208
0
  return 0;
209
0
}
210
211
int read_mailmap(struct repository *repo, struct string_list *map)
212
0
{
213
0
  int err = 0;
214
0
  char *mailmap_file = NULL, *mailmap_blob = NULL;
215
216
0
  repo_config_get_pathname(repo, "mailmap.file", &mailmap_file);
217
0
  repo_config_get_string(repo, "mailmap.blob", &mailmap_blob);
218
219
0
  map->strdup_strings = 1;
220
0
  map->cmp = namemap_cmp;
221
222
0
  if (!mailmap_blob && is_bare_repository())
223
0
    mailmap_blob = xstrdup("HEAD:.mailmap");
224
225
0
  if (!startup_info->have_repository || !is_bare_repository())
226
0
    err |= read_mailmap_file(map, ".mailmap",
227
0
           startup_info->have_repository ?
228
0
           MAILMAP_NOFOLLOW : 0);
229
0
  if (startup_info->have_repository)
230
0
    err |= read_mailmap_blob(repo, map, mailmap_blob);
231
232
0
  err |= read_mailmap_file(map, mailmap_file, 0);
233
234
0
  free(mailmap_file);
235
0
  free(mailmap_blob);
236
237
0
  return err;
238
0
}
239
240
void clear_mailmap(struct string_list *map)
241
0
{
242
0
  map->strdup_strings = 1;
243
0
  string_list_clear_func(map, free_mailmap_entry);
244
0
}
245
246
/*
247
 * Look for an entry in map that match string[0:len]; string[len]
248
 * does not have to be NUL (but it could be).
249
 */
250
static struct string_list_item *lookup_prefix(struct string_list *map,
251
                const char *string, size_t len)
252
0
{
253
0
  bool exact_match;
254
0
  size_t i = string_list_find_insert_index(map, string, &exact_match);
255
0
  if (exact_match) {
256
0
    if (!string[len])
257
0
      return &map->items[i];
258
    /*
259
     * that map entry matches exactly to the string, including
260
     * the cruft at the end beyond "len".  That is not a match
261
     * with string[0:len] that we are looking for.
262
     */
263
0
  } else if (!string[len]) {
264
    /*
265
     * asked with the whole string, and got nothing.  No
266
     * matching entry can exist in the map.
267
     */
268
0
    return NULL;
269
0
  }
270
271
  /*
272
   * i is at the exact match to an overlong key, or location the
273
   * overlong key would be inserted, which must come after the
274
   * real location of the key if one exists.
275
   */
276
0
  while (i-- && i < map->nr) {
277
0
    int cmp = strncasecmp(map->items[i].string, string, len);
278
0
    if (cmp < 0)
279
      /*
280
       * "i" points at a key definitely below the prefix;
281
       * the map does not have string[0:len] in it.
282
       */
283
0
      break;
284
0
    else if (!cmp && !map->items[i].string[len])
285
      /* found it */
286
0
      return &map->items[i];
287
    /*
288
     * otherwise, the string at "i" may be string[0:len]
289
     * followed by a string that sorts later than string[len:];
290
     * keep trying.
291
     */
292
0
  }
293
0
  return NULL;
294
0
}
295
296
int map_user(struct string_list *map,
297
       const char **email, size_t *emaillen,
298
       const char **name, size_t *namelen)
299
0
{
300
0
  struct string_list_item *item;
301
0
  struct mailmap_entry *me;
302
303
0
  item = lookup_prefix(map, *email, *emaillen);
304
0
  if (item) {
305
0
    me = (struct mailmap_entry *)item->util;
306
0
    if (me->namemap.nr) {
307
      /*
308
       * The item has multiple items, so we'll look up on
309
       * name too. If the name is not found, we choose the
310
       * simple entry.
311
       */
312
0
      struct string_list_item *subitem;
313
0
      subitem = lookup_prefix(&me->namemap, *name, *namelen);
314
0
      if (subitem)
315
0
        item = subitem;
316
0
    }
317
0
  }
318
0
  if (item) {
319
0
    struct mailmap_info *mi = (struct mailmap_info *)item->util;
320
0
    if (mi->name == NULL && mi->email == NULL)
321
0
      return 0;
322
0
    if (mi->email) {
323
0
        *email = mi->email;
324
0
        *emaillen = strlen(*email);
325
0
    }
326
0
    if (mi->name) {
327
0
        *name = mi->name;
328
0
        *namelen = strlen(*name);
329
0
    }
330
0
    return 1;
331
0
  }
332
0
  return 0;
333
0
}