Wt examples  4.9.1
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
GitModel Class Reference

A model that retrieves revision trees from a git repository. More...

#include <GitModel.h>

Inheritance diagram for GitModel:
[legend]

Classes

class  ChildIndex
 Index usable as a key to a map, that identifies a child/row within a tree. More...
 
class  Tree
 Used to uniquely locate a folder within the folder hierarchy. More...
 

Public Member Functions

 GitModel ()
 Constructor.
 
void setRepositoryPath (const std::string &repositoryPath)
 Set the repository and load its 'master' revision.
 
void loadRevision (const std::string &revName)
 Load a particular revision.
 
virtual WModelIndex parent (const WModelIndex &index) const
 Returns the parent index.
 
virtual int columnCount (const WModelIndex &parent=WModelIndex()) const
 Returns the column count.
 
virtual int rowCount (const WModelIndex &parent=WModelIndex()) const
 Returns the row count.
 
virtual WModelIndex index (int row, int column, const WModelIndex &parent=WModelIndex()) const
 Returns a child index.
 
virtual cpp17::any data (const WModelIndex &index, ItemDataRole role=ItemDataRole::Display) const
 Returns data.
 
virtual cpp17::any headerData (int section, Orientation orientation=Orientation::Horizontal, ItemDataRole role=ItemDataRole::Display) const
 Returns header data.
 

Static Public Attributes

static const ItemDataRole ContentsRole = Wt::ItemDataRole::User
 The role which may be used on a file to retrieve its contents.
 
static const ItemDataRole FilePathRole = Wt::ItemDataRole::User + 1
 

Private Types

typedef std::map< ChildIndex, int > ChildPointerMap
 

Private Member Functions

int getTreeId (int parentId, int childIndex) const
 Get or allocate an id for a folder.
 
Git::Object getObject (const WModelIndex &index) const
 Get the Git::Object that corresponds to an index.
 

Private Attributes

Git git_
 The git repository.
 
std::vector< TreetreeData_
 List of folder objects.
 
ChildPointerMap childPointer_
 Maps child indexes to tree indexes.
 

Detailed Description

A model that retrieves revision trees from a git repository.

In its present form, it presents only a single column of data: the file names. Additional data could be easily added. Git "tree" objects correspond to folders, and "blob" objects to files.

The model is read-only, does not support sorting (that could be provided by using a WSortFilterProxyModel).

The model loads only minimal information in memory: to create model indexes for folders. These cannot be uniquely identified by their SHA1 id, since two identical folders at different locations would have the same SHA1 id.

The internal id of model indexes created by the model uniquely identify a containing folder for a particular file.

Definition at line 38 of file GitModel.h.

Member Typedef Documentation

◆ ChildPointerMap

typedef std::map<ChildIndex, int> GitModel::ChildPointerMap
private

Definition at line 170 of file GitModel.h.

Constructor & Destructor Documentation

◆ GitModel()

GitModel::GitModel ( )

Constructor.

Definition at line 12 of file GitModel.C.

13 : WAbstractItemModel()
14{ }

Member Function Documentation

◆ columnCount()

int GitModel::columnCount ( const WModelIndex &  parent = WModelIndex()) const
virtual

Returns the column count.

Returns 1.

Definition at line 98 of file GitModel.C.

99{
100 // currently only one column
101 return 1;
102}

◆ data()

cpp17::any GitModel::data ( const WModelIndex &  index,
ItemDataRole  role = ItemDataRole::Display 
) const
virtual

Returns data.

Returns only data corresponding to DisplayRole and ContentsRole.

Definition at line 136 of file GitModel.C.

