commitgraph.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "commitgraph.h"
  2. #include <gitcommit.h>
  3. #include <graphpoint.h>
  4. #include <QDateTime>
  5. #include <git2/revwalk.h>
  6. #include <git2/commit.h>
  7. CommitGraph::CommitGraph() : QObject()
  8. {
  9. qsrand(QDateTime::currentMSecsSinceEpoch());
  10. }
  11. void CommitGraph::addHead(const GitOid &oid)
  12. {
  13. //Random color generation to be replaced with resets
  14. int red = qrand() % 205 + 50;
  15. int green = qrand() % 205 + 50;
  16. int blue = qrand() % 205 + 50;
  17. m_color = QString::number(red, 16) + QString::number(green, 16) + QString::number(blue, 16);
  18. git_revwalk* walk;
  19. git_revwalk_new(&walk, oid.repository()->raw());
  20. git_revwalk_push(walk, oid.raw());
  21. git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
  22. git_oid newOid;
  23. while(git_revwalk_next(&newOid, walk) == 0) {
  24. GitOid commitOid(&newOid, oid.repository());
  25. GitCommit *commit = GitCommit::fromOid(commitOid);
  26. findParents(commit);
  27. }
  28. git_revwalk_free(walk);
  29. QList<GitOid> openedBranches;
  30. qDebug() << "Update Y coordinate after head added";
  31. for(int i = 0; i < m_sortedPoints.count(); i++) {
  32. GraphPoint* point = static_cast<GraphPoint*>(m_sortedPoints.at(i));
  33. point->setY(m_sortedPoints.count() - i - 1);
  34. GitCommit *commit = GitCommit::fromOid(point->oid());
  35. git_commit* commitRaw = nullptr;
  36. int parentCount = git_commit_parentcount(commit->raw());
  37. int minX = point->x();
  38. for(int j = 0; j < parentCount; j++) {
  39. git_commit_parent(&commitRaw, commit->raw(), j);
  40. GitOid oidParent(git_commit_id(commitRaw), oid.repository());
  41. GraphPoint* parentPoint = m_points.value(oidParent);
  42. int x = parentPoint->x() + parentPoint->childPointsCount();
  43. qDebug() << "MinX = " << minX << " x:" << x;
  44. if(parentPoint->childPoints().indexOf(point) < 0) {
  45. minX = x;
  46. // if(x > minX) {
  47. // }
  48. point->setX(minX);
  49. }
  50. parentPoint->addChildPoint(point);
  51. }
  52. qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  53. point->setX(minX);
  54. }
  55. }
  56. void CommitGraph::findParents(GitCommit* commit)
  57. {
  58. QList<GitOid> reverseList;
  59. git_commit* commitRaw = commit->raw();
  60. while(commitRaw != nullptr)
  61. {
  62. GitOid parentOid(git_commit_id(commitRaw), commit->repository());
  63. commit = GitCommit::fromOid(parentOid);
  64. reverseList.push_front(parentOid);
  65. if(m_points.contains(parentOid)) { //Finish parents lookup once parent found in tree. We will see nothing new in this branch
  66. break;
  67. }
  68. // qDebug() << "Add commit to reverselist" << parentOid.toString();
  69. commitRaw = nullptr;
  70. git_commit_parent(&commitRaw, commit->raw(), 0);
  71. }
  72. if(reverseList.count() < 2) { //In case if only original commit in list, we didn't find anything new
  73. return;
  74. }
  75. addCommits(reverseList);
  76. }
  77. void CommitGraph::addCommits(QList<GitOid>& reversList)
  78. {
  79. GraphPoint* point = nullptr;
  80. GraphPoint* parentPoint = nullptr;
  81. for(int i = 0; i < (reversList.count() - 1); i++) {
  82. GitOid& parentIter = reversList[i];
  83. GitOid& childIter = reversList[i + 1];
  84. parentPoint = m_points.value(parentIter, nullptr);
  85. if(parentPoint == nullptr) {
  86. parentPoint = new GraphPoint(parentIter, this);
  87. parentPoint->setColor(m_color);
  88. m_sortedPoints.append(parentPoint);
  89. m_points.insert(parentPoint->oid(), parentPoint);
  90. }
  91. point = m_points.value(childIter, nullptr);
  92. if(point == nullptr) {
  93. int parentPosition = m_sortedPoints.indexOf(parentPoint);
  94. // int x = parentPoint->x() + parentPoint->childPointsCount();
  95. // point = new GraphPoint(childIter, x, 0, m_color, this);
  96. point = new GraphPoint(childIter, 0, 0, m_color, this);
  97. m_points.insert(point->oid(), point);
  98. // parentPoint->addChildPoint(point);
  99. //Ordered commits
  100. if(parentPosition >= 0) {
  101. m_sortedPoints.insert(parentPosition + 1, point);
  102. }
  103. // qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  104. // qDebug() << "New commit parent: " << parentPoint->oid().toString() << parentPoint->x() << parentPoint->y();
  105. }
  106. }
  107. }