Skip to content

Commit e3dcaca

Browse files
committed
mailmap: Integrate mailmaps with blame and signatures
1 parent b05fbba commit e3dcaca

File tree

8 files changed

+117
-4
lines changed

8 files changed

+117
-4
lines changed

include/git2/blame.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ typedef enum {
4343
/** Restrict the search of commits to those reachable following only the
4444
* first parents. */
4545
GIT_BLAME_FIRST_PARENT = (1<<4),
46+
/** Use mailmap file to map author and committer names and email addresses
47+
* to canonical real names and email addresses. The mailmap will be read
48+
* from the working directory, or HEAD in a bare repository. */
49+
GIT_BLAME_USE_MAILMAP = (1<<5),
4650
} git_blame_flag_t;
4751

4852
/**
@@ -108,6 +112,9 @@ GIT_EXTERN(int) git_blame_init_options(
108112
* changed.
109113
* - `final_start_line_number` is the 1-based line number where this hunk
110114
* begins, in the final version of the file
115+
* - `final_signature` is the author of `final_commit_id`. If
116+
* `GIT_BLAME_USE_MAILMAP` has been specified, it will contain the canonical
117+
* real name and email address.
111118
* - `orig_commit_id` is the OID of the commit where this hunk was found. This
112119
* will usually be the same as `final_commit_id`, except when
113120
* `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified.
@@ -116,6 +123,9 @@ GIT_EXTERN(int) git_blame_init_options(
116123
* - `orig_start_line_number` is the 1-based line number where this hunk begins
117124
* in the file named by `orig_path` in the commit specified by
118125
* `orig_commit_id`.
126+
* - `orig_signature` is the author of `orig_commit_id`. If
127+
* `GIT_BLAME_USE_MAILMAP` has been specified, it will contain the canonical
128+
* real name and email address.
119129
* - `boundary` is 1 iff the hunk has been tracked to a boundary commit (the
120130
* root, or the commit specified in git_blame_options.oldest_commit)
121131
*/

include/git2/commit.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit)
172172
*/
173173
GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit);
174174

175+
/**
176+
* Get the committer of a commit, using the mailmap to map names and email
177+
* addresses to canonical real names and email addresses.
178+
*
179+
* Call `git_signature_free` to free the signature.
180+
*
181+
* @param out a pointer to store the resolved signature.
182+
* @param commit a previously loaded commit.
183+
* @param mailmap the mailmap to resolve with. (may be NULL)
184+
* @return 0 or an error code
185+
*/
186+
GIT_EXTERN(int) git_commit_committer_with_mailmap(
187+
git_signature **out, const git_commit *commit, const git_mailmap *mailmap);
188+
189+
/**
190+
* Get the author of a commit, using the mailmap to map names and email
191+
* addresses to canonical real names and email addresses.
192+
*
193+
* Call `git_signature_free` to free the signature.
194+
*
195+
* @param out a pointer to store the resolved signature.
196+
* @param commit a previously loaded commit.
197+
* @param mailmap the mailmap to resolve with. (may be NULL)
198+
* @return 0 or an error code
199+
*/
200+
GIT_EXTERN(int) git_commit_author_with_mailmap(
201+
git_signature **out, const git_commit *commit, const git_mailmap *mailmap);
202+
175203
/**
176204
* Get the full raw text of the commit header.
177205
*

include/git2/signature.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo)
7575
*/
7676
GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf);
7777

78+
/**
79+
* Create a signature with names updated respecting the mailmap.
80+
*
81+
* Call `git_signature_free()` to free the data.
82+
*
83+
* @param out new signature
84+
* @param sig signature to resolve
85+
* @param mailmap mailmap to resolve with
86+
* @return 0 or an error code
87+
*/
88+
GIT_EXTERN(int) git_signature_with_mailmap(
89+
git_signature **out, const git_signature *sig, const git_mailmap *mailmap);
90+
7891
/**
7992
* Create a copy of an existing signature. All internal strings are also
8093
* duplicated.

include/git2/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ struct git_writestream {
434434
void (*free)(git_writestream *stream);
435435
};
436436

437+
/** A parsed representation of a .mailmap file. */
438+
typedef struct git_mailmap git_mailmap;
439+
437440
/** @} */
438441
GIT_END_DECL
439442

