gitrepository.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <gitremote.h>
  9. #include <gitdiff.h>
  10. #include <git2.h>
  11. GitRepository::GitRepository(const QString& root) : QObject(nullptr)
  12. {
  13. if(git_repository_open(&m_raw, root.toUtf8().data()) != 0) {
  14. qDebug() << "Cannot open repository";
  15. close();
  16. return;
  17. }
  18. m_root = root;
  19. m_path = git_repository_workdir(m_raw);
  20. m_name = m_path;//TODO: replace with Human readable name
  21. qDebug() << "New repo:" << m_name << m_root << m_path;
  22. readBranches();
  23. readTags();
  24. readRemotes();
  25. }
  26. GitRepository::~GitRepository()
  27. {
  28. close();
  29. }
  30. void GitRepository::close()
  31. {
  32. if(m_raw) {
  33. git_repository_free(m_raw);
  34. }
  35. m_raw = nullptr;
  36. }
  37. void GitRepository::readBranches()
  38. {
  39. git_reference *branchRef;
  40. git_branch_t branchType;
  41. git_branch_iterator* iter;
  42. git_branch_iterator_new(&iter, m_raw, GIT_BRANCH_ALL);
  43. while(git_branch_next(&branchRef, &branchType, iter) == 0)
  44. {
  45. GitBranch* branch = new GitBranch(branchRef, branchType, this);
  46. m_branches.insert(branch->name(), QPointer<GitBranch>(branch));
  47. qDebug() << branch->name();
  48. qDebug() << branch->type();
  49. }
  50. }
  51. void GitRepository::readTags()
  52. {
  53. git_tag_foreach(raw(),
  54. [](const char *name, git_oid *oid, void *payload) -> int
  55. {
  56. Q_UNUSED(payload)
  57. Q_UNUSED(name)
  58. GitRepository* repo = static_cast<GitRepository*>(payload);
  59. git_tag* tagraw = 0;
  60. if(git_tag_lookup(&tagraw, repo->raw(), oid) != 0) {
  61. qCritical() << "Invalid tag found. Broken repository";
  62. return 1;
  63. }
  64. GitTag* tag = new GitTag(tagraw, repo);
  65. if(tag->isValid()) {
  66. repo->m_tags.insert(tag->targetId(), tag);
  67. }
  68. qDebug() << "Tag found: " << tag->name() << tag->sha1();
  69. return 0;
  70. },
  71. this);
  72. }
  73. void GitRepository::readRemotes()
  74. {
  75. git_reference_foreach(raw(),[](git_reference *reference, void *payload) -> int
  76. {
  77. if(git_reference_is_remote(reference)) {
  78. GitRemote* remote = GitRemote::fromName(QString::fromLatin1(git_reference_name(reference)), static_cast<GitRepository*>(payload));
  79. }
  80. }, this);
  81. // git_remote* remoteRaw;
  82. // git_reference_is_remote()
  83. }