gitrepository.cpp 2.4 KB

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