gitsubmiteditor.cpp 6.96 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

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

37
#include <coreplugin/editormanager/editormanager.h>
38
#include <utils/qtcassert.h>
39
#include <vcsbase/submitfilemodel.h>
40
#include <vcsbase/vcsbaseoutputwindow.h>
41

42
43
44
#include <QDebug>
#include <QStringList>
#include <QTextCodec>
con's avatar
con committed
45
46
47
48

namespace Git {
namespace Internal {

49
50
51
52
53
54
class GitSubmitFileModel : public VcsBase::SubmitFileModel
{
public:
    GitSubmitFileModel(QObject *parent = 0) : VcsBase::SubmitFileModel(parent)
    { }

Orgad Shaneh's avatar
Orgad Shaneh committed
55
    void updateSelections(SubmitFileModel *source)
56
    {
57
58
        QTC_ASSERT(source, return);
        GitSubmitFileModel *gitSource = static_cast<GitSubmitFileModel *>(source);
59
60
61
62
        int j = 0;
        for (int i = 0; i < rowCount() && j < source->rowCount(); ++i) {
            CommitData::StateFilePair stateFile = stateFilePair(i);
            for (; j < source->rowCount(); ++j) {
63
                CommitData::StateFilePair sourceStateFile = gitSource->stateFilePair(j);
64
65
66
67
68
69
70
71
72
73
74
                if (stateFile == sourceStateFile) {
                    setChecked(i, source->checked(j));
                    break;
                } else if (stateFile < sourceStateFile) {
                    break;
                }
            }
        }
    }

private:
75
    CommitData::StateFilePair stateFilePair(int row) const
76
77
78
79
80
    {
        return CommitData::StateFilePair(static_cast<FileStates>(extraData(row).toInt()), file(row));
    }
};

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

hjk's avatar
hjk committed
86
87
GitSubmitEditor::GitSubmitEditor(const VcsBase::VcsBaseSubmitEditorParameters *parameters, QWidget *parent) :
    VcsBaseSubmitEditor(parameters, new GitSubmitEditorWidget(parent)),
88
    m_model(0),
89
90
    m_amend(false),
    m_forceClose(false)
con's avatar
con committed
91
{
92
    connect(this, SIGNAL(diffSelectedFiles(QList<int>)), this, SLOT(slotDiffSelected(QList<int>)));
con's avatar
con committed
93
94
95
96
97
98
99
100
101
}

GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
{
    return static_cast<GitSubmitEditorWidget *>(widget());
}

void GitSubmitEditor::setCommitData(const CommitData &d)
{
102
103
104
105
    GitSubmitEditorWidget *w = submitEditorWidget();
    w->setPanelData(d.panelData);
    w->setPanelInfo(d.panelInfo);
    w->setHasUnmerged(false);
con's avatar
con committed
106

107
    m_commitEncoding = d.commitEncoding;
108
    m_workingDirectory = d.panelInfo.repository;
109

110
    m_model = new GitSubmitFileModel(this);
111
112
113
    if (!d.files.isEmpty()) {
        for (QList<CommitData::StateFilePair>::const_iterator it = d.files.constBegin();
             it != d.files.constEnd(); ++it) {
114
            const FileStates state = it->first;
115
            const QString file = it->second;
116
            VcsBase::CheckMode checkMode;
117
            if (state & UnmergedFile) {
118
                checkMode = VcsBase::Uncheckable;
119
120
                w->setHasUnmerged(true);
            } else if (state & StagedFile) {
121
                checkMode = VcsBase::Checked;
122
            } else {
123
                checkMode = VcsBase::Unchecked;
124
            }
125
            m_model->addFile(file, CommitData::stateDisplayName(state), checkMode,
126
127
                             QVariant(static_cast<int>(state)));
        }
128
    }
129
    setFileModel(m_model, d.panelInfo.repository);
130
131
}

132
133
134
135
136
137
void GitSubmitEditor::setAmend(bool amend)
{
    m_amend = amend;
    setEmptyFileListEnabled(amend); // Allow for just correcting the message
}

138
void GitSubmitEditor::slotDiffSelected(const QList<int> &rows)
139
{
140
141
    // Sort it apart into unmerged/staged/unstaged files
    QStringList unmergedFiles;
142
143
    QStringList unstagedFiles;
    QStringList stagedFiles;
144
145
146
147
148
149
150
151
152
    foreach (int row, rows) {
        const QString fileName = m_model->file(row);
        const FileStates state = static_cast<FileStates>(m_model->extraData(row).toInt());
        if (state & UnmergedFile)
            unmergedFiles.push_back(fileName);
        else if (state & StagedFile)
            stagedFiles.push_back(fileName);
        else if (state != UntrackedFile)
            unstagedFiles.push_back(fileName);
153
    }
154
155
    if (!unstagedFiles.empty() || !stagedFiles.empty())
        emit diff(unstagedFiles, stagedFiles);
156
157
    if (!unmergedFiles.empty())
        emit merge(unmergedFiles);
con's avatar
con committed
158
159
}

160
161
void GitSubmitEditor::updateFileModel()
{
162
163
    if (m_workingDirectory.isEmpty())
        return;
164
165
166
    GitClient *client = GitPlugin::instance()->gitClient();
    QString errorMessage, commitTemplate;
    CommitData data;
167
    if (client->getCommitData(m_workingDirectory, m_amend, &commitTemplate, &data, &errorMessage)) {
168
        setCommitData(data);
169
    } else {
170
        VcsBase::VcsBaseOutputWindow::instance()->append(errorMessage);
171
172
173
        m_forceClose = true;
        Core::EditorManager::instance()->closeEditors(QList<IEditor*>() << this);
    }
174
175
}

con's avatar
con committed
176
177
178
179
180
GitSubmitEditorPanelData GitSubmitEditor::panelData() const
{
    return const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->panelData();
}

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
QByteArray GitSubmitEditor::fileContents() const
{
    const QString& text = const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->descriptionText();

    if (!m_commitEncoding.isEmpty()) {
        // Do the encoding convert, When use user-defined encoding
        // e.g. git config --global i18n.commitencoding utf-8
        QTextCodec *codec = QTextCodec::codecForName(m_commitEncoding.toLocal8Bit());
        if (codec)
            return codec->fromUnicode(text);
    }

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

hjk's avatar
hjk committed
197
198
} // namespace Internal
} // namespace Git