gitrepository.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "gitrepository.h"
  2. #include <QDebug>
  3. #include <QFileInfo>
  4. #include <QDir>
  5. #include <git2.h>
  6. GitRepository::GitRepository(const QString& root) : QObject()
  7. ,m_repo(nullptr)
  8. {
  9. if(git_repository_open(&m_repo, root.toUtf8().data()) != 0) {
  10. qDebug() << "Cannot open repository";
  11. close();
  12. return;
  13. }
  14. m_root = root;
  15. m_path = git_repository_workdir(m_repo);
  16. m_name = m_path;//TODO: replace with Human readable name
  17. qDebug() << "New repo:" << m_name << m_root << m_path;
  18. // git_reflog* reflog;
  19. // if(git_reflog_read(&reflog, m_repo, "HEAD") != 0) {
  20. // qDebug() << "reflogs could not be read";
  21. // return;
  22. // }
  23. // quint64 count = git_reflog_entrycount(reflog);
  24. // qDebug() << count;
  25. // for(quint64 i = 0; i < count; i++)
  26. // {
  27. // git_reflog_entry* entry = git_reflog_entry_byindex(reflog, i);
  28. // git_oid* oid = git_reflog_entry_id_new(entry);
  29. // }
  30. // git_reflog_free(reflog);
  31. git_reference *out;
  32. git_branch_t branch;
  33. git_branch_iterator* iter;
  34. git_branch_iterator_new(&iter, m_repo, GIT_BRANCH_ALL);
  35. while(git_branch_next(&out, &branch, iter) == 0)
  36. {
  37. const char* branch_name;
  38. git_branch_name(&branch_name, out);
  39. qDebug() << branch_name;
  40. git_revwalk* walk;
  41. git_revwalk_new(&walk, m_repo);
  42. git_revwalk_push_head(walk);
  43. git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
  44. git_oid newoid;
  45. while(git_revwalk_next(&newoid, walk) == 0)
  46. {
  47. git_commit *wcommit;
  48. if(git_commit_lookup(&wcommit, m_repo, &newoid) != 0 )
  49. {
  50. qDebug() << "git_commit_lookup error";
  51. continue;
  52. }
  53. qDebug() << git_commit_id(wcommit);
  54. qDebug() << git_commit_time( wcommit );
  55. qDebug() << git_commit_message( wcommit );
  56. qDebug() << git_commit_author( wcommit );
  57. qDebug() << "=================================================";
  58. git_commit_free( wcommit );
  59. }
  60. git_revwalk_free( walk );
  61. git_reference_free(out);
  62. }
  63. }
  64. GitRepository::~GitRepository()
  65. {
  66. close();
  67. }
  68. void GitRepository::close()
  69. {
  70. if(m_repo) {
  71. git_repository_free(m_repo);
  72. }
  73. m_repo = nullptr;
  74. }