Commit c149046b authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Fixes: Add revert to git, name stage options according to git gui

parent f93d2d80
......@@ -80,16 +80,16 @@ void CommitData::clear()
panelInfo.clear();
panelData.clear();
commitFiles.clear();
notUpdatedFiles.clear();
stagedFiles.clear();
unstagedFiles.clear();
untrackedFiles.clear();
}
QDebug operator<<(QDebug d, const CommitData &data)
{
d << data.panelInfo << data.panelData;
d.nospace() << "Commit: " << data.commitFiles << " Not updated: "
<< data.notUpdatedFiles << " Untracked: " << data.untrackedFiles;
d.nospace() << "Commit: " << data.stagedFiles << " Not updated: "
<< data.unstagedFiles << " Untracked: " << data.untrackedFiles;
return d;
}
......
......@@ -71,8 +71,8 @@ struct CommitData
void clear();
GitSubmitEditorPanelInfo panelInfo;
GitSubmitEditorPanelData panelData;
QStringList commitFiles;
QStringList notUpdatedFiles;
QStringList stagedFiles;
QStringList unstagedFiles;
QStringList untrackedFiles;
};
......
......@@ -35,6 +35,7 @@
#include "gitplugin.h"
#include "gitconstants.h"
#include "commitdata.h"
#include "gitsubmiteditor.h"
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
......@@ -51,7 +52,9 @@
#include <QtCore/QFuture>
#include <QtCore/QTime>
#include <QtGui/QErrorMessage>
#include <QtGui/QMessageBox>
#include <QtGui/QMainWindow> // for msg box parent
#include <QtGui/QPushButton>
using namespace Git;
using namespace Git::Internal;
......@@ -77,12 +80,24 @@ inline Core::IEditor* locateEditor(const Core::ICore *core, const char *property
return 0;
}
static inline QString msgRepositoryNotFound(const QString &dir)
{
return GitClient::tr("Unable to determine the repository for %1.").arg(dir);
}
static inline QString msgParseFilesFailed()
{
return GitClient::tr("Unable to parse the file output.");
}
// Format a command for the status window
static QString formatCommand(const QString &binary, const QStringList &args)
{
const QString timeStamp = QTime::currentTime().toString(QLatin1String("HH:mm"));
return GitClient::tr("%1 Executing: %2 %3\n").arg(timeStamp, binary, args.join(QString(QLatin1Char(' '))));
}
// ---------------- GitClient
GitClient::GitClient(GitPlugin* plugin, Core::ICore *core) :
m_msgWait(tr("Waiting for data...")),
m_plugin(plugin),
......@@ -311,6 +326,19 @@ bool GitClient::synchronousAdd(const QString &workingDirectory, const QStringLis
bool GitClient::synchronousReset(const QString &workingDirectory,
const QStringList &files)
{
QString errorMessage;
const bool rc = synchronousReset(workingDirectory, files, &errorMessage);
if (!rc) {
m_plugin->outputWindow()->append(errorMessage);
m_plugin->outputWindow()->popup(false);
}
return rc;
}
bool GitClient::synchronousReset(const QString &workingDirectory,
const QStringList &files,
QString *errorMessage)
{
if (Git::Constants::debug)
qDebug() << Q_FUNC_INFO << workingDirectory << files;
......@@ -325,9 +353,25 @@ bool GitClient::synchronousReset(const QString &workingDirectory,
// Note that git exits with 1 even if the operation is successful
// Assume real failure if the output does not contain "foo.cpp modified"
if (!rc && !output.contains(QLatin1String("modified"))) {
const QString errorMessage = tr("Unable to reset %n file(s) in %1: %2", 0, files.size()).
arg(workingDirectory, QString::fromLocal8Bit(errorText));
m_plugin->outputWindow()->append(errorMessage);
*errorMessage = tr("Unable to reset %n file(s) in %1: %2", 0, files.size()).arg(workingDirectory, QString::fromLocal8Bit(errorText));
return false;
}
return true;
}
bool GitClient::synchronousCheckout(const QString &workingDirectory,
const QStringList &files,
QString *errorMessage)
{
if (Git::Constants::debug)
qDebug() << Q_FUNC_INFO << workingDirectory << files;
QByteArray outputText;
QByteArray errorText;
QStringList arguments;
arguments << QLatin1String("checkout") << QLatin1String("--") << files;
const bool rc = synchronousGit(workingDirectory, arguments, &outputText, &errorText);
if (!rc) {
*errorMessage = tr("Unable to checkout %n file(s) in %1: %2", 0, files.size()).arg(workingDirectory, QString::fromLocal8Bit(errorText));
return false;
}
return true;
......@@ -365,16 +409,18 @@ void GitClient::executeGit(const QString &workingDirectory, const QStringList &a
command->execute(arguments, workingDirectory, environment);
}
bool GitClient::synchronousGit(const QString &workingDirectory
, const QStringList &arguments
, QByteArray* outputText
, QByteArray* errorText)
bool GitClient::synchronousGit(const QString &workingDirectory,
const QStringList &arguments,
QByteArray* outputText,
QByteArray* errorText,
bool logCommandToWindow)
{
if (Git::Constants::debug)
qDebug() << "synchronousGit" << workingDirectory << arguments;
const QString binary = QLatin1String(kGitCommand);
m_plugin->outputWindow()->append(formatCommand(binary, arguments));
if (logCommandToWindow)
m_plugin->outputWindow()->append(formatCommand(binary, arguments));
QProcess process;
process.setWorkingDirectory(workingDirectory);
......@@ -456,10 +502,11 @@ GitClient::StatusResult GitClient::gitStatus(const QString &workingDirectory,
#<tab>modified:<blanks>git.pro
\endcode
*/
static bool parseFiles(const QStringList &lines, CommitData *d)
static bool parseFiles(const QString &output, CommitData *d)
{
enum State { None, CommitFiles, NotUpdatedFiles, UntrackedFiles };
const QStringList lines = output.split(QLatin1Char('\n'));
const QString branchIndicator = QLatin1String(kBranchIndicatorC);
const QString commitIndicator = QLatin1String("# Changes to be committed:");
const QString notUpdatedIndicator = QLatin1String("# Changed but not updated:");
......@@ -492,10 +539,10 @@ static bool parseFiles(const QStringList &lines, CommitData *d)
const QString fileSpec = line.mid(2).trimmed();
switch (s) {
case CommitFiles:
d->commitFiles.push_back(trimFileSpecification(fileSpec));
d->stagedFiles.push_back(trimFileSpecification(fileSpec));
break;
case NotUpdatedFiles:
d->notUpdatedFiles.push_back(trimFileSpecification(fileSpec));
d->unstagedFiles.push_back(trimFileSpecification(fileSpec));
break;
case UntrackedFiles:
d->untrackedFiles.push_back(QLatin1String("untracked: ") + fileSpec);
......@@ -509,7 +556,7 @@ static bool parseFiles(const QStringList &lines, CommitData *d)
}
}
}
return !d->commitFiles.empty() || !d->notUpdatedFiles.empty() || !d->untrackedFiles.empty();
return !d->stagedFiles.empty() || !d->unstagedFiles.empty() || !d->untrackedFiles.empty();
}
bool GitClient::getCommitData(const QString &workingDirectory,
......@@ -525,7 +572,7 @@ bool GitClient::getCommitData(const QString &workingDirectory,
// Find repo
const QString repoDirectory = GitClient::findRepositoryForDirectory(workingDirectory);
if (repoDirectory.isEmpty()) {
*errorMessage = tr("Unable to determine the repository for %1.").arg(workingDirectory);
*errorMessage = msgRepositoryNotFound(workingDirectory);
return false;
}
......@@ -551,7 +598,7 @@ bool GitClient::getCommitData(const QString &workingDirectory,
case StatusChanged:
break;
case StatusUnchanged:
*errorMessage = tr("There are no modified files.");
*errorMessage = msgNoChangedFiles();
return false;
case StatusFailed:
return false;
......@@ -575,9 +622,8 @@ bool GitClient::getCommitData(const QString &workingDirectory,
// #
// # list of files...
const QStringList lines = output.split(QLatin1Char('\n'));
if (!parseFiles(lines, d)) {
*errorMessage = tr("Unable to parse the file output.");
if (!parseFiles(output, d)) {
*errorMessage = msgParseFilesFailed();
return false;
}
......@@ -638,6 +684,129 @@ bool GitClient::addAndCommit(const QString &repositoryDirectory,
return rc;
}
static inline bool askWithInformativeText(QWidget *parent,
const QString &title,
const QString &msg,
const QString &inf,
bool defaultValue)
{
QMessageBox msgBox(QMessageBox::Question, title, msg, QMessageBox::Yes|QMessageBox::No, parent);
msgBox.setInformativeText(inf);
msgBox.setDefaultButton(defaultValue ? QMessageBox::Yes : QMessageBox::No);
return msgBox.exec() == QMessageBox::Yes;
}
/* Revert: This function can be called with a file list (to revert single
* files) or a single directory (revert all). Qt Creator currently has only
* 'revert single' in its VCS menus, but the code is prepared to deal with
* reverting a directory pending a sophisticated selection dialog in the
* VCSBase plugin. */
GitClient::RevertResult GitClient::revertI(QStringList files, bool *ptrToIsDirectory, QString *errorMessage)
{
if (Git::Constants::debug)
qDebug() << Q_FUNC_INFO << files;
if (files.empty())
return RevertCanceled;
// Figure out the working directory
const QFileInfo firstFile(files.front());
const bool isDirectory = firstFile.isDir();
if (ptrToIsDirectory)
*ptrToIsDirectory = isDirectory;
const QString workingDirectory = isDirectory ? firstFile.absoluteFilePath() : firstFile.absolutePath();
const QString repoDirectory = GitClient::findRepositoryForDirectory(workingDirectory);
if (repoDirectory.isEmpty()) {
*errorMessage = msgRepositoryNotFound(workingDirectory);
return RevertFailed;
}
// Check for changes
QString output;
switch (gitStatus(repoDirectory, false, &output, errorMessage)) {
case StatusChanged:
break;
case StatusUnchanged:
return RevertUnchanged;
case StatusFailed:
return RevertFailed;
}
CommitData d;
if (!parseFiles(output, &d)) {
*errorMessage = msgParseFilesFailed();
return RevertFailed;
}
// If we are looking at files, make them relative to the repository
// directory to match them in the status output list.
if (!isDirectory) {
const QDir repoDir(repoDirectory);
const QStringList::iterator cend = files.end();
for (QStringList::iterator it = files.begin(); it != cend; ++it)
*it = repoDir.relativeFilePath(*it);
}
// From the status output, determine all modified [un]staged files.
const QString modifiedPattern = QLatin1String("modified: ");
const QStringList allStagedFiles = GitSubmitEditor::statusListToFileList(d.stagedFiles.filter(modifiedPattern));
const QStringList allUnstagedFiles = GitSubmitEditor::statusListToFileList(d.unstagedFiles.filter(modifiedPattern));
// Unless a directory was passed, filter all modified files for the
// argument file list.
QStringList stagedFiles = allStagedFiles;
QStringList unstagedFiles = allUnstagedFiles;
if (!isDirectory) {
const QSet<QString> filesSet = files.toSet();
stagedFiles = allStagedFiles.toSet().intersect(filesSet).toList();
unstagedFiles = allUnstagedFiles.toSet().intersect(filesSet).toList();
}
if (Git::Constants::debug)
qDebug() << Q_FUNC_INFO << d.stagedFiles << d.unstagedFiles << allStagedFiles << allUnstagedFiles << stagedFiles << unstagedFiles;
if (stagedFiles.empty() && unstagedFiles.empty())
return RevertUnchanged;
// Ask to revert (to do: Handle lists with a selection dialog)
const QMessageBox::StandardButton answer
= QMessageBox::question(m_core->mainWindow(),
tr("Revert"),
tr("The file has been changed. Do you want to revert it?"),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if (answer == QMessageBox::No)
return RevertCanceled;
// Unstage the staged files
if (!stagedFiles.empty() && !synchronousReset(repoDirectory, stagedFiles, errorMessage))
return RevertFailed;
// Finally revert!
if (!synchronousCheckout(repoDirectory, stagedFiles + unstagedFiles, errorMessage))
return RevertFailed;
return RevertOk;
}
void GitClient::revert(const QStringList &files)
{
bool isDirectory;
QString errorMessage;
switch (revertI(files, &isDirectory, &errorMessage)) {
case RevertOk:
case RevertCanceled:
break;
case RevertUnchanged: {
const QString msg = (isDirectory || files.size() > 1) ? msgNoChangedFiles() : tr("The file is not modified.");
m_plugin->outputWindow()->append(msg);
m_plugin->outputWindow()->popup();
}
break;
case RevertFailed:
m_plugin->outputWindow()->append(errorMessage);
m_plugin->outputWindow()->popup();
break;
}
}
void GitClient::pull(const QString &workingDirectory)
{
executeGit(workingDirectory, QStringList(QLatin1String("pull")), m_plugin->outputWindow(), 0, true);
......@@ -648,6 +817,11 @@ void GitClient::push(const QString &workingDirectory)
executeGit(workingDirectory, QStringList(QLatin1String("push")), m_plugin->outputWindow(), 0, true);
}
QString GitClient::msgNoChangedFiles()
{
return tr("There are no modified files.");
}
void GitClient::stash(const QString &workingDirectory)
{
// Check for changes and stash
......@@ -657,7 +831,7 @@ void GitClient::stash(const QString &workingDirectory)
executeGit(workingDirectory, QStringList(QLatin1String("stash")), m_plugin->outputWindow(), 0, true);
break;
case StatusUnchanged:
m_plugin->outputWindow()->append(tr("There are no modified files."));
m_plugin->outputWindow()->append(msgNoChangedFiles());
m_plugin->outputWindow()->popup();
break;
case StatusFailed:
......@@ -694,8 +868,8 @@ QString GitClient::readConfig(const QString &workingDirectory, const QStringList
arguments << QLatin1String("config") << configVar;
QByteArray outputText;
if (synchronousGit(workingDirectory, arguments, &outputText))
return QString::fromLocal8Bit(outputText);
if (synchronousGit(workingDirectory, arguments, &outputText, 0, false))
return QString::fromLocal8Bit(outputText).remove(QLatin1Char('\r'));
return QString();
}
......
......@@ -90,11 +90,14 @@ public:
void addFile(const QString &workingDirectory, const QString &fileName);
bool synchronousAdd(const QString &workingDirectory, const QStringList &files);
bool synchronousReset(const QString &workingDirectory, const QStringList &files);
bool synchronousReset(const QString &workingDirectory, const QStringList &files, QString *errorMessage);
bool synchronousCheckout(const QString &workingDirectory, const QStringList &files, QString *errorMessage);
void pull(const QString &workingDirectory);
void push(const QString &workingDirectory);
void stash(const QString &workingDirectory);
void stashPop(const QString &workingDirectory);
void revert(const QStringList &files);
void branchList(const QString &workingDirectory);
void stashList(const QString &workingDirectory);
......@@ -113,19 +116,21 @@ public:
const QStringList &checkedFiles,
const QStringList &origCommitFiles);
enum StatusResult { StatusChanged, StatusUnchanged, StatusFailed };
StatusResult gitStatus(const QString &workingDirectory,
bool untracked,
QString *output = 0,
QString *errorMessage = 0);
GitSettings settings() const;
void setSettings(const GitSettings &s);
static QString msgNoChangedFiles();
public slots:
void show(const QString &source, const QString &id);
private:
enum StatusResult { StatusChanged, StatusUnchanged, StatusFailed };
StatusResult gitStatus(const QString &workingDirectory,
bool untracked,
QString *output = 0,
QString *errorMessage = 0);
VCSBase::VCSBaseEditor *createVCSEditor(const QString &kind,
QString title,
const QString &source,
......@@ -141,9 +146,13 @@ private:
bool outputToWindow = false);
bool synchronousGit(const QString &workingDirectory,
const QStringList &arguments,
QByteArray* outputText = 0,
QByteArray* errorText = 0);
const QStringList &arguments,
QByteArray* outputText = 0,
QByteArray* errorText = 0,
bool logCommandToWindow = true);
enum RevertResult { RevertOk, RevertUnchanged, RevertCanceled, RevertFailed };
RevertResult revertI(QStringList files, bool *isDirectory, QString *errorMessage);
const QString m_msgWait;
GitPlugin *m_plugin;
......
......@@ -118,7 +118,9 @@ GitPlugin::GitPlugin() :
m_undoFileAction(0),
m_undoProjectAction(0),
m_showAction(0),
m_addAction(0),
m_stageAction(0),
m_unstageAction(0),
m_revertAction(0),
m_commitAction(0),
m_pullAction(0),
m_pushAction(0),
......@@ -311,11 +313,23 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *error_message)
connect(m_undoFileAction, SIGNAL(triggered()), this, SLOT(undoFileChanges()));
gitContainer->addAction(command);
m_addAction = new QAction(tr("Add File"), this);
command = actionManager->registerAction(m_addAction, "Git.Add", globalcontext);
m_stageAction = new QAction(tr("Stage file for commit"), this);
command = actionManager->registerAction(m_stageAction, "Git.Stage", globalcontext);
command->setDefaultKeySequence(QKeySequence(tr("Alt+G,Alt+A")));
command->setAttribute(Core::ICommand::CA_UpdateText);
connect(m_addAction, SIGNAL(triggered()), this, SLOT(addFile()));
connect(m_stageAction, SIGNAL(triggered()), this, SLOT(stageFile()));
gitContainer->addAction(command);
m_unstageAction = new QAction(tr("Unstage file from commit"), this);
command = actionManager->registerAction(m_unstageAction, "Git.Unstage", globalcontext);
command->setAttribute(Core::ICommand::CA_UpdateText);
connect(m_unstageAction, SIGNAL(triggered()), this, SLOT(unstageFile()));
gitContainer->addAction(command);
m_revertAction = new QAction(tr("Revert..."), this);
command = actionManager->registerAction(m_revertAction, "Git.Revert", globalcontext);
command->setAttribute(Core::ICommand::CA_UpdateText);
connect(m_revertAction, SIGNAL(triggered()), this, SLOT(revertFile()));
gitContainer->addAction(command);
gitContainer->addAction(createSeparator(actionManager, globalcontext, QLatin1String("Git.Sep.Project"), this));
......@@ -537,14 +551,28 @@ void GitPlugin::undoProjectChanges()
m_gitClient->hardReset(workingDirectory, QString());
}
void GitPlugin::addFile()
void GitPlugin::stageFile()
{
QFileInfo fileInfo = currentFile();
QString fileName = fileInfo.fileName();
QString workingDirectory = fileInfo.absolutePath();
const QFileInfo fileInfo = currentFile();
const QString fileName = fileInfo.fileName();
const QString workingDirectory = fileInfo.absolutePath();
m_gitClient->addFile(workingDirectory, fileName);
}
void GitPlugin::unstageFile()
{
const QFileInfo fileInfo = currentFile();
const QString fileName = fileInfo.fileName();
const QString workingDirectory = fileInfo.absolutePath();
m_gitClient->synchronousReset(workingDirectory, QStringList(fileName));
}
void GitPlugin::revertFile()
{
const QFileInfo fileInfo = currentFile();
m_gitClient->revert(QStringList(fileInfo.absoluteFilePath()));
}
void GitPlugin::startCommit()
{
if (m_changeTmpFile) {
......@@ -570,7 +598,7 @@ void GitPlugin::startCommit()
// Store repository for diff and the original list of
// files to be able to unstage files the user unchecks
m_submitRepository = data.panelInfo.repository;
m_submitOrigCommitFiles = GitSubmitEditor::statusListToFileList(data.commitFiles);
m_submitOrigCommitFiles = GitSubmitEditor::statusListToFileList(data.stagedFiles);
if (Git::Constants::debug)
qDebug() << Q_FUNC_INFO << data << commitTemplate;
......@@ -602,7 +630,7 @@ Core::IEditor *GitPlugin::openSubmitEditor(const QString &fileName, const Commit
Q_ASSERT(submitEditor);
// The actions are for some reason enabled by the context switching
// mechanism. Disable them correctly.
m_submitCurrentAction->setEnabled(!cd.commitFiles.empty());
m_submitCurrentAction->setEnabled(!cd.stagedFiles.empty());
m_diffSelectedFilesAction->setEnabled(false);
m_undoAction->setEnabled(false);
m_redoAction->setEnabled(false);
......@@ -722,7 +750,9 @@ void GitPlugin::updateActions()
m_logAction->setText(tr("Log %1").arg(fileName));
m_blameAction->setText(tr("Blame %1").arg(fileName));
m_undoFileAction->setText(tr("Undo changes for %1").arg(fileName));
m_addAction->setText(tr("Add %1").arg(fileName));
m_stageAction->setText(tr("Stage %1 for commit").arg(fileName));
m_unstageAction->setText(tr("Unstage %1 from commit").arg(fileName));
m_revertAction->setText(tr("Revert %1...").arg(fileName));
if (repository.isEmpty()) {
// If the file is not in a repository, the corresponding project will
// be neither and we can disable everything and return
......@@ -731,7 +761,9 @@ void GitPlugin::updateActions()
m_logAction->setEnabled(false);
m_blameAction->setEnabled(false);
m_undoFileAction->setEnabled(false);
m_addAction->setEnabled(false);
m_stageAction->setEnabled(false);
m_unstageAction->setEnabled(false);
m_revertAction->setEnabled(false);
m_diffProjectAction->setEnabled(false);
m_diffProjectAction->setText(tr("Diff Project"));
m_statusProjectAction->setText(tr("Status Project"));
......@@ -747,7 +779,9 @@ void GitPlugin::updateActions()
m_logAction->setEnabled(true);
m_blameAction->setEnabled(true);
m_undoFileAction->setEnabled(true);
m_addAction->setEnabled(true);
m_stageAction->setEnabled(true);
m_unstageAction->setEnabled(true);
m_revertAction->setEnabled(true);
}
if (m_projectExplorer && m_projectExplorer->currentNode()
......
......@@ -116,7 +116,9 @@ private slots:
void logProject();
void undoFileChanges();
void undoProjectChanges();
void addFile();
void stageFile();
void unstageFile();
void revertFile();
void showCommit();
void startCommit();
......@@ -144,7 +146,9 @@ private:
QAction *m_undoFileAction;
QAction *m_undoProjectAction;
QAction *m_showAction;
QAction *m_addAction;
QAction *m_stageAction;
QAction *m_unstageAction;
QAction *m_revertAction;
QAction *m_commitAction;
QAction *m_pullAction;
QAction *m_pushAction;
......
......@@ -67,9 +67,9 @@ void GitSubmitEditor::setCommitData(const CommitData &d)
submitEditorWidget()->setPanelData(d.panelData);
submitEditorWidget()->setPanelInfo(d.panelInfo);
addFiles(d.commitFiles, true, true);
addFiles(d.stagedFiles, true, true);
// Not Updated: Initially unchecked
addFiles(d.notUpdatedFiles, false, true);
addFiles(d.unstagedFiles, false, true);
addFiles(d.untrackedFiles, false, true);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment