gitsubmiteditor.cpp 11.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

31
#include "gitsubmiteditor.h"
32 33 34
#include "commitdata.h"
#include "gitclient.h"
#include "gitplugin.h"
con's avatar
con committed
35 36
#include "gitsubmiteditorwidget.h"

37
#include <coreplugin/editormanager/editormanager.h>
38
#include <coreplugin/iversioncontrol.h>
39
#include <coreplugin/progressmanager/progressmanager.h>
40
#include <utils/qtcassert.h>
41
#include <vcsbase/submitfilemodel.h>
42
#include <vcsbase/vcsoutputwindow.h>
43

44 45 46
#include <QDebug>
#include <QStringList>
#include <QTextCodec>
47
#include <QTimer>
48 49 50
#include <QtConcurrentRun>

static const char TASK_UPDATE_COMMIT[] = "Git.UpdateCommit";
con's avatar
con committed
51

52 53
using namespace VcsBase;

con's avatar
con committed
54 55 56
namespace Git {
namespace Internal {

57
class GitSubmitFileModel : public SubmitFileModel
58 59
{
public:
60
    GitSubmitFileModel(QObject *parent = 0) : SubmitFileModel(parent)
61 62
    { }

Orgad Shaneh's avatar
Orgad Shaneh committed
63
    void updateSelections(SubmitFileModel *source) override
64
    {
65 66
        QTC_ASSERT(source, return);
        GitSubmitFileModel *gitSource = static_cast<GitSubmitFileModel *>(source);
67 68 69 70
        int j = 0;
        for (int i = 0; i < rowCount() && j < source->rowCount(); ++i) {
            CommitData::StateFilePair stateFile = stateFilePair(i);
            for (; j < source->rowCount(); ++j) {
71
                CommitData::StateFilePair sourceStateFile = gitSource->stateFilePair(j);
72
                if (stateFile == sourceStateFile) {
73 74
                    if (isCheckable(i) && source->isCheckable(j))
                        setChecked(i, source->checked(j));
75
                    break;
76 77 78
                } else if (((stateFile.first & UntrackedFile)
                            == (sourceStateFile.first & UntrackedFile))
                           && (stateFile < sourceStateFile)) {
79 80 81 82 83 84 85
                    break;
                }
            }
        }
    }

private:
86
    CommitData::StateFilePair stateFilePair(int row) const
87 88 89 90 91
    {
        return CommitData::StateFilePair(static_cast<FileStates>(extraData(row).toInt()), file(row));
    }
};

92 93 94 95 96 97 98 99 100 101 102 103 104
class CommitDataFetcher : public QObject
{
    Q_OBJECT

public:
    CommitDataFetcher(CommitType commitType, const QString &workingDirectory) :
        m_commitData(commitType),
        m_workingDirectory(workingDirectory)
    {
    }

    void start()
    {
105
        GitClient *client = GitPlugin::instance()->client();
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        QString commitTemplate;
        bool success = client->getCommitData(m_workingDirectory, &commitTemplate,
                                             m_commitData, &m_errorMessage);
        emit finished(success);
    }

