2022-02-10 12:03:56 +08:00
|
|
|
#include "gitrepository.h"
|
|
|
|
#include "gitmanager.h"
|
|
|
|
|
2022-02-25 21:32:48 +08:00
|
|
|
#include <QDir>
|
|
|
|
|
2022-02-15 00:01:50 +08:00
|
|
|
GitRepository::GitRepository(const QString& folder, QObject *parent)
|
2022-02-10 12:03:56 +08:00
|
|
|
: QObject{parent},
|
2022-02-15 00:01:50 +08:00
|
|
|
mInRepository(false)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-15 00:01:50 +08:00
|
|
|
mManager = new GitManager();
|
|
|
|
setFolder(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
GitRepository::~GitRepository()
|
|
|
|
{
|
|
|
|
delete mManager;
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &GitRepository::folder() const
|
|
|
|
{
|
2022-02-15 17:22:44 +08:00
|
|
|
return mRealFolder;
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GitRepository::createRepository()
|
|
|
|
{
|
2022-02-15 17:22:44 +08:00
|
|
|
mManager->createRepository(mRealFolder);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-14 00:13:00 +08:00
|
|
|
bool GitRepository::hasRepository(QString& currentBranch)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-15 00:01:50 +08:00
|
|
|
currentBranch = mBranch;
|
|
|
|
return mInRepository;
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::add(const QString &path, QString& output)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->add(mFolder,path, output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::remove(const QString &path, QString& output)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->remove(mFolder,path, output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::rename(const QString &oldName, const QString &newName, QString& output)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->rename(mFolder, oldName, newName,output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::restore(const QString &path, QString& output)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->restore(mFolder, path, output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:01:50 +08:00
|
|
|
QSet<QString> GitRepository::listFiles(bool refresh)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-15 00:01:50 +08:00
|
|
|
if (refresh)
|
|
|
|
update();
|
|
|
|
return mFilesInRepositories;
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::clone(const QString &url, QString& output)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->clone(mFolder,url, output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::commit(const QString &message, QString& output, bool autoStage)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->commit(mRealFolder, message, autoStage, output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 11:07:30 +08:00
|
|
|
bool GitRepository::revert(QString& output)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-25 11:07:30 +08:00
|
|
|
return mManager->revert(mRealFolder, output);
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:01:50 +08:00
|
|
|
void GitRepository::setFolder(const QString &newFolder)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-15 00:01:50 +08:00
|
|
|
mFolder = newFolder;
|
2022-03-11 20:51:33 +08:00
|
|
|
if (!newFolder.isEmpty())
|
|
|
|
mRealFolder = mManager->rootFolder(mFolder);
|
|
|
|
else
|
|
|
|
mRealFolder = newFolder;
|
2022-02-15 00:01:50 +08:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GitRepository::update()
|
|
|
|
{
|
2022-03-11 20:51:33 +08:00
|
|
|
if (!mManager->isValid() || mFolder.isEmpty()) {
|
2022-02-15 00:01:50 +08:00
|
|
|
mInRepository = false;
|
|
|
|
mBranch = "";
|
|
|
|
mFilesInRepositories.clear();
|
|
|
|
mChangedFiles.clear();
|
|
|
|
mStagedFiles.clear();
|
2022-02-21 11:37:29 +08:00
|
|
|
mConflicts.clear();
|
2022-02-15 00:01:50 +08:00
|
|
|
} else {
|
2022-02-15 17:22:44 +08:00
|
|
|
mInRepository = mManager->hasRepository(mRealFolder,mBranch);
|
|
|
|
convertFilesListToSet(mManager->listFiles(mRealFolder),mFilesInRepositories);
|
|
|
|
convertFilesListToSet(mManager->listChangedFiles(mRealFolder),mChangedFiles);
|
|
|
|
convertFilesListToSet(mManager->listStagedFiles(mRealFolder),mStagedFiles);
|
2022-02-21 11:37:29 +08:00
|
|
|
convertFilesListToSet(mManager->listConflicts(mRealFolder),mConflicts);
|
2022-02-21 09:48:04 +08:00
|
|
|
// qDebug()<<"update"<<mRealFolder<<mBranch;
|
|
|
|
// qDebug()<<mFilesInRepositories;
|
|
|
|
// qDebug()<<mChangedFiles;
|
|
|
|
// qDebug()<<mStagedFiles;
|
2022-02-15 00:01:50 +08:00
|
|
|
}
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 17:22:44 +08:00
|
|
|
const QString &GitRepository::realFolder() const
|
|
|
|
{
|
|
|
|
return mRealFolder;
|
|
|
|
}
|
|
|
|
|
2022-02-15 00:01:50 +08:00
|
|
|
void GitRepository::convertFilesListToSet(const QStringList &filesList, QSet<QString> &set)
|
2022-02-10 12:03:56 +08:00
|
|
|
{
|
2022-02-15 00:01:50 +08:00
|
|
|
set.clear();
|
2022-02-25 21:32:48 +08:00
|
|
|
QDir dir(mRealFolder);
|
2022-02-15 00:01:50 +08:00
|
|
|
foreach (const QString& s, filesList) {
|
2022-02-25 21:32:48 +08:00
|
|
|
set.insert(dir.absoluteFilePath(s));
|
2022-02-15 00:01:50 +08:00
|
|
|
}
|
2022-02-10 12:03:56 +08:00
|
|
|
}
|
2022-02-14 00:13:00 +08:00
|
|
|
|