Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GitRepository::GitRepository(const QString &ending)
- {
- git_libgit2_init();
- }
- GitRepository::~GitRepository()
- {
- if (_repository)
- git_repository_free(_repository);
- _repository = 0;
- }
- bool GitRepository::clone(const QString &url)
- {
- freeGitStuff();
- if (!CHECK_GIT( git_clone(&_repository, CSTR(url), CSTR(path()), NULL) ))
- {
- return false;
- }
- return true;
- }
- bool GitRepository::addFile(const QString & path)
- {
- CHECK_IS_REPOSITORY();
- if (_index == NULL)
- {
- if (!CHECK_GIT( git_repository_index(&_index, _repository)))
- {
- qWarning() << "cannot retrieve index of repository.";
- return false;
- }
- }
- if (!CHECK_GIT( git_index_add_bypath(_index, CSTR(path))))
- {
- qWarning() << "add file " << makeAbsolute(path);
- return false;
- }
- return true;
- }
- bool GitRepository::commit(const QString &message, const Identity &id)
- {
- CHECK_IS_REPOSITORY();
- if (_index == NULL)
- {
- qWarning() << "nothing to commit.";
- return false;
- }
- bool success = true;
- // look up parent
- git_commit* parent = getLastCommit(_repository);
- if (success && parent == 0)
- {
- qWarning() << "cannot get commit parent.";
- success = false;
- }
- // create tree
- git_tree* tree = 0;
- git_oid tree_id;
- if (success && !CHECK_GIT( git_index_write_tree(&tree_id, _index) ))
- {
- qWarning() << "cannot write index to tree.";
- success = false;
- }
- if (success && !CHECK_GIT( git_tree_lookup(&tree, _repository, &tree_id) ))
- {
- qWarning() << "cannot create tree object from tree id.";
- success = false;
- }
- //create a tree from _index
- git_signature *me = 0;
- if ( success && !(me = id.signatureNow()) )
- {
- qWarning() << "cannot get signature.";
- git_commit_free(parent);
- git_tree_free(tree);
- success = false;
- }
- const git_commit *parents[] = {parent};
- git_oid new_commit_id;
- if (success && !CHECK_GIT( git_commit_create(
- &new_commit_id,
- _repository,
- "HEAD", /* name of ref to update */
- me, /* author */
- me, /* committer */
- "UTF-8", /* message encoding */
- CSTR(message), /* message */
- tree, /* root tree */
- 1, /* parent count */
- parents /* parents */ ) ))
- {
- qWarning() << "commit failed.";
- success = false;
- }
- if (success && !CHECK_GIT( git_index_write(_index) ))
- {
- qWarning() << "cannot write index";
- success = false;
- }
- if (success)
- {
- git_index_free(_index);
- _index = 0;
- }
- git_signature_free(me);
- git_commit_free(parent);
- git_tree_free(tree);
- return success;
- }
- bool GitRepository::load(const QString &filename)
- {
- if (!Zipped::load(filename))
- {
- return false;
- }
- freeGitStuff();
- if (QFileInfo(makeAbsolute(".git")).exists())
- {
- if (!CHECK_GIT(git_repository_open(&_repository, CSTR(path()))))
- {
- qWarning() << "cannot open git repository " << filename;
- return false;
- }
- }
- return true;
- }
- bool GitRepository::status( QStringList & newFiles,
- QStringList & removedFiles,
- QStringList & modifiedFiles,
- QStringList & renamedFiles ) const
- {
- CHECK_IS_REPOSITORY();
- newFiles.clear();
- removedFiles.clear();
- modifiedFiles.clear();
- renamedFiles.clear();
- bool success = true;
- for (const QString & filename : find())
- {
- if (filename.startsWith(".git"))
- {
- continue;
- }
- unsigned int status;
- if (!CHECK_GIT( git_status_file(&status, _repository, CSTR( filename )) ))
- {
- qWarning() << "Cannot retrieve status of " << filename;
- success = false;
- }
- if ( status & GIT_STATUS_INDEX_NEW )
- {
- newFiles << filename;
- }
- if ( status & GIT_STATUS_INDEX_DELETED )
- {
- removedFiles << filename;
- }
- if ( status & GIT_STATUS_INDEX_MODIFIED )
- {
- modifiedFiles << filename;
- }
- if ( status & GIT_STATUS_INDEX_RENAMED )
- {
- renamedFiles << filename;
- }
- }
- return success;
- }
- void GitRepository::freeGitStuff()
- {
- if (isGitRepository())
- {
- git_repository_free(_repository);
- _repository = 0;
- }
- }
- git_remote* GitRepository::remote() const
- {
- git_remote* remote = 0;
- if (!CHECK_GIT( git_remote_lookup(&remote, _repository, "origin") ))
- {
- qWarning() << "Cannot get remote from repository.";
- return NULL;
- }
- return remote;
- }
- QString GitRepository::url() const
- {
- if (!isGitRepository())
- {
- return QString();
- }
- git_remote* rem = remote();
- if (rem == 0)
- {
- return QString();
- }
- QString rurl(git_remote_url(rem));
- git_remote_free(rem);
- return rurl;
- }
- bool GitRepository::push()
- {
- /* ??? */
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement