Wt examples  4.10.4
Loading...
Searching...
No Matches
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.
 
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

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

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

201 : is_bare_(false),
202 cache_(3) // cache of 3 git results
203{ }
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 291 of file Git.C.

292{
293 if (is_bare_)
294 return "git --git-dir=" + repository_;
295 else
296 return "git --git-dir=" + repository_ + "/.git"
297 " --work-tree=" + repository_;
298}
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 220 of file Git.C.

221{
222 std::string result;
223
224 if (!getCmdResult("cat-file -p " + id.toString(), result, -1))
225 throw Exception("Git: could not cat '" + id.toString() + "'");
226
227 return result;
228}
Wt::Auth::Dbo::UserDatabase< AuthInfo > UserDatabase
Definition Session.h:22
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:323

◆ checkRepository()

void Git::checkRepository ( ) const
private

Checks the repository.

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

Definition at line 358 of file Git.C.

359{
360 POpenWrapper p(baseCmd() + " branch", cache_);
361
362 std::string r;
363 if (p.exitStatus() != 0)
364 throw Exception("Git error: " + p.readLine(r));
365}
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition Git.C:291

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

325{
326 POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
327
328 if (p.exitStatus() != 0)
329 throw Exception("Git error: " + p.readLine(result));
330
331 while (!p.finished()) {
332 p.readLine(result);
333 if (boost::starts_with(result, tag))
334 return true;
335 }
336
337 return false;
338}

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

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

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

341{
342 POpenWrapper p(baseCmd() + " " + gitCmd, cache_);
343
344 std::string r;
345
346 if (p.exitStatus() != 0)
347 throw Exception("Git error: " + p.readLine(r));
348
349 int result = 0;
350 while (!p.finished()) {
351 p.readLine(r);
352 ++result;
353 }
354
355 return result;
356}

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

231{
232 std::string sha1Commit;
233 getCmdResult("rev-parse " + revision, sha1Commit, 0);
234 return ObjectId(sha1Commit);
235}

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

215{
218}
Git object Id.
Definition Git.h:39
ObjectId getCommit(const std::string &revision) const
Get the commit for a particular revision.
Definition Git.C:230
ObjectId getTreeFromCommit(const ObjectId &commit) const
Get the tree for a particular commit.
Definition Git.C:237

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

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

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

206{
207 namespace fs = boost::filesystem;
208 boost::system::error_code ignored;
209 is_bare_ = !fs::is_directory(fs::path(repositoryPath) / ".git", ignored);
212}
void checkRepository() const
Checks the repository.
Definition Git.C:358

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

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

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

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: