gitcommit.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "gitcommit.h"
  2. #include <gitoid.h>
  3. #include <gitdiff.h>
  4. #include <QDebug>
  5. #include <git2/commit.h>
  6. #include <git2/tag.h>
  7. #include <git2/diff.h>
  8. #include <git2/patch.h>
  9. #include <git2/buffer.h>
  10. GitCommit::GitCommit(git_commit* raw, GitRepository* parent) : GitBaseOid(raw, parent)
  11. ,m_diff(nullptr)
  12. {
  13. m_oid = GitOid(git_commit_id(m_raw), m_repository);
  14. }
  15. GitCommit::GitCommit() : GitBaseOid(nullptr, nullptr)
  16. {
  17. }
  18. GitCommit* GitCommit::fromOid(const GitOid& oid)
  19. {
  20. if(!oid.isValid()) {
  21. return nullptr;
  22. }
  23. git_commit *commit;
  24. if(git_commit_lookup(&commit, oid.repository()->raw(), oid.raw()) != 0) {
  25. return nullptr;
  26. }
  27. return new GitCommit(commit, oid.repository());
  28. }
  29. GitCommit::~GitCommit()
  30. {
  31. git_commit_free(m_raw);
  32. }
  33. QString GitCommit::author() const
  34. {
  35. return QString(git_commit_author(m_raw)->name);
  36. }
  37. QDateTime GitCommit::time() const
  38. {
  39. return QDateTime::fromTime_t(git_commit_time(m_raw), Qt::OffsetFromUTC, git_commit_time_offset(m_raw));
  40. }
  41. QString GitCommit::message() const
  42. {
  43. return QString(git_commit_message(m_raw));
  44. }
  45. QString GitCommit::email() const
  46. {
  47. return QString(git_commit_author(m_raw)->email);
  48. }
  49. QString GitCommit::sha1() const
  50. {
  51. return oid().toString();
  52. }
  53. QString GitCommit::shortSha1() const
  54. {
  55. return oid().toShorten();
  56. }
  57. bool GitCommit::isMerge() const
  58. {
  59. return git_commit_parentcount(m_raw) > 1;
  60. }
  61. QString GitCommit::summary() const
  62. {
  63. return QString(git_commit_summary(m_raw));
  64. }
  65. GitDiff* GitCommit::diff()
  66. {
  67. if(m_diff.isNull()) {
  68. git_commit *parentRaw = nullptr;
  69. QScopedPointer<GitCommit> parent;
  70. git_commit_parent(&parentRaw, raw(), 0);
  71. if(parentRaw) {
  72. parent.reset(new GitCommit(parentRaw, repository()));
  73. }
  74. m_diff = GitDiff::diff(parent.data(), this);
  75. }
  76. return m_diff.data();
  77. }
  78. void GitCommit::setAuthor(QString author)
  79. {
  80. Q_UNUSED(author)
  81. //TODO
  82. }
  83. void GitCommit::setTime(QDateTime time)
  84. {
  85. Q_UNUSED(time)
  86. //TODO
  87. }
  88. void GitCommit::setMessage(QString message)
  89. {
  90. Q_UNUSED(message)
  91. //TODO
  92. }
  93. void GitCommit::setEmail(QString email)
  94. {
  95. Q_UNUSED(email)
  96. //TODO
  97. }
  98. void GitCommit::setSummary(QString summary)
  99. {
  100. Q_UNUSED(summary)
  101. //TODO
  102. }