Git Class Reference
[Git model example]

Git utility class for browsing git archives. More...

#include <Git.h>

List of all members.

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...


Detailed Description

Git utility class for browsing git archives.

Far from complete! Only browses git revisions.

Definition at line 23 of file Git.h.


Member Typedef Documentation

typedef std::list<std::pair<std::string, std::string> > Git::Cache

Definition at line 119 of file Git.h.


Member Enumeration Documentation

Git object type.

Enumerator:
Tree 
Commit 
Blob 

Definition at line 58 of file Git.h.

00058 { Tree, Commit, Blob };


Constructor & Destructor Documentation

Git::Git (  ) 

Constructor.

Definition at line 175 of file Git.C.

00176   : cache_(3) // cache of 3 git results
00177 { }


Member Function Documentation

void Git::setRepositoryPath ( const std::string &  repository  ) 

Set the git repository path.

Exceptions:
Exception : if the path does not specify a valid repository.

Definition at line 179 of file Git.C.

00180 { 
00181   repository_ = repositoryPath;
00182   checkRepository();
00183 }

Git::ObjectId Git::getCommitTree ( const std::string &  revision  )  const

Get the tree for a particular revision.

Exceptions:
Exception : in case of a git error.

Definition at line 185 of file Git.C.

00186 {
00187   Git::ObjectId commit = getCommit(revision);
00188   return getTreeFromCommit(commit);
00189 }

Git::ObjectId Git::getCommit ( const std::string &  revision  )  const

Get the commit for a particular revision.

Exceptions:
Exception : in case of a git error.

Definition at line 201 of file Git.C.

00202 {
00203   std::string sha1Commit;
00204   getCmdResult("rev-parse " + revision, sha1Commit, 0);
00205   return ObjectId(sha1Commit);
00206 }

Git::ObjectId Git::getTreeFromCommit ( const ObjectId commit  )  const

Get the tree for a particular commit.

Exceptions:
Exception : in case of a git error.

Definition at line 208 of file Git.C.

00209 {
00210   std::string treeLine;
00211   if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree"))
00212     throw Exception("Git: could not parse tree from commit '" 
00213                     + commit.toString() + "'");
00214 
00215   std::vector<std::string> v;
00216   boost::split(v, treeLine, boost::is_any_of(" "));
00217   if (v.size() != 2)
00218     throw Exception("Git: could not parse tree from commit '"
00219                     + commit.toString() + "': '" + treeLine + "'");
00220   return ObjectId(v[1]);
00221 }

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.

Exceptions:
Exception : in case of a git error.

Definition at line 223 of file Git.C.

00224 {
00225   std::string objectLine;
00226   if (!getCmdResult("cat-file -p " + tree.toString(), objectLine, index))
00227     throw Exception("Git: could not read object %"
00228                     + boost::lexical_cast<std::string>(index)
00229                     + "  from tree " + tree.toString());
00230   else {
00231     std::vector<std::string> v1, v2;
00232     boost::split(v1, objectLine, boost::is_any_of("\t"));
00233     if (v1.size() != 2)
00234       throw Exception("Git: could not parse tree object line: '"
00235                       + objectLine + "'");
00236     boost::split(v2, v1[0], boost::is_any_of(" "));
00237     if (v2.size() != 3)
00238       throw Exception("Git: could not parse tree object line: '"
00239                       + objectLine + "'");
00240  
00241     const std::string& stype = v2[1];
00242     ObjectType type;
00243     if (stype == "tree")
00244       type = Tree;
00245     else if (stype == "blob")
00246       type = Blob;
00247     else
00248       throw Exception("Git: Unknown type: " + stype);
00249 
00250     Git::Object result(ObjectId(v2[2]), type);
00251     result.name = v1[1];
00252 
00253     return result;
00254   }
00255 }

int Git::treeSize ( const ObjectId tree  )  const

Return the number of objects inside a tree object.

Exceptions:
Exception : in case of a git error.

Definition at line 257 of file Git.C.

00258 {
00259   return getCmdResultLineCount("cat-file -p " + tree.toString());
00260 }

std::string Git::catFile ( const ObjectId id  )  const

Return the raw contents of a git object.

Exceptions:
Exception : in case of a git error.

Definition at line 191 of file Git.C.

00192 {
00193   std::string result;
00194 
00195   if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
00196     throw Exception("Git: could not cat '" + id.toString() + "'");
00197 
00198   return result;
00199 }

void Git::checkRepository (  )  const [private]

Checks the repository.

Exceptions:
Exception : in case the repository is not a valid.

Definition at line 318 of file Git.C.

00319 {
00320   POpenWrapper p("git --git-dir=" + repository_ + " branch", cache_);
00321 
00322   if (p.exitStatus() != 0)
00323     throw Exception("Git error: " + p.readLine());
00324 }

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.

Exceptions:
Exception : in case the command failed

Definition at line 285 of file Git.C.

00287 {
00288   POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
00289 
00290   if (p.exitStatus() != 0)
00291     throw Exception("Git error: " + p.readLine());
00292 
00293   while (!p.finished()) {
00294     result = p.readLine();
00295     if (boost::starts_with(result, tag))
00296       return true;
00297   }
00298 
00299   return false;
00300 }

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.

Exceptions:
Exception : in case the command failed

Definition at line 262 of file Git.C.

00264 {
00265   POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
00266 
00267   if (p.exitStatus() != 0)
00268     throw Exception("Git error: " + p.readLine());
00269 
00270   if (index == -1) {
00271     result = p.contents();
00272     return true;
00273   } else
00274     result = p.readLine();
00275 
00276   for (int i = 0; i < index; ++i) {
00277     if (p.finished())
00278       return false;
00279     result = p.readLine();
00280   }
00281 
00282   return true;
00283 }

int Git::getCmdResultLineCount ( const std::string &  cmd  )  const [private]

Returns the number of lines in the output of a git command.

Exceptions:
Exception : in case the command failed

Definition at line 302 of file Git.C.

00303 {
00304   POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
00305 
00306   if (p.exitStatus() != 0)
00307     throw Exception("Git error: " + p.readLine());
00308 
00309   int result = 0;
00310   while (!p.finished()) {
00311     p.readLine();
00312     ++result;
00313   }
00314 
00315   return result;
00316 }


Member Data Documentation

std::string Git::repository_ [private]

The path to the repository.

Definition at line 124 of file Git.h.

Cache Git::cache_ [mutable, private]

A small LRU cache that stores results of git commands.

Definition at line 128 of file Git.h.


The documentation for this class was generated from the following files:

Generated on Mon Jan 26 14:14:21 2009 for Wt by doxygen 1.5.6