githandler.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <tagmodel.h>
  9. #include <git2.h>
  10. #include <commitgraph.h>
  11. #include <graphpoint.h>
  12. GitHandler::GitHandler() : QObject()
  13. ,m_repositories(new RepositoryModel(this))
  14. {
  15. git_libgit2_init();
  16. }
  17. GitHandler::~GitHandler()
  18. {
  19. git_libgit2_shutdown();
  20. }
  21. void GitHandler::open(const QUrl &url)
  22. {
  23. if(url.isLocalFile()) {
  24. open(url.toLocalFile());
  25. }
  26. }
  27. void GitHandler::open(const QString &path)
  28. {
  29. git_buf root = {0,0,0};
  30. if(git_repository_discover(&root, path.toUtf8().data(), 0, NULL) != 0) {
  31. qDebug() << lastError();
  32. return;
  33. }
  34. GitRepository* repo = new GitRepository(QString::fromUtf8(root.ptr, root.size));
  35. if(!repo->isValid()) {
  36. qDebug() << lastError();
  37. return;
  38. }
  39. BranchContainer &branches = repo->branches();
  40. CommitGraph* graph = new CommitGraph();
  41. graph->addHead(branches.value("master").data()->oid());
  42. foreach(GitBranch* branch, branches) {
  43. qDebug() << "Next head " << branch->name();
  44. graph->addHead(branch);
  45. }
  46. setGraph(graph);
  47. m_repositories->add(repo);
  48. }
  49. CommitModel* GitHandler::modelByHead(const QString& head)
  50. {
  51. return m_commits.value(head).data();
  52. }
  53. QString GitHandler::lastError() const
  54. {
  55. const git_error *e = giterr_last();
  56. if(e) {
  57. return QString("(%1): %2").arg(e->klass).arg(e->message);
  58. giterr_clear();
  59. }
  60. giterr_clear();
  61. return QString();
  62. }