commitgraph.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. qDebug() << "Update Y coordinate after head added";
  30. for(int i = 0; i < m_sortedPoints.count(); i++) {
  31. GraphPoint* point = static_cast<GraphPoint*>(m_sortedPoints.at(i));
  32. point->setY(m_sortedPoints.count() - i - 1);
  33. GitCommit *commit = GitCommit::fromOid(point->oid());
  34. git_commit* commitRaw = nullptr;
  35. int parentCount = git_commit_parentcount(commit->raw());
  36. for(int j = 0; j < parentCount; j++) {//Add connection to parent in case if count of parents > 1
  37. git_commit_parent(&commitRaw, commit->raw(), j);
  38. GitOid oidParent(git_commit_id(commitRaw), oid.repository());
  39. GraphPoint* parentPoint = m_points.value(oidParent);
  40. parentPoint->addChildPoint(point);
  41. }
  42. qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  43. }
  44. }
  45. void CommitGraph::findParents(GitCommit* commit)
  46. {
  47. QList<GitOid> reverseList;
  48. git_commit* commitRaw = commit->raw();
  49. while(commitRaw != nullptr)
  50. {
  51. GitOid parentOid(git_commit_id(commitRaw), commit->repository());
  52. commit = GitCommit::fromOid(parentOid);
  53. reverseList.push_front(parentOid);
  54. if(m_points.contains(parentOid)) { //Finish parents lookup once parent found in tree. We will see nothing new in this branch
  55. break;
  56. }
  57. // qDebug() << "Add commit to reverselist" << parentOid.toString();
  58. commitRaw = nullptr;
  59. git_commit_parent(&commitRaw, commit->raw(), 0);
  60. }
  61. if(reverseList.count() < 2) { //In case if only original commit in list, we didn't find anything new
  62. return;
  63. }
  64. addCommits(reverseList);
  65. }
  66. void CommitGraph::addCommits(QList<GitOid>& reversList)
  67. {
  68. GraphPoint* point = nullptr;
  69. GraphPoint* parentPoint = nullptr;
  70. for(int i = 0; i < (reversList.count() - 1); i++) {
  71. GitOid& parentIter = reversList[i];
  72. GitOid& childIter = reversList[i + 1];
  73. parentPoint = m_points.value(parentIter, nullptr);
  74. if(parentPoint == nullptr) {
  75. parentPoint = new GraphPoint(parentIter, this);
  76. parentPoint->setColor(m_color);
  77. m_sortedPoints.prepend(parentPoint);
  78. m_points.insert(parentPoint->oid(), parentPoint);
  79. }
  80. point = m_points.value(childIter, nullptr);
  81. if(point == nullptr) {
  82. int parentPosition = m_sortedPoints.indexOf(parentPoint);
  83. int x = parentPoint->x() + parentPoint->childPointsCount();
  84. point = new GraphPoint(childIter, x, 0, m_color, this);
  85. m_points.insert(point->oid(), point);
  86. //Ordered commits
  87. if(parentPosition >= 0) {
  88. m_sortedPoints.insert(parentPosition + 1, point);
  89. }
  90. // qDebug() << "New commit: " << point->oid().toString() << point->x() << point->y();
  91. // qDebug() << "New commit parent: " << parentPoint->oid().toString() << parentPoint->x() << parentPoint->y();
  92. }
  93. if(point) { //add child point in any case
  94. parentPoint->addChildPoint(point);
  95. }
  96. }
  97. }