commitgraph.cpp 6.0 KB

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