Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes

Git Class Reference
[Git model example]

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

#include <Git.h>

List of all members.

Classes

class  Exception
 Exception class. More...
struct  Object
 Git object. More...
class  ObjectId
 Git object Id. More...

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.

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.

{ Tree, Commit, Blob };

Constructor & Destructor Documentation

Git::Git (  )

Constructor.

Definition at line 177 of file Git.C.

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

Member Function Documentation

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 193 of file Git.C.

{
  std::string result;

  if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
    throw Exception("Git: could not cat '" + id.toString() + "'");

  return result;
}
void Git::checkRepository (  ) const [private]

Checks the repository.

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

Definition at line 322 of file Git.C.

{
  POpenWrapper p("git --git-dir=" + repository_ + " branch", cache_);

  std::string r;
  if (p.exitStatus() != 0)
    throw Exception("Git error: " + p.readLine(r));
}
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 287 of file Git.C.

{
  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);

  if (p.exitStatus() != 0)
    throw Exception("Git error: " + p.readLine(result));

  while (!p.finished()) {
    p.readLine(result);
    if (boost::starts_with(result, tag))
      return true;
  }

  return false;
}
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 264 of file Git.C.

{
  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);

  if (p.exitStatus() != 0)
    throw Exception("Git error: " + p.readLine(result));

  if (index == -1) {
    result = p.contents();
    return true;
  } else
    p.readLine(result);

  for (int i = 0; i < index; ++i) {
    if (p.finished())
      return false;
    p.readLine(result);
  }

  return true;
}
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 304 of file Git.C.

{
  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);

  std::string r;

  if (p.exitStatus() != 0)
    throw Exception("Git error: " + p.readLine(r));

  int result = 0;
  while (!p.finished()) {
    p.readLine(r);
    ++result;
  }

  return result;
}
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 203 of file Git.C.

{
  std::string sha1Commit;
  getCmdResult("rev-parse " + revision, sha1Commit, 0);
  return ObjectId(sha1Commit);
}
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 187 of file Git.C.

{
  Git::ObjectId commit = getCommit(revision);
  return getTreeFromCommit(commit);
}
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 210 of file Git.C.

{
  std::string treeLine;
  if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree"))
    throw Exception("Git: could not parse tree from commit '" 
                    + commit.toString() + "'");

  std::vector<std::string> v;
  boost::split(v, treeLine, boost::is_any_of(" "));
  if (v.size() != 2)
    throw Exception("Git: could not parse tree from commit '"
                    + commit.toString() + "': '" + treeLine + "'");
  return ObjectId(v[1]);
}
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 181 of file Git.C.

{ 
  repository_ = repositoryPath;
  checkRepository();
}
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 225 of file Git.C.

{
  std::string objectLine;
  if (!getCmdResult("cat-file -p " + tree.toString(), objectLine, index))
    throw Exception("Git: could not read object %"
                    + boost::lexical_cast<std::string>(index)
                    + "  from tree " + tree.toString());
  else {
    std::vector<std::string> v1, v2;
    boost::split(v1, objectLine, boost::is_any_of("\t"));
    if (v1.size() != 2)
      throw Exception("Git: could not parse tree object line: '"
                      + objectLine + "'");
    boost::split(v2, v1[0], boost::is_any_of(" "));
    if (v2.size() != 3)
      throw Exception("Git: could not parse tree object line: '"
                      + objectLine + "'");
 
    const std::string& stype = v2[1];
    ObjectType type;
    if (stype == "tree")
      type = Tree;
    else if (stype == "blob")
      type = Blob;
    else
      throw Exception("Git: Unknown type: " + stype);

    Git::Object result(ObjectId(v2[2]), type);
    result.name = v1[1];

    return result;
  }
}
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 259 of file Git.C.

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

Member Data Documentation

Cache Git::cache_ [mutable, private]

A small LRU cache that stores results of git commands.

Definition at line 128 of file Git.h.

std::string Git::repository_ [private]

The path to the repository.

Definition at line 124 of file Git.h.


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

Generated on Fri Feb 4 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.2