Browse Source

Added GitBaseOid and basic Remotes handling

Alexey Edelev 7 years ago
parent
commit
b2c91f84cc
26 changed files with 188 additions and 42 deletions
  1. 8 2
      NiceGit.pro
  2. 5 2
      commitgraph.cpp
  3. 11 5
      commitgraph.h
  4. 3 2
      commitmodel.cpp
  5. 0 8
      gitbase.h
  6. 1 0
      gitbaseoid.cpp
  7. 24 0
      gitbaseoid.h
  8. 2 2
      gitcommit.cpp
  9. 2 2
      gitcommit.h
  10. 1 2
      gitdiff.cpp
  11. 12 1
      githandler.cpp
  12. 2 0
      githandler.h
  13. 1 1
      gitreference.cpp
  14. 2 2
      gitreference.h
  15. 16 0
      gitremote.cpp
  16. 20 0
      gitremote.h
  17. 15 0
      gitrepository.cpp
  18. 8 0
      gitrepository.h
  19. 1 1
      gittag.cpp
  20. 2 2
      gittag.h
  21. 6 0
      graphlistmodel.cpp
  22. 32 0
      graphlistmodel.h
  23. 3 1
      main.cpp
  24. 4 3
      qml/Graph.qml
  25. 1 1
      qml/TopBar.qml
  26. 6 5
      universallistmodel.h

+ 8 - 2
NiceGit.pro

@@ -20,7 +20,10 @@ SOURCES += \
     graphpoint.cpp \
     gittag.cpp \
     tagmodel.cpp \
-    gitdiff.cpp
+    gitdiff.cpp \
+    graphlistmodel.cpp \
+    gitremote.cpp \
+    gitbaseoid.cpp
 
 HEADERS += \
     githandler.h \
@@ -37,7 +40,10 @@ HEADERS += \
     graphpoint.h \
     gittag.h \
     tagmodel.h \
-    gitdiff.h
+    gitdiff.h \
+    graphlistmodel.h \
+    gitremote.h \
+    gitbaseoid.h
 
 RESOURCES += \
     resources.qrc

+ 5 - 2
commitgraph.cpp

@@ -5,6 +5,7 @@
 #include <gittag.h>
 
 #include <graphpoint.h>
+#include <graphlistmodel.h>
 
 #include <QDateTime>
 #include <QScopedPointer>
@@ -13,6 +14,7 @@
 #include <git2/commit.h>
 
 CommitGraph::CommitGraph() : QObject()
+  ,m_pointsModel(new GraphListModel(this))
   ,m_branchesCount(0)
 {
     qsrand(QDateTime::currentMSecsSinceEpoch());
@@ -52,7 +54,7 @@ void CommitGraph::addHead(const GitOid &oid)
 //    qDebug() << "Update Y coordinate after head added";
     QList<int> branchStarted;
     for(int i = 0; i < m_sortedPoints.count(); i++) {
-        GraphPoint* point = static_cast<GraphPoint*>(m_sortedPoints.at(i));
+        GraphPoint* point = m_sortedPoints.at(i);
         point->setY(m_sortedPoints.count() - i - 1);
         GitCommit *commit = GitCommit::fromOid(point->oid());
         git_commit* commitRaw = nullptr;
@@ -96,6 +98,7 @@ void CommitGraph::addHead(const GitOid &oid)
         }
         m_branchesCount = m_branchesCount < (point->x() + 1) ? (point->x() + 1) : m_branchesCount;
     }
+    m_pointsModel->reset(m_sortedPoints);
     emit branchesCountChanged(m_branchesCount);
 }
 
@@ -136,7 +139,7 @@ void CommitGraph::addCommits(QList<GitOid>& reversList)
         if(parentPoint == nullptr) {
             parentPoint = new GraphPoint(parentIter, this);
             parentPoint->setColor(m_color);
-            m_sortedPoints.prepend(parentPoint);
+            m_sortedPoints.prepend(QPointer<GraphPoint>(parentPoint));
             m_points.insert(parentPoint->oid(), parentPoint);
         }
 

+ 11 - 5
commitgraph.h

@@ -7,25 +7,28 @@
 
 #include <QString>
 #include <QHash>
-#include <QObjectList>
+#include <QList>
+#include <QPointer>
 
 class GitCommit;
 class GraphPoint;