137{
138 if (!index.isValid())
139 return cpp17::any();
140
141 /* Only 3 data roles on column 0 data are supported:
142 * - DisplayRole: the file name
143 * - DecorationRole: an icon (folder or file)
144 * - ContentsRole: the file contents
145 */
146 if (index.column() == 0) {
147 Git::Object object = getObject(index);
148 if (role == ItemDataRole::Display) {
149 if (object.type == Git::Tree)
150 return object.name + '/';
151 else
152 return object.name;
153 } else if (role == ItemDataRole::Decoration) {
154 if (object.type == Git::Blob)
155 return static_cast<const char*>("icons/git-blob.png");
156 else if (object.type == Git::Tree)
157 return static_cast<const char*>("icons/git-tree.png");
158 } else if (role == ContentsRole) {
159 if (object.type == Git::Blob)
160 return git_.catFile(object.id);
161 } else if (role == FilePathRole) {
162 return cpp17::any();
163 }
164 }
165
166 return cpp17::any();
167}
virtual WModelIndex index(int row, int column, const WModelIndex &parent=WModelIndex()) const
Returns a child index.
Definition: GitModel.C:57
Git::Object getObject(const WModelIndex &index) const
Get the Git::Object that corresponds to an index.
Definition: GitModel.C:178
static const ItemDataRole ContentsRole
The role which may be used on a file to retrieve its contents.
Definition: GitModel.h:43
static const ItemDataRole FilePathRole
Definition: GitModel.h:44
Git git_
The git repository.
Definition: GitModel.h:108
std::string catFile(const ObjectId &id) const
Return the raw contents of a git object.
Definition: Git.C:221
@ Blob
Definition: Git.h:59
@ Tree
Definition: Git.h:59
Git object.
Definition: Git.h:63

◆ getObject()

Git::Object GitModel::getObject ( const WModelIndex &  index) const
private

Get the Git::Object that corresponds to an index.

Definition at line 178 of file GitModel.C.

179{
180 int parentId = index.internalId();
181 const Tree& parentItem = treeData_[parentId];
182 return git_.treeGetObject(parentItem.treeObject(), index.row());
183}
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:184
Object treeGetObject(const ObjectId &tree, int index) const
Get some info on a tree object.
Definition: Git.C:253

◆ getTreeId()

int GitModel::getTreeId ( int  parentId,
int  childIndex 
) const
private

Get or allocate an id for a folder.

The folder is identified by a given childIndex within a parent folder. This method adds data to the treeData_ (and childPointer_) data structures.

Definition at line 77 of file GitModel.C.

78{
79 ChildIndex index(parentId, childIndex);
80
81 ChildPointerMap::const_iterator i = childPointer_.find(index);
82 if (i == childPointer_.end()) {
83 // no tree object was already allocated, so do that now.
84
85 // lookup the git SHA1 object Id (within the parent)
86 const Tree& parentItem = treeData_[parentId];
87 Git::Object o = git_.treeGetObject(parentItem.treeObject(), childIndex);
88
89 // and add to treeData_ and childPointer_ data structures
90 treeData_.push_back(Tree(parentId, childIndex, o.id, git_.treeSize(o.id)));
91 int result = treeData_.size() - 1;
92 childPointer_[index] = result;
93 return result;
94 } else
95 return i->second;
96}
ChildPointerMap childPointer_
Maps child indexes to tree indexes.
Definition: GitModel.h:194
int treeSize(const ObjectId &tree) const
Return the number of objects inside a tree object.
Definition: Git.C:287
ObjectId id
Definition: Git.h:64

◆ headerData()

cpp17::any GitModel::headerData ( int  section,
Orientation  orientation = Orientation::Horizontal,
ItemDataRole  role = ItemDataRole::Display 
) const
virtual

Returns header data.

Definition at line 169 of file GitModel.C.

171{
172 if (orientation == Orientation::Horizontal && role == ItemDataRole::Display)
173 return static_cast<const char*>("File");
174 else
175 return cpp17::any();
176}

◆ index()

WModelIndex GitModel::index ( int  row,
int  column,
const WModelIndex &  parent = WModelIndex() 
) const
virtual

Returns a child index.

Consults the internal data structure to create a child index. If necessary, the internal data structure is expanded by adding an entry for using the parent index as a parent index.

Definition at line 57 of file GitModel.C.

59{
60 int parentId;
61
62 // the top-level parent has id=0.
63 if (!parent.isValid())
64 parentId = 0;
65 else {
66 // the internal id of the parent identifies the grand parent
67 int grandParentId = parent.internalId();
68
69 // lookup the parent id for the parent himself, based on grand parent
70 // and child-index (=row) within the grand parent
71 parentId = getTreeId(grandParentId, parent.row());
72 }
73
74 return createIndex(row, column, parentId);
75}
virtual WModelIndex parent(const WModelIndex &index) const
Returns the parent index.
Definition: GitModel.C:41
int getTreeId(int parentId, int childIndex) const
Get or allocate an id for a folder.
Definition: GitModel.C:77

◆ loadRevision()

void GitModel::loadRevision ( const std::string &  revName)

Load a particular revision.

