githandler.cpp 1.7 KB

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