Browse Source

Make initial idea of OOPzation

Alexey Edelev 7 years ago
parent
commit
7097e8e9bc
12 changed files with 317 additions and 40 deletions
  1. 10 2
      NiceGit.pro
  2. 1 0
      gitbase.cpp
  3. 43 0
      gitbase.h
  4. 29 0
      gitbranch.cpp
  5. 46 0
      gitbranch.h
  6. 11 0
      gitoid.cpp
  7. 18 0
      gitoid.h
  8. 68 0
      gitreference.cpp
  9. 43 0
      gitreference.h
  10. 39 35
      gitrepository.cpp
  11. 8 3
      gitrepository.h
  12. 1 0
      main.cpp

+ 10 - 2
NiceGit.pro

@@ -9,13 +9,21 @@ SOURCES += \
     githandler.cpp \
     gitrepository.cpp \
     repositorymodel.cpp \
-    gitreflog.cpp
+    gitreflog.cpp \
+    gitbranch.cpp \
+    gitreference.cpp \
+    gitbase.cpp \
+    gitoid.cpp
 
 HEADERS += \
     githandler.h \
     gitrepository.h \
     repositorymodel.h \
-    gitreflog.h
+    gitreflog.h \
+    gitbranch.h \
+    gitreference.h \
+    gitbase.h \
+    gitoid.h
 
 DISTFILES += \
     qml/MainView.qml

+ 1 - 0
gitbase.cpp

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

+ 43 - 0
gitbase.h

@@ -0,0 +1,43 @@
+#ifndef GITBASE_H
+#define GITBASE_H
+
+#include <gitrepository.h>
+
+#include <QObject>
+#include <git2/types.h>
+#include <git2/oid.h>
+
+class GitRepository;
+
+template <typename T>
+class GitBase : public QObject
+{
+public:
+    GitBase(T* raw, GitRepository* parent) : QObject(parent)
+      ,m_raw(raw)
+      ,m_repository(parent)
+    {}
+
+    T* raw() const {
+        return m_raw;
+    }
+
+    bool isValid() const {
+        return m_raw != nullptr;
+    }
+
+    GitRepository* repository() const {
+        return m_repository;
+    }
+
+    git_oid* oid() const {
+        return &m_oid;
+    }
+
+protected:
+    T* m_raw;
+    GitRepository* m_repository;
+    git_oid m_oid;
+};
+
+#endif // GITBASE_H

+ 29 - 0
gitbranch.cpp

@@ -0,0 +1,29 @@
+#include "gitbranch.h"
+
+#include <QDebug>
+#include <gitrepository.h>
+
+#include <git2.h>
+
+GitBranch::GitBranch(git_reference *ref, GitRepository *parent) : GitReference(ref, parent)
+  ,m_commit(nullptr)
+{
+    char oid_buf[GIT_OID_HEXSZ+1];
+    const char* tmpName;
+    git_branch_name(&tmpName, m_raw);
+    m_name = tmpName;
+    git_annotated_commit_from_ref(&m_commit, repository()->raw(), m_raw);
+    const git_oid* tmpOid = git_annotated_commit_id(m_commit);
+    memcpy(m_oid, tmpOid, sizeof(m_oid));
+    git_oid_tostr(oid_buf, GIT_OID_HEXSZ+1,branch_oid);
+    qDebug() << oid_buf;
+    //        m_branches.insert(out)
+
+}
+
+GitBranch::~GitBranch()
+{
+    if(m_commit != nullptr) {
+        git_annotated_commit_free(m_commit);
+    }
+}

+ 46 - 0
gitbranch.h

@@ -0,0 +1,46 @@
+#ifndef GITBRANCH_H
+#define GITBRANCH_H
+
+#include <gitreference.h>
+
+struct git_oid;
+
+class GitBranch : public GitReference
+{
+    Q_OBJECT
+    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
+
+public:
+    GitBranch(git_reference* ref, GitRepository* parent);
+
+    QString name() const
+    {
+        return m_name;
+    }
+
+    virtual ~GitBranch();
+
+public slots:
+    void setName(QString name)
+    {
+        if (m_name == name)
+            return;
+
+        m_name = name;
+        emit nameChanged(name);
+    }
+
+signals:
+    void nameChanged(QString name);
+
+private:
+    void free();
+    GitBranch();
+    Q_DISABLE_COPY(GitBranch)
+    QString m_name;
+
+    git_annotated_commit* m_commit;
+    git_oid* m_oid;
+};
+
+#endif // GITBRANCH_H

+ 11 - 0
gitoid.cpp

