1 /*
2     Copyright (C) 2009 Andrew Caudwell (acaudwell@gmail.com)
3 
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License
6     as published by the Free Software Foundation; either version
7     3 of the License, or (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "custom.h"
19 
20 Regex custom_regex("^(?:\\xEF\\xBB\\xBF)?(-?[0-9]+)\\|([^|]*)\\|([ADM]?)\\|([^|]+)(?:\\|#?([a-fA-F0-9]{6}))?");
21 
CustomLog(const std::string & logfile)22 CustomLog::CustomLog(const std::string& logfile) : RCommitLog(logfile) {
23 }
24 
parseColour(const std::string & cstr)25 vec3 CustomLog::parseColour(const std::string& cstr) {
26 
27     vec3 colour;
28     int r,g,b;
29 
30     if(sscanf(cstr.c_str(), "%02x%02x%02x", &r, &g, &b) == 3) {
31         colour = vec3( r, g, b );
32         colour /= 255.0f;
33     }
34 
35     return colour;
36 }
37 
38 // parse modified cvs format log entries
39 
parseCommit(RCommit & commit)40 bool CustomLog::parseCommit(RCommit& commit) {
41 
42     while(parseCommitEntry(commit));
43 
44     return !commit.files.empty();
45 }
46 
parseCommitEntry(RCommit & commit)47 bool CustomLog::parseCommitEntry(RCommit& commit) {
48 
49     std::string line;
50     std::vector<std::string> entries;
51 
52     if(!getNextLine(line)) return false;
53 
54     //custom line
55     if(!custom_regex.match(line, &entries)) return false;
56 
57     long timestamp       = atol(entries[0].c_str());
58 
59     std::string username = (entries[1].size()>0) ? entries[1] : "Unknown";
60     std::string action   = (entries[2].size()>0) ? entries[2] : "A";
61 
62     //if this file is for the same person and timestamp
63     //we add to the commit, else we save the lastline
64     //and return false
65     if(commit.files.empty()) {
66         commit.timestamp = timestamp;
67         commit.username  = username;
68     } else {
69         if(commit.timestamp != timestamp || commit.username  != username) {
70             lastline = line;
71             return false;
72         }
73     }
74 
75     bool has_colour = false;
76     vec3 colour;
77 
78     if(entries.size()>=5 && entries[4].size()>0) {
79         has_colour = true;
80         colour = parseColour(entries[4]);
81     }
82 
83     if(has_colour) {
84         commit.addFile(entries[3], action, colour);
85     } else {
86         commit.addFile(entries[3], action);
87     }
88 
89     return true;
90 }
91