commitmodel.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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->add(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. model->m_container.prepend(GitCommit::fromOid(point->oid()));
  38. // QPointer<GitTag> tag = commit->repository()->tags().value(commit->oid());
  39. // if(!tag.isNull()) {
  40. // point->setTag(tag.data()->name());
  41. // }
  42. }
  43. return model;
  44. }