@@ -0,0 +1,11 @@
+#include "gitoid.h"
+
+GitOid::GitOid(const git_oid *oid, QObject *parent) : QObject(parent)
+{
+
+}
+
+QString GitOid::toString() const
+{
+
+}

+ 18 - 0
gitoid.h

@@ -0,0 +1,18 @@
+#ifndef GITOID_H
+#define GITOID_H
+
+#include <QObject>
+#include <git2/oid.h>
+
+class GitOid : public QObject
+{
+    Q_OBJECT
+public:
+    explicit GitOid(const git_oid* oid = 0, QObject *parent = 0);
+    QString toString() const;
+private:
+    git_oid m_oid;
+    bool isValid;
+};
+
+#endif // GITOID_H

+ 68 - 0
gitreference.cpp

@@ -0,0 +1,68 @@
+#include "gitreference.h"
+
+#include <QDebug>
+
+#include <git2.h>
+
+GitReference::GitReference(git_reference *ref, GitRepository *parent) : GitBase(nullptr, parent)
+  ,m_namespace(Invalid)
+{
+    if(ref == nullptr) {
+        qDebug() << "Null reference ptr";
+        return;
+    }
+
+    m_raw = ref;
+
+    detectNamespace();
+    if(m_namespace == Invalid) {
+        qDebug() << "Namespace for reference is invalid";
+        free();
+        return;
+    }
+}
+
+void GitReference::detectNamespace()
+{
+    if(m_raw == nullptr) {
+        return;
+    }
+
+    if(git_reference_is_branch(m_raw)) {
+        m_namespace = Branch;
+        qDebug() << "Reference namespace Branch";
+        return;
+    }
+
+    if(git_reference_is_tag(m_raw)) {
+        m_namespace = Tag;
+        qDebug() << "Reference namespace Tag";
+        return;
+    }
+
+    if(git_reference_is_note(m_raw)) {
+        m_namespace = Note;
+        qDebug() << "Reference namespace Note";
+        return;
+    }
+
+    if(git_reference_is_remote(m_raw)) {
+        m_namespace = Remote;
+        qDebug() << "Reference namespace Remote";
+        return;
+    }
+}
+
+GitReference::~GitReference()
+{
+    free();
+}
+
+void GitReference::free()
+{
+    if(m_raw != nullptr) {
+        git_reference_free(m_raw);
+    }
+
+    m_raw = nullptr;
+}

+ 43 - 0
gitreference.h

@@ -0,0 +1,43 @@
+#ifndef GITREFERENCE_H
+#define GITREFERENCE_H
+
+#include <gitbase.h>
+
+class GitRepository;
+struct git_reference;
+
+class GitReference : public GitBase<git_reference>
+{
+    Q_OBJECT
+    Q_PROPERTY(ReferenceNamespace referenceNamespace READ referenceNamespace CONSTANT)
+    Q_ENUMS(ReferenceNamespace)
+
+public:
+    enum ReferenceNamespace {
+        Invalid,
+        Branch,
+        Note,
+        Tag,
+        Remote
+    };
+
+    virtual ~GitReference();
+
+    ReferenceNamespace referenceNamespace() const
+    {
+        return m_namespace;
+    }
+
+protected:
+    GitReference(git_reference* ref, GitRepository* parent);
+    void free();
+
+    ReferenceNamespace m_namespace;
+
+private:
+    GitReference();
+    Q_DISABLE_COPY(GitReference)
+    void detectNamespace();
+};
+
+#endif // GITREFERENCE_H

+ 39 - 35
gitrepository.cpp

@@ -4,25 +4,28 @@
 #include <QFileInfo>
 #include <QDir>
 
+#include <gitbranch.h>
+
 #include <git2.h>
 