The revision name may be any revision accepted by git, by git-rev-parse(1).

Definition at line 22 of file GitModel.C.

23{
24 Git::ObjectId treeRoot = git_.getCommitTree(revName);
25
26 // You need to call this method before invalidating all existing
27 // model indexes. Anyone listening for this event could temporarily
28 // convert some model indexes to a raw index pointer, but this model
29 // does not reimplement these methods.
30 layoutAboutToBeChanged().emit();
31
32 treeData_.clear();
33 childPointer_.clear();
34
35 // Store the tree root as treeData_[0]
36 treeData_.push_back(Tree(-1, -1, treeRoot, git_.treeSize(treeRoot)));
37
38 layoutChanged().emit();
39}
Git object Id.
Definition: Git.h:39
ObjectId getCommitTree(const std::string &revision) const
Get the tree for a particular revision.
Definition: Git.C:215

◆ parent()

WModelIndex GitModel::parent ( const WModelIndex &  index) const
virtual

Returns the parent index.

Consults the internal data structure to find the parent index.

Definition at line 41 of file GitModel.C.

42{
43 // treeData_[0] indicates the top-level parent.
44 if (!index.isValid() || index.internalId() == 0)
45 return WModelIndex();
46 else {
47 // get the item that corresponds to the parent ...
48 const Tree& item = treeData_[index.internalId()];
49
50 // ... and construct that identifies the parent:
51 // row = child index in the grand parent
52 // internalId = id of the grand parent
53 return createIndex(item.index(), 0, item.parentId());
54 }
55}

◆ rowCount()

int GitModel::rowCount ( const WModelIndex &  parent = WModelIndex()) const
virtual

Returns the row count.

Returns 0 unless the item represents a folder, in which case it returns the number of items in the tree object that corresponds to the folder.

Definition at line 104 of file GitModel.C.

105{
106 // we are looking for the git SHA1 id of a tree object (since only folders
107 // may contain children).
108 Git::ObjectId objectId;
109 int treeId;
110
111 if (index.isValid()) {
112 // only column 0 items may contain children
113 if (index.column() != 0)
114 return 0;
115
117 if (o.type == Git::Tree) {
118 objectId = o.id;
119 treeId = getTreeId(index.internalId(), index.row());
120 } else
121 // not a folder: no children
122 return 0;
123 } else {
124 treeId = 0;
125 // the index corresponds to the root object
126 if (treeData_.empty())
127 // model not yet loaded !
128 return 0;
129 else
130 objectId = treeData_[0].treeObject();
131 }
132
133 return treeData_[treeId].rowCount();
134}
ObjectType type
Definition: Git.h:65

◆ setRepositoryPath()

void GitModel::setRepositoryPath ( const std::string &  repositoryPath)

Set the repository and load its 'master' revision.

Definition at line 16 of file GitModel.C.

17{
18 git_.setRepositoryPath(gitRepositoryPath);
19 loadRevision("master");
20}
void loadRevision(const std::string &revName)
Load a particular revision.
Definition: GitModel.C:22
void setRepositoryPath(const std::string &repository)
Set the git repository path.
Definition: Git.C:206

Member Data Documentation

◆ childPointer_

ChildPointerMap GitModel::childPointer_
mutableprivate

Maps child indexes to tree indexes.

This map provides a way to lookup data in treeData_. It has an entry corresponding to every entry in treeData_: it maps child indexes for folders to indexes in the treeData_ vector.

It is populated on-the-fly, as the user navigates the model.

Definition at line 194 of file GitModel.h.

◆ ContentsRole

const ItemDataRole GitModel::ContentsRole = Wt::ItemDataRole::User
static

The role which may be used on a file to retrieve its contents.

Definition at line 43 of file GitModel.h.

◆ FilePathRole

const ItemDataRole GitModel::FilePathRole = Wt::ItemDataRole::User + 1
static

Definition at line 44 of file GitModel.h.

◆ git_

Git GitModel::git_
private

The git repository.

Definition at line 108 of file GitModel.h.

◆ treeData_

std::vector<Tree> GitModel::treeData_
mutableprivate

List of folder objects.

This list contains folders for which a model index has been allocated.

Model indexes have an internal id that are indexes into this vector, identifying the folder that contains a particular file.

Note: only for folders this is needed, since files will never be used as a 'parent' index.

It is populated on-the-fly, as the user navigates the model.

Definition at line 184 of file GitModel.h.


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