    const CommitData &commitData() const { return m_commitData; }
    const QString &errorMessage() const { return m_errorMessage; }

signals:
    void finished(bool result);

private:
    CommitData m_commitData;
    QString m_workingDirectory;
    QString m_errorMessage;
};

124 125
/* The problem with git is that no diff can be obtained to for a random
 * multiselection of staged/unstaged files; it requires the --cached
126 127
 * option for staged files. So, we sort apart the diff file lists
 * according to a type flag we add to the model. */
128

129
GitSubmitEditor::GitSubmitEditor(const VcsBaseSubmitEditorParameters *parameters) :
130
    VcsBaseSubmitEditor(parameters, new GitSubmitEditorWidget),
131
    m_model(0),
132
    m_commitEncoding(0),
133
    m_commitType(SimpleCommit),
134
    m_firstUpdate(true),
135 136
    m_commitDataFetcher(0),
    m_gitClient(GitPlugin::instance()->client())
con's avatar
con committed
137
{
Tobias Hunger's avatar
Tobias Hunger committed
138 139
    connect(this, &VcsBaseSubmitEditor::diffSelectedRows, this, &GitSubmitEditor::slotDiffSelected);
    connect(submitEditorWidget(), &GitSubmitEditorWidget::show, this, &GitSubmitEditor::showCommit);
140 141
    connect(GitPlugin::instance()->versionControl(), &Core::IVersionControl::repositoryChanged,
            this, &GitSubmitEditor::forceUpdateFileModel);
con's avatar
con committed
142 143
}

144 145 146 147 148
GitSubmitEditor::~GitSubmitEditor()
{
    resetCommitDataFetcher();
}

con's avatar
con committed
149 150 151 152 153
GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
{
    return static_cast<GitSubmitEditorWidget *>(widget());
}

Orgad Shaneh's avatar
Orgad Shaneh committed
154 155 156 157 158
const GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget() const
{
    return static_cast<GitSubmitEditorWidget *>(widget());
}

159 160 161 162
void GitSubmitEditor::resetCommitDataFetcher()
{
    if (!m_commitDataFetcher)
        return;
Tobias Hunger's avatar
Tobias Hunger committed
163 164
    disconnect(m_commitDataFetcher, &CommitDataFetcher::finished, this, &GitSubmitEditor::commitDataRetrieved);
    connect(m_commitDataFetcher, &CommitDataFetcher::finished, m_commitDataFetcher, &QObject::deleteLater);
165 166
}

con's avatar
con committed
167 168
void GitSubmitEditor::setCommitData(const CommitData &d)
{
169 170 171 172 173
    m_commitEncoding = d.commitEncoding;
    m_workingDirectory = d.panelInfo.repository;
    m_commitType = d.commitType;
    m_amendSHA1 = d.amendSHA1;

174
    GitSubmitEditorWidget *w = submitEditorWidget();
175
    w->initialize(m_commitType, m_workingDirectory, d.panelData, d.panelInfo, d.enablePush);
176
    w->setHasUnmerged(false);
con's avatar
con committed
177

178
    setEmptyFileListEnabled(m_commitType == AmendCommit); // Allow for just correcting the message
179

180
    m_model = new GitSubmitFileModel(this);
181
    m_model->setRepositoryRoot(d.panelInfo.repository);
182 183 184
    m_model->setFileStatusQualifier([](const QString &, const QVariant &extraData)
                                    -> SubmitFileModel::FileStatusHint
    {
185 186 187 188 189 190 191 192 193 194 195 196
        const FileStates state = static_cast<FileStates>(extraData.toInt());
        if (state.testFlag(AddedFile) || state.testFlag(UntrackedFile))
            return SubmitFileModel::FileAdded;
        if (state.testFlag(ModifiedFile))
            return SubmitFileModel::FileModified;
        if (state.testFlag(DeletedFile))
            return SubmitFileModel::FileDeleted;
        if (state.testFlag(RenamedFile))
            return SubmitFileModel::FileRenamed;
        return SubmitFileModel::FileStatusUnknown;
    } );

197 198 199
    if (!d.files.isEmpty()) {
        for (QList<CommitData::StateFilePair>::const_iterator it = d.files.constBegin();
             it != d.files.constEnd(); ++it) {
200
            const FileStates state = it->first;
201
            const QString file = it->second;
202
            CheckMode checkMode;
203
            if (state & UnmergedFile) {
204
                checkMode = Uncheckable;
205 206
                w->setHasUnmerged(true);
            } else if (state & StagedFile) {
207
                checkMode = Checked;
208
            } else {
209
                checkMode = Unchecked;
210
            }
211
            m_model->addFile(file, CommitData::stateDisplayName(state), checkMode,
212 213
                             QVariant(static_cast<int>(state)));
        }
214
    }
215
    setFileModel(m_model);
216 217
}

218
void GitSubmitEditor::slotDiffSelected(const QList<int> &rows)
219
{
220 221
    // Sort it apart into unmerged/staged/unstaged files
    QStringList unmergedFiles;
222 223
    QStringList unstagedFiles;
    QStringList stagedFiles;
224 225 226
    foreach (int row, rows) {
        const QString fileName = m_model->file(row);
        const FileStates state = static_cast<FileStates>(m_model->extraData(row).toInt());
227
        if (state & UnmergedFile) {
228
            unmergedFiles.push_back(fileName);
229 230 231 232 233 234 235 236 237
        } else if (state & StagedFile) {
            if (state & (RenamedFile | CopiedFile)) {
                const int arrow = fileName.indexOf(QLatin1String(" -> "));
                if (arrow != -1) {
                    stagedFiles.push_back(fileName.left(arrow));
                    stagedFiles.push_back(fileName.mid(arrow + 4));
                    continue;
                }
            }
238
            stagedFiles.push_back(fileName);
239
        } else if (state == UntrackedFile) {
240
            Core::EditorManager::openEditor(m_workingDirectory + QLatin1Char('/') + fileName);
241
        } else {
242
            unstagedFiles.push_back(fileName);
243
        }
244
    }
245
    if (!unstagedFiles.empty() || !stagedFiles.empty())
246
        m_gitClient->diffFiles(m_workingDirectory, unstagedFiles, stagedFiles);
247
    if (!unmergedFiles.empty())
248
        m_gitClient->merge(m_workingDirectory, unmergedFiles);
con's avatar
con committed
249 250
}

251 252 253
void GitSubmitEditor::showCommit(const QString &commit)
{
    if (!m_workingDirectory.isEmpty())
254
        m_gitClient->show(m_workingDirectory, commit);
255 256
}

257 258
void GitSubmitEditor::updateFileModel()
{
259 260 261 262 263 264
    // Commit data is set when the editor is initialized, and updateFileModel immediately follows,
    // when the editor is activated. Avoid another call to git status
    if (m_firstUpdate) {
        m_firstUpdate = false;
        return;
    }
265 266
    GitSubmitEditorWidget *w = submitEditorWidget();
    if (w->updateInProgress() || m_workingDirectory.isEmpty())
267
        return;
268
    w->setUpdateInProgress(true);
269 270
    resetCommitDataFetcher();
    m_commitDataFetcher = new CommitDataFetcher(m_commitType, m_workingDirectory);
Tobias Hunger's avatar
Tobias Hunger committed
271
    connect(m_commitDataFetcher, &CommitDataFetcher::finished, this, &GitSubmitEditor::commitDataRetrieved);
272 273 274
    QFuture<void> future = QtConcurrent::run(m_commitDataFetcher, &CommitDataFetcher::start);
    Core::ProgressManager::addTask(future, tr("Refreshing Commit Data"), TASK_UPDATE_COMMIT);

275
    m_gitClient->addFuture(future);
276 277
}

278 279 280 281
void GitSubmitEditor::forceUpdateFileModel()
{
    GitSubmitEditorWidget *w = submitEditorWidget();
    if (w->updateInProgress())
Nikolai Kosjar's avatar
Nikolai Kosjar committed
282
        QTimer::singleShot(10, this, [this] { forceUpdateFileModel(); });
283 284 285 286
    else
        updateFileModel();
}

287 288 289 290 291
void GitSubmitEditor::commitDataRetrieved(bool success)
{
    GitSubmitEditorWidget *w = submitEditorWidget();
    if (success) {
        setCommitData(m_commitDataFetcher->commitData());
292
        w->refreshLog(m_workingDirectory);
293
        w->setEnabled(true);
294
    } else {
295
        // Nothing to commit left!
296
        VcsOutputWindow::appendError(m_commitDataFetcher->errorMessage());
297
        m_model->clear();
298
        w->setEnabled(false);
299
    }
300 301
    m_commitDataFetcher->deleteLater();
    m_commitDataFetcher = 0;
302
    w->setUpdateInProgress(false);
303 304
}

con's avatar
con committed
305 306
GitSubmitEditorPanelData GitSubmitEditor::panelData() const
{
Orgad Shaneh's avatar
Orgad Shaneh committed
307
    return submitEditorWidget()->panelData();
con's avatar
con committed
308 309
}

310 311 312 313 314 315
QString GitSubmitEditor::amendSHA1() const
{
    QString commit = submitEditorWidget()->amendSHA1();
    return commit.isEmpty() ? m_amendSHA1 : commit;
}

316 317
QByteArray GitSubmitEditor::fileContents() const
{
318
    const QString &text = description();
319

320 321 322 323
    // Do the encoding convert, When use user-defined encoding
    // e.g. git config --global i18n.commitencoding utf-8
    if (m_commitEncoding)
        return m_commitEncoding->fromUnicode(text);
324 325 326 327 328

    // Using utf-8 as the default encoding
    return text.toUtf8();
}

hjk's avatar
hjk committed
329 330
} // namespace Internal
} // namespace Git
331 332

#include "gitsubmiteditor.moc"