tests/test-mailmap.t
author Connor Sheehan <sheehan@mozilla.com>
Mon, 19 Mar 2018 11:16:21 -0400
changeset 37210 2a2ce93e12f4
child 37260 8e57c3b0dce4
permissions -rw-r--r--
templatefuncs: add mailmap template function This commit adds a template function to support the .mailmap file in Mercurial repositories. The .mailmap file comes from git, and can be used to map new emails and names for old commits. The general use case is that someone may change their name or author commits under different emails and aliases, which would make these commits appear as though they came from different persons. The file allows you to specify the correct name that should be used in place of the author field specified in the commit. The mailmap file has 4 possible formats used to map old "commit" names to new "proper" names: 1. <proper@email.com> <commit@email.com> 2. Proper Name <commit@email.com> 3. Proper Name <proper@email.com> <commit@email.com> 4. Proper Name <proper@email.com> Commit Name <commit@email.com> Essentially there is a commit email present in each mailmap entry, that maps to either an updated name, email, or both. The final possible format allows commits authored by a person who used both an old name and an old email to map to a new name and email. To parse the file, we split by spaces and build a name out of every element that does not start with "<". Once we find an element that does start with "<" we concatenate all the name elements that preceded and add that as a parsed name. We then add the email as the first parsed email. We repeat the process until the end of the line, or a comment is found. We will be left with all parsed names in a list, and all parsed emails in a list, with the 0 index being the proper values and the 1 index being the commit values (if they were specified in the entry). The commit values are added as the keys to a dict, and with the proper fields as the values. The mapname function takes the mapping object and the commit author field and attempts to look for a corresponding entry. To do so we try (commit name, commit email) first, and if no results are returned then (None, commit email) is also looked up. This is due to format 4 from above, where someone may have a mailmap entry with both name and email, and if they don't it is possible they have an entry that uses only the commit email. Differential Revision: https://phab.mercurial-scm.org/D2904
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37210
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     1
Create a repo and add some commits
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     2
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     3
  $ hg init mm
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     4
  $ cd mm
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     5
  $ echo "Test content" > testfile1
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     6
  $ hg add testfile1
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     7
  $ hg commit -m "First commit" -u "Proper <commit@m.c>"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     8
  $ echo "Test content 2" > testfile2
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
     9
  $ hg add testfile2
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    10
  $ hg commit -m "Second commit" -u "Commit Name 2 <commit2@m.c>"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    11
  $ echo "Test content 3" > testfile3
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    12
  $ hg add testfile3
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    13
  $ hg commit -m "Third commit" -u "Commit Name 3 <commit3@m.c>"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    14
  $ echo "Test content 4" > testfile4
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    15
  $ hg add testfile4
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    16
  $ hg commit -m "Fourth commit" -u "Commit Name 4 <commit4@m.c>"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    17
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    18
Add a .mailmap file with each possible entry type plus comments
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    19
  $ cat > .mailmap << EOF
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    20
  > # Comment shouldn't break anything
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    21
  > <proper@m.c> <commit@m.c> # Should update email only
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    22
  > Proper Name 2 <commit2@m.c> # Should update name only
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    23
  > Proper Name 3 <proper@m.c> <commit3@m.c> # Should update name, email due to email
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    24
  > Proper Name 4 <proper@m.c> Commit Name 4 <commit4@m.c> # Should update name, email due to name, email
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    25
  > EOF
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    26
  $ hg add .mailmap
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    27
  $ hg commit -m "Add mailmap file" -u "Testuser <test123@m.c>"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    28
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    29
Output of commits should be normal without filter
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    30
  $ hg log -T "{author}\n" -r "all()"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    31
  Proper <commit@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    32
  Commit Name 2 <commit2@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    33
  Commit Name 3 <commit3@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    34
  Commit Name 4 <commit4@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    35
  Testuser <test123@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    36
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    37
Output of commits with filter shows their mailmap values
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    38
  $ hg log -T "{mailmap(author)}\n" -r "all()"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    39
  Proper <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    40
  Proper Name 2 <commit2@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    41
  Proper Name 3 <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    42
  Proper Name 4 <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    43
  Testuser <test123@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    44
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    45
Add new mailmap entry for testuser
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    46
  $ cat >> .mailmap << EOF
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    47
  > <newmmentry@m.c> <test123@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    48
  > EOF
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    49
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    50
Output of commits with filter shows their updated mailmap values
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    51
  $ hg log -T "{mailmap(author)}\n" -r "all()"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    52
  Proper <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    53
  Proper Name 2 <commit2@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    54
  Proper Name 3 <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    55
  Proper Name 4 <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    56
  Testuser <newmmentry@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    57
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    58
A commit with improperly formatted user field should not break the filter
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    59
  $ echo "some more test content" > testfile1
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    60
  $ hg commit -m "Commit with improper user field" -u "Improper user"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    61
  $ hg log -T "{mailmap(author)}\n" -r "all()"
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    62
  Proper <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    63
  Proper Name 2 <commit2@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    64
  Proper Name 3 <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    65
  Proper Name 4 <proper@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    66
  Testuser <newmmentry@m.c>
2a2ce93e12f4 templatefuncs: add mailmap template function
Connor Sheehan <sheehan@mozilla.com>
parents:
diff changeset
    67
  Improper user