Skip to content

Commit 983b8c2

Browse files
committed
mailmap: Add a bunch of tests for the new mailmap functionality
1 parent e3dcaca commit 983b8c2

File tree

58 files changed

+841
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+841
-0
lines changed

tests/mailmap/blame.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "clar_libgit2.h"
2+
#include "git2/repository.h"
3+
#include "git2/blame.h"
4+
#include "git2/mailmap.h"
5+
#include "mailmap_helpers.h"
6+
7+
static git_repository *g_repo;
8+
static git_blame *g_blame;
9+
static git_mailmap *g_mailmap;
10+
11+
void test_mailmap_blame__initialize(void)
12+
{
13+
g_repo = NULL;
14+
g_blame = NULL;
15+
}
16+
17+
void test_mailmap_blame__cleanup(void)
18+
{
19+
cl_git_sandbox_cleanup();
20+
g_repo = NULL;
21+
22+
git_blame_free(g_blame);
23+
g_blame = NULL;
24+
}
25+
26+
void test_mailmap_blame__hunks(void)
27+
{
28+
size_t idx = 0;
29+
const git_blame_hunk *hunk = NULL;
30+
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
31+
32+
g_repo = cl_git_sandbox_init("mailmap");
33+
34+
opts.flags |= GIT_BLAME_USE_MAILMAP;
35+
36+
cl_check_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts));
37+
if (!g_blame)
38+
return;
39+
40+
for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) {
41+
hunk = git_blame_get_hunk_byline(g_blame, idx + 1);
42+
43+
cl_assert(hunk->final_signature != NULL);
44+
cl_assert(hunk->orig_signature != NULL);
45+
cl_assert_equal_s(hunk->final_signature->name, resolved[idx].real_name);
46+
cl_assert_equal_s(hunk->final_signature->email, resolved[idx].real_email);
47+
}
48+
}
49+
50+
void test_mailmap_blame__hunks_no_mailmap(void)
51+
{
52+
size_t idx = 0;
53+
const git_blame_hunk *hunk = NULL;
54+
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
55+
56+
g_repo = cl_git_sandbox_init("mailmap");
57+
58+
cl_check_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts));
59+
if (!g_blame)
60+
return;
61+
62+
for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) {
63+
hunk = git_blame_get_hunk_byline(g_blame, idx + 1);
64+
65+
cl_assert(hunk->final_signature != NULL);
66+
cl_assert(hunk->orig_signature != NULL);
67+
cl_assert_equal_s(hunk->final_signature->name, resolved[idx].replace_name);
68+
cl_assert_equal_s(hunk->final_signature->email, resolved[idx].replace_email);
69+
}
70+
}

