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

« back to all changes in this revision

Viewing changes to src/logmill.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) 2012 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 "logmill.h"
 
19
#include "gource_settings.h"
 
20
 
 
21
#include "formats/git.h"
 
22
#include "formats/gitraw.h"
 
23
#include "formats/custom.h"
 
24
#include "formats/hg.h"
 
25
#include "formats/bzr.h"
 
26
#include "formats/svn.h"
 
27
#include "formats/apache.h"
 
28
 
 
29
extern "C" {
 
30
 
 
31
    static int logmill_thread(void *lmill) {
 
32
 
 
33
        RLogMill *logmill = static_cast<RLogMill*> (lmill);
 
34
        logmill->run();
 
35
 
 
36
        return 0;
 
37
    }
 
38
 
 
39
};
 
40
 
 
41
RLogMill::RLogMill(const std::string& logfile)
 
42
    : logfile(logfile) {
 
43
 
 
44
    logmill_thread_state = LOGMILL_STATE_STARTUP;
 
45
    clog = 0;
 
46
 
 
47
#if SDL_VERSION_ATLEAST(1,3,0)
 
48
    thread = SDL_CreateThread( logmill_thread, "logmill", this );
 
49
#else
 
50
    thread = SDL_CreateThread( logmill_thread, this );
 
51
#endif
 
52
}
 
53
 
 
54
RLogMill::~RLogMill() {
 
55
 
 
56
    abort();
 
57
 
 
58
    if(thread) SDL_KillThread(thread);
 
59
    if(clog) delete clog;
 
60
}
 
61
 
 
62
void RLogMill::run() {
 
63
    logmill_thread_state = LOGMILL_STATE_FETCHING;
 
64
 
 
65
#if defined(HAVE_PTHREAD) && !defined(_WIN32)
 
66
    sigset_t mask;
 
67
    sigemptyset(&mask);
 
68
 
 
69
    // unblock SIGINT so user can cancel
 
70
    // NOTE: assumes SDL is using pthreads
 
71
 
 
72
    sigaddset(&mask, SIGINT);
 
73
    pthread_sigmask(SIG_UNBLOCK, &mask, 0);
 
74
#endif
 
75
 
 
76
    std::string log_format = gGourceSettings.log_format;
 
77
 
 
78
    try {
 
79
 
 
80
        clog = fetchLog(log_format);
 
81
 
 
82
    } catch(SeekLogException& exception) {
 
83
        error = "unable to read log file";
 
84
    } catch(SDLAppException& exception) {
 
85
        error = exception.what();
 
86
    }
 
87
 
 
88
    if(!clog && error.empty()) {
 
89
        if(SDLAppDirExists(logfile)) {
 
90
            if(!log_format.empty()) {
 
91
                error = "failed to generate log file";
 
92
            } else {
 
93
                error = "directory not supported";
 
94
            }
 
95
        } else {
 
96
            error = "unsupported log format (you may need to regenerate your log file)";
 
97
        }
 
98
    }
 
99
 
 
100
    logmill_thread_state = clog ? LOGMILL_STATE_SUCCESS : LOGMILL_STATE_FAILURE;
 
101
}
 
102
 
 
103
void RLogMill::abort() {
 
104
    // TODO: make abort nicer by notifying the log process
 
105
    //       we want to shutdown
 
106
 
 
107
    while(logmill_thread_state <= LOGMILL_STATE_FETCHING) {
 
108
        SDL_Delay(100);
 
109
    }
 
110
}
 
111
 
 
112
bool RLogMill::isFinished() {
 
113
    return logmill_thread_state > LOGMILL_STATE_FETCHING;
 
114
}
 
115
 
 
116
int RLogMill::getStatus() {
 
117
    return logmill_thread_state;
 
118
}
 
119
 
 
120
std::string RLogMill::getError() {
 
121
    return error;
 
122
}
 
123
 
 
124
 
 
125
RCommitLog* RLogMill::getLog() {
 
126
 
 
127
    while(logmill_thread_state <= LOGMILL_STATE_FETCHING) {
 
128
        SDL_Delay(100);
 
129
    }
 
130
 
 
131
    return clog;
 
132
}
 
133
 
 
134
bool RLogMill::findRepository(boost::filesystem::path& dir, std::string& log_format) {
 
135
 
 
136
    dir = absolute(dir);
 
137
 
 
138
    //fprintf(stderr, "find repository from initial path: %s\n", dir.string().c_str());
 
139
 
 
140
    while(is_directory(dir)) {
 
141
 
 
142
             if(is_directory(dir / ".git")) log_format = "git";
 
143
        else if(is_directory(dir / ".hg"))  log_format = "hg";
 
144
        else if(is_directory(dir / ".bzr")) log_format = "bzr";
 
145
        else if(is_directory(dir / ".svn")) log_format = "svn";
 
146
 
 
147
        if(!log_format.empty()) {
 
148
            //fprintf(stderr, "found '%s' repository at: %s\n", log_format.c_str(), dir.string().c_str());
 
149
            return true;
 
150
        }
 
151
 
 
152
        if(!dir.has_parent_path()) return false;
 
153
 
 
154
        dir = dir.parent_path();
 
155
    }
 
156
 
 
157
    return false;
 
158
}
 
