gitconsole.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "gitconsole.h"
  2. #include <QProcess>
  3. #include <gitrepository.h>
  4. GitConsole::GitConsole(QObject *parent) : QObject(parent)
  5. ,m_process(new QProcess(this))
  6. ,m_busy(false)
  7. ,m_recentIndex(-1)
  8. {
  9. connect(m_process, &QProcess::readyRead, this, &GitConsole::onOutputReady);
  10. connect(m_process, SIGNAL(finished(int)), this, SLOT(onFinished(int)));
  11. }
  12. GitConsole::~GitConsole()
  13. {
  14. if(m_process && m_process->state() != QProcess::NotRunning) {
  15. m_process->terminate();
  16. }
  17. }
  18. void GitConsole::exec(const QString& command)
  19. {
  20. m_recentIndex = -1;
  21. if(!command.startsWith("git ")) {
  22. emit commandError();
  23. return;
  24. }
  25. qDebug() << "Execute:" << command << "in" << m_process->workingDirectory();
  26. m_process->start(command);
  27. emit commandLog(QString("<b>$&nbsp;") + command.toHtmlEscaped() + QString("</b><br/>"), false);
  28. setBusy(true);
  29. if(m_recentContainer.count() <= 0 || m_recentContainer.first() != command) {
  30. m_recentContainer.push_front(command);
  31. }
  32. }
  33. void GitConsole::setRepository(GitRepository *repo)
  34. {
  35. m_repo = repo;
  36. m_process->setWorkingDirectory(m_repo->path());
  37. }
  38. void GitConsole::onFinished(int exitCode)
  39. {
  40. if(exitCode != 0 ) {
  41. emit commandError();
  42. }
  43. setBusy(false);
  44. }
  45. void GitConsole::onOutputReady()
  46. {
  47. QByteArray log = m_process->readAll();
  48. emit commandLog(QString::fromUtf8(log).toHtmlEscaped().replace("\n","<br/>"), true);
  49. }
  50. void GitConsole::recentUp()
  51. {
  52. if(m_recentContainer.count() <= 0) {
  53. return;
  54. }
  55. if(m_recentIndex >= (m_recentContainer.count() - 2)) {
  56. m_recentIndex = m_recentContainer.count() - 2;
  57. }
  58. emit recentChanged(m_recentContainer.at(++m_recentIndex));
  59. }
  60. void GitConsole::recentDown()
  61. {
  62. if(m_recentContainer.count() <= 0) {
  63. return;
  64. }
  65. if(m_recentIndex <= 0) {
  66. m_recentIndex = -1;
  67. emit recentChanged(QString());
  68. return;
  69. }
  70. emit recentChanged(m_recentContainer.at(--m_recentIndex));
  71. }