Wt examples  3.7.1
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 24 of file Git.h.

Member Typedef Documentation

◆ Cache

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

Definition at line 120 of file Git.h.

Member Enumeration Documentation

◆ ObjectType

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::Git ( )

Constructor.

Definition at line 197 of file Git.C.

198  : cache_(3) // cache of 3 git results
199 { }
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:129

Member Function Documentation

◆ catFile()

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

214 {
215  std::string result;
216 
217  if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
218  throw Exception("Git: could not cat '" + id.toString() + "'");
219 
220  return result;
221 }
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:307

◆ checkRepository()

void Git::checkRepository ( ) const
private

Checks the repository.

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

Definition at line 342 of file Git.C.

343 {
344  POpenWrapper p("git --git-dir=" + repository_ + " branch", cache_);
345 
346  std::string r;
347  if (p.exitStatus() != 0)
348  throw Exception("Git error: " + p.readLine(r));
349 }
std::string repository_
The path to the repository.
Definition: Git.h:125
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:129

◆ getCmdResult() [1/2]

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

309 {
310  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
311 
312  if (p.exitStatus() != 0)
313  throw Exception("Git error: " + p.readLine(result));
314 
315  while (!p.finished()) {
316  p.readLine(result);
317  if (boost::starts_with(result, tag))
318  return true;
319  }
320 
321  return false;
322 }
std::string repository_
The path to the repository.
Definition: Git.h:125
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:129

◆ getCmdResult() [2/2]

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

286 {
287  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
288 
289  if (p.exitStatus() != 0)
290  throw Exception("Git error: " + p.readLine(result));
291 
292  if (index == -1) {
293  result = p.contents();
294  return true;
295  } else
296  p.readLine(result);
297 
298  for (int i = 0; i < index; ++i) {
299  if (p.finished())
300  return false;
301  p.readLine(result);
302  }
303 
304  return true;
305 }
std::string repository_
The path to the repository.
Definition: Git.h:125
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:129

◆ getCmdResultLineCount()

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

325 {
326  POpenWrapper p("git --git-dir=" + repository_ + " " + gitCmd, cache_);
327 
328  std::string r;
329 
330  if (p.exitStatus() != 0)
331  throw Exception("Git error: " + p.readLine(r));
332 
333  int result = 0;
334  while (!p.finished()) {
335  p.readLine(r);
336  ++result;
337  }
338 
339  return result;
340 }
std::string repository_
The path to the repository.
Definition: Git.h:125
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:129

◆ getCommit()

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

224 {
225  std::string sha1Commit;
226  getCmdResult("rev-parse " + revision, sha1Commit, 0);
227  return ObjectId(sha1Commit);
228 }
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:307

◆ getCommitTree()

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

208 {
209  Git::ObjectId commit = getCommit(revision);
210  return getTreeFromCommit(commit);
211 }
Git object Id.
Definition: Git.h:39
ObjectId getCommit(const std::string &revision) const
Get the commit for a particular revision.
Definition: Git.C:223
ObjectId getTreeFromCommit(const ObjectId &commit) const
Get the tree for a particular commit.
Definition: Git.C:230

◆ getTreeFromCommit()

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

231 {
232  std::string treeLine;
233  if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree"))
234  throw Exception("Git: could not parse tree from commit '"
235  + commit.toString() + "'");
236 
237  std::vector<std::string> v;
238  boost::split(v, treeLine, boost::is_any_of(" "));
239  if (v.size() != 2)
240  throw Exception("Git: could not parse tree from commit '"
241  + commit.toString() + "': '" + treeLine + "'");
242  return ObjectId(v[1]);
243 }
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:307

◆ setRepositoryPath()

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

202 {
203  repository_ = repositoryPath;
204  checkRepository();
205 }
std::string repository_
The path to the repository.
Definition: Git.h:125
void checkRepository() const
Checks the repository.
Definition: Git.C:342

◆ treeGetObject()

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

246 {
247  std::string objectLine;
248  if (!getCmdResult("cat-file -p " + tree.toString(), objectLine, index))
249  throw Exception("Git: could not read object %"
250  + boost::lexical_cast<std::string>(index)
251  + " from tree " + tree.toString());
252  else {
253  std::vector<std::string> v1, v2;
254  boost::split(v1, objectLine, boost::is_any_of("\t"));
255  if (v1.size() != 2)
256  throw Exception("Git: could not parse tree object line: '"
257  + objectLine + "'");
258  boost::split(v2, v1[0], boost::is_any_of(" "));
259  if (v2.size() != 3)
260  throw Exception("Git: could not parse tree object line: '"
261  + objectLine + "'");
262 
263  const std::string& stype = v2[1];
264  ObjectType type;
265  if (stype == "tree")
266  type = Tree;
267  else if (stype == "blob")
268  type = Blob;
269  else
270  throw Exception("Git: Unknown type: " + stype);
271 
272  Git::Object result(ObjectId(v2[2]), type);
273  result.name = v1[1];
274 
275  return result;
276  }
277 }
Definition: Git.h:59
Definition: Git.h:59
Git object.
Definition: Git.h:63
ObjectType
Git object type.
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:307

◆ treeSize()

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

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

Member Data Documentation

◆ cache_

Cache Git::cache_
mutableprivate

A small LRU cache that stores results of git commands.

Definition at line 129 of file Git.h.

◆ repository_

std::string Git::repository_
private

The path to the repository.

Definition at line 125 of file Git.h.


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

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13