gitrepository.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "gitrepository.h"
  2. #include <QDebug>
  3. #include <QFileInfo>
  4. #include <QDir>
  5. #include <gitbranch.h>
  6. #include <gitcommit.h>
  7. #include <git2.h>
  8. GitRepository::GitRepository(const QString& root) : QObject(nullptr)
  9. {
  10. if(git_repository_open(&m_raw, root.toUtf8().data()) != 0) {
  11. qDebug() << "Cannot open repository";
  12. close();
  13. return;
  14. }
  15. m_root = root;
  16. m_path = git_repository_workdir(m_raw);
  17. m_name = m_path;//TODO: replace with Human readable name
  18. qDebug() << "New repo:" << m_name << m_root << m_path;
  19. readBranches();
  20. }
  21. GitRepository::~GitRepository()
  22. {
  23. close();
  24. }
  25. void GitRepository::close()
  26. {
  27. if(m_raw) {
  28. git_repository_free(m_raw);
  29. }
  30. m_raw = nullptr;
  31. }
  32. void GitRepository::readBranches()
  33. {
  34. git_reference *branchRef;
  35. git_branch_t branchType;
  36. git_branch_iterator* iter;
  37. git_branch_iterator_new(&iter, m_raw, GIT_BRANCH_ALL);
  38. while(git_branch_next(&branchRef, &branchType, iter) == 0)
  39. {
  40. GitBranch* branch = new GitBranch(branchRef, branchType, this);
  41. m_branches.insert(branch->name(), QPointer<GitBranch>(branch));
  42. qDebug() << branch->name();
  43. qDebug() << branch->type();
  44. }
  45. }