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

◆ 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 };
@ Commit
Definition: Git.h:59
@ Blob
Definition: Git.h:59
@ Tree
Definition: Git.h:59

Constructor & Destructor Documentation

◆ Git()

Git::Git ( )

Constructor.

Definition at line 202 of file Git.C.

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

Member Function Documentation

◆ baseCmd()

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

The git base command after which extra arguments are added.

Definition at line 297 of file Git.C.

298 {
299  if (is_bare_)
300  return "git --git-dir=" + repository_;
301  else
302  return "git --git-dir=" + repository_ + "/.git"
303  " --work-tree=" + repository_;
304 }
std::string repository_
The path to the repository.
Definition: Git.h:133

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

227 {
228  std::string result;
229 
230  if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
231  throw Exception("Git: could not cat '" + id.toString() + "'");
232 
233  return result;
234 }
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:329

◆ checkRepository()

void Git::checkRepository ( ) const
private

Checks the repository.

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

Definition at line 364 of file Git.C.

365 {
366  POpenWrapper p(baseCmd() + " branch", cache_);
367 
368  std::string r;
369  if (p.exitStatus() != 0)
370  throw Exception("Git error: " + p.readLine(r));
371 }
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition: Git.C:297

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

331 {
332  POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
333 
334  if (p.exitStatus() != 0)
335  throw Exception("Git error: " + p.readLine(result));
336 
337  while (!p.finished()) {
338  p.readLine(result);
339  if (boost::starts_with(result, tag))
340  return true;
341  }
342 
343  return false;
344 }

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

308 {
309  POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
310 
311  if (p.exitStatus() != 0)
312  throw Exception("Git error: " + p.readLine(result));
313 
314  if (index == -1) {
315  result = p.contents();
316  return true;
317  } else
318  p.readLine(result);
319 
320  for (int i = 0; i < index; ++i) {
321  if (p.finished())
322  return false;
323  p.readLine(result);
324  }
325 
326  return true;
327 }

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

347 {
348  POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
349 
350  std::string r;
351 
352  if (p.exitStatus() != 0)
353  throw Exception("Git error: " + p.readLine(r));
354 
355  int result = 0;
356  while (!p.finished()) {
357  p.readLine(r);
358  ++result;
359  }
360 
361  return result;
362 }

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

237 {
238  std::string sha1Commit;
239  getCmdResult("rev-parse " + revision, sha1Commit, 0);
240  return ObjectId(sha1Commit);
241 }

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

221 {
222  Git::ObjectId commit = getCommit(revision);
223  return getTreeFromCommit(commit);
224 }
Git object Id.
Definition: Git.h:39
ObjectId getCommit(const std::string &revision) const
Get the commit for a particular revision.
Definition: Git.C:236
ObjectId getTreeFromCommit(const ObjectId &commit) const
Get the tree for a particular commit.
Definition: Git.C:243

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

244 {
245  std::string treeLine;
246  if (!getCmdResult("cat-file -p " + commit.toString(), treeLine, "tree"))
247  throw Exception("Git: could not parse tree from commit '"
248  + commit.toString() + "'");
249 
250  std::vector<std::string> v;
251  boost::split(v, treeLine, boost::is_any_of(" "));
252  if (v.size() != 2)
253  throw Exception("Git: could not parse tree from commit '"
254  + commit.toString() + "': '" + treeLine + "'");
255  return ObjectId(v[1]);
256 }

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

208 {
209  namespace fs = cpp17::filesystem;
210 #ifdef WT_FILESYSTEM_IMPL_BOOST
211  boost::system::error_code ignored;
212 #else // !WT_FILESYSTEM_IMPL_BOOST
213  std::error_code ignored;
214 #endif // WT_FILESYSTEM_IMPL_BOOST
215  is_bare_ = !fs::is_directory(fs::path(repositoryPath) / ".git", ignored);
216  repository_ = repositoryPath;
217  checkRepository();
218 }
void checkRepository() const
Checks the repository.
Definition: Git.C:364

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

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

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

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

Member Data Documentation

◆ cache_

Cache Git::cache_
mutableprivate

A small LRU cache that stores results of git commands.

Definition at line 137 of file Git.h.

◆ is_bare_

bool Git::is_bare_
private

Whether the repositoy is a bare repository.

Definition at line 129 of file Git.h.

◆ repository_

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: