Skip to content

Commit 8ff0504

Browse files
committed
mailmap: Rewrite API to support accurate mailmap resolution
1 parent 18ff9ba commit 8ff0504

File tree

5 files changed

+344
-208
lines changed

5 files changed

+344
-208
lines changed

include/git2/mailmap.h

Lines changed: 60 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -21,103 +21,93 @@
2121
GIT_BEGIN_DECL
2222

2323
/**
24-
* A single entry parsed from a mailmap.
25-
*/
26-
typedef struct git_mailmap_entry {
27-
unsigned int version;
28-
29-
const char *real_name; /**< the real name (may be NULL) */
30-
const char *real_email; /**< the real email (may be NULL) */
31-
const char *replace_name; /**< the name to replace (may be NULL) */
32-
const char *replace_email; /**< the email to replace */
33-
} git_mailmap_entry;
34-
35-
#define GIT_MAILMAP_ENTRY_VERSION 1
36-
#define GIT_MAILMAP_ENTRY_INIT {GIT_MAILMAP_ENTRY_VERSION}
37-
38-
/**
39-
* Create a mailmap object by parsing a mailmap file. Malformed entries in the
40-
* mailmap are ignored.
24+
* Allocate a new mailmap object.
4125
*
42-
* The mailmap must be freed with 'git_mailmap_free'.
26+
* This object is empty, so you'll have to add a mailmap file before you can do
27+
* anything with it. The mailmap must be freed with 'git_mailmap_free'.
4328
*
44-
* @param out pointer to store the mailmap
45-
* @param buffer buffer to parse the mailmap from
46-
* @return 0 on success, otherwise an error code
29+
* @param out pointer to store the new mailmap
30+
* @return 0 on success, or an error code
4731
*/
48-
GIT_EXTERN(int) git_mailmap_from_buffer(git_mailmap **out, git_buf *buffer);
32+
GIT_EXTERN(int) git_mailmap_new(git_mailmap **out);
4933

5034
/**
51-
* Create a mailmap object from the given repository. Malformed entries in the
52-
* mailmap are ignored.
53-
*
54-
* If the repository is not bare, the repository's working directory root will
55-
* be checked for the '.mailmap' file to be parsed.
56-
*
57-
* If the repository is bare, the repository's HEAD commit's tree root will be
58-
* searched for the '.mailmap' file to be parsed.
59-
*
60-
* The mailmap must be freed with 'git_mailmap_free'.
35+
* Free the mailmap and its associated memory.
6136
*
62-
* @param out pointer to store the mailmap
63-
* @param repo repository to find the .mailmap in
64-
* @return 0 on success; GIT_ENOTFOUND if .mailmap could not be found,
65-
* otherwise an error code.
37+
* @param mm the mailmap to free
6638
*/
67-
GIT_EXTERN(int) git_mailmap_from_repo(git_mailmap **out, git_repository *repo);
39+
GIT_EXTERN(void) git_mailmap_free(git_mailmap *mm);
6840

6941
/**
70-
* Free a mailmap created by 'git_mailmap_from_buffer' or
71-
* 'git_mailmap_from_repo'.
42+
* Add a single entry to the given mailmap object. If the entry already exists,
43+
* it will be replaced with the new entry.
44+
*
45+
* @param mm mailmap to add the entry to
46+
* @param real_name the real name to use, or NULL
47+
* @param real_email the real email to use, or NULL
48+
* @param replace_name the name to replace, or NULL
49+
* @param replace_email the email to replace
50+
* @return 0 if it was added, EEXISTS if it replaced an entry, or an error code
7251
*/
73-
GIT_EXTERN(void) git_mailmap_free(git_mailmap *mailmap);
52+
GIT_EXTERN(int) git_mailmap_add_entry(
53+
git_mailmap *mm, const char *real_name, const char *real_email,
54+
const char *replace_name, const char *replace_email);
7455

