commitgraph.cpp 6.1 KB

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