commitgraph.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "commitgraph.h"
  2. #include <gitcommit.h>
  3. #include <gitbranch.h>
  4. #include <gittag.h>
  5. #include <colorhandler.h>
  6. #include <graphpoint.h>
  7. #include <graphlistmodel.h>
  8. #include <QDateTime>
  9. #include <QScopedPointer>
  10. #include <git2/revwalk.h>
  11. #include <git2/commit.h>
  12. CommitGraph::CommitGraph() : QObject()
  13. ,m_pointsModel(new GraphListModel(this))
  14. ,m_branchesCount(0)
  15. {
  16. qsrand(QDateTime::currentMSecsSinceEpoch());
  17. }
  18. void CommitGraph::addHead(GitBranch* branch)
  19. {
  20. const GitOid& oid = branch->oid();
  21. addHead(oid);
  22. }
  23. void CommitGraph::addHead(const GitOid &oid)
  24. {
  25. //Read color for this head
  26. m_color = ColorHandler::instance().color(oid);
  27. //Random color generation to be replaced with resets
  28. git_revwalk* walk;
  29. git_revwalk_new(&walk, oid.repository()->raw());
  30. git_revwalk_push(walk, oid.raw());
  31. git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
  32. git_oid newOid;
  33. while(git_revwalk_next(&newOid, walk) == 0) {
  34. GitOid commitOid(&newOid, oid.repository());
  35. GitCommit *commit = GitCommit::fromOid(commitOid);
  36. findParents(commit);
  37. delete commit;
  38. }
  39. git_revwalk_free(walk);
  40. // qDebug() << "Update Y coordinate after head added";
  41. QList<int> branchStarted;
  42. for(int i = 0; i < m_sortedPoints.count(); i++) {
  43. GraphPoint* point = m_sortedPoints.at(i);
  44. point->setY(m_sortedPoints.count() - i - 1);
  45. GitCommit *commit = GitCommit::fromOid(point->oid());
  46. git_commit* commitRaw = nullptr;
  47. int parentCount = git_commit_parentcount(commit->raw());
  48. // qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  49. for(int j = 0; j < parentCount; j++) {//Add connection to parent in case if count of parents > 1
  50. git_commit_parent(&commitRaw, commit->raw(), j);
  51. GitOid oidParent(git_commit_id(commitRaw), oid.repository());
  52. GraphPoint* parentPoint = m_points.value(oidParent);
  53. bool rearrangmentRequired = parentPoint->addChildPoint(point);
  54. Q_UNUSED(rearrangmentRequired)
  55. // if(rearrangmentRequired) { //TODO: need to investigate how to avoid croses on branches
  56. //in case of while addChildPoint operations we have conflict
  57. //with logic bellow
  58. // i = m_sortedPoints.indexOf(parentPoint) - 1;
  59. // break;
  60. // }
  61. //This logic denie to produce branches that are on the same 'x' line with other branches
  62. //that are active at this time. The loop bellow finds free 'x' line to use it for branch
  63. //allocation.
  64. if(parentPoint->x() < point->x()) {
  65. bool contains = false;
  66. while(branchStarted.contains(point->x())) {
  67. contains = true;
  68. point->setX(point->x() + 1);
  69. }
  70. if(!contains) {
  71. branchStarted.append(point->x());
  72. }
  73. //Once branch is merged to another branch or ends "in the air" this logic releases branch
  74. //line and provides possibility to use freed 'x' for other branches.
  75. } else if(parentPoint->x() > point->x()) {
  76. branchStarted.removeAll(parentPoint->x());
  77. }
  78. if( point->childPoints().count() <= 0) {
  79. branchStarted.removeAll(point->x());
  80. }
  81. }
  82. delete commit;
  83. m_branchesCount = m_branchesCount < (point->x() + 1) ? (point->x() + 1) : m_branchesCount;
  84. }
  85. m_pointsModel->reset(m_sortedPoints);
  86. emit branchesCountChanged(m_branchesCount);
  87. }
  88. void CommitGraph::findParents(GitCommit* commit)
  89. {
  90. QList<GitOid> reverseList;
  91. git_commit* commitRaw = commit->raw();
  92. while(commitRaw != nullptr)
  93. {
  94. GitOid parentOid(git_commit_id(commitRaw), commit->repository());
  95. commit = GitCommit::fromOid(parentOid);
  96. reverseList.push_front(parentOid);
  97. if(m_points.contains(parentOid)) { //Finish parents lookup once parent found in tree. We will see nothing new in this branch
  98. break;
  99. }
  100. // qDebug() << "Add commit to reverselist" << parentOid.toString();
  101. commitRaw = nullptr;
  102. git_commit_parent(&commitRaw, commit->raw(), 0);
  103. }
  104. if(reverseList.count() < 2) { //In case if only original commit in list, we didn't find anything new
  105. return;
  106. }
  107. addCommits(reverseList);
  108. }
  109. void CommitGraph::addCommits(QList<GitOid>& reversList)
  110. {
  111. GraphPoint* point = nullptr;
  112. GraphPoint* parentPoint = nullptr;
  113. for(int i = 0; i < (reversList.count() - 1); i++) {
  114. GitOid& parentIter = reversList[i];
  115. GitOid& childIter = reversList[i + 1];
  116. parentPoint = m_points.value(parentIter, nullptr);
  117. if(parentPoint == nullptr) {
  118. parentPoint = new GraphPoint(parentIter, this);
  119. parentPoint->setColor(m_color);
  120. m_sortedPoints.prepend(QPointer<GraphPoint>(parentPoint));
  121. m_points.insert(parentPoint->oid(), parentPoint);
  122. }
  123. point = m_points.value(childIter, nullptr);
  124. if(point == nullptr) {
  125. int parentPosition = m_sortedPoints.indexOf(parentPoint);
  126. int x = parentPoint->x() + parentPoint->childPointsCount();
  127. point = new GraphPoint(childIter, x, 0, m_color, this);
  128. m_points.insert(point->oid(), point);
  129. //Ordered commits
  130. if(parentPosition >= 0) {
  131. m_sortedPoints.insert(parentPosition + 1, point);
  132. }
  133. // qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  134. // qDebug() << "New commit parent: " << parentPoint->oid().toString() << parentPoint->x() << parentPoint->y();
  135. }
  136. if(point) { //add child point in any case
  137. parentPoint->addChildPoint(point);
  138. }
  139. }
  140. }
  141. GraphPoint* CommitGraph::point(const GitOid& oid)
  142. {
  143. GraphPoint* point = m_points.value(oid, nullptr);
  144. return point;
  145. }
  146. GraphPoint* CommitGraph::point(int i)
  147. {
  148. return m_sortedPoints.at(i);
  149. }