commitgraph.cpp 5.9 KB

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