commitmodel.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "commitmodel.h"
  2. #include <gitbranch.h>
  3. #include <git2/revwalk.h>
  4. #include <commitgraph.h>
  5. #include <graphpoint.h>
  6. #include <graphlistmodel.h>
  7. CommitModel::CommitModel(const QString &head, QObject* parent) : UniversalListModel(parent)
  8. ,m_head(head)
  9. {
  10. }
  11. CommitModel* CommitModel::fromBranch(GitBranch* branch)
  12. {
  13. CommitModel* tmpModel = new CommitModel(branch->fullName(), branch);
  14. git_revwalk* walk;
  15. git_revwalk_new(&walk, branch->repository()->raw());
  16. git_revwalk_push(walk, branch->oid().raw());
  17. git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
  18. git_oid newOid;
  19. while(git_revwalk_next(&newOid, walk) == 0) {
  20. GitOid commitOid(&newOid, branch->repository());
  21. GitCommit *commit = GitCommit::fromOid(commitOid);
  22. if(commit != nullptr) {
  23. tmpModel->append(commit);
  24. } else {
  25. qDebug() << "Commit is null";
  26. }
  27. }
  28. git_revwalk_free(walk);
  29. return tmpModel;
  30. }
  31. CommitModel* CommitModel::fromGraph(CommitGraph *graph)
  32. {
  33. CommitModel* model = new CommitModel("HEAD");
  34. QList<QPointer<GraphPoint> > points = graph->points()->container();
  35. for(int i = 0; i < points.count(); i++) {
  36. GraphPoint* point = points.at(i).data();
  37. if(!point->oid().isValid()) {
  38. qDebug() << "prepend empty";
  39. model->m_container.prepend(nullptr);
  40. } else {
  41. model->m_container.prepend(GitCommit::fromOid(point->oid()));
  42. }
  43. // QPointer<GitTag> tag = commit->repository()->tags().value(commit->oid());
  44. // if(!tag.isNull()) {
  45. // point->setTag(tag.data()->name());
  46. // }
  47. }
  48. return model;
  49. }