Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Search for git path recursively #1

Closed
wants to merge 2 commits into from

2 participants

Jörg Bachmann Andrew Caudwell
Jörg Bachmann

Hi,

the above mentioned feature was just a small issue when i saw gource today the first time. I really like that program, but i wanted it to automatically go to the top git directory (e.g. when i'm in the src directory and like to run gource). Here just a hint in form of code. Perhaps you could introduce such a feature?

Andrew Caudwell
Owner

Hi. This is a good idea.

Could you maybe generalize this into findRepositoryRoot( logfile, ".git" ) and make it a method of CommitLog, and then all the VCS can use it.

It might also be a bit brittle right now. Maybe you should append the ../ to the path, get stat to confirm it is a directory, then test for the sub directory name.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Nov 09, 2011
frygge Search for git path recursively 09cff20
Nov 11, 2011
frygge Introduced common recursive VCS path search 325a3f2
This page is out of date. Refresh to see the latest.
2  src/bzr.cpp
@@ -26,7 +26,7 @@ std::string gGourceBzrLogCommand() {
26 26 return std::string("bzr log --verbose -r 1..-1 --short -n0 --forward");
27 27 }
28 28
29   -BazaarLog::BazaarLog(const std::string& logfile) : RCommitLog(logfile) {
  29 +BazaarLog::BazaarLog(const std::string& logfile) : RCommitLog(logfile, ".bzr") {
30 30
31 31 log_command = gGourceBzrLogCommand();
32 32
80 src/commitlog.cpp
@@ -16,6 +16,9 @@
16 16 */
17 17
18 18 #include "commitlog.h"
  19 +#include <sys/param.h>
  20 +#include <unistd.h>
  21 +
19 22
20 23 std::string munge_utf8(const std::string& str) {
21 24
@@ -32,7 +35,9 @@ std::string munge_utf8(const std::string& str) {
32 35
33 36 //RCommitLog
34 37
35   -RCommitLog::RCommitLog(const std::string& logfile, int firstChar) {
  38 +RCommitLog::RCommitLog(const std::string& logfile, const std::string& vcssub, int firstChar) :
  39 + logfile( logfile == "-" ? "-" : findRepositoryRoot( logfile, vcssub ) )
  40 +{
36 41
37 42 logf = 0;
38 43 seekable = false;
@@ -40,7 +45,7 @@ RCommitLog::RCommitLog(const std::string& logfile, int firstChar) {
40 45 is_dir = false;
41 46 buffered = false;
42 47
43   - if(logfile == "-") {
  48 + if(this->logfile == "-") {
44 49
45 50 //check first char
46 51 if(checkFirstChar(firstChar, std::cin)) {
@@ -54,7 +59,7 @@ RCommitLog::RCommitLog(const std::string& logfile, int firstChar) {
54 59 }
55 60
56 61 struct stat fileinfo;
57   - int rc = stat(logfile.c_str(), &fileinfo);
  62 + int rc = stat(this->logfile.c_str(), &fileinfo);
58 63
59 64 if(rc==0) {
60 65 is_dir = (fileinfo.st_mode & S_IFDIR) ? true : false;
@@ -62,14 +67,14 @@ RCommitLog::RCommitLog(const std::string& logfile, int firstChar) {
62 67 if(!is_dir) {
63 68
64 69 //check first char
65   - std::ifstream testf(logfile.c_str());
  70 + std::ifstream testf(this->logfile.c_str());
66 71
67 72 bool firstOK = checkFirstChar(firstChar, testf);
68 73
69 74 testf.close();
70 75
71 76 if(firstOK) {
72   - logf = new SeekLog(logfile);
  77 + logf = new SeekLog(this->logfile);
73 78 seekable = true;
74 79 success = true;
75 80 }
@@ -206,6 +211,70 @@ bool RCommitLog::isFinished() {
206 211 return false;
207 212 }
208 213
  214 +const std::string RCommitLog::findRepositoryRoot(const std::string& prjdir, const std::string& vcssub) {
  215 + std::string canonicaldir;
  216 +
  217 + if( prjdir[0] != '/' ) {
  218 + int cwdsize = 512;
  219 + char* cwd = new char[ cwdsize ];
  220 +
  221 + // get current working directory
  222 + while( NULL == getcwd( cwd, cwdsize ) ) {
  223 + cwdsize *= 2;
  224 + delete cwd;
  225 + cwd = new char[ cwdsize ];
  226 + }
  227 +
  228 + // get absoloute path of prjdir
  229 + std::string concatenated = std::string( cwd ) + "/" + prjdir;
  230 +
  231 + // convert to canonical path
  232 + char canonicalcstr[MAXPATHLEN];
  233 + if( NULL == realpath( concatenated.c_str(), canonicalcstr ) ) {
  234 + std::cerr << std::string( "Could not resolve path " ) + canonicalcstr + ".";
  235 + return prjdir;
  236 + }
  237 +
  238 + canonicaldir = canonicalcstr;
  239 + }
  240 + else
  241 + {
  242 + // convert to canonical path
  243 + char canonicalcstr[MAXPATHLEN];
  244 + if( NULL != realpath( prjdir.c_str(), canonicalcstr ) ) {
  245 + std::cerr << std::string( "Could not resolve path " ) + canonicalcstr + ".";
  246 + return prjdir;
  247 + }
  248 +
  249 + canonicaldir = canonicalcstr;
  250 + }
  251 +
  252 + // does that path exist?!
  253 + struct stat st;
  254 + if( 0 != stat( canonicaldir.c_str(), &st ) ) {
  255 + return prjdir;
  256 + }
  257 +
  258 + // search for vcssubdir recursively to root
  259 + while( true ) {
  260 + std::string d = canonicaldir + "/" + vcssub;
  261 +
  262 + if( 0 == stat( d.c_str(), &st ) ) {
  263 + return canonicaldir;
  264 + }
  265 +
  266 + size_t slashindex = canonicaldir.find_last_of( "/\\" );
  267 +
  268 + // we are at root an have not been successfully
  269 + if( slashindex == std::string::npos || slashindex == 0 ) {
  270 + return prjdir;
  271 + }
  272 +
  273 + // go one step to root
  274 + canonicaldir[slashindex] = 0;
  275 + }
  276 +}
  277 +
209 278 //create temp file
210 279 void RCommitLog::createTempLog() {
211 280
@@ -296,3 +365,4 @@ void RCommit::debug() {
296 365 debugLog("%s %s\n", f.action.c_str(), f.filename.c_str());
297 366 }
298 367 }
  368 +
6 src/commitlog.h
@@ -81,8 +81,10 @@ class RCommitLog {
81 81 bool getNextLine(std::string& line);
82 82
83 83 virtual bool parseCommit(RCommit& commit) { return false; };
  84 +
  85 + const std::string logfile;
84 86 public:
85   - RCommitLog(const std::string& logfile, int firstChar = -1);
  87 + RCommitLog(const std::string& logfile, const std::string& vcssub = "", int firstChar = -1);
86 88 ~RCommitLog();
87 89
88 90 void seekTo(float percent);
@@ -97,6 +99,8 @@ class RCommitLog {
97 99 bool isFinished();
98 100 bool isSeekable();
99 101 float getPercent();
  102 +
  103 + static const std::string findRepositoryRoot(const std::string& prjdir, const std::string& vcssub);
100 104 };
101 105
102 106 #endif
2  src/cvs2cl.cpp
@@ -24,7 +24,7 @@ Regex cvs2cl_logentry_timestamp("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\
24 24
25 25 std::string gGourceCVS2CLLogCommand = "cvs2cl --chrono --stdout --xml -g-q";
26 26
27   -CVS2CLCommitLog::CVS2CLCommitLog(const std::string& logfile) : RCommitLog(logfile, '<') {
  27 +CVS2CLCommitLog::CVS2CLCommitLog(const std::string& logfile) : RCommitLog(logfile, "", '<') {
28 28 }
29 29
30 30 bool CVS2CLCommitLog::parseCommit(RCommit& commit) {
5 src/git.cpp
@@ -29,7 +29,7 @@ std::string gGourceGitLogCommand = "git log "
29 29 "--reverse --raw --encoding=UTF-8 "
30 30 "--no-renames";
31 31
32   -GitCommitLog::GitCommitLog(const std::string& logfile) : RCommitLog(logfile, 'u') {
  32 +GitCommitLog::GitCommitLog(const std::string& logfile) : RCommitLog(logfile, ".git", 'u') {
33 33
34 34 log_command = gGourceGitLogCommand;
35 35
@@ -40,7 +40,7 @@ GitCommitLog::GitCommitLog(const std::string& logfile) : RCommitLog(logfile, 'u'
40 40
41 41 //can generate log from directory
42 42 if(!logf && is_dir) {
43   - logf = generateLog(logfile);
  43 + logf = generateLog(this->logfile);
44 44
45 45 if(logf) {
46 46 success = true;
@@ -164,3 +164,4 @@ bool GitCommitLog::parseCommit(RCommit& commit) {
164 164
165 165 return true;
166 166 }
  167 +
2  src/gitraw.cpp
@@ -28,7 +28,7 @@ Regex git_raw_file("^:[0-9]+ [0-9]+ [0-9a-z]+\\.* ([0-9a-z]+)\\.* ([A-Z])[ \\t]+
28 28
29 29 std::string gGourceGitRawLogCommand = "git log --reverse --raw --pretty=raw";
30 30
31   -GitRawCommitLog::GitRawCommitLog(const std::string& logfile) : RCommitLog(logfile, 'c') {
  31 +GitRawCommitLog::GitRawCommitLog(const std::string& logfile) : RCommitLog(logfile, "", 'c') {
32 32
33 33 log_command = gGourceGitRawLogCommand;
34 34 }
2  src/gource.cpp
@@ -244,7 +244,7 @@ RCommitLog* Gource::determineFormat(const std::string& logfile) {
244 244
245 245 //git
246 246 debugLog("trying git...\n");
247   - clog = new GitCommitLog(logfile);
  247 + clog = new GitCommitLog( logfile );
248 248 if(clog->checkFormat()) return clog;
249 249
250 250 delete clog;
2  src/hg.cpp
@@ -28,7 +28,7 @@ std::string gGourceMercurialCommand() {
28 28 return std::string("hg log -r 0:tip --style \"") + gource_style_path + std::string("\"");
29 29 }
30 30
31   -MercurialLog::MercurialLog(const std::string& logfile) : RCommitLog(logfile) {
  31 +MercurialLog::MercurialLog(const std::string& logfile) : RCommitLog(logfile, ".hg") {
32 32
33 33 log_command = gGourceMercurialCommand();
34 34
2  src/svn.cpp
@@ -24,7 +24,7 @@ Regex svn_logentry_timestamp("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{
24 24
25 25 std::string gGourceSVNLogCommand = "svn log -r 1:HEAD --xml --verbose --quiet";
26 26
27   -SVNCommitLog::SVNCommitLog(const std::string& logfile) : RCommitLog(logfile, '<') {
  27 +SVNCommitLog::SVNCommitLog(const std::string& logfile) : RCommitLog(logfile, ".svn", '<') {
28 28
29 29 log_command = gGourceSVNLogCommand;
30 30

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.