7556
/**
76-
* Resolve a name and email to the corresponding real name and email.
57+
* Parse mailmap entries from a buffer.
7758
*
78-
* The lifetime of the string results is tied to the `mailmap`, `name`, and
79-
* `email` parameters.
80-
*
81-
* @param name_out either 'name', or the real name to use.
82-
* You should NOT free this value.
83-
* @param email_out either 'email' or the real email to use,
84-
* You should NOT free this value.
85-
* @param mailmap the mailmap to perform the lookup in. (may be NULL)
86-
* @param name the name to resolve.
87-
* @param email the email to resolve.
88-
* @return 0 on success, otherwise an error code.
59+
* @param mm mailmap to add the entries to
60+
* @param buf the buffer to read the mailmap file from
61+
* @return 0 on success, or an error code
8962
*/
90-
GIT_EXTERN(int) git_mailmap_resolve(
91-
const char **name_out, const char **email_out,
92-
const git_mailmap *mailmap, const char *name, const char *email);
63+
GIT_EXTERN(int) git_mailmap_add_buffer(git_mailmap *mm, const git_buf *buf);
9364

9465
/**
95-
* Get the number of mailmap entries in this mailmap.
66+
* Create a new mailmap instance containing a single mailmap file
67+
*
68+
* This method is a simple utility wrapper for the following sequence
69+
* of calls:
70+
* - git_mailmap_new
71+
* - git_mailmap_add_buffer
72+
*
73+
* @param out pointer to store the new mailmap
74+
* @param buf buffer to parse the mailmap from
75+
* @return 0 on success, or an error code
9676
*/
97-
GIT_EXTERN(size_t) git_mailmap_entry_count(const git_mailmap *mailmap);
77+
GIT_EXTERN(int) git_mailmap_from_buffer(git_mailmap **out, const git_buf *buf);
9878

9979
/**
100-
* Lookup a mailmap entry by index.
80+
* Create a new mailmap instance from a repository, loading mailmap files based
81+
* on the repository's configuration.
10182
*
102-
* Do not free the mailmap entry, it is owned by the mailmap.
83+
* Mailmaps are loaded in the following order:
84+
* 1. '.mailmap' in the root of the repository's working directory, if present.
85+
* 2. The blob object identified by the 'mailmap.blob' config entry, if set.
86+
* [NOTE: 'mailmap.blob' defaults to 'HEAD:.mailmap' in bare repositories]
87+
* 3. The path in the 'mailmap.file' config entry, if set.
10388
*
104-
* @return the mailmap entry at index, or NULL if it cannot be found.
89+
* @param out pointer to store the new mailmap
90+
* @param repo repository to load mailmap information from
91+
* @return 0 on success, or an error code
10592
*/
106-
GIT_EXTERN(const git_mailmap_entry *) git_mailmap_entry_byindex(
107-
const git_mailmap *mailmap, size_t idx);
93+
GIT_EXTERN(int) git_mailmap_from_repository(
94+
git_mailmap **out, git_repository *repo);
10895

10996
/**
110-
* Lookup a mailmap entry by name/email pair.
97+
* Resolve a name and email to the corresponding real name and email.
11198
*
112-
* Do not free the mailmap entry, it is owned by the mailmap.
99+
* The lifetime of the strings are tied to `mm`, `name`, and `email` parameters.
113100
*
114-
* @param mailmap the mailmap to perform the lookup in. (may be NULL)
115-
* @param name the name to perform the lookup with.
116-
* @param email the email to perform the lookup with.
117-
* @return the corresponding mailmap entry, or NULL if it cannot be found.
101+
* @param real_name pointer to store the real name
102+
* @param real_email pointer to store the real email
103+
* @param mm the mailmap to perform a lookup with (may be NULL)
104+
* @param name the name to look up
105+
* @param email the email to look up
106+
* @return 0 on success, or an error code
118107
*/
119-
GIT_EXTERN(const git_mailmap_entry *) git_mailmap_entry_lookup(
120-
const git_mailmap *mailmap, const char *name, const char *email);
108+
GIT_EXTERN(int) git_mailmap_resolve(
109+
const char **real_name, const char **real_email,
110+
const git_mailmap *mm, const char *name, const char *email);
121111

122112
/** @} */
123113
GIT_END_DECL

src/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ git_blame* git_blame__alloc(
134134
}
135135

136136
if (opts.flags & GIT_BLAME_USE_MAILMAP)
137-
git_mailmap_from_repo(&gbr->mailmap, repo);
137+
git_mailmap_from_repository(&gbr->mailmap, repo);
138138

139139
return gbr;
140140
}

0 commit comments

Comments
 (0)