159
 
 
160
 
 
161
RCommitLog* RLogMill::fetchLog(std::string& log_format) {
 
162
 
 
163
    RCommitLog* clog = 0;
 
164
 
 
165
    //if the log format is not specified and 'logfile' is a directory, recursively look for a version control repository.
 
166
    //this method allows for something strange like someone who having an svn repository inside a git repository
 
167
    //(in which case it would pick the svn directory as it would encounter that first)
 
168
 
 
169
    if(log_format.empty() && logfile != "-") {
 
170
 
 
171
        try {
 
172
            boost::filesystem::path repo_path(logfile);
 
173
 
 
174
            if(is_directory(repo_path)) {
 
175
                if(findRepository(repo_path, log_format)) {
 
176
                    logfile = repo_path.string();
 
177
                }
 
178
            }
 
179
        } catch(boost::filesystem3::filesystem_error& error) {
 
180
        }
 
181
    }
 
182
 
 
183
    //we've been told what format to use
 
184
    if(log_format.size() > 0) {
 
185
        debugLog("log-format = %s", log_format.c_str());
 
186
 
 
187
        if(log_format == "git") {
 
188
            clog = new GitCommitLog(logfile);
 
189
            if(clog->checkFormat()) return clog;
 
190
            delete clog;
 
191
 
 
192
            clog = new GitRawCommitLog(logfile);
 
193
            if(clog->checkFormat()) return clog;
 
194
            delete clog;
 
195
        }
 
196
 
 
197
        if(log_format == "hg") {
 
198
            clog = new MercurialLog(logfile);
 
199
            if(clog->checkFormat()) return clog;
 
200
            delete clog;
 
201
        }
 
202
 
 
203
        if(log_format == "bzr") {
 
204
            clog = new BazaarLog(logfile);
 
205
            if(clog->checkFormat()) return clog;
 
206
            delete clog;
 
207
        }
 
208
 
 
209
        if(log_format == "cvs") {
 
210
            clog = new CVSEXPCommitLog(logfile);
 
211
            if(clog->checkFormat()) return clog;
 
212
            delete clog;
 
213
        }
 
214
 
 
215
        if(log_format == "custom") {
 
216
            clog = new CustomLog(logfile);
 
217
            if(clog->checkFormat()) return clog;
 
218
            delete clog;
 
219
        }
 
220
 
 
221
        if(log_format == "apache") {
 
222
            clog = new ApacheCombinedLog(logfile);
 
223
            if(clog->checkFormat()) return clog;
 
224
            delete clog;
 
225
        }
 
226
 
 
227
        if(log_format == "svn") {
 
228
            clog = new SVNCommitLog(logfile);
 
229
            if(clog->checkFormat()) return clog;
 
230
            delete clog;
 
231
        }
 
232
 
 
233
        if(log_format == "cvs2cl") {
 
234
            clog = new CVS2CLCommitLog(logfile);
 
235
            if(clog->checkFormat()) return clog;
 
236
            delete clog;
 
237
        }
 
238
 
 
239
        return 0;
 
240
    }
 
241
 
 
242
    // try different formats until one works
 
243
 
 
244
    //git
 
245
    debugLog("trying git...");
 
246
    clog = new GitCommitLog(logfile);
 
247
    if(clog->checkFormat()) return clog;
 
248
 
 
249
    delete clog;
 
250
 
 
251
    //mercurial
 
252
    debugLog("trying mercurial...");
 
253
    clog = new MercurialLog(logfile);
 
254
    if(clog->checkFormat()) return clog;
 
255
 
 
256
    delete clog;
 
257
 
 
258
    //bzr
 
259
    debugLog("trying bzr...");
 
260
    clog = new BazaarLog(logfile);
 
261
    if(clog->checkFormat()) return clog;
 
262
 
 
263
    delete clog;
 
264
 
 
265
    //git raw
 
266
    debugLog("trying git raw...");
 
267
    clog = new GitRawCommitLog(logfile);
 
268
    if(clog->checkFormat()) return clog;
 
269
 
 
270
    delete clog;
 
271
 
 
272
    //cvs exp
 
273
    debugLog("trying cvs-exp...");
 
274
    clog = new CVSEXPCommitLog(logfile);
 
275
    if(clog->checkFormat()) return clog;
 
276
 
 
277
    delete clog;
 
278
 
 
279
    //svn
 
280
    debugLog("trying svn...");
 
281
    clog = new SVNCommitLog(logfile);
 
282
    if(clog->checkFormat()) return clog;
 
283
 
 
284
    delete clog;
 
285
 
 
286
    //cvs2cl
 
287
    debugLog("trying cvs2cl...");
 
288
    clog = new CVS2CLCommitLog(logfile);
 
289
    if(clog->checkFormat()) return clog;
 
290
 
 
291
    delete clog;
 
292
 
 
293
    //custom
 
294
    debugLog("trying custom...");
 
295
    clog = new CustomLog(logfile);
 
296
    if(clog->checkFormat()) return clog;
 
297
 
 
298
    delete clog;
 
299
 
 
300
    //apache
 
301
    debugLog("trying apache combined...");
 
302
    clog = new ApacheCombinedLog(logfile);
 
303
    if(clog->checkFormat()) return clog;
 
304
 
 
305
    delete clog;
 
306
 
 
307
    return 0;
 
308
}