mailmap: Make everything a bit more style conforming
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
diff --git a/include/git2/mailmap.h b/include/git2/mailmap.h
index 0f582c2..a80ecb9 100644
--- a/include/git2/mailmap.h
+++ b/include/git2/mailmap.h
@@ -8,7 +8,7 @@
#define INCLUDE_git_mailmap_h__
#include "common.h"
-#include "tree.h"
+#include "types.h"
/**
* @file git2/mailmap.h
@@ -19,24 +19,27 @@
*/
GIT_BEGIN_DECL
-typedef struct git_mailmap git_mailmap;
-
/**
* A single entry parsed from a mailmap.
*/
-typedef struct git_mailmap_entry {
+struct git_mailmap_entry {
+ unsigned int version;
+
const char *real_name; /**< the real name (may be NULL) */
const char *real_email; /**< the real email (may be NULL) */
const char *replace_name; /**< the name to replace (may be NULL) */
const char *replace_email; /**< the email to replace */
-} git_mailmap_entry;
+};
+
+#define GIT_MAILMAP_ENTRY_VERSION 1
+#define GIT_MAILMAP_ENTRY_INIT {GIT_MAILMAP_ENTRY_VERSION}
/**
* Create a mailmap object by parsing a mailmap file.
*
* The mailmap must be freed with 'git_mailmap_free'.
*
- * @param out Pointer to store the mailmap
+ * @param out pointer to store the mailmap
* @param data raw data buffer to parse
* @param size size of the raw data buffer
* @return 0 on success
@@ -47,35 +50,26 @@ GIT_EXTERN(int) git_mailmap_parse(
size_t size);
/**
- * Create a mailmap object by parsing the ".mailmap" file in the tree root.
+ * Create a mailmap object from the given repository.
*
- * The mailmap must be freed with 'git_mailmap_free'.
+ * If the repository is not bare, the repository's working directory root will
+ * be checked for the '.mailmap' file to be parsed.
*
- * @param out pointer to store the mailmap
- * @param treeish root object that can be peeled to a tree
- * @return 0 on success; GIT_ENOTFOUND if .mailmap does not exist.
- */
-GIT_EXTERN(int) git_mailmap_from_tree(
- git_mailmap **out,
- const git_object *treeish);
-
-/**
- * Create a mailmap object by parsing the ".mailmap" file in the repository's
- * HEAD's tree root.
+ * If the repository is bare, the repository's HEAD commit's tree root will be
+ * searched for the '.mailmap' file to be parsed.
*
* The mailmap must be freed with 'git_mailmap_free'.
*
* @param out pointer to store the mailmap
* @param repo repository to find the .mailmap in
- * @return 0 on success; GIT_ENOTFOUND if .mailmap does not exist.
+ * @return 0 on success; GIT_ENOTFOUND if .mailmap could not be found.
*/
GIT_EXTERN(int) git_mailmap_from_repo(
git_mailmap **out,
git_repository *repo);
/**
- * Free a mailmap created by 'git_mailmap_parse', 'git_mailmap_from_tree' or
- * 'git_mailmap_from_repo'.
+ * Free a mailmap created by 'git_mailmap_parse' or 'git_mailmap_from_repo'.
*/
GIT_EXTERN(void) git_mailmap_free(git_mailmap *mailmap);
@@ -86,38 +80,45 @@ GIT_EXTERN(void) git_mailmap_free(git_mailmap *mailmap);
* You should NOT free this value.
* @param email_out either 'email' or the real email to use,
* You should NOT free this value.
- * @param mailmap the mailmap to perform the lookup in.
+ * @param mailmap the mailmap to perform the lookup in. (may be NULL)
* @param name the name to resolve.
* @param email the email to resolve.
*/
GIT_EXTERN(void) git_mailmap_resolve(
const char **name_out,
const char **email_out,
- git_mailmap *mailmap,
+ const git_mailmap *mailmap,
const char *name,
const char *email);
/**
- * Get the number of mailmap entries.
+ * Get the number of mailmap entries in this mailmap.
*/
-GIT_EXTERN(size_t) git_mailmap_entry_count(git_mailmap *mailmap);
+GIT_EXTERN(size_t) git_mailmap_entry_count(const git_mailmap *mailmap);
/**
* Lookup a mailmap entry by index.
*
* Do not free the mailmap entry, it is owned by the mailmap.
+ *
+ * @return the mailmap entry at index, or NULL if it cannot be found.
*/
-GIT_EXTERN(git_mailmap_entry *) git_mailmap_entry_byindex(
- git_mailmap *mailmap,
+GIT_EXTERN(const git_mailmap_entry *) git_mailmap_entry_byindex(
+ const git_mailmap *mailmap,
size_t idx);
/**
* Lookup a mailmap entry by name/email pair.
*
* Do not free the mailmap entry, it is owned by the mailmap.
+ *
+ * @param mailmap the mailmap to perform the lookup in. (may be NULL)
+ * @param name the name to perform the lookup with.
+ * @param email the email to perform the lookup with.
+ * @return the corresponding mailmap entry, or NULL if it cannot be found.
*/
-GIT_EXTERN(git_mailmap_entry *) git_mailmap_entry_lookup(
- git_mailmap *mailmap,
+GIT_EXTERN(const git_mailmap_entry *) git_mailmap_entry_lookup(
+ const git_mailmap *mailmap,
const char *name,
const char *email);
diff --git a/src/mailmap.c b/src/mailmap.c
index dfff47d..aaf5446 100644
--- a/src/mailmap.c
+++ b/src/mailmap.c
@@ -135,7 +135,8 @@ static int git_mailmap_parse_single(
*real_name = name_a;
if (two_emails) {
- *real_email = email_a;
+ if (email_a.len > 0)
+ *real_email = email_a;
*replace_email = email_b;
if (name_b.len > 0)
@@ -158,6 +159,9 @@ int git_mailmap_parse(
git_mailmap_entry* entry = NULL;
int error = 0;
+ if (memchr(data, '\0', size) != NULL)
+ return -1; /* data may not contain '\0's */
+
*mailmap = git__calloc(1, sizeof(git_mailmap));
if (!*mailmap)
return -1;
@@ -194,6 +198,7 @@ int git_mailmap_parse(
error = -1;
goto cleanup;
}
+ entry->version = GIT_MAILMAP_ENTRY_VERSION;
buf = (char*)(entry + 1);
entry->real_name = range_copyz(&buf, NULL, real_name);
@@ -209,9 +214,8 @@ int git_mailmap_parse(
}
cleanup:
- if (entry)
- git__free(entry);
- if (error < 0 && *mailmap) {
+ git__free(entry);
+ if (error < 0)
git_mailmap_free(*mailmap);
*mailmap = NULL;
}
@@ -220,6 +224,9 @@ cleanup:
void git_mailmap_free(git_mailmap *mailmap)
{
+ if (!mailmap)
+ return;
+
git_vector_free_deep(&mailmap->entries);
git__free(mailmap);
}
@@ -227,15 +234,19 @@ void git_mailmap_free(git_mailmap *mailmap)
void git_mailmap_resolve(
const char **name_out,
const char **email_out,
- git_mailmap *mailmap,
+ const git_mailmap *mailmap,
const char *name,
const char *email)
{
- git_mailmap_entry *entry = NULL;
+ const git_mailmap_entry *entry = NULL;
+ assert(name && email);
*name_out = name;
*email_out = email;
+ if (!mailmap)
+ return;
+
entry = git_mailmap_entry_lookup(mailmap, name, email);
if (entry) {
if (entry->real_name)
@@ -245,14 +256,17 @@ void git_mailmap_resolve(
}
}
-git_mailmap_entry *git_mailmap_entry_lookup(
- git_mailmap *mailmap,
+const git_mailmap_entry *git_mailmap_entry_lookup(
+ const git_mailmap *mailmap,
const char *name,
const char *email)
{
size_t i;
git_mailmap_entry *entry;
- assert(mailmap && name && email);
+ assert(name && email);
+
+ if (!mailmap)
+ return NULL;
git_vector_foreach(&mailmap->entries, i, entry) {
if (!git__strcmp(email, entry->replace_email) &&
@@ -264,26 +278,42 @@ git_mailmap_entry *git_mailmap_entry_lookup(
return NULL;
}
-git_mailmap_entry *git_mailmap_entry_byindex(git_mailmap *mailmap, size_t idx)
+const git_mailmap_entry *git_mailmap_entry_byindex(
+ const git_mailmap *mailmap, size_t idx)
{
- return git_vector_get(&mailmap->entries, idx);
+ if (mailmap)
+ return git_vector_get(&mailmap->entries, idx);
+ return NULL;
}
-size_t git_mailmap_entry_count(git_mailmap *mailmap)
+size_t git_mailmap_entry_count(const git_mailmap *mailmap)
{
- return git_vector_length(&mailmap->entries);
+ if (mailmap)
+ return git_vector_length(&mailmap->entries);
+ return 0;
}
-int git_mailmap_from_tree(
+static int git_mailmap_from_bare_repo(
git_mailmap **mailmap,
- const git_object *treeish)
+ git_repository *repo)
{
+ git_reference *head = NULL;
+ git_object *tree = NULL;
git_blob *blob = NULL;
const char *content = NULL;
git_off_t size = 0;
int error;
- *mailmap = NULL;
+ assert(git_repository_is_bare(repo));
+
+ /* In bare repositories, fall back to reading from HEAD's tree */
+ error = git_repository_head(&head, repo);
+ if (error < 0)
+ goto cleanup;
+
+ error = git_reference_peel(&tree, head, GIT_OBJ_TREE);
+ if (error < 0)
+ goto cleanup;
error = git_object_lookup_bypath(
(git_object **) &blob,
@@ -297,28 +327,55 @@ int git_mailmap_from_tree(
size = git_blob_rawsize(blob);
error = git_mailmap_parse(mailmap, content, size);
+ if (error < 0)
+ goto cleanup;
cleanup:
- if (blob != NULL)
- git_blob_free(blob);
+ git_reference_free(head);
+ git_object_free(tree);
+ git_blob_free(blob);
+
return error;
}
-int git_mailmap_from_repo(git_mailmap **mailmap, git_repository *repo)
+static int git_mailmap_from_workdir_repo(
+ git_mailmap **mailmap,
+ git_repository *repo)
{
- git_object *head = NULL;
+ git_buf path = GIT_BUF_INIT;
+ git_buf data = GIT_BUF_INIT;
int error;
- *mailmap = NULL;
+ assert(!git_repository_is_bare(repo));
- error = git_revparse_single(&head, repo, "HEAD");
+ /* In non-bare repositories, .mailmap should be read from the workdir */
+ error = git_buf_joinpath(&path, git_repository_workdir(repo), ".mailmap");
if (error < 0)
goto cleanup;
- error = git_mailmap_from_tree(mailmap, head);
+ error = git_futils_readbuffer(&data, git_buf_cstr(&path));
+ if (error < 0)
+ goto cleanup;
+
+ error = git_mailmap_parse(mailmap, data.ptr, data.size);
+ if (error < 0)
+ goto cleanup;
cleanup:
- if (head)
- git_object_free(head);
+ git_buf_free(&path);
+ git_buf_free(&data);
+
return error;
}
+
+int git_mailmap_from_repo(git_mailmap **mailmap, git_repository *repo)
+{
+ assert(mailmap && repo);
+
+ *mailmap = NULL;
+
+ if (git_repository_is_bare(repo))
+ return git_mailmap_from_bare_repo(mailmap, repo);
+ else
+ return git_mailmap_from_workdir_repo(mailmap, repo);
+}