Unverified Commit 96882f20 by Edward Thomson Committed by GitHub

Merge pull request #4586 from emilio/mailmap

Add mailmap support.
parents 0ecf0e33 f98131be
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "git2/ignore.h" #include "git2/ignore.h"
#include "git2/index.h" #include "git2/index.h"
#include "git2/indexer.h" #include "git2/indexer.h"
#include "git2/mailmap.h"
#include "git2/merge.h" #include "git2/merge.h"
#include "git2/message.h" #include "git2/message.h"
#include "git2/net.h" #include "git2/net.h"
......
...@@ -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
......
/*
* 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_git_mailmap_h__
#define INCLUDE_git_mailmap_h__
#include "common.h"
#include "types.h"
#include "buffer.h"
/**
* @file git2/mailmap.h
* @brief Mailmap parsing routines
* @defgroup git_mailmap Git mailmap routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Allocate a new mailmap object.
*
* This object is empty, so you'll have to add a mailmap file before you can do
* anything with it. The mailmap must be freed with 'git_mailmap_free'.
*
* @param out pointer to store the new mailmap
* @return 0 on success, or an error code
*/
GIT_EXTERN(int) git_mailmap_new(git_mailmap **out);
/**
* Free the mailmap and its associated memory.
*
* @param mm the mailmap to free
*/
GIT_EXTERN(void) git_mailmap_free(git_mailmap *mm);
/**
* Add a single entry to the given mailmap object. If the entry already exists,
* it will be replaced with the new entry.
*
* @param mm mailmap to add the entry to
* @param real_name the real name to use, or NULL
* @param real_email the real email to use, or NULL
* @param replace_name the name to replace, or NULL
* @param replace_email the email to replace
* @return 0 on success, or an error code
*/
GIT_EXTERN(int) git_mailmap_add_entry(
git_mailmap *mm, const char *real_name, const char *real_email,
const char *replace_name, const char *replace_email);
/**
* Create a new mailmap instance containing a single mailmap file
*
* @param out pointer to store the new mailmap
* @param buf buffer to parse the mailmap from
* @param len the length of the input buffer
* @return 0 on success, or an error code
*/
GIT_EXTERN(int) git_mailmap_from_buffer(
git_mailmap **out, const char *buf, size_t len);
/**
* Create a new mailmap instance from a repository, loading mailmap files based
* on the repository's configuration.
*
* Mailmaps are loaded in the following order:
* 1. '.mailmap' in the root of the repository's working directory, if present.
* 2. The blob object identified by the 'mailmap.blob' config entry, if set.
* [NOTE: 'mailmap.blob' defaults to 'HEAD:.mailmap' in bare repositories]
* 3. The path in the 'mailmap.file' config entry, if set.
*
* @param out pointer to store the new mailmap
* @param repo repository to load mailmap information from
* @return 0 on success, or an error code
*/
GIT_EXTERN(int) git_mailmap_from_repository(
git_mailmap **out, git_repository *repo);
/**
* Resolve a name and email to the corresponding real name and email.
*
* The lifetime of the strings are tied to `mm`, `name`, and `email` parameters.
*
* @param real_name pointer to store the real name
* @param real_email pointer to store the real email
* @param mm the mailmap to perform a lookup with (may be NULL)
* @param name the name to look up
* @param email the email to look up
* @return 0 on success, or an error code
*/
GIT_EXTERN(int) git_mailmap_resolve(
const char **real_name, const char **real_email,
const git_mailmap *mm, const char *name, const char *email);
/**
* Resolve a signature to use real names and emails with a mailmap.
*
* Call `git_signature_free()` to free the data.
*
* @param out new signature
* @param mm mailmap to resolve with
* @param sig signature to resolve
* @return 0 or an error code
*/
GIT_EXTERN(int) git_mailmap_resolve_signature(
git_signature **out, const git_mailmap *mm, const git_signature *sig);
/** @} */
GIT_END_DECL
#endif
...@@ -434,6 +434,9 @@ struct git_writestream { ...@@ -434,6 +434,9 @@ struct git_writestream {
void (*free)(git_writestream *stream); void (*free)(git_writestream *stream);
}; };
/** Representation of .mailmap file state. */
typedef struct git_mailmap git_mailmap;
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "git2/diff.h" #include "git2/diff.h"
#include "git2/blob.h" #include "git2/blob.h"
#include "git2/signature.h" #include "git2/signature.h"
#include "git2/mailmap.h"
#include "util.h" #include "util.h"
#include "repository.h" #include "repository.h"
#include "blame_git.h" #include "blame_git.h"
...@@ -132,6 +133,9 @@ git_blame* git_blame__alloc( ...@@ -132,6 +133,9 @@ git_blame* git_blame__alloc(
return NULL; return NULL;
} }
if (opts.flags & GIT_BLAME_USE_MAILMAP)
git_mailmap_from_repository(&gbr->mailmap, repo);
return gbr; return gbr;
} }
...@@ -150,6 +154,8 @@ void git_blame_free(git_blame *blame) ...@@ -150,6 +154,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 +285,7 @@ static int index_blob_lines(git_blame *blame) ...@@ -279,7 +285,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 +295,9 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e) ...@@ -289,8 +295,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 +348,7 @@ static int blame_internal(git_blame *blame) ...@@ -341,7 +348,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);
......
...@@ -67,6 +67,7 @@ typedef struct git_blame__entry { ...@@ -67,6 +67,7 @@ typedef struct git_blame__entry {
struct git_blame { struct git_blame {
char *path; char *path;
git_repository *repository; git_repository *repository;
git_mailmap *mailmap;
git_blame_options options; git_blame_options options;
git_vector hunks; git_vector hunks;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "git2/object.h" #include "git2/object.h"
#include "git2/repository.h" #include "git2/repository.h"
#include "git2/signature.h" #include "git2/signature.h"
#include "git2/mailmap.h"
#include "git2/sys/commit.h" #include "git2/sys/commit.h"
#include "odb.h" #include "odb.h"
...@@ -889,3 +890,15 @@ cleanup: ...@@ -889,3 +890,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_mailmap_resolve_signature(out, mailmap, commit->committer);
}
int git_commit_author_with_mailmap(
git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
{
return git_mailmap_resolve_signature(out, mailmap, commit->author);
}
This diff is collapsed. Click to expand it.
/*
* 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 "git2/mailmap.h"
#include "vector.h"
/*
* A mailmap is stored as a sorted vector of 'git_mailmap_entry's. These entries
* are sorted first by 'replace_email', and then by 'replace_name'. NULL
* replace_names are ordered first.
*
* Looking up a name and email in the mailmap is done with a binary search.
*/
struct git_mailmap {
git_vector entries;
};
/* Single entry parsed from a mailmap */
typedef struct git_mailmap_entry {
char *real_name; /**< the real name (may be NULL) */
char *real_email; /**< the real email (may be NULL) */
char *replace_name; /**< the name to replace (may be NULL) */
char *replace_email; /**< the email to replace */
} git_mailmap_entry;
const git_mailmap_entry *git_mailmap_entry_lookup(
const git_mailmap *mm, const char *name, const char *email);
#endif
...@@ -72,6 +72,7 @@ void cl_trace_register(cl_trace_cb *cb, void *payload); ...@@ -72,6 +72,7 @@ void cl_trace_register(cl_trace_cb *cb, void *payload);
const char *cl_fixture(const char *fixture_name); const char *cl_fixture(const char *fixture_name);
void cl_fixture_sandbox(const char *fixture_name); void cl_fixture_sandbox(const char *fixture_name);
void cl_fixture_cleanup(const char *fixture_name); void cl_fixture_cleanup(const char *fixture_name);
const char *cl_fixture_basename(const char *fixture_name);
#endif #endif
/** /**
......
...@@ -20,19 +20,6 @@ fixture_path(const char *base, const char *fixture_name) ...@@ -20,19 +20,6 @@ fixture_path(const char *base, const char *fixture_name)
return _path; return _path;
} }
static const char *
fixture_basename(const char *fixture_name)
{
const char *p;
for (p = fixture_name; *p; p++) {
if (p[0] == '/' && p[1] && p[1] != '/')
fixture_name = p+1;
}
return fixture_name;
}
#ifdef CLAR_FIXTURE_PATH #ifdef CLAR_FIXTURE_PATH
const char *cl_fixture(const char *fixture_name) const char *cl_fixture(const char *fixture_name)
{ {
...@@ -44,8 +31,20 @@ void cl_fixture_sandbox(const char *fixture_name) ...@@ -44,8 +31,20 @@ void cl_fixture_sandbox(const char *fixture_name)
fs_copy(cl_fixture(fixture_name), _clar_path); fs_copy(cl_fixture(fixture_name), _clar_path);
} }
const char *cl_fixture_basename(const char *fixture_name)
{
const char *p;
for (p = fixture_name; *p; p++) {
if (p[0] == '/' && p[1] && p[1] != '/')
fixture_name = p+1;
}
return fixture_name;
}
void cl_fixture_cleanup(const char *fixture_name) void cl_fixture_cleanup(const char *fixture_name)
{ {
fs_rm(fixture_path(_clar_path, fixture_basename(fixture_name))); fs_rm(fixture_path(_clar_path, cl_fixture_basename(fixture_name)));
} }
#endif #endif
...@@ -171,13 +171,16 @@ static git_repository *_cl_repo = NULL; ...@@ -171,13 +171,16 @@ static git_repository *_cl_repo = NULL;
git_repository *cl_git_sandbox_init(const char *sandbox) git_repository *cl_git_sandbox_init(const char *sandbox)
{ {
/* Get the name of the sandbox folder which will be created */
const char *basename = cl_fixture_basename(sandbox);
/* Copy the whole sandbox folder from our fixtures to our test sandbox /* Copy the whole sandbox folder from our fixtures to our test sandbox
* area. After this it can be accessed with `./sandbox` * area. After this it can be accessed with `./sandbox`
*/ */
cl_fixture_sandbox(sandbox); cl_fixture_sandbox(sandbox);
_cl_sandbox = sandbox; _cl_sandbox = sandbox;
cl_git_pass(p_chdir(sandbox)); cl_git_pass(p_chdir(basename));
/* If this is not a bare repo, then rename `sandbox/.gitted` to /* If this is not a bare repo, then rename `sandbox/.gitted` to
* `sandbox/.git` which must be done since we cannot store a folder * `sandbox/.git` which must be done since we cannot store a folder
...@@ -200,7 +203,7 @@ git_repository *cl_git_sandbox_init(const char *sandbox) ...@@ -200,7 +203,7 @@ git_repository *cl_git_sandbox_init(const char *sandbox)
cl_git_pass(p_chdir("..")); cl_git_pass(p_chdir(".."));
/* Now open the sandbox repository and make it available for tests */ /* Now open the sandbox repository and make it available for tests */
cl_git_pass(git_repository_open(&_cl_repo, sandbox)); cl_git_pass(git_repository_open(&_cl_repo, basename));
/* Adjust configs after copying to new filesystem */ /* Adjust configs after copying to new filesystem */
cl_git_pass(git_repository_reinit_filesystem(_cl_repo, 0)); cl_git_pass(git_repository_reinit_filesystem(_cl_repo, 0));
...@@ -222,7 +225,8 @@ git_repository *cl_git_sandbox_reopen(void) ...@@ -222,7 +225,8 @@ git_repository *cl_git_sandbox_reopen(void)
git_repository_free(_cl_repo); git_repository_free(_cl_repo);
_cl_repo = NULL; _cl_repo = NULL;
cl_git_pass(git_repository_open(&_cl_repo, _cl_sandbox)); cl_git_pass(git_repository_open(
&_cl_repo, cl_fixture_basename(_cl_sandbox)));
} }
return _cl_repo; return _cl_repo;
......
#include "clar.h"
#include "clar_libgit2.h"
#include "common.h"
#include "mailmap.h"
static git_mailmap *mailmap = NULL;
const char TEST_MAILMAP[] =
"Foo bar <foo@bar.com> <foo@baz.com> \n"
"Blatantly invalid line\n"
"Foo bar <foo@bar.com> <foo@bal.com>\n"
"<email@foo.com> <otheremail@foo.com>\n"
"<email@foo.com> Other Name <yetanotheremail@foo.com>\n";
struct {
const char *real_name;
const char *real_email;
const char *replace_name;
const char *replace_email;
} expected[] = {
{ "Foo bar", "foo@bar.com", NULL, "foo@baz.com" },
{ "Foo bar", "foo@bar.com", NULL, "foo@bal.com" },
{ NULL, "email@foo.com", NULL, "otheremail@foo.com" },
{ NULL, "email@foo.com", "Other Name", "yetanotheremail@foo.com" }
};
void test_mailmap_basic__initialize(void)
{
cl_git_pass(git_mailmap_from_buffer(
&mailmap, TEST_MAILMAP, strlen(TEST_MAILMAP)));
}
void test_mailmap_basic__cleanup(void)
{
git_mailmap_free(mailmap);
mailmap = NULL;
}
void test_mailmap_basic__entry(void)
{
size_t idx;
const git_mailmap_entry *entry;
/* Check that we have the expected # of entries */
cl_assert_equal_sz(ARRAY_SIZE(expected), git_vector_length(&mailmap->entries));
for (idx = 0; idx < ARRAY_SIZE(expected); ++idx) {
/* Try to look up each entry and make sure they match */
entry = git_mailmap_entry_lookup(
mailmap, expected[idx].replace_name, expected[idx].replace_email);
cl_assert(entry);
cl_assert_equal_s(entry->real_name, expected[idx].real_name);
cl_assert_equal_s(entry->real_email, expected[idx].real_email);
cl_assert_equal_s(entry->replace_name, expected[idx].replace_name);
cl_assert_equal_s(entry->replace_email, expected[idx].replace_email);
}
}
void test_mailmap_basic__lookup_not_found(void)
{
const git_mailmap_entry *entry = git_mailmap_entry_lookup(
mailmap, "Whoever", "doesnotexist@fo.com");
cl_assert(!entry);
}
void test_mailmap_basic__lookup(void)
{
const git_mailmap_entry *entry = git_mailmap_entry_lookup(
mailmap, "Typoed the name once", "foo@baz.com");
cl_assert(entry);
cl_assert_equal_s(entry->real_name, "Foo bar");
}
void test_mailmap_basic__empty_email_query(void)
{
const char *name;
const char *email;
cl_git_pass(git_mailmap_resolve(
&name, &email, mailmap, "Author name", "otheremail@foo.com"));
cl_assert_equal_s(name, "Author name");
cl_assert_equal_s(email, "email@foo.com");
}
void test_mailmap_basic__name_matching(void)
{
const char *name;
const char *email;
cl_git_pass(git_mailmap_resolve(
&name, &email, mailmap, "Other Name", "yetanotheremail@foo.com"));
cl_assert_equal_s(name, "Other Name");
cl_assert_equal_s(email, "email@foo.com");
cl_git_pass(git_mailmap_resolve(
&name, &email, mailmap,
"Other Name That Doesn't Match", "yetanotheremail@foo.com"));
cl_assert_equal_s(name, "Other Name That Doesn't Match");
cl_assert_equal_s(email, "yetanotheremail@foo.com");
}
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/blame.h"
#include "mailmap.h"
#include "mailmap_testdata.h"
static git_repository *g_repo;
static git_blame *g_blame;
void test_mailmap_blame__initialize(void)
{
g_repo = NULL;
g_blame = NULL;
}
void test_mailmap_blame__cleanup(void)
{
git_blame_free(g_blame);
cl_git_sandbox_cleanup();
}
void test_mailmap_blame__hunks(void)
{
size_t idx = 0;
const git_blame_hunk *hunk = NULL;
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("mailmap");
opts.flags |= GIT_BLAME_USE_MAILMAP;
cl_git_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts));
cl_assert(g_blame);
for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) {
hunk = git_blame_get_hunk_byline(g_blame, idx + 1);
cl_assert(hunk->final_signature != NULL);
cl_assert(hunk->orig_signature != NULL);
cl_assert_equal_s(hunk->final_signature->name, resolved[idx].real_name);
cl_assert_equal_s(hunk->final_signature->email, resolved[idx].real_email);
}
}
void test_mailmap_blame__hunks_no_mailmap(void)
{
size_t idx = 0;
const git_blame_hunk *hunk = NULL;
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("mailmap");
cl_git_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts));
cl_assert(g_blame);
for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) {
hunk = git_blame_get_hunk_byline(g_blame, idx + 1);
cl_assert(hunk->final_signature != NULL);
cl_assert(hunk->orig_signature != NULL);
cl_assert_equal_s(hunk->final_signature->name, resolved[idx].replace_name);
cl_assert_equal_s(hunk->final_signature->email, resolved[idx].replace_email);
}
}
#include "mailmap.h"
typedef struct mailmap_entry {
const char *real_name;
const char *real_email;
const char *replace_name;
const char *replace_email;
} mailmap_entry;
static const char string_mailmap[] =
"# Simple Comment line\n"
"<cto@company.xx> <cto@coompany.xx>\n"
"Some Dude <some@dude.xx> nick1 <bugs@company.xx>\n"
"Other Author <other@author.xx> nick2 <bugs@company.xx>\n"
"Other Author <other@author.xx> <nick2@company.xx>\n"
"Phil Hill <phil@company.xx> # Comment at end of line\n"
"<joseph@company.xx> Joseph <bugs@company.xx>\n"
"Santa Claus <santa.claus@northpole.xx> <me@company.xx>\n"
"Untracked <untracked@company.xx>";
static const mailmap_entry entries[] = {
{ NULL, "cto@company.xx", NULL, "cto@coompany.xx" },
{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
{ "Other Author", "other@author.xx", NULL, "nick2@company.xx" },
{ "Phil Hill", NULL, NULL, "phil@company.xx" },
{ NULL, "joseph@company.xx", "Joseph", "bugs@company.xx" },
{ "Santa Claus", "santa.claus@northpole.xx", NULL, "me@company.xx" },
/* This entry isn't in the bare repository */
{ "Untracked", NULL, NULL, "untracked@company.xx" }
};
static const mailmap_entry resolved[] = {
{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
{ "Phil Hill", "phil@company.xx", "unknown", "phil@company.xx" },
{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" }
};
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/sys/repository.h"
#include "mailmap_testdata.h"
#include "buf_text.h"
static git_repository *g_repo;
static git_mailmap *g_mailmap;
static git_config *g_config;
void test_mailmap_parsing__initialize(void)
{
g_repo = NULL;
g_mailmap = NULL;
g_config = NULL;
}
void test_mailmap_parsing__cleanup(void)
{
git_mailmap_free(g_mailmap);
git_config_free(g_config);
cl_git_sandbox_cleanup();
}
static void check_mailmap_entries(
const git_mailmap *mailmap, const mailmap_entry *entries, size_t entries_size)
{
const git_mailmap_entry *parsed;
size_t idx;
/* Check the correct # of entries were parsed */
cl_assert_equal_sz(entries_size, git_vector_length(&mailmap->entries));
/* Make sure looking up each entry succeeds */
for (idx = 0; idx < entries_size; ++idx) {
parsed = git_mailmap_entry_lookup(
mailmap, entries[idx].replace_name, entries[idx].replace_email);
cl_assert(parsed);
cl_assert_equal_s(parsed->real_name, entries[idx].real_name);
cl_assert_equal_s(parsed->real_email, entries[idx].real_email);
cl_assert_equal_s(parsed->replace_name, entries[idx].replace_name);
cl_assert_equal_s(parsed->replace_email, entries[idx].replace_email);
}
}
static void check_mailmap_resolve(
const git_mailmap *mailmap, const mailmap_entry *resolved, size_t resolved_size)
{
const char *resolved_name = NULL;
const char *resolved_email = NULL;
size_t idx;
/* Check that the resolver behaves correctly */
for (idx = 0; idx < resolved_size; ++idx) {
cl_git_pass(git_mailmap_resolve(
&resolved_name, &resolved_email, mailmap,
resolved[idx].replace_name, resolved[idx].replace_email));
cl_assert_equal_s(resolved_name, resolved[idx].real_name);
cl_assert_equal_s(resolved_email, resolved[idx].real_email);
}
}
static const mailmap_entry resolved_untracked[] = {
{ "Untracked", "untracked@company.xx", "xx", "untracked@company.xx" }
};
void test_mailmap_parsing__string(void)
{
cl_git_pass(git_mailmap_from_buffer(
&g_mailmap, string_mailmap, strlen(string_mailmap)));
/* We should have parsed all of the entries */
check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries));
/* Check that resolving the entries works */
check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved));
check_mailmap_resolve(
g_mailmap, resolved_untracked, ARRAY_SIZE(resolved_untracked));
}
void test_mailmap_parsing__windows_string(void)
{
git_buf unixbuf = GIT_BUF_INIT;
git_buf winbuf = GIT_BUF_INIT;
/* Parse with windows-style line endings */
git_buf_attach_notowned(&unixbuf, string_mailmap, strlen(string_mailmap));
git_buf_text_lf_to_crlf(&winbuf, &unixbuf);
cl_git_pass(git_mailmap_from_buffer(&g_mailmap, winbuf.ptr, winbuf.size));
git_buf_dispose(&winbuf);
/* We should have parsed all of the entries */
check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries));
/* Check that resolving the entries works */
check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved));
check_mailmap_resolve(
g_mailmap, resolved_untracked, ARRAY_SIZE(resolved_untracked));
}
void test_mailmap_parsing__fromrepo(void)
{
g_repo = cl_git_sandbox_init("mailmap");
cl_check(!git_repository_is_bare(g_repo));
cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
/* We should have parsed all of the entries */
check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries));
/* Check that resolving the entries works */
check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved));
check_mailmap_resolve(
g_mailmap, resolved_untracked, ARRAY_SIZE(resolved_untracked));
}
static const mailmap_entry resolved_bare[] = {
{ "xx", "untracked@company.xx", "xx", "untracked@company.xx" }
};
void test_mailmap_parsing__frombare(void)
{
g_repo = cl_git_sandbox_init("mailmap/.gitted");
cl_git_pass(git_repository_set_bare(g_repo));
cl_check(git_repository_is_bare(g_repo));
cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
/* We should have parsed all of the entries, except for the untracked one */
check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries) - 1);
/* Check that resolving the entries works */
check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved));
check_mailmap_resolve(
g_mailmap, resolved_bare, ARRAY_SIZE(resolved_bare));
}
static const mailmap_entry resolved_with_file_override[] = {
{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" },
/* This name is overridden by file_override */
{ "File Override", "phil@company.xx", "unknown", "phil@company.xx" },
{ "Other Name", "fileoverridename@company.xx", "override", "fileoverridename@company.xx" }
};
void test_mailmap_parsing__file_config(void)
{
g_repo = cl_git_sandbox_init("mailmap");
cl_git_pass(git_repository_config(&g_config, g_repo));
cl_git_pass(git_config_set_string(
g_config, "mailmap.file", cl_fixture("mailmap/file_override")));
cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
/* Check we don't have duplicate entries */
cl_assert_equal_sz(git_vector_length(&g_mailmap->entries), 9);
/* Check that resolving the entries works */
check_mailmap_resolve(
g_mailmap, resolved_with_file_override,
ARRAY_SIZE(resolved_with_file_override));
}
static const mailmap_entry resolved_with_blob_override[] = {
{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" },
/* This name is overridden by blob_override */
{ "Blob Override", "phil@company.xx", "unknown", "phil@company.xx" },
{ "Other Name", "bloboverridename@company.xx", "override", "bloboverridename@company.xx" }
};
void test_mailmap_parsing__blob_config(void)
{
g_repo = cl_git_sandbox_init("mailmap");
cl_git_pass(git_repository_config(&g_config, g_repo));
cl_git_pass(git_config_set_string(
g_config, "mailmap.blob", "HEAD:blob_override"));
cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
/* Check we don't have duplicate entries */
cl_assert_equal_sz(git_vector_length(&g_mailmap->entries), 9);
/* Check that resolving the entries works */
check_mailmap_resolve(
g_mailmap, resolved_with_blob_override,
ARRAY_SIZE(resolved_with_blob_override));
}
static const mailmap_entry bare_resolved_with_blob_override[] = {
/* As mailmap.blob is set, we won't load HEAD:.mailmap */
{ "Brad", "cto@coompany.xx", "Brad", "cto@coompany.xx" },
{ "Brad L", "cto@coompany.xx", "Brad L", "cto@coompany.xx" },
{ "nick1", "bugs@company.xx", "nick1", "bugs@company.xx" },
{ "nick2", "bugs@company.xx", "nick2", "bugs@company.xx" },
{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
{ "Some Garbage", "nick2@company.xx", "Some Garbage", "nick2@company.xx" },
{ "Joseph", "bugs@company.xx", "Joseph", "bugs@company.xx" },
{ "Clause", "me@company.xx", "Clause", "me@company.xx" },
{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" },
/* This name is overridden by blob_override */
{ "Blob Override", "phil@company.xx", "unknown", "phil@company.xx" },
{ "Other Name", "bloboverridename@company.xx", "override", "bloboverridename@company.xx" }
};
void test_mailmap_parsing__bare_blob_config(void)
{
g_repo = cl_git_sandbox_init("mailmap/.gitted");
cl_git_pass(git_repository_set_bare(g_repo));
cl_check(git_repository_is_bare(g_repo));
cl_git_pass(git_repository_config(&g_config, g_repo));
cl_git_pass(git_config_set_string(
g_config, "mailmap.blob", "HEAD:blob_override"));
cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
/* Check that we only have the 2 entries */
cl_assert_equal_sz(git_vector_length(&g_mailmap->entries), 2);
/* Check that resolving the entries works */
check_mailmap_resolve(
g_mailmap, bare_resolved_with_blob_override,
ARRAY_SIZE(bare_resolved_with_blob_override));
}
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
Unnamed repository; edit this file 'description' to name the repository.
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
f63578091d884c3066a003c50eb6c85ae7542269
# Simple Comment line
<cto@company.xx> <cto@coompany.xx>
Some Dude <some@dude.xx> nick1 <bugs@company.xx>
Other Author <other@author.xx> nick2 <bugs@company.xx>
Other Author <other@author.xx> <nick2@company.xx>
Phil Hill <phil@company.xx> # Comment at end of line
<joseph@company.xx> Joseph <bugs@company.xx>
Santa Claus <santa.claus@northpole.xx> <me@company.xx>
Untracked <untracked@company.xx>
Added by Brad <cto@coompany.xx>
Added by Brad L. <cto@coompany.xx>
Added by nick1 <bugs@company.xx>
Added by nick2 <bugs@company.xx>
Added by nick3 <bugs@company.xx>
Added by Some Garbage <nick2@company.xx>
Added by unknown <phil@company.xx>
Added by Joseph <bugs@company.xx>
Added by Clause <me@company.xx>
Added by Charles <charles@charles.xx>
File Override <phil@company.xx>
Other Name <fileoverridename@company.xx>
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