#include <Git.h>
Public Types | |
enum | ObjectType { Tree, Commit, Blob } |
Git object type. More... | |
typedef std::list< std::pair < std::string, std::string > > | Cache |
Public Member Functions | |
Git () | |
Constructor. | |
void | setRepositoryPath (const std::string &repository) |
Set the git repository path. | |
ObjectId | getCommitTree (const std::string &revision) const |
Get the tree for a particular revision. | |
ObjectId | getCommit (const std::string &revision) const |
Get the commit for a particular revision. | |
ObjectId | getTreeFromCommit (const ObjectId &commit) const |
Get the tree for a particular commit. | |
Object | treeGetObject (const ObjectId &tree, int index) const |
Get some info on a tree object. | |
int | treeSize (const ObjectId &tree) const |
Return the number of objects inside a tree object. | |
std::string | catFile (const ObjectId &id) const |
Return the raw contents of a git object. | |
Private Member Functions | |
void | checkRepository () const |
Checks the repository. | |
bool | getCmdResult (const std::string &cmd, std::string &result, const std::string &tag) const |
Returns a line identified by a tag from the output of a git command. | |
bool | getCmdResult (const std::string &cmd, std::string &result, int index) const |
Returns the ith line from the output of a git command. | |
int | getCmdResultLineCount (const std::string &cmd) const |
Returns the number of lines in the output of a git command. | |
Private Attributes | |
std::string | repository_ |
The path to the repository. | |
Cache | cache_ |
A small LRU cache that stores results of git commands. | |
Classes | |
class | Exception |
Exception class. More... | |
struct | Object |
Git object. More... | |
class | ObjectId |
Git object Id. More... |
Far from complete! Only browses git revisions.
Definition at line 23 of file Git.h.
typedef std::list<std::pair<std::string, std::string> > Git::Cache |
enum Git::ObjectType |
Git::Git | ( | ) |
void Git::setRepositoryPath | ( | const std::string & | repository | ) |
Set the git repository path.
Exception | : if the path does not specify a valid repository. |
Definition at line 181 of file Git.C.
00182 { 00183 repository_ = repositoryPath; 00184 checkRepository(); 00185 }
Git::ObjectId Git::getCommitTree | ( | const std::string & | revision | ) | const |
Get the tree for a particular revision.
Exception | : in case of a git error. |
Definition at line 187 of file Git.C.
00188 { 00189 Git::ObjectId commit = getCommit(revision); 00190 return getTreeFromCommit(commit); 00191 }
Git::ObjectId Git::getCommit | ( | const std::string & | revision | ) | const |
Get the commit for a particular revision.
Exception | : in case of a git error. |
Definition at line 203 of file Git.C.
00204 { 00205 std::string sha1Commit; 00206 getCmdResult("rev-parse " + revision, sha1Commit, 0); 00207 return ObjectId(sha1Commit); 00208 }
Git::ObjectId Git::getTreeFromCommit | ( | const ObjectId & | commit | ) | const |
Get the tree for a particular commit.
Exception | : in case of a git error. |
Definition at line 210 of file Git.C.
00211 { 00212 std::string treeLine; 00213 if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree")) 00214 throw Exception("Git: could not parse tree from commit '" 00215 + commit.toString() + "'"); 00216 00217 std::vector<std::string> v; 00218 boost::split(v, treeLine, boost::is_any_of(" ")); 00219 if (v.size() != 2) 00220 throw Exception("Git: could not parse tree from commit '" 00221 + commit.toString() + "': '" + treeLine + "'"); 00222 return ObjectId(v[1]); 00223 }
Git::Object Git::treeGetObject | ( | const ObjectId & | tree, | |
int | index | |||
) | const |
Get some info on a tree object.
The object is specified based on its index in the parent tree object.
Exception | : in case of a git error. |
Definition at line 225 of file Git.C.
00226 { 00227 std::string objectLine; 00228 if (!getCmdResult("cat-file -p " + tree.toString(), objectLine, index)) 00229 throw Exception("Git: could not read object %" 00230 + boost::lexical_cast<std::string>(index) 00231 + " from tree " + tree.toString()); 00232 else { 00233 std::vector<std::string> v1, v2; 00234 boost::split(v1, objectLine, boost::is_any_of("\t")); 00235 if (v1.size() != 2) 00236 throw Exception("Git: could not parse tree object line: '" 00237 + objectLine + "'"); 00238 boost::split(v2, v1[0], boost::is_any_of(" ")); 00239 if (v2.size() != 3) 00240 throw Exception("Git: could not parse tree object line: '" 00241 + objectLine + "'"); 00242 00243 const std::string& stype = v2[1]; 00244 ObjectType type; 00245 if (stype == "tree") 00246 type = Tree; 00247 else if (stype == "blob") 00248 type = Blob; 00249 else 00250 throw Exception("Git: Unknown type: " + stype); 00251 00252 Git::Object result(ObjectId(v2[2]), type); 00253 result.name = v1[1]; 00254 00255 return result; 00256 } 00257 }
int Git::treeSize | ( | const ObjectId & | tree | ) | const |
Return the number of objects inside a tree object.
Exception | : in case of a git error. |
Definition at line 259 of file Git.C.
00260 { 00261 return getCmdResultLineCount("cat-file -p " + tree.toString()); 00262 }
std::string Git::catFile | ( | const ObjectId & | id | ) | const |
Return the raw contents of a git object.
Exception | : in case of a git error. |
Definition at line 193 of file Git.C.
00194 { 00195 std::string result; 00196 00197 if (!getCmdResult("cat-file -p " + id.toString(), result, -1)) 00198 throw Exception("Git: could not cat '" + id.toString() + "'"); 00199 00200 return result; 00201 }
void Git::checkRepository | ( | ) | const [private] |
Checks the repository.
Exception | : in case the repository is not a valid. |
Definition at line 322 of file Git.C.
00323 { 00324 POpenWrapper p("git --git-dir=" + repository_ + " branch", cache_); 00325 00326 std::string r; 00327 if (p.exitStatus() != 0) 00328 throw Exception("Git error: " + p.readLine(r)); 00329 }
bool Git::getCmdResult | ( | const std::string & | cmd, | |
std::string & | result, | |||
const std::string & | tag | |||
) | const [private] |
Returns a line identified by a tag from the output of a git command.
The line is filled in result. Returns whether a line starting with tag could be found.
Exception | : in case the command failed |
Definition at line 287 of file Git.C.
00289 { 00290 POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_); 00291 00292 if (p.exitStatus() != 0) 00293 throw Exception("Git error: " + p.readLine(result)); 00294 00295 while (!p.finished()) { 00296 p.readLine(result); 00297 if (boost::starts_with(result, tag)) 00298 return true; 00299 } 00300 00301 return false; 00302 }
bool Git::getCmdResult | ( | const std::string & | cmd, | |
std::string & | result, | |||
int | index | |||
) | const [private] |
Returns the ith line from the output of a git command.
The line is filled in result. Returns the whole git output if index = -1, otherwise the line with line number index.
Exception | : in case the command failed |
Definition at line 264 of file Git.C.
00266 { 00267 POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_); 00268 00269 if (p.exitStatus() != 0) 00270 throw Exception("Git error: " + p.readLine(result)); 00271 00272 if (index == -1) { 00273 result = p.contents(); 00274 return true; 00275 } else 00276 p.readLine(result); 00277 00278 for (int i = 0; i < index; ++i) { 00279 if (p.finished()) 00280 return false; 00281 p.readLine(result); 00282 } 00283 00284 return true; 00285 }
int Git::getCmdResultLineCount | ( | const std::string & | cmd | ) | const [private] |
Returns the number of lines in the output of a git command.
Exception | : in case the command failed |
Definition at line 304 of file Git.C.
00305 { 00306 POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_); 00307 00308 std::string r; 00309 00310 if (p.exitStatus() != 0) 00311 throw Exception("Git error: " + p.readLine(r)); 00312 00313 int result = 0; 00314 while (!p.finished()) { 00315 p.readLine(r); 00316 ++result; 00317 } 00318 00319 return result; 00320 }
std::string Git::repository_ [private] |
Cache Git::cache_ [mutable, private] |