summaryrefslogtreecommitdiff
path: root/committers.cxx
blob: f00033412f5f2c2b0b9ce3602acc6c2dba8766c0 (plain)
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
/*
 * Read committers from a file, assign commits to them.
 *
 * Author: Jan Holesovsky <[email protected]>
 * License: MIT <http://www.opensource.org/licenses/mit-license.php>
 */

#include "committers.hxx"
#include "error.hxx"

#include <cstring>
#include <fstream>
#include <string>

using namespace std;

typedef std::map< string, Committer > CommittersMap;

static CommittersMap committers;

static string default_address( "@localhost" );

void Committers::load( const char *fname )
{
    ifstream input( fname, ifstream::in );
    string line;

    while ( !input.eof() )
    {
        getline( input, line );

        // comments
        if ( line.length() == 0 || line[0] == '#' )
            continue;

        // default committer address
        if ( line[0] == '@' )
        {
            default_address = line;
            continue;
        }

        // find the separators
        size_t delim1 = line.find( "|" );
        size_t delim2 = ( delim1 == string::npos )? string::npos: line.find( "|", delim1 + 1 );

        if ( delim2 == string::npos )
        {
            Error::report( "Wrong committer '" + line + "'" );
            continue;
        }

        // store the data
        string login = line.substr( 0, delim1 );
        committers[login] = Committer( line.substr( delim1 + 1, delim2 - delim1 - 1 ),
                                       line.substr( delim2 + 1 ) );
    }
}

const Committer& Committers::getAuthor( const char* name )
{
    return getAuthor( string( name ) );
}

const Committer& Committers::getAuthor( const string& name )
{
    CommittersMap::iterator it( committers.find( name ) );

    if ( it != committers.end() )
        return it->second;

    // name + email
    size_t addr = name.rfind( " <" );
    if ( addr != string::npos )
    {
        size_t at = name.find( "@", addr );
        if ( at != string::npos )
        {
            size_t end = name.find( ">", at );
            if ( end != string::npos )
            {
                return ( committers[name] = Committer( name.substr( 0, addr ) , name.substr( addr + 2, end - addr - 2 ) ) );
            }
        }
    }

    // email only
    size_t at = name.find( "@" );
    if ( at != string::npos )
    {
        return ( committers[name] = Committer( name.substr( 0, at ) , name ) );
    }

    Error::report( string( "Author '" ) + name + "' is missing, adding as '" + name + default_address + "'" );
    return ( committers[name] = Committer( name, name + default_address ) );
}