commitgraph.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if(commitRaw == nullptr) {
  104. checkRoot(commit);
  105. }
  106. }
  107. if(reverseList.count() <= 1) { //In case if only original commit in list, we didn't find anything new
  108. return;
  109. }
  110. addCommits(reverseList);
  111. }
  112. void CommitGraph::checkRoot(GitCommit *commit)
  113. {
  114. qDebug() << "Check root commit";
  115. if(m_points.contains(commit->oid())) {
  116. return;
  117. }
  118. GraphPoint* rootPoint = new GraphPoint(commit->oid(), this);
  119. m_sortedPoints.prepend(QPointer<GraphPoint>(rootPoint));
  120. m_points.insert(rootPoint->oid(), rootPoint);
  121. }
  122. void CommitGraph::addCommits(QList<GitOid>& reversList)
  123. {
  124. GraphPoint* point = nullptr;
  125. GraphPoint* parentPoint = nullptr;
  126. for(int i = 0; i < (reversList.count() - 1); i++) {
  127. GitOid& parentIter = reversList[i];
  128. GitOid& childIter = reversList[i + 1];
  129. parentPoint = m_points.value(parentIter, nullptr);
  130. if(parentPoint == nullptr) {
  131. parentPoint = new GraphPoint(parentIter, this);
  132. parentPoint->setColor(m_color);
  133. m_sortedPoints.prepend(QPointer<GraphPoint>(parentPoint));
  134. m_points.insert(parentPoint->oid(), parentPoint);
  135. }
  136. point = m_points.value(childIter, nullptr);
  137. if(point == nullptr) {
  138. int parentPosition = m_sortedPoints.indexOf(parentPoint);
  139. int x = parentPoint->x() + parentPoint->childPointsCount();
  140. point = new GraphPoint(childIter, x, 0, m_color, this);
  141. m_points.insert(point->oid(), point);
  142. //Ordered commits
  143. if(parentPosition >= 0) {
  144. m_sortedPoints.insert(parentPosition + 1, point);
  145. }
  146. // qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  147. // qDebug() << "New commit parent: " << parentPoint->oid().toString() << parentPoint->x() << parentPoint->y();
  148. }
  149. if(point) { //add child point in any case
  150. parentPoint->addChildPoint(point);
  151. }
  152. }
  153. }
  154. GraphPoint* CommitGraph::point(const GitOid& oid)
  155. {
  156. GraphPoint* point = m_points.value(oid, nullptr);
  157. return point;
  158. }
  159. GraphPoint* CommitGraph::point(int i)
  160. {
  161. return m_sortedPoints.at(i);
  162. }
  163. void CommitGraph::addWorkdir()
  164. {
  165. GraphPoint* point = new GraphPoint(GitOid(), this);
  166. m_points.insert(GitOid(), new GraphPoint(GitOid(), this));
  167. m_sortedPoints.last()->addChildPoint(point);
  168. m_sortedPoints.append(point);
  169. for(int i = 0; i < m_sortedPoints.count(); i++) {
  170. GraphPoint* point = m_sortedPoints.at(i);
  171. point->setY(m_sortedPoints.count() - i - 1);
  172. }
  173. m_pointsModel->reset(m_sortedPoints);
  174. }