mailmap: Integrate mailmaps with blame and signatures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
diff --git a/include/git2/blame.h b/include/git2/blame.h
index 34cb699..cc91317 100644
--- a/include/git2/blame.h
+++ b/include/git2/blame.h
@@ -43,6 +43,10 @@ typedef enum {
/** Restrict the search of commits to those reachable following only the
* first parents. */
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;
/**
@@ -108,6 +112,9 @@ GIT_EXTERN(int) git_blame_init_options(
* changed.
* - `final_start_line_number` is the 1-based line number where this hunk
* 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
* will usually be the same as `final_commit_id`, except when
* `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified.
@@ -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
* in the file named by `orig_path` in the commit specified by
* `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
* root, or the commit specified in git_blame_options.oldest_commit)
*/
diff --git a/include/git2/commit.h b/include/git2/commit.h
index 692b3bd..50f2fc9 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -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);
/**
+ * 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.
*
* @param commit a previously loaded commit
diff --git a/include/git2/signature.h b/include/git2/signature.h
index 7a2a023..18fae98 100644
--- a/include/git2/signature.h
+++ b/include/git2/signature.h
@@ -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);
/**
+ * 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
* duplicated.
*
diff --git a/include/git2/types.h b/include/git2/types.h
index 67e5bd1..943a7e9 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -434,6 +434,9 @@ struct git_writestream {
void (*free)(git_writestream *stream);
};
+/** A parsed representation of a .mailmap file. */
+typedef struct git_mailmap git_mailmap;
+
/** @} */
GIT_END_DECL
diff --git a/src/blame.c b/src/blame.c
index a923bf0..967530b 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -132,6 +132,9 @@ git_blame* git_blame__alloc(
return NULL;
}
+ if (opts.flags & GIT_BLAME_USE_MAILMAP)
+ git_mailmap_from_repo(&gbr->mailmap, repo);
+
return gbr;
}
@@ -150,6 +153,8 @@ void git_blame_free(git_blame *blame)
git_array_clear(blame->line_index);
+ git_mailmap_free(blame->mailmap);
+
git__free(blame->path);
git_blob_free(blame->final_blob);
git__free(blame);
@@ -279,7 +284,7 @@ static int index_blob_lines(git_blame *blame)
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(
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)
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_signature_dup(&h->final_signature, git_commit_author(e->suspect->commit));
- git_signature_dup(&h->orig_signature, git_commit_author(e->suspect->commit));
+ git_commit_author_with_mailmap(
+ &h->final_signature, e->suspect->commit, blame->mailmap);
+ git_signature_dup(&h->orig_signature, h->final_signature);
h->boundary = e->is_boundary ? 1 : 0;
return h;
}
@@ -341,7 +347,7 @@ static int blame_internal(git_blame *blame)
cleanup:
for (ent = blame->ent; ent; ) {
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);
diff --git a/src/blame.h b/src/blame.h
index 8fd3ee5..0574132 100644
--- a/src/blame.h
+++ b/src/blame.h
@@ -84,6 +84,8 @@ struct git_blame {
int num_lines;
const char *final_buf;
git_off_t final_buf_size;
+
+ git_mailmap *mailmap;
};
git_blame *git_blame__alloc(
diff --git a/src/commit.c b/src/commit.c
index e0c090a..03ecea8 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -889,3 +889,15 @@ cleanup:
git_buf_dispose(&commit);
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);
+}
diff --git a/src/signature.c b/src/signature.c
index cd68523..a93ba10 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -9,6 +9,7 @@
#include "repository.h"
#include "git2/common.h"
+#include "git2/mailmap.h"
#include "posix.h"
void git_signature_free(git_signature *sig)
@@ -121,6 +122,44 @@ int git_signature_dup(git_signature **dest, const git_signature *source)
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)
{
git_signature *signature;