+class GraphListModel;
 class GitBranch;
 class GitDiff;
 
 class CommitGraph : public QObject
 {
     Q_OBJECT
-    Q_PROPERTY(QList<QObject*> points READ points CONSTANT)
+//    Q_PROPERTY(QList<QObject*> points READ points CONSTANT)
     Q_PROPERTY(int branchesCount READ branchesCount NOTIFY branchesCountChanged)
+    Q_PROPERTY(GraphListModel* points READ points NOTIFY pointsChanged)
 public:
     CommitGraph();
     void addHead(GitBranch* branch);
     void addHead(const GitOid& oid);
 
-    QList<QObject*> points() const {
-        return m_sortedPoints;
+    GraphListModel* points() const {
+        return m_pointsModel;
     }
 
     int branchesCount() const
@@ -36,6 +39,8 @@ public:
 signals:
     void branchesCountChanged(int branchesCount);
 
+    void pointsChanged(GraphListModel* points);
+
 private:
     void findParents(GitCommit *commit);
     void addCommits(QList<GitOid> &reversList);
@@ -43,7 +48,8 @@ private:
     QString m_color;
 
     QHash<GitOid, GraphPoint*> m_points;
-    QObjectList m_sortedPoints;
+    QList<QPointer<GraphPoint> > m_sortedPoints;
+    GraphListModel* m_pointsModel;
     int m_branchesCount;
 };
 

+ 3 - 2
commitmodel.cpp

@@ -5,6 +5,7 @@
 
 #include <commitgraph.h>
 #include <graphpoint.h>
+#include <graphlistmodel.h>
 
 CommitModel::CommitModel(const QString &head, QObject* parent) : UniversalListModel(parent)
   ,m_head(head)
@@ -39,9 +40,9 @@ CommitModel* CommitModel::fromBranch(GitBranch* branch)
 CommitModel* CommitModel::fromGraph(CommitGraph *graph)
 {
     CommitModel* model = new CommitModel("HEAD");
-    QList<QObject*> points = graph->points();
+    QList<QPointer<GraphPoint> > points = graph->points()->container();
     for(int i = 0; i < points.count(); i++) {
-        GraphPoint* point = static_cast<GraphPoint*>(points.at(i));
+        GraphPoint* point = points.at(i).data();
         model->m_container.prepend(GitCommit::fromOid(point->oid()));
 //        QPointer<GitTag> tag = commit->repository()->tags().value(commit->oid());
 //        if(!tag.isNull()) {

+ 0 - 8
gitbase.h

@@ -2,11 +2,9 @@
 #define GITBASE_H
 
 #include <gitrepository.h>
-#include <gitoid.h>
 
 #include <QObject>
 #include <git2/types.h>
-#include <git2/oid.h>
 
 class GitRepository;
 
@@ -17,7 +15,6 @@ public:
     GitBase(T* raw, GitRepository* parent) : QObject(parent)
       ,m_raw(raw)
       ,m_repository(parent)
-      ,m_oid(nullptr, parent)
     {}
 
     T* raw() const {
@@ -32,14 +29,9 @@ public:
         return m_repository;
     }
 
-    const GitOid& oid() const {
-        return m_oid;
-    }
-
 protected:
     T* m_raw;
     GitRepository* m_repository;
-    GitOid m_oid;
 };
 
 #endif // GITBASE_H

+ 1 - 0
gitbaseoid.cpp

@@ -0,0 +1 @@
+#include "gitbaseoid.h"

+ 24 - 0
gitbaseoid.h

@@ -0,0 +1,24 @@
+#ifndef GITBASEOID_H
+#define GITBASEOID_H
+
+#include <gitbase.h>
+#include <gitoid.h>
+
+#include <git2/oid.h>
+
+template<typename T>
+class GitBaseOid : public GitBase<T>
+{
+public:
+    GitBaseOid(T* raw, GitRepository* parent) : GitBase<T>(raw, parent)
+      ,m_oid(nullptr, parent)
+    {}
+
+    const GitOid& oid() const {
+        return m_oid;
+    }
+protected:
+    GitOid m_oid;
+};
+
+#endif // GITBASEOID_H

+ 2 - 2
gitcommit.cpp

@@ -11,13 +11,13 @@
 #include <git2/patch.h>
 #include <git2/buffer.h>
 
-GitCommit::GitCommit(git_commit* raw, GitRepository* parent) : GitBase(raw, parent)
+GitCommit::GitCommit(git_commit* raw, GitRepository* parent) : GitBaseOid(raw, parent)
   ,m_diff(nullptr)
 {
     m_oid = GitOid(git_commit_id(m_raw), m_repository);
 }
 
-GitCommit::GitCommit() : GitBase(nullptr, nullptr)
+GitCommit::GitCommit() : GitBaseOid(nullptr, nullptr)
 {
 }
 

+ 2 - 2
gitcommit.h

@@ -1,7 +1,7 @@
 #ifndef GITCOMMIT_H
 #define GITCOMMIT_H
 
-#include <gitbase.h>
+#include <gitbaseoid.h>
 #include <gittag.h>
 
 #include <QDateTime>
@@ -12,7 +12,7 @@
 
 class GitDiff;
 
-class GitCommit : public GitBase<git_commit>
+class GitCommit : public GitBaseOid<git_commit>
 {
     Q_OBJECT
     Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY commitChanged)

+ 1 - 2
gitdiff.cpp

@@ -33,7 +33,7 @@ void GitDiff::readBody(git_commit *a, git_commit *b)
     git_diff_print(diff,
                    GIT_DIFF_FORMAT_PATCH,
                    [](const git_diff_delta *delta, const git_diff_hunk *hunk,
-                   const git_diff_line *line, void *payload)
+                   const git_diff_line *line, void *payload) -> int
     {
         Q_UNUSED(hunk)
 
@@ -55,7 +55,6 @@ void GitDiff::readBody(git_commit *a, git_commit *b)
         case GIT_DIFF_LINE_HUNK_HDR:
             prefix = "<br/><b>";
             suffix = "</b><br/>";
-//            prefix = prefix.arg("#000000").arg("");
             break;
         default:
             prefix = prefix.arg("#000000").arg("&nbsp;");

+ 12 - 1
githandler.cpp

@@ -16,6 +16,9 @@
 
 GitHandler::GitHandler() : QObject()
   ,m_repositories(new RepositoryModel(this))
+  ,m_commits(nullptr)
+  ,m_graph(nullptr)
+  ,m_activeRepo(nullptr)
   ,m_activeDiff(nullptr)
 {
     git_libgit2_init();
@@ -35,6 +38,7 @@ void GitHandler::open(const QUrl &url)
 
 void GitHandler::open(const QString &path)
 {
+    qDebug() << "path" << path;
     git_buf root = {0,0,0};
     if(git_repository_discover(&root, path.toUtf8().data(), 0, NULL) != 0) {
         qDebug() << lastError();
@@ -58,7 +62,8 @@ void GitHandler::open(const QString &path)
 
     setGraph(graph);
     m_commits = CommitModel::fromGraph(graph);
-    m_repositories->add(m_activeRepo);
+    emit commitsChanged(m_commits);
+//    m_repositories->add(m_activeRepo);
 }
 
 QString GitHandler::lastError() const
@@ -97,3 +102,9 @@ GitDiff* GitHandler::activeDiff() const
 {
     return m_activeDiff.data();
 }
+
+
+void GitHandler::pull() const
+{
+//    git_remote_fetch(m_activeRepo->remote)
+}

+ 2 - 0
githandler.h

@@ -49,6 +49,8 @@ public:
         return m_commits;
     }
 
+    void pull() const;
+
 public slots:
     void setGraph(CommitGraph* graph)
     {

+ 1 - 1
gitreference.cpp

@@ -4,7 +4,7 @@
 
 #include <git2.h>
 
-GitReference::GitReference(git_reference *ref, GitRepository *parent) : GitBase(nullptr, parent)
+GitReference::GitReference(git_reference *ref, GitRepository *parent) : GitBaseOid(nullptr, parent)
   ,m_namespace(Invalid)
 {
     if(ref == nullptr) {

+ 2 - 2
gitreference.h

@@ -1,12 +1,12 @@
 #ifndef GITREFERENCE_H
 #define GITREFERENCE_H
 
-#include <gitbase.h>
+#include <gitbaseoid.h>
 
 class GitRepository;
 struct git_reference;
 
-class GitReference : public GitBase<git_reference>
+class GitReference : public GitBaseOid<git_reference>
 {
     Q_OBJECT
     Q_PROPERTY(ReferenceNamespace referenceNamespace READ referenceNamespace CONSTANT)

+ 16 - 0
gitremote.cpp

@@ -0,0 +1,16 @@
+#include "gitremote.h"
+#include <git2/remote.h>
+
+GitRemote::GitRemote(git_remote* raw, GitRepository* parent) : GitBase(raw, parent)
+{
+
+}
+
+GitRemote* GitRemote::fromName(const QString& remoteName, GitRepository* parent)
+{
+    qDebug() << "new remote: " << remoteName;
+    git_remote* remote = nullptr;
+    git_remote_lookup(&remote, parent->raw(), remoteName.toUtf8().data());
+
+    return new GitRemote(remote, parent);
+}

+ 20 - 0
gitremote.h

@@ -0,0 +1,20 @@
+#ifndef GITREMOTE_H
+#define GITREMOTE_H
+
+#include <QObject>
+#include <gitbase.h>
+
+struct git_remote;
+
+class GitRemote : public GitBase<git_remote>
+{
+    Q_OBJECT
+
+public:
+    GitRemote(git_remote* raw, GitRepository* parent);
+    static GitRemote* fromName(const QString& remoteName, GitRepository* parent);
+
+private:
+};
+
+#endif // GITREMOTE_H

+ 15 - 0
gitrepository.cpp

@@ -7,6 +7,7 @@
 #include <gitbranch.h>
 #include <gitcommit.h>
 #include <gittag.h>
+#include <gitremote.h>
 #include <gitdiff.h>
 
 #include <git2.h>
@@ -25,6 +26,7 @@ GitRepository::GitRepository(const QString& root) : QObject(nullptr)
     qDebug() << "New repo:" << m_name << m_root << m_path;
     readBranches();
     readTags();
+    readRemotes();
 }
 
 GitRepository::~GitRepository()
@@ -78,3 +80,16 @@ void GitRepository::readTags()
     },
     this);
 }
+
+void GitRepository::readRemotes()
+{
+    git_reference_foreach(raw(),[](git_reference *reference, void *payload) -> int
+    {
+        if(git_reference_is_remote(reference)) {
+            GitRemote* remote = GitRemote::fromName(QString::fromLatin1(git_reference_name(reference)), static_cast<GitRepository*>(payload));
+        }
+    }, this);
+
+//    git_remote* remoteRaw;
+//    git_reference_is_remote()
+}

+ 8 - 0
gitrepository.h

@@ -14,10 +14,12 @@ class GitBranch;
 class GitTag;
 class GitDiff;
 class GitCommit;
+class GitRemote;
 struct git_oid;
 
 typedef QMap<QString, QPointer<GitBranch> > BranchContainer;
 typedef QMap<GitOid, QPointer<GitTag> > TagContainer;
+typedef QMap<QString, QPointer<GitRemote> > RemoteContainer;
 
 class GitRepository : public QObject
 {
@@ -58,6 +60,10 @@ public:
         return m_tags;
     }
 
+    RemoteContainer& remotes() {
+        return m_remotes;
+    }
+
 public slots:
     void setRoot(QString root) {
         if (m_root == root)
@@ -83,6 +89,7 @@ private:
     void close();
     void readBranches();
     void readTags();
+    void readRemotes();
 
     QString m_root;
     QString m_name;
@@ -91,6 +98,7 @@ private:
 
     BranchContainer m_branches;
     TagContainer m_tags;
+    RemoteContainer m_remotes;
 };
 
 #endif // GITREPOSITORY_H

+ 1 - 1
gittag.cpp

@@ -1,7 +1,7 @@
 #include "gittag.h"
 #include <git2/tag.h>
 
-GitTag::GitTag(git_tag *raw, GitRepository *parent) : GitBase(raw, parent)
+GitTag::GitTag(git_tag *raw, GitRepository *parent) : GitBaseOid(raw, parent)
 {
     if(raw == nullptr) {
         return;

+ 2 - 2
gittag.h

@@ -1,9 +1,9 @@
 #ifndef GITTAG_H
 #define GITTAG_H
 
-#include <gitbase.h>
+#include <gitbaseoid.h>
 
-class GitTag : public GitBase<git_tag>
+class GitTag : public GitBaseOid<git_tag>
 {
     Q_OBJECT
     Q_PROPERTY(QString name READ name NOTIFY tagChanged)

+ 6 - 0
graphlistmodel.cpp

@@ -0,0 +1,6 @@
+#include "graphlistmodel.h"
+
+GraphListModel::GraphListModel(QObject *parent) : UniversalListModel(parent)
+{
+
+}

+ 32 - 0
graphlistmodel.h

@@ -0,0 +1,32 @@
+#ifndef GRAPHLISTMODEL_H
+#define GRAPHLISTMODEL_H
+
+#include <universallistmodel.h>
+#include <graphpoint.h>
+#include <QPointer>
+
+class GraphListModel : public UniversalListModel<GraphPoint>
+{
+    Q_OBJECT
+    Q_PROPERTY(int count READ count NOTIFY countChanged)
+
+public:
+    GraphListModel(QObject* parent = 0);
+
+    QList<QPointer<GraphPoint> > container() const {
+        return m_container;
+    }
+
+    Q_INVOKABLE GraphPoint* at(int i) const {
+        return m_container.at(i).data();
+    }
+
+    int count() const
+    {
+        return m_container.count();
+    }
+signals:
+    void countChanged(int count);
+};
+
+#endif // GRAPHLISTMODEL_H

+ 3 - 1
main.cpp

@@ -10,6 +10,7 @@
 #include <gitbranch.h>
 #include <commitmodel.h>
 #include <graphpoint.h>
+#include <graphlistmodel.h>
 #include <commitgraph.h>
 #include <gittag.h>
 #include <gitdiff.h>
@@ -25,6 +26,7 @@ int main(int argc, char *argv[])
     qmlRegisterUncreatableType<CommitModel>("org.semlanik.nicegit", 1, 0, "CommitModel", "Owned only by GitHandler");
     qmlRegisterUncreatableType<CommitGraph>("org.semlanik.nicegit", 1, 0, "CommitGraph", "Owned only by GitHandler");
     qmlRegisterUncreatableType<GraphPoint>("org.semlanik.nicegit", 1, 0, "GraphPoint", "Owned only by GitHandler");
+    qmlRegisterUncreatableType<GraphListModel>("org.semlanik.nicegit", 1, 0, "GraphListModel", "Owned only by GitHandler");
     qmlRegisterUncreatableType<RepositoryModel>("org.semlanik.nicegit", 1, 0, "RepositoryModel", "Owned only by GitHandler");
     qmlRegisterUncreatableType<GitRepository>("org.semlanik.nicegit", 1, 0, "GitRepository", "Owned only by GitHandler");
     qmlRegisterUncreatableType<GitBranch>("org.semlanik.nicegit", 1, 0, "GitBranch", "Owned only by GitHandler");
@@ -35,7 +37,7 @@ int main(int argc, char *argv[])
 
 
     GitHandler handler;
-    handler.open("/home/semlanik/Projects/HCAT/hmi_hcat/demo/default/HCAT/");
+//    handler.open("/home/semlanik/Projects/HCAT/hmi_hcat/demo/default/HCAT/");
     view.rootContext()->setContextProperty("_handler", &handler);
     view.setSource(QUrl("qrc:/qml/MainView.qml"));
     view.setResizeMode(QQuickView::SizeRootObjectToView);

+ 4 - 3
qml/Graph.qml

@@ -8,7 +8,7 @@ Canvas {
     property int spacingV: 20
     property int lineWidth: 1
     property QtObject model: null
-    height: model.points.length*(elementWidth + spacingV)
+    height: model.points.count*(elementWidth + spacingV)
     width: (elementWidth + spacingH)*model.branchesCount
     QtObject {
         id: d
@@ -16,10 +16,11 @@ Canvas {
         property int halfElementHeight: elementHeight/2
     }
 
+
     onPaint: {
         var ctx = getContext("2d")
-        for(var i = 0; i < model.points.length; i++) {
-            var point = model.points[i]
+        for(var i = 0; i < model.points.count; i++) {
+            var point = model.points.at(i)
             var pointAbsX = point.x*(elementWidth + spacingH)
             var pointAbsY = point.y*(elementHeight + spacingV)
             var childPoints = point.childPoints

+ 1 - 1
qml/TopBar.qml

@@ -30,7 +30,7 @@ Item {
             selectMultiple: false
             onAccepted: {
     //TODO: repo open is not available
-    //            _handler.open(repoOpenDialog.fileUrl)
+                _handler.open(repoOpenDialog.fileUrl)
             }
         }
 

+ 6 - 5
universallistmodel.h

@@ -89,11 +89,12 @@ public:
 
 protected:
     void clear() {
-        foreach (const QPointer<T>& value, m_container) {
-            if(!value.isNull()) {
-                delete value.data();
-            }
-        }
+//TODO: need to verify if wee really should cleanup these commits.
+//        foreach (const QPointer<T>& value, m_container) {
+//            if(!value.isNull()) {
+//                delete value.data();
+//            }
+//        }
         m_container.clear();
     }