Advertisement
Guest User

Untitled

a guest
Jan 20th, 2015
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. GitRepository::GitRepository(const QString &ending)
  2. {
  3.     git_libgit2_init();
  4. }
  5.  
  6. GitRepository::~GitRepository()
  7. {
  8.     if (_repository)
  9.         git_repository_free(_repository);
  10.  
  11.     _repository = 0;
  12. }
  13.  
  14. bool GitRepository::clone(const QString &url)
  15. {
  16.     freeGitStuff();
  17.  
  18.     if (!CHECK_GIT( git_clone(&_repository, CSTR(url), CSTR(path()), NULL) ))
  19.     {
  20.         return false;
  21.     }
  22.  
  23.     return true;
  24. }
  25.  
  26.  
  27. bool GitRepository::addFile(const QString & path)
  28. {
  29.     CHECK_IS_REPOSITORY();
  30.  
  31.     if (_index == NULL)
  32.     {
  33.         if (!CHECK_GIT( git_repository_index(&_index, _repository)))
  34.         {
  35.             qWarning() << "cannot retrieve index of repository.";
  36.             return false;
  37.         }
  38.     }
  39.  
  40.     if (!CHECK_GIT( git_index_add_bypath(_index, CSTR(path))))
  41.     {
  42.         qWarning() << "add file " << makeAbsolute(path);
  43.         return false;
  44.     }
  45.  
  46.     return true;
  47. }
  48.  
  49. bool GitRepository::commit(const QString &message, const Identity &id)
  50. {
  51.     CHECK_IS_REPOSITORY();
  52.  
  53.     if (_index == NULL)
  54.     {
  55.         qWarning() << "nothing to commit.";
  56.         return false;
  57.     }
  58.  
  59.     bool success = true;
  60.  
  61.     // look up parent
  62.     git_commit* parent = getLastCommit(_repository);
  63.     if (success && parent == 0)
  64.     {
  65.         qWarning() << "cannot get commit parent.";
  66.         success = false;
  67.     }
  68.  
  69.     // create tree
  70.     git_tree* tree = 0;
  71.     git_oid tree_id;
  72.     if (success && !CHECK_GIT( git_index_write_tree(&tree_id, _index) ))
  73.     {
  74.         qWarning() << "cannot write index to tree.";
  75.         success = false;
  76.     }
  77.     if (success && !CHECK_GIT( git_tree_lookup(&tree, _repository, &tree_id) ))
  78.     {
  79.         qWarning() << "cannot create tree object from tree id.";
  80.         success = false;
  81.     }
  82.  
  83.  
  84.     //create a tree from _index
  85.  
  86.     git_signature *me = 0;
  87.     if ( success && !(me = id.signatureNow()) )
  88.     {
  89.         qWarning() << "cannot get signature.";
  90.         git_commit_free(parent);
  91.         git_tree_free(tree);
  92.         success = false;
  93.     }
  94.  
  95.     const git_commit *parents[] = {parent};
  96.  
  97.     git_oid new_commit_id;
  98.     if (success && !CHECK_GIT( git_commit_create(
  99.       &new_commit_id,
  100.       _repository,
  101.       "HEAD",                      /* name of ref to update */
  102.       me,                          /* author */
  103.       me,                          /* committer */
  104.       "UTF-8",                     /* message encoding */
  105.       CSTR(message),               /* message */
  106.       tree,                        /* root tree */
  107.       1,                           /* parent count */
  108.       parents                      /* parents */  ) ))
  109.     {
  110.         qWarning() << "commit failed.";
  111.         success = false;
  112.     }
  113.  
  114.     if (success && !CHECK_GIT( git_index_write(_index) ))
  115.     {
  116.         qWarning() << "cannot write index";
  117.         success = false;
  118.     }
  119.  
  120.     if (success)
  121.     {
  122.         git_index_free(_index);
  123.         _index = 0;
  124.     }
  125.  
  126.     git_signature_free(me);
  127.     git_commit_free(parent);
  128.     git_tree_free(tree);
  129.  
  130.     return success;
  131. }
  132.  
  133. bool GitRepository::load(const QString &filename)
  134. {
  135.  
  136.     if (!Zipped::load(filename))
  137.     {
  138.         return false;
  139.     }
  140.  
  141.     freeGitStuff();
  142.  
  143.     if (QFileInfo(makeAbsolute(".git")).exists())
  144.     {
  145.         if (!CHECK_GIT(git_repository_open(&_repository, CSTR(path()))))
  146.         {
  147.             qWarning() << "cannot open git repository " << filename;
  148.             return false;
  149.         }
  150.     }
  151.  
  152.     return true;
  153. }
  154.  
  155. bool GitRepository::status( QStringList & newFiles,
  156.                             QStringList & removedFiles,
  157.                             QStringList & modifiedFiles,
  158.                             QStringList & renamedFiles  ) const
  159. {
  160.     CHECK_IS_REPOSITORY();
  161.  
  162.     newFiles.clear();
  163.     removedFiles.clear();
  164.     modifiedFiles.clear();
  165.     renamedFiles.clear();
  166.  
  167.     bool success = true;
  168.  
  169.     for (const QString & filename : find())
  170.     {
  171.         if (filename.startsWith(".git"))
  172.         {
  173.             continue;
  174.         }
  175.  
  176.         unsigned int status;
  177.  
  178.         if (!CHECK_GIT( git_status_file(&status, _repository, CSTR( filename )) ))
  179.         {
  180.             qWarning() << "Cannot retrieve status of " << filename;
  181.             success = false;
  182.         }
  183.  
  184.         if ( status & GIT_STATUS_INDEX_NEW )
  185.         {
  186.             newFiles << filename;
  187.         }
  188.  
  189.         if ( status & GIT_STATUS_INDEX_DELETED )
  190.         {
  191.             removedFiles << filename;
  192.         }
  193.  
  194.         if ( status & GIT_STATUS_INDEX_MODIFIED )
  195.         {
  196.             modifiedFiles << filename;
  197.         }
  198.  
  199.         if ( status & GIT_STATUS_INDEX_RENAMED )
  200.         {
  201.             renamedFiles << filename;
  202.         }
  203.     }
  204.  
  205.     return success;
  206. }
  207.  
  208. void GitRepository::freeGitStuff()
  209. {
  210.     if (isGitRepository())
  211.     {
  212.         git_repository_free(_repository);
  213.         _repository = 0;
  214.     }
  215. }
  216.  
  217.  
  218. git_remote* GitRepository::remote() const
  219. {
  220.     git_remote* remote = 0;
  221.     if (!CHECK_GIT( git_remote_lookup(&remote, _repository, "origin") ))
  222.     {
  223.         qWarning() << "Cannot get remote from repository.";
  224.         return NULL;
  225.     }
  226.  
  227.     return remote;
  228. }
  229.  
  230. QString GitRepository::url() const
  231. {
  232.     if (!isGitRepository())
  233.     {
  234.         return QString();
  235.     }
  236.  
  237.     git_remote* rem = remote();
  238.     if (rem == 0)
  239.     {
  240.         return QString();
  241.     }
  242.  
  243.     QString rurl(git_remote_url(rem));
  244.     git_remote_free(rem);
  245.  
  246.     return rurl;
  247. }
  248.  
  249. bool GitRepository::push()
  250. {
  251.     /* ??? */
  252.     return false;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement