Wt examples  4.10.4
Git.h
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef GIT_H_
8 #define GIT_H_
9 
10 #include <stdexcept>
11 #include <list>
12 #include <boost/array.hpp>
13 #include <string>
14 
19 
24 class Git {
25 public:
28  class Exception : public std::runtime_error {
29  public:
32  Exception(const std::string& msg);
33  };
34 
39  class ObjectId : public boost::array<unsigned char, 20> {
40  public:
43  ObjectId();
44 
50  explicit ObjectId(const std::string& id);
51 
54  std::string toString() const;
55  };
56 
59  enum ObjectType { Tree, Commit, Blob };
60 
63  struct Object {
66  std::string name;
67 
68  Object(const ObjectId& id, ObjectType type);
69  };
70 
73  Git();
74 
79  void setRepositoryPath(const std::string& repository);
80 
85  ObjectId getCommitTree(const std::string& revision) const;
86 
91  ObjectId getCommit(const std::string& revision) const;
92 
97  ObjectId getTreeFromCommit(const ObjectId& commit) const;
98 
106  Object treeGetObject(const ObjectId& tree, int index) const;
107 
112  int treeSize(const ObjectId& tree) const;
113 
118  std::string catFile(const ObjectId& id) const;
119 
120  typedef std::list<std::pair<std::string, std::string> > Cache;
121 
122 private:
125  std::string baseCmd() const;
126 
129  bool is_bare_;
130 
133  std::string repository_;
134 
137  mutable Cache cache_;
138 
143  void checkRepository() const;
144 
153  bool getCmdResult(const std::string& cmd, std::string& result,
154  const std::string& tag) const;
155 
164  bool getCmdResult(const std::string& cmd, std::string& result,
165  int index) const;
166 
171  int getCmdResultLineCount(const std::string& cmd) const;
172 };
173 
176 #endif // GIT_H_
Exception class.
Definition: Git.h:28
Exception(const std::string &msg)
Constructor.
Definition: Git.C:169
Git object Id.
Definition: Git.h:39
std::string toString() const
Print as a 40-digit hexadecimal number.
Definition: Git.C:185
ObjectId()
Default constructor.
Definition: Git.C:173
Git utility class for browsing git archives.
Definition: Git.h:24
Cache cache_
A small LRU cache that stores results of git commands.
Definition: Git.h:137
std::list< std::pair< std::string, std::string > > Cache
Definition: Git.h:120
ObjectId getCommit(const std::string &revision) const
Get the commit for a particular revision.
Definition: Git.C:230
std::string repository_
The path to the repository.
Definition: Git.h:133
ObjectId getTreeFromCommit(const ObjectId &commit) const
Get the tree for a particular commit.
Definition: Git.C:237
std::string catFile(const ObjectId &id) const
Return the raw contents of a git object.
Definition: Git.C:220
int getCmdResultLineCount(const std::string &cmd) const
Returns the number of lines in the output of a git command.
Definition: Git.C:340
bool is_bare_
Whether the repositoy is a bare repository.
Definition: Git.h:129
Object treeGetObject(const ObjectId &tree, int index) const
Get some info on a tree object.
Definition: Git.C:252
void checkRepository() const
Checks the repository.
Definition: Git.C:358
int treeSize(const ObjectId &tree) const
Return the number of objects inside a tree object.
Definition: Git.C:286
void setRepositoryPath(const std::string &repository)
Set the git repository path.
Definition: Git.C:205
ObjectType
Git object type.
Definition: Git.h:59
@ Commit
Definition: Git.h:59
@ Blob
Definition: Git.h:59
@ Tree
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:323
std::string baseCmd() const
The git base command after which extra arguments are added.
Definition: Git.C:291
ObjectId getCommitTree(const std::string &revision) const
Get the tree for a particular revision.
Definition: Git.C:214
Git()
Constructor.
Definition: Git.C:200
Git object.
Definition: Git.h:63
Object(const ObjectId &id, ObjectType type)
Definition: Git.C:195
ObjectId id
Definition: Git.h:64
ObjectType type
Definition: Git.h:65
std::string name
Definition: Git.h:66