Browse Source

Initial repo reafing

semlanik 7 years ago
parent
commit
e72600ebc9
8 changed files with 266 additions and 0 deletions
  1. 22 0
      NiceGit.pro
  2. 65 0
      githandler.cpp
  3. 44 0
      githandler.h
  4. 22 0
      gitrepository.cpp
  5. 61 0
      gitrepository.h
  6. 22 0
      main.cpp
  7. 25 0
      qml/MainView.qml
  8. 5 0
      resources.qrc

+ 22 - 0
NiceGit.pro

@@ -0,0 +1,22 @@
+QT += qml quick widgets
+
+TEMPLATE = app
+
+LIBS += -lgit2
+
+SOURCES += \
+    main.cpp \
+    githandler.cpp \
+    gitrepository.cpp
+
+HEADERS += \
+    githandler.h \
+    gitrepository.h
+
+DISTFILES += \
+    qml/MainView.qml
+
+DEPLOYMENT += $$DISTFILES
+
+RESOURCES += \
+    resources.qrc

+ 65 - 0
githandler.cpp

@@ -0,0 +1,65 @@
+#include "githandler.h"
+
+#include <QDebug>
+#include <QUrl>
+
+extern "C" {
+#include <git2.h>
+}
+
+GitHandler::GitHandler() : QObject()
+    ,_repo(nullptr)
+{
+    git_libgit2_init();
+}
+
+GitHandler::~GitHandler()
+{
+    close();
+    git_libgit2_shutdown();
+}
+
+void GitHandler::open(const QUrl &url)
+{
+    if(url.isLocalFile()) {
+        open(url.toLocalFile());
+    }
+}
+
+void GitHandler::open(const QString &path)
+{
+    close();
+
+    git_buf root = {0,0,0};
+    if(git_repository_discover(&root, path.toUtf8().data(), 0, NULL) != 0) {
+        qDebug() << lastError();
+        return;
+    }
+
+    QString str = QString::fromUtf8(root.ptr, root.size);
+
+    if(git_repository_open(&_repo, str.toUtf8().data()) != 0) {
+        qDebug() << "Cannot open repository";
+        qDebug() << "Error:" << lastError();
+        close();
+    }
+}
+
+void GitHandler::close()
+{
+    if(_repo) {
+        git_repository_free(_repo);
+    }
+    _repo = nullptr;
+}
+
+QString GitHandler::lastError() const
+{
+    const git_error *e = giterr_last();
+    if(e) {
+        return QString("(%1): %2").arg(e->klass).arg(e->message);
+        giterr_clear();
+    }
+    giterr_clear();
+    return QString();
+}

+ 44 - 0
githandler.h

@@ -0,0 +1,44 @@
+#ifndef GITHANDLER_H
+#define GITHANDLER_H
+
+#include <QObject>
+
+#include <gitrepository.h>
+
+class GitHandler : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY(GitRepository* repo READ repo WRITE setRepo NOTIFY repoChanged)
+
+public:
+    GitHandler();
+    virtual ~GitHandler();
+    Q_INVOKABLE void open(const QString &path);
+    Q_INVOKABLE void open(const QUrl &url);
+    Q_INVOKABLE void close();
+
+    GitRepository* repo() const
+    {
+        return m_repo;
+    }
+
+public slots:
+    void setRepo(GitRepository* repo)
+    {
+        if (m_repo == repo)
+            return;
+
+        m_repo = repo;
+        emit repoChanged(repo);
+    }
+
+signals:
+    void repoChanged(GitRepository* repo);
+
+protected:
+    QString lastError() const;
+private:
+    GitRepository* m_repo;
+};
+
+#endif // GITHANDLER_H

+ 22 - 0
gitrepository.cpp

@@ -0,0 +1,22 @@
+#include "gitrepository.h"
+
+GitRepository::GitRepository() : QObject()
+  ,m_repo(nullptr)
+{
+
+}
+
+GitRepository~GitRepository()
+{
+
+}
+
+void GitRepository::open(const QString &path)
+{
+
+}
+
+void GitRepository::close()
+{
+
+}

+ 61 - 0
gitrepository.h

@@ -0,0 +1,61 @@
+#ifndef GITREPOSITORY_H
+#define GITREPOSITORY_H
+
+#include <QObject>
+#include <QString>
+
+extern "C"
+{
+    struct git_repository;
+}
+
+
+class GitRepository : public QObject
+{
+    Q_PROPERTY(QString root READ root WRITE setRoot NOTIFY rootChanged)
+    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
+
+public:
+    GitRepository();
+    ~GitRepository();
+
+    Q_INVOKABLE void open(const QString &path);
+    Q_INVOKABLE void open(const QUrl &url);
+    Q_INVOKABLE void close();
+
+    QString root() const {
+        return m_root;
+    }
+
+    QString name() const {
+        return m_name;
+    }
+
+public slots:
+    void setRoot(QString root) {
+        if (m_root == root)
+            return;
+
+        m_root = root;
+        emit rootChanged(root);
+    }
+
+    void setName(QString name) {
+        if (m_name == name)
+            return;
+
+        m_name = name;
+        emit nameChanged(name);
+    }
+
+signals:
+    void rootChanged(QString root);
+    void nameChanged(QString name);
+
+private:
+    QString m_root;
+    QString m_name;
+    git_repository* m_repo;
+};
+
+#endif // GITREPOSITORY_H

+ 22 - 0
main.cpp

@@ -0,0 +1,22 @@
+#include <QApplication>
+#include <QQmlEngine>
+#include <QQuickView>
+#include <QQmlContext>
+
+#include <githandler.h>
+
+#include <QDebug>
+
+int main(int argc, char *argv[])
+{
+    QApplication app(argc, argv);
+
+    QQuickView view;
+
+    qmlRegisterUncreatableType<GitHandler>("org.semlanik.nicegit", 1, 0, "GitHandler", "Global for qml");
+    GitHandler handler;
+    view.rootContext()->setContextProperty("_handler", &handler);
+    view.setSource(QUrl("qrc:/qml/MainView.qml"));
+    view.showMaximized();
+    return app.exec();
+}

+ 25 - 0
qml/MainView.qml

@@ -0,0 +1,25 @@
+import QtQuick 2.0
+import QtQuick.Controls 1.4
+import QtQuick.Dialogs 1.2
+
+Item {
+    Row {
+        Text {
+            text: "Active repository" + repoOpenDialog.fileUrl
+        }
+        Button {
+            text: "Choose..."
+            onClicked: repoOpenDialog.open()
+        }
+    }
+
+    FileDialog {
+        id: repoOpenDialog
+        folder: "."
+        selectFolder: true
+        selectMultiple: false
+        onAccepted: {
+            _handler.open(repoOpenDialog.fileUrl)
+        }
+    }
+}

+ 5 - 0
resources.qrc

@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/">
+        <file>qml/MainView.qml</file>
+    </qresource>
+</RCC>