work save: git reset

This commit is contained in:
royqh1979@gmail.com 2022-02-22 17:12:54 +08:00
parent 18c9f62787
commit ed7f3d971e
10 changed files with 80 additions and 15 deletions

View File

@ -6896,6 +6896,7 @@ void MainWindow::on_actionGit_Log_triggered()
return;
GitLogDialog dialog(folder);
if (dialog.exec()==QDialog::Accepted) {
qDebug()<<"yes";
//update project view
if (mProject) {
mProject->model()->beginUpdate();

View File

@ -1,6 +1,7 @@
#include "gitlogdialog.h"
#include "ui_gitlogdialog.h"
#include "gitmanager.h"
#include "gitresetdialog.h"
#include <QMenu>
@ -77,6 +78,20 @@ QVariant GitLogModel::headerData(int section, Qt::Orientation orientation, int r
return QVariant();
}
PGitCommitInfo GitLogModel::commitInfo(const QModelIndex &index)
{
if (!index.isValid())
return PGitCommitInfo();
int row = index.row();
GitManager manager;
QList<PGitCommitInfo> listCommitInfos =
manager.log(mFolder,row,1);
if (listCommitInfos.isEmpty()) {
return PGitCommitInfo();
}
return listCommitInfos[0];
}
const QString &GitLogModel::folder() const
{
return mFolder;
@ -94,10 +109,10 @@ void GitLogDialog::onLogsContextMenu(const QPoint &pos)
GitManager manager;
if (!manager.hasRepository(mModel.folder(), branch))
return;
menu.addAction(ui->actionRevert);
// menu.addAction(ui->actionRevert);
menu.addAction(ui->actionReset);
menu.addAction(ui->actionBranch);
menu.addAction(ui->actionTag);
// menu.addAction(ui->actionBranch);
// menu.addAction(ui->actionTag);
ui->actionReset->setText(tr("Reset \"%1\" to this...").arg(branch));
ui->actionRevert->setText(tr("Revert \"%1\" to this...").arg(branch));
ui->actionBranch->setText(tr("Create Branch at this version..."));
@ -105,3 +120,15 @@ void GitLogDialog::onLogsContextMenu(const QPoint &pos)
menu.exec(ui->tblLogs->mapToGlobal(pos));
}
void GitLogDialog::on_actionReset_triggered()
{
QModelIndex index = ui->tblLogs->currentIndex();
if (!index.isValid())
return;
PGitCommitInfo commitInfo = mModel.commitInfo(index);
GitResetDialog resetDialog(mModel.folder());
if (resetDialog.resetToCommit(commitInfo->commitHash)==QDialog::Accepted)
accept();
}

View File

@ -20,12 +20,12 @@ public:
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
PGitCommitInfo commitInfo(const QModelIndex &index);
const QString &folder() const;
private:
QString mFolder;
int mCount;
QMap<int,PGitCommitInfo> mGitCommitInfos;
};
class GitLogDialog : public QDialog
@ -40,6 +40,8 @@ private slots:
void on_btnClose_clicked();
void onLogsContextMenu(const QPoint &pos);
void on_actionReset_triggered();
private:
Ui::GitLogDialog *ui;
GitLogModel mModel;

View File

@ -148,7 +148,7 @@ QList<PGitCommitInfo> GitManager::log(const QString &folder, int start, int coun
QStringList args;
args.append("log");
args.append("--skip");
args.append(QString("%1").arg(start-1));
args.append(QString("%1").arg(start));
args.append("-n");
args.append(QString("%1").arg(count));
args.append("--format=medium");
@ -332,7 +332,9 @@ void GitManager::revert(const QString &folder)
runGit(folder,args);
}
void GitManager::reset(const QString &folder, const QString &commit, GitResetStrategy strategy)
bool GitManager::reset(const QString &folder, const QString &commit,
GitResetStrategy strategy,
QString& ouput)
{
//todo reset type
QStringList args;
@ -355,7 +357,8 @@ void GitManager::reset(const QString &folder, const QString &commit, GitResetStr
break;
}
args.append(commit);
runGit(folder,args);
QString output = runGit(folder,args);
return !output.startsWith("error") && !output.startsWith("fatal");
}
bool GitManager::isValid()

View File

@ -56,7 +56,7 @@ public:
void clone(const QString& folder, const QString& url);
void commit(const QString& folder, const QString& message, bool autoStage);
void revert(const QString& folder);
void reset(const QString& folder, const QString& commit, GitResetStrategy strategy);
bool reset(const QString& folder, const QString& commit, GitResetStrategy strategy, QString& output);
bool isValid();

View File

@ -72,11 +72,6 @@ void GitRepository::revert()
mManager->revert(mRealFolder);
}
void GitRepository::reset(const QString &commit, GitResetStrategy strategy)
{
mManager->reset(mRealFolder,commit,strategy);
}
void GitRepository::setFolder(const QString &newFolder)
{
mFolder = newFolder;

View File

@ -63,7 +63,6 @@ public:
void clone(const QString& url);
void commit(const QString& message, bool autoStage=true);
void revert();
void reset(const QString& commit, GitResetStrategy strategy);
void setFolder(const QString &newFolder);

View File

@ -1,5 +1,7 @@
#include "gitresetdialog.h"
#include "ui_gitresetdialog.h"
#include "gitmanager.h"
#include "../widgets/infomessagebox.h"
GitResetDialog::GitResetDialog(const QString& folder, QWidget *parent) :
QDialog(parent),
@ -16,6 +18,10 @@ GitResetDialog::~GitResetDialog()
int GitResetDialog::resetToCommit(const QString &commit)
{
GitManager manager;
QString branch;
if (!manager.hasRepository(mFolder,branch))
return QDialog::Rejected;
ui->rbBranch->setEnabled(false);
ui->cbBranches->setEnabled(false);
ui->rbTag->setEnabled(false);
@ -23,12 +29,37 @@ int GitResetDialog::resetToCommit(const QString &commit)
ui->rbCommit->setChecked(true);
ui->cbCommits->addItem(commit);
ui->rbMixed->setChecked(true);
ui->grpTarget->setTitle(tr("Reset current branch \"%1\" to").arg(branch));
return exec();
}
void GitResetDialog::on_btnOk_clicked()
{
accept();
GitManager manager;
GitResetStrategy strategy = GitResetStrategy::Mixed;
if (ui->rbSoft->isChecked())
strategy = GitResetStrategy::Soft;
else if (ui->rbHard->isChecked())
strategy = GitResetStrategy::Hard;
QString commit;
if (ui->rbCommit->isChecked())
commit = ui->cbCommits->currentText();
else if (ui->rbTag->isChecked())
commit = ui->cbTags->currentText();
else if (ui->rbBranch->isChecked())
commit = ui->cbBranches->currentText();
if (commit.isEmpty())
reject();
QString output;
bool result = manager.reset(mFolder,commit,strategy,output);
if (!output.trimmed().isEmpty()) {
InfoMessageBox infoBox;
infoBox.showMessage(output);
}
if (result)
accept();
else
reject();
}

View File

@ -18,6 +18,12 @@ InfoMessageBox::~InfoMessageBox()
delete ui;
}
void InfoMessageBox::showMessage(const QString message)
{
setMessage(message);
exec();
}
void InfoMessageBox::on_btnOk_clicked()
{
accept();

View File

@ -15,6 +15,7 @@ public:
explicit InfoMessageBox(QWidget *parent = nullptr);
void setMessage(const QString message);
~InfoMessageBox();
void showMessage(const QString message);
private slots:
void on_btnOk_clicked();