forked from qt-creator/qt-creator
git: Added an option to undo unstaged changes only
This commit is contained in:
@@ -584,18 +584,22 @@ bool GitClient::synchronousInit(const QString &workingDirectory)
|
|||||||
bool GitClient::synchronousCheckoutFiles(const QString &workingDirectory,
|
bool GitClient::synchronousCheckoutFiles(const QString &workingDirectory,
|
||||||
QStringList files /* = QStringList() */,
|
QStringList files /* = QStringList() */,
|
||||||
QString revision /* = QString() */,
|
QString revision /* = QString() */,
|
||||||
QString *errorMessage /* = 0 */)
|
QString *errorMessage /* = 0 */,
|
||||||
|
bool revertStaging /* = true */)
|
||||||
{
|
{
|
||||||
if (Git::Constants::debug)
|
if (Git::Constants::debug)
|
||||||
qDebug() << Q_FUNC_INFO << workingDirectory << files;
|
qDebug() << Q_FUNC_INFO << workingDirectory << files;
|
||||||
if (revision.isEmpty())
|
if (revertStaging && revision.isEmpty())
|
||||||
revision = QLatin1String("HEAD");
|
revision = QLatin1String("HEAD");
|
||||||
if (files.isEmpty())
|
if (files.isEmpty())
|
||||||
files = QStringList(QString(QLatin1Char('.')));
|
files = QStringList(QString(QLatin1Char('.')));
|
||||||
QByteArray outputText;
|
QByteArray outputText;
|
||||||
QByteArray errorText;
|
QByteArray errorText;
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
arguments << QLatin1String("checkout") << revision << QLatin1String("--") << files;
|
arguments << QLatin1String("checkout");
|
||||||
|
if (revertStaging)
|
||||||
|
arguments << revision;
|
||||||
|
arguments << QLatin1String("--") << files;
|
||||||
const bool rc = synchronousGit(workingDirectory, arguments, &outputText, &errorText);
|
const bool rc = synchronousGit(workingDirectory, arguments, &outputText, &errorText);
|
||||||
if (!rc) {
|
if (!rc) {
|
||||||
const QString fileArg = files.join(QLatin1String(", "));
|
const QString fileArg = files.join(QLatin1String(", "));
|
||||||
@@ -1392,7 +1396,10 @@ bool GitClient::addAndCommit(const QString &repositoryDirectory,
|
|||||||
* reverting a directory pending a sophisticated selection dialog in the
|
* reverting a directory pending a sophisticated selection dialog in the
|
||||||
* VCSBase plugin. */
|
* VCSBase plugin. */
|
||||||
|
|
||||||
GitClient::RevertResult GitClient::revertI(QStringList files, bool *ptrToIsDirectory, QString *errorMessage)
|
GitClient::RevertResult GitClient::revertI(QStringList files,
|
||||||
|
bool *ptrToIsDirectory,
|
||||||
|
QString *errorMessage,
|
||||||
|
bool revertStaging)
|
||||||
{
|
{
|
||||||
if (Git::Constants::debug)
|
if (Git::Constants::debug)
|
||||||
qDebug() << Q_FUNC_INFO << files;
|
qDebug() << Q_FUNC_INFO << files;
|
||||||
@@ -1454,7 +1461,7 @@ GitClient::RevertResult GitClient::revertI(QStringList files, bool *ptrToIsDirec
|
|||||||
if (Git::Constants::debug)
|
if (Git::Constants::debug)
|
||||||
qDebug() << Q_FUNC_INFO << data.stagedFiles << data.unstagedFiles << allStagedFiles << allUnstagedFiles << stagedFiles << unstagedFiles;
|
qDebug() << Q_FUNC_INFO << data.stagedFiles << data.unstagedFiles << allStagedFiles << allUnstagedFiles << stagedFiles << unstagedFiles;
|
||||||
|
|
||||||
if (stagedFiles.empty() && unstagedFiles.empty())
|
if ((!revertStaging || stagedFiles.empty()) && unstagedFiles.empty())
|
||||||
return RevertUnchanged;
|
return RevertUnchanged;
|
||||||
|
|
||||||
// Ask to revert (to do: Handle lists with a selection dialog)
|
// Ask to revert (to do: Handle lists with a selection dialog)
|
||||||
@@ -1468,19 +1475,22 @@ GitClient::RevertResult GitClient::revertI(QStringList files, bool *ptrToIsDirec
|
|||||||
return RevertCanceled;
|
return RevertCanceled;
|
||||||
|
|
||||||
// Unstage the staged files
|
// Unstage the staged files
|
||||||
if (!stagedFiles.empty() && !synchronousReset(repoDirectory, stagedFiles, errorMessage))
|
if (revertStaging && !stagedFiles.empty() && !synchronousReset(repoDirectory, stagedFiles, errorMessage))
|
||||||
return RevertFailed;
|
return RevertFailed;
|
||||||
|
QStringList filesToRevert = unstagedFiles;
|
||||||
|
if (revertStaging)
|
||||||
|
filesToRevert += stagedFiles;
|
||||||
// Finally revert!
|
// Finally revert!
|
||||||
if (!synchronousCheckoutFiles(repoDirectory, stagedFiles + unstagedFiles, QString(), errorMessage))
|
if (!synchronousCheckoutFiles(repoDirectory, filesToRevert, QString(), errorMessage, revertStaging))
|
||||||
return RevertFailed;
|
return RevertFailed;
|
||||||
return RevertOk;
|
return RevertOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GitClient::revert(const QStringList &files)
|
void GitClient::revert(const QStringList &files, bool revertStaging)
|
||||||
{
|
{
|
||||||
bool isDirectory;
|
bool isDirectory;
|
||||||
QString errorMessage;
|
QString errorMessage;
|
||||||
switch (revertI(files, &isDirectory, &errorMessage)) {
|
switch (revertI(files, &isDirectory, &errorMessage, revertStaging)) {
|
||||||
case RevertOk:
|
case RevertOk:
|
||||||
m_plugin->gitVersionControl()->emitFilesChanged(files);
|
m_plugin->gitVersionControl()->emitFilesChanged(files);
|
||||||
break;
|
break;
|
||||||
|
@@ -106,7 +106,8 @@ public:
|
|||||||
bool synchronousInit(const QString &workingDirectory);
|
bool synchronousInit(const QString &workingDirectory);
|
||||||
bool synchronousCheckoutFiles(const QString &workingDirectory,
|
bool synchronousCheckoutFiles(const QString &workingDirectory,
|
||||||
QStringList files = QStringList(),
|
QStringList files = QStringList(),
|
||||||
QString revision = QString(), QString *errorMessage = 0);
|
QString revision = QString(), QString *errorMessage = 0,
|
||||||
|
bool revertStaging = true);
|
||||||
// Checkout branch
|
// Checkout branch
|
||||||
bool synchronousCheckoutBranch(const QString &workingDirectory, const QString &branch, QString *errorMessage = 0);
|
bool synchronousCheckoutBranch(const QString &workingDirectory, const QString &branch, QString *errorMessage = 0);
|
||||||
|
|
||||||
@@ -156,7 +157,7 @@ public:
|
|||||||
void subversionLog(const QString &workingDirectory);
|
void subversionLog(const QString &workingDirectory);
|
||||||
|
|
||||||
void stashPop(const QString &workingDirectory);
|
void stashPop(const QString &workingDirectory);
|
||||||
void revert(const QStringList &files);
|
void revert(const QStringList &files, bool revertStaging);
|
||||||
void branchList(const QString &workingDirectory);
|
void branchList(const QString &workingDirectory);
|
||||||
void stashList(const QString &workingDirectory);
|
void stashList(const QString &workingDirectory);
|
||||||
bool synchronousStashList(const QString &workingDirectory,
|
bool synchronousStashList(const QString &workingDirectory,
|
||||||
@@ -241,7 +242,10 @@ private:
|
|||||||
unsigned synchronousGitVersion(bool silent, QString *errorMessage = 0);
|
unsigned synchronousGitVersion(bool silent, QString *errorMessage = 0);
|
||||||
|
|
||||||
enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
|
enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
|
||||||
RevertResult revertI(QStringList files, bool *isDirectory, QString *errorMessage);
|
RevertResult revertI(QStringList files,
|
||||||
|
bool *isDirectory,
|
||||||
|
QString *errorMessage,
|
||||||
|
bool revertStaging);
|
||||||
void connectRepositoryChanged(const QString & repository, GitCommand *cmd);
|
void connectRepositoryChanged(const QString & repository, GitCommand *cmd);
|
||||||
void pull(const QString &workingDirectory, bool rebase);
|
void pull(const QString &workingDirectory, bool rebase);
|
||||||
|
|
||||||
|
@@ -334,9 +334,15 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
|||||||
globalcontext, true, SLOT(blameFile()));
|
globalcontext, true, SLOT(blameFile()));
|
||||||
parameterActionCommand.second->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+B")));
|
parameterActionCommand.second->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+B")));
|
||||||
|
|
||||||
|
parameterActionCommand
|
||||||
|
= createFileAction(actionManager, gitContainer,
|
||||||
|
tr("Undo Unstaged Changes"), tr("Undo Unstaged Changes for \"%1\""),
|
||||||
|
QLatin1String("Git.UndoUnstaged"), globalcontext,
|
||||||
|
true, SLOT(undoUnstagedFileChanges()));
|
||||||
|
|
||||||
parameterActionCommand
|
parameterActionCommand
|
||||||
= createFileAction(actionManager, gitContainer,
|
= createFileAction(actionManager, gitContainer,
|
||||||
tr("Undo Changes"), tr("Undo Changes for \"%1\""),
|
tr("Undo Uncommitted Changes"), tr("Undo Uncommitted Changes for \"%1\""),
|
||||||
QLatin1String("Git.Undo"), globalcontext,
|
QLatin1String("Git.Undo"), globalcontext,
|
||||||
true, SLOT(undoFileChanges()));
|
true, SLOT(undoFileChanges()));
|
||||||
parameterActionCommand.second->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+U")));
|
parameterActionCommand.second->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+U")));
|
||||||
@@ -560,12 +566,17 @@ void GitPlugin::logProject()
|
|||||||
m_gitClient->log(state.currentProjectTopLevel(), state.relativeCurrentProject());
|
m_gitClient->log(state.currentProjectTopLevel(), state.relativeCurrentProject());
|
||||||
}
|
}
|
||||||
|
|
||||||
void GitPlugin::undoFileChanges()
|
void GitPlugin::undoFileChanges(bool revertStaging)
|
||||||
{
|
{
|
||||||
const VCSBase::VCSBasePluginState state = currentState();
|
const VCSBase::VCSBasePluginState state = currentState();
|
||||||
QTC_ASSERT(state.hasFile(), return)
|
QTC_ASSERT(state.hasFile(), return)
|
||||||
Core::FileChangeBlocker fcb(state.currentFile());
|
Core::FileChangeBlocker fcb(state.currentFile());
|
||||||
m_gitClient->revert(QStringList(state.currentFile()));
|
m_gitClient->revert(QStringList(state.currentFile()), revertStaging);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GitPlugin::undoUnstagedFileChanges()
|
||||||
|
{
|
||||||
|
undoFileChanges(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GitPlugin::undoRepositoryChanges()
|
void GitPlugin::undoRepositoryChanges()
|
||||||
|
@@ -108,7 +108,8 @@ private slots:
|
|||||||
void logFile();
|
void logFile();
|
||||||
void blameFile();
|
void blameFile();
|
||||||
void logProject();
|
void logProject();
|
||||||
void undoFileChanges();
|
void undoFileChanges(bool revertStaging = true);
|
||||||
|
void undoUnstagedFileChanges();
|
||||||
void undoRepositoryChanges();
|
void undoRepositoryChanges();
|
||||||
void stageFile();
|
void stageFile();
|
||||||
void unstageFile();
|
void unstageFile();
|
||||||
|
Reference in New Issue
Block a user