Wt examples  3.3.0
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Git Class Reference

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

#include <Git.h>

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. More...
 
void setRepositoryPath (const std::string &repository)
 Set the git repository path. More...
 
ObjectId getCommitTree (const std::string &revision) const
 Get the tree for a particular revision. More...
 
ObjectId getCommit (const std::string &revision) const
 Get the commit for a particular revision. More...
 
ObjectId getTreeFromCommit (const ObjectId &commit) const
 Get the tree for a particular commit. More...
 
Object treeGetObject (const ObjectId &tree, int index) const
 Get some info on a tree object. More...
 
int treeSize (const ObjectId &tree) const
 Return the number of objects inside a tree object. More...
 
std::string catFile (const ObjectId &id) const
 Return the raw contents of a git object. More...
 

Private Member Functions

void checkRepository () const
 Checks the repository. More...
 
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. More...
 
bool getCmdResult (const std::string &cmd, std::string &result, int index) const
 Returns the ith line from the output of a git command. More...
 
int getCmdResultLineCount (const std::string &cmd) const
 Returns the number of lines in the output of a git command. More...
 

Private Attributes

std::string repository_
 The path to the repository. More...
 
Cache cache_
 A small LRU cache that stores results of git commands. 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.

58 { Tree, Commit, Blob };

Constructor & Destructor Documentation

Git::Git ( )

Constructor.

Definition at line 178 of file Git.C.

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

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

195 {
196  std::string result;
197 
198  if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
199  throw Exception("Git: could not cat '" + id.toString() + "'");
200 
201  return result;
202 }
void Git::checkRepository ( ) const
private

Checks the repository.

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

Definition at line 323 of file Git.C.

324 {
325  POpenWrapper p("git --git-dir=" + repository_ + " branch", cache_);
326 
327  std::string r;
328  if (p.exitStatus() != 0)
329  throw Exception("Git error: " + p.readLine(r));
330 }
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 <i>result</i>.
Returns whether a line starting with <i>tag</i> could be found.
Exceptions
Exception: in case the command failed

Definition at line 288 of file Git.C.

290 {
291  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
292 
293  if (p.exitStatus() != 0)
294  throw Exception("Git error: " + p.readLine(result));
295 
296  while (!p.finished()) {
297  p.readLine(result);
298  if (boost::starts_with(result, tag))
299  return true;
300  }
301 
302  return false;
303 }
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 <i>result</i>.
Returns the whole git output if <i>index</i> = -1, otherwise the line
with line number <i>index</i>.
Exceptions
Exception: in case the command failed

Definition at line 265 of file Git.C.

267 {
268  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
269 
270  if (p.exitStatus() != 0)
271  throw Exception("Git error: " + p.readLine(result));
272 
273  if (index == -1) {
274  result = p.contents();
275  return true;
276  } else
277  p.readLine(result);
278 
279  for (int i = 0; i < index; ++i) {
280  if (p.finished())
281  return false;
282  p.readLine(result);
283  }
284 
285  return true;
286 }
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 305 of file Git.C.

306 {
307  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
308 
309  std::string r;
310 
311  if (p.exitStatus() != 0)
312  throw Exception("Git error: " + p.readLine(r));
313 
314  int result = 0;
315  while (!p.finished()) {
316  p.readLine(r);
317  ++result;
318  }
319 
320  return result;
321 }
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 204 of file Git.C.

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

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

212 {
213  std::string treeLine;
214  if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree"))
215  throw Exception("Git: could not parse tree from commit '"
216  + commit.toString() + "'");
217 
218  std::vector<std::string> v;
219  boost::split(v, treeLine, boost::is_any_of(" "));
220  if (v.size() != 2)
221  throw Exception("Git: could not parse tree from commit '"
222  + commit.toString() + "': '" + treeLine + "'");
223  return ObjectId(v[1]);
224 }
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 182 of file Git.C.

183 {
184  repository_ = repositoryPath;
185  checkRepository();
186 }
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 226 of file Git.C.

227 {
228  std::string objectLine;
229  if (!getCmdResult("cat-file -p " + tree.toString(), objectLine, index))
230  throw Exception("Git: could not read object %"
231  + boost::lexical_cast<std::string>(index)
232  + " from tree " + tree.toString());
233  else {
234  std::vector<std::string> v1, v2;
235  boost::split(v1, objectLine, boost::is_any_of("\t"));
236  if (v1.size() != 2)
237  throw Exception("Git: could not parse tree object line: '"
238  + objectLine + "'");
239  boost::split(v2, v1[0], boost::is_any_of(" "));
240  if (v2.size() != 3)
241  throw Exception("Git: could not parse tree object line: '"
242  + objectLine + "'");
243 
244  const std::string& stype = v2[1];
245  ObjectType type;
246  if (stype == "tree")
247  type = Tree;
248  else if (stype == "blob")
249  type = Blob;
250  else
251  throw Exception("Git: Unknown type: " + stype);
252 
253  Git::Object result(ObjectId(v2[2]), type);
254  result.name = v1[1];
255 
256  return result;
257  }
258 }
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 260 of file Git.C.

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

Member Data Documentation

Cache Git::cache_
mutableprivate

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 May 31 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.3.1