Wt examples  4.0.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

std::string baseCmd () const
 The git base command after which extra arguments are added. More...
 
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

bool is_bare_
 Whether the repositoy is a bare repository. More...
 
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 24 of file Git.h.

Member Typedef Documentation

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

Definition at line 120 of file Git.h.

Member Enumeration Documentation

Git object type.

Enumerator
Tree 
Commit 
Blob 

Definition at line 59 of file Git.h.

59 { Tree, Commit, Blob };
Definition: Git.h:59
Definition: Git.h:59

Constructor & Destructor Documentation

Git::Git ( )

Constructor.

Definition at line 201 of file Git.C.

202  : is_bare_(false),
203  cache_(3) // cache of 3 git results
204 { }
bool is_bare_
Whether the repositoy is a bare repository.
Definition: Git.h:129
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:137

Member Function Documentation

std::string Git::baseCmd ( ) const
private

The git base command after which extra arguments are added.

Definition at line 292 of file Git.C.

293 {
294  if (is_bare_)
295  return "git --git-dir=" + repository_;
296  else
297  return "git --git-dir=" + repository_ + "/.git"
298  " --work-tree=" + repository_;
299 }
bool is_bare_
Whether the repositoy is a bare repository.
Definition: Git.h:129
std::string repository_
The path to the repository.
Definition: Git.h:133
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 221 of file Git.C.

222 {
223  std::string result;
224 
225  if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
226  throw Exception("Git: could not cat '" + id.toString() + "'");
227 
228  return result;
229 }
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.
Definition: Git.C:324
void Git::checkRepository ( ) const
private

Checks the repository.

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

Definition at line 359 of file Git.C.

360 {
361  POpenWrapper p(baseCmd() + " branch", cache_);
362 
363  std::string r;
364  if (p.exitStatus() != 0)
365  throw Exception("Git error: " + p.readLine(r));
366 }
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition: Git.C:292
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:137
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 324 of file Git.C.

326 {
327  POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
328 
329  if (p.exitStatus() != 0)
330  throw Exception("Git error: " + p.readLine(result));
331 
332  while (!p.finished()) {
333  p.readLine(result);
334  if (boost::starts_with(result, tag))
335  return true;
336  }
337 
338  return false;
339 }
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition: Git.C:292
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:137
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 301 of file Git.C.

303 {
304  POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
305 
306  if (p.exitStatus() != 0)
307  throw Exception("Git error: " + p.readLine(result));
308 
309  if (index == -1) {
310  result = p.contents();
311  return true;
312  } else
313  p.readLine(result);
314 
315  for (int i = 0; i < index; ++i) {
316  if (p.finished())
317  return false;
318  p.readLine(result);
319  }
320 
321  return true;
322 }
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition: Git.C:292
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:137
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 341 of file Git.C.

342 {
343  POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
344 
345  std::string r;
346 
347  if (p.exitStatus() != 0)
348  throw Exception("Git error: " + p.readLine(r));
349 
350  int result = 0;
351  while (!p.finished()) {
352  p.readLine(r);
353  ++result;
354  }
355 
356  return result;
357 }
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition: Git.C:292
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:137
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 231 of file Git.C.

232 {
233  std::string sha1Commit;
234  getCmdResult("rev-parse " + revision, sha1Commit, 0);
235  return ObjectId(sha1Commit);
236 }
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.
Definition: Git.C:324
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 215 of file Git.C.

216 {
217  Git::ObjectId commit = getCommit(revision);
218  return getTreeFromCommit(commit);
219 }
Git object Id.
Definition: Git.h:39
ObjectId getCommit(const std::string &revision) const
Get the commit for a particular revision.
Definition: Git.C:231
ObjectId getTreeFromCommit(const ObjectId &commit) const
Get the tree for a particular commit.
Definition: Git.C:238
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 238 of file Git.C.

239 {
240  std::string treeLine;
241  if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree"))
242  throw Exception("Git: could not parse tree from commit '"
243  + commit.toString() + "'");
244 
245  std::vector<std::string> v;
246  boost::split(v, treeLine, boost::is_any_of(" "));
247  if (v.size() != 2)
248  throw Exception("Git: could not parse tree from commit '"
249  + commit.toString() + "': '" + treeLine + "'");
250  return ObjectId(v[1]);
251 }
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.
Definition: Git.C:324
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 206 of file Git.C.

207 {
208  struct stat sb;
209  is_bare_ = !(stat((repositoryPath + "/.git").c_str(), &sb) == 0 &&
210  S_ISDIR(sb.st_mode));
211  repository_ = repositoryPath;
212  checkRepository();
213 }
void checkRepository() const
Checks the repository.
Definition: Git.C:359
bool is_bare_
Whether the repositoy is a bare repository.
Definition: Git.h:129
std::string repository_
The path to the repository.
Definition: Git.h:133
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 253 of file Git.C.

254 {
255  std::string objectLine;
256  if (!getCmdResult("cat-file -p " + tree.toString(), objectLine, index))
257  throw Exception("Git: could not read object %"
258  + asString(index).toUTF8()
259  + " from tree " + tree.toString());
260  else {
261  std::vector<std::string> v1, v2;
262  boost::split(v1, objectLine, boost::is_any_of("\t"));
263  if (v1.size() != 2)
264  throw Exception("Git: could not parse tree object line: '"
265  + objectLine + "'");
266  boost::split(v2, v1[0], boost::is_any_of(" "));
267  if (v2.size() != 3)
268  throw Exception("Git: could not parse tree object line: '"
269  + objectLine + "'");
270 
271  const std::string& stype = v2[1];
272  ObjectType type;
273  if (stype == "tree")
274  type = Tree;
275  else if (stype == "blob")
276  type = Blob;
277  else
278  throw Exception("Git: Unknown type: " + stype);
279 
280  Git::Object result(ObjectId(v2[2]), type);
281  result.name = v1[1];
282 
283  return result;
284  }
285 }
Definition: Git.h:59
Definition: Git.h:59
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.
Definition: Git.C:324
Git object.
Definition: Git.h:63
WString asString(const cpp17::any &v, const WString &formatString=WString())
ObjectType
Git object type.
Definition: Git.h:59
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 287 of file Git.C.

288 {
289  return getCmdResultLineCount("cat-file -p " + tree.toString());
290 }
int getCmdResultLineCount(const std::string &cmd) const
Returns the number of lines in the output of a git command.
Definition: Git.C:341

Member Data Documentation

Cache Git::cache_
mutableprivate

A small LRU cache that stores results of git commands.

Definition at line 137 of file Git.h.

bool Git::is_bare_
private

Whether the repositoy is a bare repository.

Definition at line 129 of file Git.h.

std::string Git::repository_
private

The path to the repository.

Definition at line 133 of file Git.h.


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

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11