mailmap: WIP mailmap support
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
diff --git a/include/git2.h b/include/git2.h
index 5f6104e..e182ce9 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -29,6 +29,7 @@
#include "git2/ignore.h"
#include "git2/index.h"
#include "git2/indexer.h"
+#include "git2/mailmap.h"
#include "git2/merge.h"
#include "git2/message.h"
#include "git2/net.h"
diff --git a/include/git2/mailmap.h b/include/git2/mailmap.h
new file mode 100644
index 0000000..bb126d1
--- /dev/null
+++ b/include/git2/mailmap.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_mailmap_h__
+#define INCLUDE_mailmap_h__
+
+#include "common.h"
+#include "repository.h"
+
+/**
+ * @file git2/mailmap.h
+ * @brief Mailmap access subroutines.
+ * @defgroup git_rebase Git merge routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+typedef struct git_mailmap git_mailmap;
+
+struct git_mailmap_entry {
+ const char* name;
+ const char* email;
+};
+
+GIT_EXTERN(int) git_mailmap_create(git_mailmap**, git_repository*);
+GIT_EXTERN(void) git_mailmap_free(git_mailmap*);
+GIT_EXTERN(struct git_mailmap_entry) git_mailmap_lookup(
+ git_mailmap* map,
+ const char* name,
+ const char* email);
+
+/** @} */
+GIT_END_DECL
+
+#endif
diff --git a/src/mailmap.c b/src/mailmap.c
new file mode 100644
index 0000000..26b5f8b
--- /dev/null
+++ b/src/mailmap.c
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "git2/mailmap.h"
+
+#include "blob.h"
+#include "commit.h"
+#include "git2/common.h"
+#include "git2/repository.h"
+#include "git2/revparse.h"
+#include "git2/sys/commit.h"
+
+struct mailmap_entry {
+ char* to_name;
+ char* to_email;
+ char* from_name;
+ char* from_email;
+};
+
+struct git_mailmap {
+ git_vector lines;
+};
+
+// Returns -1 on failure, length of the string scanned successfully on success,
+// guaranteed to be less that `length`.
+ssize_t parse_name_and_email(
+ const char *line,
+ size_t length,
+ const char** name,
+ size_t* name_len,
+ const char** email,
+ size_t* email_len,
+ bool allow_empty_email)
+{
+ const char* email_start;
+ const char* email_end;
+ const char* name_start;
+ const char* name_end;
+
+ email_start = memchr(line, '<', length);
+ if (!email_start)
+ return -1;
+ email_end = memchr(email_start, '>', length - (email_start - line));
+ if (!email_end)
+ return -1;
+ assert(email_end > email_start);
+
+ *email_len = email_end - email_start - 1;
+ *email = email_start + 1;
+ if (*email == email_end && !allow_empty_email)
+ return -1;
+
+ // Now look for the name.
+ name_start = line;
+ while (name_start < email_start && isspace(*name_start))
+ ++name_start;
+
+ *name = name_start;
+
+ name_end = email_start;
+ while (name_end > name_start && isspace(*(name_end - 1)))
+ name_end--;
+
+ assert(name_end >= name_start);
+ *name_len = name_end - name_start;
+
+ return email_end - line;
+}
+
+static void git_mailmap_parse_line(
+ git_mailmap* mailmap,
+ const char* contents,
+ size_t size)
+{
+ struct mailmap_entry* entry;
+
+ const char* to_name;
+ size_t to_name_length;
+
+ const char* to_email;
+ size_t to_email_length;
+
+ const char* from_name;
+ size_t from_name_length;
+
+ const char* from_email;
+ size_t from_email_length;
+
+ ssize_t ret;
+
+ if (!size)
+ return;
+ if (contents[0] == '#')
+ return;
+
+ ret = parse_name_and_email(
+ contents,
+ size,
+ &to_name,
+ &to_name_length,
+ &to_email,
+ &to_email_length,
+ false);
+ if (ret < 0)
+ return;
+
+ ret = parse_name_and_email(
+ contents + ret + 1,
+ size - ret - 1,
+ &from_name,
+ &from_name_length,
+ &from_email,
+ &from_email_length,
+ true);
+ if (ret < 0)
+ return;
+
+ entry = git__malloc(sizeof(struct mailmap_entry));
+
+ entry->to_name = git__strndup(to_name, to_name_length);
+ entry->to_email = git__strndup(to_email, to_email_length);
+ entry->from_name = git__strndup(from_name, from_name_length);
+ entry->from_email = git__strndup(from_email, from_email_length);
+
+ printf("%s <%s> \"%s\" <%s>\n",
+ entry->to_name,
+ entry->to_email,
+ entry->from_name,
+ entry->from_email);
+
+ git_vector_insert(&mailmap->lines, entry);
+}
+
+static void git_mailmap_parse(
+ git_mailmap* mailmap,
+ const char* contents,
+ size_t size)
+{
+ size_t start = 0;
+ size_t i;
+ for (i = 0; i < size; ++i) {
+ if (contents[i] != '\n')
+ continue;
+ git_mailmap_parse_line(mailmap, contents + start, i - start);
+ start = i + 1;
+ }
+}
+
+int git_mailmap_create(git_mailmap** mailmap, git_repository* repo)
+{
+ git_commit* head = NULL;
+ git_blob* mailmap_blob = NULL;
+ git_off_t size = 0;
+ const char* contents = NULL;
+ int ret;
+
+ *mailmap = git__malloc(sizeof(struct git_mailmap));
+ git_vector_init(&(*mailmap)->lines, 0, NULL);
+
+ ret = git_revparse_single((git_object **)&head, repo, "HEAD");
+ if (ret)
+ goto error;
+
+ ret = git_object_lookup_bypath(
+ (git_object**) &mailmap_blob,
+ (const git_object*) head,
+ ".mailmap",
+ GIT_OBJ_BLOB);
+ if (ret)
+ goto error;
+
+ contents = git_blob_rawcontent(mailmap_blob);
+ size = git_blob_rawsize(mailmap_blob);
+
+ git_mailmap_parse(*mailmap, contents, size);
+
+ return 0;
+
+error:
+ assert(ret);
+
+ if (mailmap_blob)
+ git_blob_free(mailmap_blob);
+ if (head)
+ git_commit_free(head);
+ git_mailmap_free(*mailmap);
+ return ret;
+}
+
+void git_mailmap_free(struct git_mailmap* mailmap)
+{
+ size_t i;
+ struct mailmap_entry* line;
+ git_vector_foreach(&mailmap->lines, i, line) {
+ git__free((char*)line->to_name);
+ git__free((char*)line->to_email);
+ git__free((char*)line->from_name);
+ git__free((char*)line->from_email);
+ git__free(line);
+ }
+
+ git_vector_clear(&mailmap->lines);
+ git__free(mailmap);
+}