-GitRepository::GitRepository(const QString& root) : QObject()
-  ,m_repo(nullptr)
+char oid_buf[GIT_OID_HEXSZ+1];
+
+GitRepository::GitRepository(const QString& root) : QObject(nullptr)
 {
-    if(git_repository_open(&m_repo, root.toUtf8().data()) != 0) {
+    if(git_repository_open(&m_raw, root.toUtf8().data()) != 0) {
         qDebug() << "Cannot open repository";
         close();
         return;
     }
 
     m_root = root;
-    m_path = git_repository_workdir(m_repo);
+    m_path = git_repository_workdir(m_raw);
     m_name = m_path;//TODO: replace with Human readable name
     qDebug() << "New repo:" << m_name << m_root << m_path;
 
     //    git_reflog* reflog;
 
-    //    if(git_reflog_read(&reflog, m_repo, "HEAD") != 0) {
+    //    if(git_reflog_read(&reflog, m_raw, "HEAD") != 0) {
     //        qDebug() << "reflogs could not be read";
     //        return;
     //    }
@@ -37,47 +40,48 @@ GitRepository::GitRepository(const QString& root) : QObject()
     //    }
     //    git_reflog_free(reflog);
 
-
     git_reference *out;
     git_branch_t branch;
     git_branch_iterator* iter;
-    git_branch_iterator_new(&iter, m_repo, GIT_BRANCH_ALL);
+    git_branch_iterator_new(&iter, m_raw, GIT_BRANCH_ALL);
 
+    git_revwalk* walk;
+    git_revwalk_new(&walk, m_raw);
+    qDebug() << "Branches found:";
     while(git_branch_next(&out, &branch, iter) == 0)
     {
-        const char* branch_name;
-        git_branch_name(&branch_name, out);
-        qDebug() << branch_name;
+        GitBranch testBranch(out, this);
+        qDebug() << testBranch.name();
+    }
 
-        git_revwalk* walk;
-        git_revwalk_new(&walk, m_repo);
-        git_revwalk_push_head(walk);
-        git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
+    return;
 
-        git_oid newoid;
-        while(git_revwalk_next(&newoid, walk) == 0)
+    git_revwalk_push_glob(walk, "refs/heads/*");
+    git_revwalk_sorting(walk, GIT_SORT_TIME);
+
+    git_oid newoid;
+    while(git_revwalk_next(&newoid, walk) == 0)
+    {
+        git_commit *wcommit;
+        if(git_commit_lookup(&wcommit, m_raw, &newoid) != 0 )
         {
-            git_commit *wcommit;
-            if(git_commit_lookup(&wcommit, m_repo, &newoid) != 0 )
-            {
-                qDebug() << "git_commit_lookup error";
-                continue;
-            }
-
-            qDebug() << git_commit_id(wcommit);
-            qDebug() << git_commit_time( wcommit );
-            qDebug() << git_commit_message( wcommit );
-            qDebug() << git_commit_author( wcommit );
-
-            qDebug() << "=================================================";
-            git_commit_free( wcommit );
+            qDebug() << "git_commit_lookup error";
+            continue;
         }
 
-        git_revwalk_free( walk );
+        const git_oid* commit_oid = git_commit_id(wcommit);
+        git_oid_tostr(oid_buf,GIT_OID_HEXSZ+1,commit_oid);
+        qDebug() << oid_buf;
+        qDebug() << git_commit_time( wcommit );
+        qDebug() << git_commit_message( wcommit );
+        qDebug() << git_commit_author( wcommit );
 
-        git_reference_free(out);
+        qDebug() << "=================================================";
+        git_commit_free( wcommit );
     }
 
+    git_revwalk_free( walk );
+
 }
 
 GitRepository::~GitRepository()
@@ -87,8 +91,8 @@ GitRepository::~GitRepository()
 
 void GitRepository::close()
 {
-    if(m_repo) {
-        git_repository_free(m_repo);
+    if(m_raw) {
+        git_repository_free(m_raw);
     }
-    m_repo = nullptr;
+    m_raw = nullptr;
 }

+ 8 - 3
gitrepository.h

@@ -9,10 +9,10 @@ struct git_repository;
 
 class GitRepository : public QObject
 {
+    Q_OBJECT
     Q_PROPERTY(QString root READ root WRITE setRoot NOTIFY rootChanged)
     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
     Q_PROPERTY(QString path READ path NOTIFY rootChanged)
-
 public:
     GitRepository(const QString &root);
     ~GitRepository();
@@ -30,10 +30,15 @@ public:
         return m_path;
     }
 
+    git_repository* raw() const {
+        return m_raw;
+    }
+
     bool isValid() const {
-        return m_repo != nullptr;
+        return m_raw != nullptr;
     }
 
+
 public slots:
     void setRoot(QString root) {
         if (m_root == root)
@@ -60,8 +65,8 @@ private:
 
     QString m_root;
     QString m_name;
-    git_repository* m_repo;
     QString m_path;
+    git_repository* m_raw;
 };
 
 #endif // GITREPOSITORY_H

+ 1 - 0
main.cpp

@@ -20,6 +20,7 @@ int main(int argc, char *argv[])
     qmlRegisterUncreatableType<RepositoryModel>("org.semlanik.nicegit", 1, 0, "RepositoryModel", "Owned only by GitHandler");
 
     GitHandler handler;
+    handler.open("/home/semlanik/Projects/testrepo/");
     view.rootContext()->setContextProperty("_handler", &handler);
     view.setSource(QUrl("qrc:/qml/MainView.qml"));
     view.showMaximized();