githandler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef GITHANDLER_H
  2. #define GITHANDLER_H
  3. #include <QObject>
  4. #include <repositorymodel.h>
  5. #include <QFutureWatcher>
  6. class CommitModel;
  7. class CommitGraph;
  8. class BranchListModel;
  9. class TagListModel;
  10. class QFileSystemWatcher;
  11. class GitConsole;
  12. using CommitModelContainer = QHash<QString, QPointer<CommitModel>>;
  13. class GitHandler : public QObject
  14. {
  15. Q_OBJECT
  16. Q_PROPERTY(RepositoryModel* repositories READ repositories NOTIFY repositoriesChanged)
  17. Q_PROPERTY(CommitGraph* graph READ graph NOTIFY graphChanged)
  18. Q_PROPERTY(GitRepository* activeRepo READ activeRepo NOTIFY activeRepoChanged)
  19. Q_PROPERTY(GitDiff* activeDiff READ activeDiff WRITE setActiveDiff NOTIFY activeDiffChanged)
  20. Q_PROPERTY(CommitModel* commits READ commits WRITE setCommits NOTIFY commitsChanged)
  21. Q_PROPERTY(BranchListModel* branchList READ branchList CONSTANT)
  22. Q_PROPERTY(TagListModel* tagList READ tagList CONSTANT)
  23. Q_PROPERTY(GitConsole* console READ console CONSTANT)
  24. Q_PROPERTY(bool isBusy READ isBusy NOTIFY isBusyChanged)
  25. Q_PROPERTY(QUrl homePath READ homePath CONSTANT)
  26. Q_ENUMS(PullStrategy)
  27. public:
  28. enum PullStrategy {
  29. Rebase,
  30. Merge
  31. };
  32. GitHandler();
  33. virtual ~GitHandler();
  34. Q_INVOKABLE void open(const QString &path, bool activate = false);
  35. Q_INVOKABLE void open(const QUrl &url, bool activate = false);
  36. Q_INVOKABLE void activateRepository(int i);
  37. Q_INVOKABLE void diff(GitCommit* a, GitCommit* b);
  38. Q_INVOKABLE void diff(); //Dif of workdir
  39. Q_INVOKABLE void diffReset();
  40. void onDiffReady();
  41. Q_INVOKABLE void copy(const QString& sha1);
  42. Q_INVOKABLE void pull(PullStrategy strategy) const;
  43. RepositoryModel* repositories() const
  44. {
  45. return m_repositories;
  46. }
  47. CommitGraph* graph() const
  48. {
  49. return m_graph;
  50. }
  51. GitRepository* activeRepo() const
  52. {
  53. return m_activeRepo;
  54. }
  55. GitDiff* activeDiff() const;
  56. CommitModel* commits() const
  57. {
  58. return m_commits;
  59. }
  60. BranchListModel* branchList() const
  61. {
  62. return m_branchList;
  63. }
  64. TagListModel* tagList() const
  65. {
  66. return m_tagList;
  67. }
  68. GitConsole* console() const
  69. {
  70. return m_console;
  71. }
  72. bool isBusy() const
  73. {
  74. return !m_graphTask.isCanceled() && !m_graphTask.isFinished();
  75. }
  76. QUrl homePath() const
  77. {
  78. return m_homePath;
  79. }
  80. public slots:
  81. void setActiveDiff(GitDiff* activeDiff);
  82. void setCommits(CommitModel* commits)
  83. {
  84. if (m_commits == commits)
  85. return;
  86. m_commits = commits;
  87. emit commitsChanged(commits);
  88. }
  89. void setActiveRepo(GitRepository* repo);
  90. signals:
  91. void activeRepoChanged(GitRepository* activeRepo);
  92. void repositoriesChanged(RepositoryModel* repositories);
  93. void graphChanged(CommitGraph* graph);
  94. void activeDiffChanged(GitDiff* activeDiff);
  95. void commitsChanged(CommitModel* commits);
  96. void isBusyChanged();
  97. protected:
  98. QString lastError() const;
  99. private:
  100. void updateModels();
  101. static CommitGraph* updateGraph(const GitOid& head, const BranchContainer &branches);
  102. void onGraphReady();
  103. void loadCachedRepos();
  104. RepositoryModel* m_repositories;
  105. CommitModel* m_commits;
  106. CommitGraph* m_graph;
  107. GitRepository* m_activeRepo;
  108. QPointer<GitDiff> m_activeDiff;
  109. BranchListModel* m_branchList;
  110. TagListModel* m_tagList;
  111. QFileSystemWatcher* m_activeRepoWatcher;
  112. GitConsole* m_console;
  113. QFutureWatcher<GitDiff*> m_diffTask;
  114. QFutureWatcher<CommitGraph*> m_graphTask;
  115. bool m_isBusy;
  116. QUrl m_homePath;
  117. // GitOid m_constantHead; //TODO:
  118. };
  119. #endif // GITHANDLER_H