gitbranch.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "gitbranch.h"
  2. #include <QDebug>
  3. #include <gitrepository.h>
  4. #include <git2.h>
  5. namespace {
  6. const char remoteAddtion = '/';
  7. }
  8. GitBranch::GitBranch() : GitReference(nullptr, nullptr)
  9. ,m_commit(nullptr)
  10. ,m_type(Invalid)
  11. {
  12. }
  13. GitBranch::GitBranch(git_reference *ref, git_branch_t type, GitRepository *parent) : GitReference(ref, parent)
  14. ,m_commit(nullptr)
  15. ,m_type(static_cast<BranchType>(type))
  16. {
  17. const char* tmpName;
  18. git_branch_name(&tmpName, m_raw);
  19. m_fullName = tmpName;
  20. m_name = m_fullName;
  21. git_annotated_commit_from_ref(&m_commit, repository()->raw(), m_raw);
  22. m_oid = GitOid(git_annotated_commit_id(m_commit), m_repository);
  23. git_buf buf = GIT_BUF_INIT_CONST("",0);
  24. if(git_branch_remote_name(&buf, repository()->raw(), refName().toUtf8().data()) == 0) {
  25. m_remote = QString::fromUtf8(buf.ptr);
  26. m_name.remove(m_remote + remoteAddtion);
  27. }
  28. }
  29. GitBranch::GitBranch(GitBranch&& other) : GitReference(std::move(other))
  30. {
  31. //TODO: looks like free() functionality might be more common for GitBase childred.
  32. //Need to think about it
  33. if(m_commit != nullptr) {
  34. git_annotated_commit_free(m_commit);
  35. m_commit = nullptr;
  36. }
  37. m_commit = other.m_commit;
  38. other.m_commit = nullptr;
  39. }
  40. GitBranch &GitBranch::operator=(GitBranch&& other)
  41. {
  42. if(&other != this) {
  43. if(m_commit != nullptr) {
  44. git_annotated_commit_free(m_commit);
  45. m_commit = nullptr;
  46. }
  47. m_commit = other.m_commit;
  48. other.m_commit = nullptr;
  49. }
  50. return static_cast<GitBranch&>(GitReference::operator=(std::move(other)));
  51. }
  52. GitBranch::~GitBranch()
  53. {
  54. if(m_commit != nullptr) {
  55. git_annotated_commit_free(m_commit);
  56. m_commit = nullptr;
  57. }
  58. }
  59. GitBranch::BranchType GitBranch::type() const
  60. {
  61. return m_type;
  62. }
  63. void GitBranch::setUpstream(const GitBranch& branch)
  64. {
  65. if(type() == GIT_BRANCH_REMOTE
  66. || branch.type() == GIT_BRANCH_LOCAL) {
  67. qWarning() << "Try to setup invalid pair of upstream/local branches";
  68. return;
  69. }
  70. git_branch_set_upstream(branch.raw(), branch.fullName().toUtf8().data());
  71. }
  72. GitBranch GitBranch::upstream() const
  73. {
  74. git_reference* ref = nullptr;
  75. if(type() == GIT_BRANCH_REMOTE) {
  76. qDebug() << "Skipping upstream read for remote branch";
  77. return GitBranch();
  78. }
  79. if(git_branch_upstream(&ref, raw()) != 0) {
  80. qWarning() << "Invalid reference or branch type" << GitBase::lastError();
  81. return GitBranch();
  82. }
  83. return GitBranch(ref, GIT_BRANCH_REMOTE, repository());
  84. }