gitreference.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "gitreference.h"
  2. #include <QDebug>
  3. #include <git2.h>
  4. GitReference::GitReference(git_reference *ref, GitRepository *parent) : GitBaseOid(nullptr, parent)
  5. ,m_namespace(Invalid)
  6. {
  7. if (ref == nullptr) {
  8. qDebug() << "Null reference ptr";
  9. return;
  10. }
  11. m_raw = ref;
  12. detectNamespace();
  13. if (m_namespace == Invalid) {
  14. qDebug() << "Namespace for reference is invalid";
  15. free();
  16. return;
  17. }
  18. }
  19. GitReference::GitReference(GitReference &&other) : GitBaseOid(std::move(other))
  20. {
  21. free();
  22. m_raw = other.m_raw;
  23. m_namespace = other.m_namespace;
  24. other.m_raw = nullptr;
  25. other.m_namespace = Invalid;
  26. }
  27. GitReference& GitReference::operator=(GitReference &&other)
  28. {
  29. if (&other != this) {
  30. free();
  31. m_raw = other.m_raw;
  32. m_namespace = other.m_namespace;
  33. other.m_raw = nullptr;
  34. other.m_namespace = Invalid;
  35. }
  36. return static_cast<GitReference&>(GitBaseOid::operator=(std::move(other)));
  37. }
  38. void GitReference::detectNamespace()
  39. {
  40. if (m_raw == nullptr) {
  41. return;
  42. }
  43. if (git_reference_is_branch(m_raw)) {
  44. m_namespace = Branch;
  45. qDebug() << "Reference namespace Branch";
  46. return;
  47. }
  48. if (git_reference_is_tag(m_raw)) {
  49. m_namespace = Tag;
  50. qDebug() << "Reference namespace Tag";
  51. return;
  52. }
  53. if (git_reference_is_note(m_raw)) {
  54. m_namespace = Note;
  55. qDebug() << "Reference namespace Note";
  56. return;
  57. }
  58. if (git_reference_is_remote(m_raw)) {
  59. m_namespace = Remote;
  60. qDebug() << "Reference namespace Remote";
  61. return;
  62. }
  63. }
  64. GitReference::~GitReference()
  65. {
  66. free();
  67. }
  68. void GitReference::free()
  69. {
  70. if (m_raw != nullptr) {
  71. git_reference_free(m_raw);
  72. }
  73. m_raw = nullptr;
  74. }
  75. QString GitReference::refName() const
  76. {
  77. return QString(git_reference_name(m_raw));
  78. }