gitrepository.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "gitrepository.h"
  2. #include <QDebug>
  3. #include <QFileInfo>
  4. #include <QDir>
  5. #include <gitbranch.h>
  6. #include <gitcommit.h>
  7. #include <gittag.h>
  8. #include <git2.h>
  9. GitRepository::GitRepository(const QString& root) : QObject(nullptr)
  10. {
  11. if(git_repository_open(&m_raw, root.toUtf8().data()) != 0) {
  12. qDebug() << "Cannot open repository";
  13. close();
  14. return;
  15. }
  16. m_root = root;
  17. m_path = git_repository_workdir(m_raw);
  18. m_name = m_path;//TODO: replace with Human readable name
  19. qDebug() << "New repo:" << m_name << m_root << m_path;
  20. readBranches();
  21. readTags();
  22. }
  23. GitRepository::~GitRepository()
  24. {
  25. close();
  26. }
  27. void GitRepository::close()
  28. {
  29. if(m_raw) {
  30. git_repository_free(m_raw);
  31. }
  32. m_raw = nullptr;
  33. }
  34. void GitRepository::readBranches()
  35. {
  36. git_reference *branchRef;
  37. git_branch_t branchType;
  38. git_branch_iterator* iter;
  39. git_branch_iterator_new(&iter, m_raw, GIT_BRANCH_ALL);
  40. while(git_branch_next(&branchRef, &branchType, iter) == 0)
  41. {
  42. GitBranch* branch = new GitBranch(branchRef, branchType, this);
  43. m_branches.insert(branch->name(), QPointer<GitBranch>(branch));
  44. qDebug() << branch->name();
  45. qDebug() << branch->type();
  46. }
  47. }
  48. void GitRepository::readTags()
  49. {
  50. git_tag_foreach(
  51. raw(),
  52. [](const char *name, git_oid *oid, void *payload) -> int
  53. {
  54. Q_UNUSED(payload)
  55. Q_UNUSED(name)
  56. GitRepository* repo = static_cast<GitRepository*>(payload);
  57. git_tag* tagraw = 0;
  58. if(git_tag_lookup(&tagraw, repo->raw(), oid) != 0) {
  59. qCritical() << "Invalid tag found. Broken repository";
  60. return 1;
  61. }
  62. GitTag* tag = new GitTag(tagraw, repo);
  63. if(tag->isValid()) {
  64. repo->m_tags.insert(tag->targetId(), tag);
  65. }
  66. qDebug() << "Tag found: " << tag->name() << tag->sha1();
  67. return 0;
  68. },
  69. this);
  70. }