Commit e3dcaca5 by Nika Layzell

mailmap: Integrate mailmaps with blame and signatures

parent b05fbba3
...@@ -43,6 +43,10 @@ typedef enum { ...@@ -43,6 +43,10 @@ typedef enum {
/** Restrict the search of commits to those reachable following only the /** Restrict the search of commits to those reachable following only the
* first parents. */ * first parents. */
GIT_BLAME_FIRST_PARENT = (1<<4), GIT_BLAME_FIRST_PARENT = (1<<4),
/** Use mailmap file to map author and committer names and email addresses
* to canonical real names and email addresses. The mailmap will be read
* from the working directory, or HEAD in a bare repository. */
GIT_BLAME_USE_MAILMAP = (1<<5),
} git_blame_flag_t; } git_blame_flag_t;
/** /**
...@@ -108,6 +112,9 @@ GIT_EXTERN(int) git_blame_init_options( ...@@ -108,6 +112,9 @@ GIT_EXTERN(int) git_blame_init_options(
* changed. * changed.
* - `final_start_line_number` is the 1-based line number where this hunk * - `final_start_line_number` is the 1-based line number where this hunk
* begins, in the final version of the file * begins, in the final version of the file
* - `final_signature` is the author of `final_commit_id`. If
* `GIT_BLAME_USE_MAILMAP` has been specified, it will contain the canonical
* real name and email address.
* - `orig_commit_id` is the OID of the commit where this hunk was found. This * - `orig_commit_id` is the OID of the commit where this hunk was found. This
* will usually be the same as `final_commit_id`, except when * will usually be the same as `final_commit_id`, except when
* `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified. * `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified.
...@@ -116,6 +123,9 @@ GIT_EXTERN(int) git_blame_init_options( ...@@ -116,6 +123,9 @@ GIT_EXTERN(int) git_blame_init_options(
* - `orig_start_line_number` is the 1-based line number where this hunk begins * - `orig_start_line_number` is the 1-based line number where this hunk begins
* in the file named by `orig_path` in the commit specified by * in the file named by `orig_path` in the commit specified by
* `orig_commit_id`. * `orig_commit_id`.
* - `orig_signature` is the author of `orig_commit_id`. If
* `GIT_BLAME_USE_MAILMAP` has been specified, it will contain the canonical
* real name and email address.
* - `boundary` is 1 iff the hunk has been tracked to a boundary commit (the * - `boundary` is 1 iff the hunk has been tracked to a boundary commit (the
* root, or the commit specified in git_blame_options.oldest_commit) * root, or the commit specified in git_blame_options.oldest_commit)
*/ */
......
...@@ -173,6 +173,34 @@ GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit) ...@@ -173,6 +173,34 @@ GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit)
GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit); GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit);
/** /**
* Get the committer of a commit, using the mailmap to map names and email
* addresses to canonical real names and email addresses.
*
* Call `git_signature_free` to free the signature.
*
* @param out a pointer to store the resolved signature.
* @param commit a previously loaded commit.
* @param mailmap the mailmap to resolve with. (may be NULL)
* @return 0 or an error code
*/
GIT_EXTERN(int) git_commit_committer_with_mailmap(
git_signature **out, const git_commit *commit, const git_mailmap *mailmap);
/**
* Get the author of a commit, using the mailmap to map names and email
* addresses to canonical real names and email addresses.
*
* Call `git_signature_free` to free the signature.
*
* @param out a pointer to store the resolved signature.
* @param commit a previously loaded commit.
* @param mailmap the mailmap to resolve with. (may be NULL)
* @return 0 or an error code
*/
GIT_EXTERN(int) git_commit_author_with_mailmap(
git_signature **out, const git_commit *commit, const git_mailmap *mailmap);
/**
* Get the full raw text of the commit header. * Get the full raw text of the commit header.
* *
* @param commit a previously loaded commit * @param commit a previously loaded commit
......
...@@ -76,6 +76,19 @@ GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo) ...@@ -76,6 +76,19 @@ GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo)
GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf); GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf);
/** /**
* Create a signature with names updated respecting the mailmap.
*
* Call `git_signature_free()` to free the data.
*
* @param out new signature
* @param sig signature to resolve
* @param mailmap mailmap to resolve with
* @return 0 or an error code
*/
GIT_EXTERN(int) git_signature_with_mailmap(
git_signature **out, const git_signature *sig, const git_mailmap *mailmap);
/**
* Create a copy of an existing signature. All internal strings are also * Create a copy of an existing signature. All internal strings are also
* duplicated. * duplicated.
* *
......
...@@ -434,6 +434,9 @@ struct git_writestream { ...@@ -434,6 +434,9 @@ struct git_writestream {
void (*free)(git_writestream *stream); void (*free)(git_writestream *stream);
}; };
/** A parsed representation of a .mailmap file. */
typedef struct git_mailmap git_mailmap;
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -132,6 +132,9 @@ git_blame* git_blame__alloc( ...@@ -132,6 +132,9 @@ git_blame* git_blame__alloc(
return NULL; return NULL;
} }
if (opts.flags & GIT_BLAME_USE_MAILMAP)
git_mailmap_from_repo(&gbr->mailmap, repo);
return gbr; return gbr;
} }
...@@ -150,6 +153,8 @@ void git_blame_free(git_blame *blame) ...@@ -150,6 +153,8 @@ void git_blame_free(git_blame *blame)
git_array_clear(blame->line_index); git_array_clear(blame->line_index);
git_mailmap_free(blame->mailmap);
git__free(blame->path); git__free(blame->path);
git_blob_free(blame->final_blob); git_blob_free(blame->final_blob);
git__free(blame); git__free(blame);
...@@ -279,7 +284,7 @@ static int index_blob_lines(git_blame *blame) ...@@ -279,7 +284,7 @@ static int index_blob_lines(git_blame *blame)
return blame->num_lines; return blame->num_lines;
} }
static git_blame_hunk* hunk_from_entry(git_blame__entry *e) static git_blame_hunk* hunk_from_entry(git_blame__entry *e, git_blame *blame)
{ {
git_blame_hunk *h = new_hunk( git_blame_hunk *h = new_hunk(
e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); 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) ...@@ -289,8 +294,9 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e)
git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit));
git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit)); git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit));
git_signature_dup(&h->final_signature, git_commit_author(e->suspect->commit)); git_commit_author_with_mailmap(
git_signature_dup(&h->orig_signature, git_commit_author(e->suspect->commit)); &h->final_signature, e->suspect->commit, blame->mailmap);
git_signature_dup(&h->orig_signature, h->final_signature);
h->boundary = e->is_boundary ? 1 : 0; h->boundary = e->is_boundary ? 1 : 0;
return h; return h;
} }
...@@ -341,7 +347,7 @@ static int blame_internal(git_blame *blame) ...@@ -341,7 +347,7 @@ static int blame_internal(git_blame *blame)
cleanup: cleanup:
for (ent = blame->ent; ent; ) { for (ent = blame->ent; ent; ) {
git_blame__entry *e = ent->next; git_blame__entry *e = ent->next;
git_blame_hunk *h = hunk_from_entry(ent); git_blame_hunk *h = hunk_from_entry(ent, blame);
git_vector_insert(&blame->hunks, h); git_vector_insert(&blame->hunks, h);
......
...@@ -84,6 +84,8 @@ struct git_blame { ...@@ -84,6 +84,8 @@ struct git_blame {
int num_lines; int num_lines;
const char *final_buf; const char *final_buf;
git_off_t final_buf_size; git_off_t final_buf_size;
git_mailmap *mailmap;
}; };
git_blame *git_blame__alloc( git_blame *git_blame__alloc(
......
...@@ -889,3 +889,15 @@ cleanup: ...@@ -889,3 +889,15 @@ cleanup:
git_buf_dispose(&commit); git_buf_dispose(&commit);
return error; return error;
} }
int git_commit_committer_with_mailmap(
git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
{
return git_signature_with_mailmap(out, commit->committer, mailmap);
}
int git_commit_author_with_mailmap(
git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
{
return git_signature_with_mailmap(out, commit->author, mailmap);
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "repository.h" #include "repository.h"
#include "git2/common.h" #include "git2/common.h"
#include "git2/mailmap.h"
#include "posix.h" #include "posix.h"
void git_signature_free(git_signature *sig) void git_signature_free(git_signature *sig)
...@@ -121,6 +122,44 @@ int git_signature_dup(git_signature **dest, const git_signature *source) ...@@ -121,6 +122,44 @@ int git_signature_dup(git_signature **dest, const git_signature *source)
return 0; return 0;
} }
int git_signature_with_mailmap(
git_signature **dest,
const git_signature *source,
const git_mailmap *mailmap)
{
git_signature *signature = NULL;
const char *name = NULL;
const char *email = NULL;
if (source == NULL)
goto on_error;
git_mailmap_resolve(&name, &email, mailmap, source->name, source->email);
signature = git__calloc(1, sizeof(git_signature));
if (!signature)
goto on_error;
signature->name = git__strdup(name);
if (!signature->name)
goto on_error;
signature->email = git__strdup(email);
if (!signature->email)
goto on_error;
signature->when.time = source->when.time;
signature->when.offset = source->when.offset;
signature->when.sign = source->when.sign;
*dest = signature;
return 0;
on_error:
git_signature_free(signature);
return -1;
}
int git_signature__pdup(git_signature **dest, const git_signature *source, git_pool *pool) int git_signature__pdup(git_signature **dest, const git_signature *source, git_pool *pool)
{ {
git_signature *signature; git_signature *signature;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment