~ubuntu-branches/debian/wheezy/gource/wheezy

« back to all changes in this revision

Viewing changes to src/formats/hg.cpp

  • Committer: Package Import Robot
  • Author(s): Andrew Caudwell
  • Date: 2012-04-24 11:25:45 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20120424112545-18fbnycu9xrsl4s5
Tags: 0.38-1
* New upstream release (closes: #667189)
* New build dependencies on libglm-dev and libboost-filesystem-dev. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "hg.h"
 
19
 
 
20
Regex hg_regex("^([0-9]+) -?[0-9]+\\|([^|]+)\\|([ADM]?)\\|(.+)$");
 
21
 
 
22
// parse Mercurial log entries (using the gource.style template)
 
23
 
 
24
std::string gGourceMercurialCommand() {
 
25
 
 
26
    std::string gource_style_path = gSDLAppResourceDir + std::string("gource.style");
 
27
 
 
28
    return std::string("hg log -r 0:tip --style \"") + gource_style_path + std::string("\"");
 
29
}
 
30
 
 
31
MercurialLog::MercurialLog(const std::string& logfile) : RCommitLog(logfile) {
 
32
 
 
33
    log_command = gGourceMercurialCommand();
 
34
 
 
35
    //can generate log from directory
 
36
    if(!logf && is_dir) {
 
37
        logf = generateLog(logfile);
 
38
 
 
39
        if(logf) {
 
40
            success  = true;
 
41
            seekable = true;
 
42
        }
 
43
    }
 
44
}
 
45
 
 
46
BaseLog* MercurialLog::generateLog(const std::string& dir) {
 
47
 
 
48
    //does directory have a .hg ?
 
49
    std::string hgdir = dir + std::string("/.hg");
 
50
    struct stat dirinfo;
 
51
    int stat_rc = stat(hgdir.c_str(), &dirinfo);
 
52
    if(stat_rc!=0 || !(dirinfo.st_mode & S_IFDIR)) {
 
53
        return 0;
 
54
    }
 
55
 
 
56
    // do we have this client installed
 
57
    requireExecutable("hg");
 
58
 
 
59
    std::string command = getLogCommand();
 
60
 
 
61
    createTempLog();
 
62
 
 
63
    if(temp_file.size()==0) return 0;
 
64
 
 
65
    char cmd_buff[2048];
 
66
    sprintf(cmd_buff, "%s -R \"%s\" > %s", command.c_str(), dir.c_str(), temp_file.c_str());
 
67
 
 
68
    int command_rc = systemCommand(cmd_buff);
 
69
 
 
70
    if(command_rc != 0) {
 
71
        return 0;
 
72
    }
 
73
 
 
74
    BaseLog* seeklog = new SeekLog(temp_file);
 
75
 
 
76
    return seeklog;
 
77
}
 
78
 
 
79
 
 
80
bool MercurialLog::parseCommit(RCommit& commit) {
 
81
 
 
82
    while(parseCommitEntry(commit));
 
83
 
 
84
    return !commit.files.empty();
 
85
}
 
86
 
 
87
bool MercurialLog::parseCommitEntry(RCommit& commit) {
 
88
 
 
89
    std::string line;
 
90
    std::vector<std::string> entries;
 
91
 
 
92
    if(!logf->getNextLine(line)) return false;
 
93
 
 
94
    //custom line
 
95
    if(!hg_regex.match(line, &entries)) return false;
 
96
 
 
97
    time_t timestamp     = atol(entries[0].c_str());
 
98
    std::string username = entries[1];
 
99
 
 
100
    //if this file is for the same person and timestamp
 
101
    //we add to the commit, else we save the lastline
 
102
    //and return false
 
103
    if(commit.files.empty()) {
 
104
        commit.timestamp = timestamp;
 
105
        commit.username  = username;
 
106
    } else {
 
107
        if(commit.timestamp != timestamp || commit.username  != username) {
 
108
            lastline = line;
 
109
            return false;
 
110
        }
 
111
    }
 
112
 
 
113
    std::string action = "A";
 
114
 
 
115
    if(!entries[2].empty()) {
 
116
        action = entries[2];
 
117
    }
 
118
 
 
119
    commit.addFile(entries[3], action);
 
120
 
 
121
    //commit.debug();
 
122
 
 
123
    return true;
 
124
}