tests/mailmap/mailmap_helpers.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "git2/mailmap.h"
2+
3+
typedef struct mailmap_entry {
4+
const char *real_name;
5+
const char *real_email;
6+
const char *replace_name;
7+
const char *replace_email;
8+
} mailmap_entry;
9+
10+
static const char string_mailmap[] =
11+
"# Simple Comment line\n"
12+
"<cto@company.xx> <cto@coompany.xx>\n"
13+
"Some Dude <some@dude.xx> nick1 <bugs@company.xx>\n"
14+
"Other Author <other@author.xx> nick2 <bugs@company.xx>\n"
15+
"Other Author <other@author.xx> <nick2@company.xx>\n"
16+
"Phil Hill <phil@company.xx> # Comment at end of line\n"
17+
"<joseph@company.xx> Joseph <bugs@company.xx>\n"
18+
"Santa Claus <santa.claus@northpole.xx> <me@company.xx>\n"
19+
"Untracked <untracked@company.xx>";
20+
21+
static const mailmap_entry entries[] = {
22+
{ NULL, "cto@company.xx", NULL, "cto@coompany.xx" },
23+
{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
24+
{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
25+
{ "Other Author", "other@author.xx", NULL, "nick2@company.xx" },
26+
{ "Phil Hill", NULL, NULL, "phil@company.xx" },
27+
{ NULL, "joseph@company.xx", "Joseph", "bugs@company.xx" },
28+
{ "Santa Claus", "santa.claus@northpole.xx", NULL, "me@company.xx" },
29+
/* This entry isn't in the bare repository */
30+
{ "Untracked", NULL, NULL, "untracked@company.xx" }
31+
};
32+
33+
static const mailmap_entry resolved[] = {
34+
{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
35+
{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
36+
{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
37+
{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
38+
{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
39+
{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
40+
{ "Phil Hill", "phil@company.xx", "unknown", "phil@company.xx" },
41+
{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
42+
{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
43+
{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" }
44+
};
45+
46+
static const mailmap_entry resolved_bare[] = {
47+
{ "xx", "untracked@company.xx", "xx", "untracked@company.xx" }
48+
};
49+
50+
static const mailmap_entry resolved_untracked[] = {
51+
{ "Untracked", "untracked@company.xx", "xx", "untracked@company.xx" }
52+
};

tests/mailmap/parsing.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include "clar_libgit2.h"
2+
#include "repository.h"
3+
#include "git2/sys/repository.h"
4+
#include "mailmap_helpers.h"
5+
6+
static git_repository *g_repo;
7+
static git_mailmap *g_mailmap;
8+
9+
void test_mailmap_parsing__initialize(void)
10+
{
11+
g_repo = NULL;
12+
g_mailmap = NULL;
13+
}
14+
15+
void test_mailmap_parsing__cleanup(void)
16+
{
17+
cl_git_sandbox_cleanup();
18+
g_repo = NULL;
19+
20+
git_mailmap_free(g_mailmap);
21+
g_mailmap = NULL;
22+
}
23+
24+
static void check_mailmap_entries(
25+
const git_mailmap *mailmap, const mailmap_entry *entries, size_t entries_size)
26+
{
27+
const mailmap_entry *parsed = NULL;
28+
size_t idx = 0;
29+
30+
/* Check that the parsed entries match */
31+
cl_assert_equal_sz(entries_size, git_mailmap_entry_count(mailmap));
32+
for (idx = 0; idx < entries_size; ++idx) {
33+
parsed = git_mailmap_entry_byindex(mailmap, idx);
34+
cl_assert_equal_s(parsed->real_name, entries[idx].real_name);
35+
cl_assert_equal_s(parsed->real_email, entries[idx].real_email);
36+
cl_assert_equal_s(parsed->replace_name, entries[idx].replace_name);
37+
cl_assert_equal_s(parsed->replace_email, entries[idx].replace_email);
38+
}
39+
}
40+
41+
static void check_mailmap_resolve(
42+
const git_mailmap *mailmap, const mailmap_entry *resolved, size_t resolved_size)
43+
{
44+
const char *resolved_name = NULL;
45+
const char *resolved_email = NULL;
46+
size_t idx = 0;
47+
48+
/* Check that the resolver behaves correctly */
49+
for (idx = 0; idx < resolved_size; ++idx) {
50+
git_mailmap_resolve(
51+
&resolved_name,
52+
&resolved_email,
53+
mailmap,
54+
resolved[idx].replace_name,
55+
resolved[idx].replace_email);
56+
cl_assert_equal_s(resolved_name, resolved[idx].real_name);
57+
cl_assert_equal_s(resolved_email, resolved[idx].real_email);
58+
}
59+
}
60+
61+
void test_mailmap_parsing__string(void)
62+
{
63+
cl_check_pass(git_mailmap_parse(
64+
&g_mailmap,
65+
string_mailmap,
66+
strlen(string_mailmap)));
67+
68+
/* We should have parsed all of the entries */
69+
check_mailmap_entries(
70+
g_mailmap,
71+
entries, ARRAY_SIZE(entries));
72+
73+
/* Check that resolving the entries works */
74+
check_mailmap_resolve(
75+
g_mailmap,
76+
resolved, ARRAY_SIZE(resolved));
77+
check_mailmap_resolve(
78+
g_mailmap,
79+
resolved_untracked, ARRAY_SIZE(resolved_untracked));
80+
}
81+
82+
void test_mailmap_parsing__fromrepo(void)
83+
{
84+
g_repo = cl_git_sandbox_init("mailmap");
85+
cl_check(!git_repository_is_bare(g_repo));
86+
87+
cl_check_pass(git_mailmap_from_repo(&g_mailmap, g_repo));
88+
89+
/* We should have parsed all of the entries */
90+
check_mailmap_entries(
91+
g_mailmap,
92+
entries, ARRAY_SIZE(entries));
93+
94+
/* Check that resolving the entries works */
95+
check_mailmap_resolve(
96+
g_mailmap,
97+
resolved, ARRAY_SIZE(resolved));
98+
check_mailmap_resolve(
99+
g_mailmap,
100+
resolved_untracked, ARRAY_SIZE(resolved_untracked));
101+
}
102+
103+
void test_mailmap_parsing__frombare(void)
104+
{
105+
g_repo = cl_git_sandbox_init("mailmap/.gitted");
106+
cl_check_pass(git_repository_set_bare(g_repo));
107+
cl_check(git_repository_is_bare(g_repo));
108+
109+
cl_check_pass(git_mailmap_from_repo(&g_mailmap, g_repo));
110+
111+
/* We should have parsed all of the entries, except for the untracked one */
112+
check_mailmap_entries(
113+
g_mailmap,
114+
entries, ARRAY_SIZE(entries) - 1);
115+
116+
/* Check that resolving the entries works */
117+
check_mailmap_resolve(
118+
g_mailmap,
119+
resolved, ARRAY_SIZE(resolved));
120+
check_mailmap_resolve(
121+
g_mailmap,
122+
resolved_bare, ARRAY_SIZE(resolved_bare));
123+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+.mailmap
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
14+
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
15+
:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
test "" = "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22+
echo >&2 Duplicate Signed-off-by lines.
23+
exit 1
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to prepare a packed repository for use over
4+
# dumb transports.
5+
#
6+
# To enable this hook, rename this file to "post-update".
7+
8+
exec git update-server-info

0 commit comments

Comments
 (0)