src/blame.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ git_blame* git_blame__alloc(
132132
return NULL;
133133
}
134134

135+
if (opts.flags & GIT_BLAME_USE_MAILMAP)
136+
git_mailmap_from_repo(&gbr->mailmap, repo);
137+
135138
return gbr;
136139
}
137140

@@ -150,6 +153,8 @@ void git_blame_free(git_blame *blame)
150153

151154
git_array_clear(blame->line_index);
152155

156+
git_mailmap_free(blame->mailmap);
157+
153158
git__free(blame->path);
154159
git_blob_free(blame->final_blob);
155160
git__free(blame);
@@ -279,7 +284,7 @@ static int index_blob_lines(git_blame *blame)
279284
return blame->num_lines;
280285
}
281286

282-
static git_blame_hunk* hunk_from_entry(git_blame__entry *e)
287+
static git_blame_hunk* hunk_from_entry(git_blame__entry *e, git_blame *blame)
283288
{
284289
git_blame_hunk *h = new_hunk(
285290
e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path);
@@ -289,8 +294,9 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e)
289294

290295
git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit));
291296
git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit));
292-
git_signature_dup(&h->final_signature, git_commit_author(e->suspect->commit));
293-
git_signature_dup(&h->orig_signature, git_commit_author(e->suspect->commit));
297+
git_commit_author_with_mailmap(
298+
&h->final_signature, e->suspect->commit, blame->mailmap);
299+
git_signature_dup(&h->orig_signature, h->final_signature);
294300
h->boundary = e->is_boundary ? 1 : 0;
295301
return h;
296302
}
@@ -341,7 +347,7 @@ static int blame_internal(git_blame *blame)
341347
cleanup:
342348
for (ent = blame->ent; ent; ) {
343349
git_blame__entry *e = ent->next;
344-
git_blame_hunk *h = hunk_from_entry(ent);
350+
git_blame_hunk *h = hunk_from_entry(ent, blame);
345351

346352
git_vector_insert(&blame->hunks, h);
347353

src/blame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ struct git_blame {
8484
int num_lines;
8585
const char *final_buf;
8686
git_off_t final_buf_size;
87+
88+
git_mailmap *mailmap;
8789
};
8890

8991
git_blame *git_blame__alloc(

src/commit.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,3 +889,15 @@ int git_commit_create_with_signature(
889889
git_buf_dispose(&commit);
890890
return error;
891891
}
892+
893+
int git_commit_committer_with_mailmap(
894+
git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
895+
{
896+
return git_signature_with_mailmap(out, commit->committer, mailmap);
897+
}
898+
899+
int git_commit_author_with_mailmap(
900+
git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
901+
{
902+
return git_signature_with_mailmap(out, commit->author, mailmap);
903+
}

src/signature.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "repository.h"
1111
#include "git2/common.h"
12+
#include "git2/mailmap.h"
1213
#include "posix.h"
1314

1415
void git_signature_free(git_signature *sig)
@@ -121,6 +122,44 @@ int git_signature_dup(git_signature **dest, const git_signature *source)
121122
return 0;
122123
}
123124

125+
int git_signature_with_mailmap(
126+
git_signature **dest,
127+
const git_signature *source,
128+
const git_mailmap *mailmap)
129+
{
130+
git_signature *signature = NULL;
131+
const char *name = NULL;
132+
const char *email = NULL;
133+
134+
if (source == NULL)
135+
goto on_error;
136+
137+
git_mailmap_resolve(&name, &email, mailmap, source->name, source->email);
138+
139+
signature = git__calloc(1, sizeof(git_signature));
140+
if (!signature)
141+
goto on_error;
142+
143+
signature->name = git__strdup(name);
144+
if (!signature->name)
145+
goto on_error;
146+
147+
signature->email = git__strdup(email);
148+
if (!signature->email)
149+
goto on_error;
150+
151+
signature->when.time = source->when.time;
152+
signature->when.offset = source->when.offset;
153+
signature->when.sign = source->when.sign;
154+
155+
*dest = signature;
156+
return 0;
157+
158+
on_error:
159+
git_signature_free(signature);
160+
return -1;
161+
}
162+
124163
int git_signature__pdup(git_signature **dest, const git_signature *source, git_pool *pool)
125164
{
126165
git_signature *signature;

0 commit comments

Comments
 (0)