githandler.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "githandler.h"
  2. #include <QDebug>
  3. #include <QUrl>
  4. #include <qqml.h>
  5. #include <gitrepository.h>
  6. #include <gitbranch.h>
  7. #include <commitmodel.h>
  8. #include <git2.h>
  9. GitHandler::GitHandler() : QObject()
  10. ,m_repositories(new RepositoryModel(this))
  11. {
  12. git_libgit2_init();
  13. }
  14. GitHandler::~GitHandler()
  15. {
  16. git_libgit2_shutdown();
  17. }
  18. void GitHandler::open(const QUrl &url)
  19. {
  20. if(url.isLocalFile()) {
  21. open(url.toLocalFile());
  22. }
  23. }
  24. void GitHandler::open(const QString &path)
  25. {
  26. git_buf root = {0,0,0};
  27. if(git_repository_discover(&root, path.toUtf8().data(), 0, NULL) != 0) {
  28. qDebug() << lastError();
  29. return;
  30. }
  31. GitRepository* repo = new GitRepository(QString::fromUtf8(root.ptr, root.size));
  32. if(!repo->isValid()) {
  33. qDebug() << lastError();
  34. return;
  35. }
  36. BranchContainer &branches = repo->branches();
  37. // foreach(GitBranch* branch, branches) {
  38. // qDebug() << "Branch: " << branch->name();
  39. // CommitModel* model = CommitModel::fromBranch(branch);
  40. // m_commits.insert(model->head(), model);
  41. // }
  42. CommitModel* model = CommitModel::fromBranch(branches.value("master"));
  43. m_commits.insert(model->head(), model);
  44. m_repositories->add(repo);
  45. }
  46. CommitModel* GitHandler::modelByHead(const QString& head)
  47. {
  48. return m_commits.value(head).data();
  49. }
  50. QString GitHandler::lastError() const
  51. {
  52. const git_error *e = giterr_last();
  53. if(e) {
  54. return QString("(%1): %2").arg(e->klass).arg(e->message);
  55. giterr_clear();
  56. }
  57. giterr_clear();
  58. return QString();
  59. }