From 149148a635129c4d54027897c0e122d63ade621b Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Wed, 9 Feb 2022 14:58:39 +0800 Subject: [PATCH] work save - basic git operations --- RedPandaIDE/vcs/gitmanager.cpp | 143 +++++++++++++++++++++++++++++++++ RedPandaIDE/vcs/gitmanager.h | 31 ++++++- 2 files changed, 173 insertions(+), 1 deletion(-) diff --git a/RedPandaIDE/vcs/gitmanager.cpp b/RedPandaIDE/vcs/gitmanager.cpp index 801a6513..69b585ce 100644 --- a/RedPandaIDE/vcs/gitmanager.cpp +++ b/RedPandaIDE/vcs/gitmanager.cpp @@ -1,4 +1,7 @@ #include "gitmanager.h" +#include "../utils.h" + +#include GitManager::GitManager(QObject *parent) : QObject(parent), mGitPathValid(false) @@ -9,3 +12,143 @@ bool GitManager::gitPathValid() const { return mGitPathValid; } + +void GitManager::createRepository(const QString &folder) +{ + if (hasRepository(folder)) + throw GitError(tr("Folder \"%1\" already has a repository!")); + QStringList args; + args.append("init"); + runGit(folder,args); +} + +bool GitManager::hasRepository(const QString &folder) +{ + + QStringList args; + args.append("status"); + QString output = runGit(folder,args); + return !output.startsWith("fatal:"); +} + +void GitManager::add(const QString &folder, const QString &path) +{ + QStringList args; + args.append("add"); + args.append(path); + runGit(folder,args); +} + +void GitManager::remove(const QString &folder, const QString &path) +{ + QStringList args; + args.append("rm"); + args.append(path); + runGit(folder,args); +} + +void GitManager::rename(const QString &folder, const QString &oldName, const QString &newName) +{ + QStringList args; + args.append("mv"); + args.append(oldName); + args.append(newName); + runGit(folder,args); +} + +void GitManager::restore(const QString &folder, const QString &path) +{ + QStringList args; + args.append("restore"); + args.append(path); + runGit(folder,args); +} + +QStringList GitManager::listFiles(const QString &folder) +{ + QStringList args; + args.append("ls-files"); + return textToLines(runGit(folder,args)); +} + +void GitManager::clone(const QString &folder, const QString &url) +{ + QStringList args; + args.append("clone"); + args.append(url); + runGit(folder,args); +} + +void GitManager::commit(const QString &folder, const QString &message) +{ + QStringList args; + args.append("commit"); + args.append("-m"); + args.append(message); + runGit(folder,args); +} + +void GitManager::revert(const QString &folder) +{ + QStringList args; + args.append("revert"); + runGit(folder,args); +} + +void GitManager::reset(const QString &folder, const QString &commit, ResetStrategy strategy) +{ + //todo reset type + QStringList args; + args.append("reset"); + switch(strategy) { + case ResetStrategy::Soft: + args.append("--soft"); + break; + case ResetStrategy::Hard: + args.append("--hard"); + break; + case ResetStrategy::Mixed: + args.append("--mixed"); + break; + case ResetStrategy::Merge: + args.append("--merge"); + break; + case ResetStrategy::Keep: + args.append("--keep"); + break; + } + args.append(commit); + runGit(folder,args); +} + +void GitManager::validate() +{ + QStringList args; + args.append("--version"); + QString output = runGit("",args); + mGitPathValid = output.startsWith("git version"); +} + +QString GitManager::runGit(const QString& workingFolder, const QStringList &args) +{ + QFileInfo fileInfo(mGitPath); + if (!fileInfo.exists()) + throw GitError("fatal: git doesn't exist"); + emit gitCmdRunning(QString("Running in \"%1\": \n \"%2\" \"%3\"") + .arg(workingFolder, + mGitPath, + args.join("\" \""))); + QString output = runAndGetOutput( + fileInfo.absoluteFilePath(), + workingFolder, + args); + emit gitCmdFinished(output); + if (output.startsWith("fatal:")) + throw GitError(output); + return output; +} + +GitError::GitError(const QString &reason):BaseError(reason) +{ + +} diff --git a/RedPandaIDE/vcs/gitmanager.h b/RedPandaIDE/vcs/gitmanager.h index b6dba53e..fc66b06f 100644 --- a/RedPandaIDE/vcs/gitmanager.h +++ b/RedPandaIDE/vcs/gitmanager.h @@ -2,21 +2,50 @@ #define GITMANAGER_H #include +#include "utils.h" + +class GitError: public BaseError { +public: + explicit GitError(const QString& reason); +}; + + class GitManager : public QObject { Q_OBJECT public: + enum class ResetStrategy { + Soft, + Hard, + Merge, + Mixed, + Keep + }; + explicit GitManager(QObject *parent = nullptr); bool gitPathValid() const; - bool createRepository(const QString& folder); + void createRepository(const QString& folder); bool hasRepository(const QString& folder); + void add(const QString& folder, const QString& path); + void remove(const QString& folder, const QString& path); + void rename(const QString& folder, const QString& oldName, const QString& newName); + void restore(const QString& folder, const QString& path); + QStringList listFiles(const QString& folder); + + void clone(const QString& folder, const QString& url); + void commit(const QString& folder, const QString& message); + void revert(const QString& folder); + void reset(const QString& folder, const QString& commit, ResetStrategy strategy); signals: + void gitCmdRunning(const QString& gitCmd); + void gitCmdFinished(const QString& message); private: void validate(); + QString runGit(const QString& workingFolder, const QStringList& args); private: QString mGitPath; bool mGitPathValid;