1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "gitsubmiteditor.h"
27 #include "gitclient.h"
28 #include "gitplugin.h"
29 #include "gitsubmiteditorwidget.h"
30
31 #include <coreplugin/editormanager/editormanager.h>
32 #include <coreplugin/iversioncontrol.h>
33 #include <coreplugin/progressmanager/progressmanager.h>
34 #include <utils/qtcassert.h>
35 #include <utils/runextensions.h>
36 #include <vcsbase/submitfilemodel.h>
37 #include <vcsbase/vcsoutputwindow.h>
38
39 #include <QDebug>
40 #include <QStringList>
41 #include <QTextCodec>
42 #include <QTimer>
43
44 static const char TASK_UPDATE_COMMIT[] = "Git.UpdateCommit";
45
46 using namespace VcsBase;
47
48 namespace Git {
49 namespace Internal {
50
51 class GitSubmitFileModel : public SubmitFileModel
52 {
53 public:
GitSubmitFileModel(QObject * parent=nullptr)54 GitSubmitFileModel(QObject *parent = nullptr) : SubmitFileModel(parent)
55 { }
56
updateSelections(SubmitFileModel * source)57 void updateSelections(SubmitFileModel *source) override
58 {
59 QTC_ASSERT(source, return);
60 auto gitSource = static_cast<GitSubmitFileModel *>(source);
61 int j = 0;
62 for (int i = 0; i < rowCount() && j < source->rowCount(); ++i) {
63 CommitData::StateFilePair stateFile = stateFilePair(i);
64 for (; j < source->rowCount(); ++j) {
65 CommitData::StateFilePair sourceStateFile = gitSource->stateFilePair(j);
66 if (stateFile == sourceStateFile) {
67 if (isCheckable(i) && source->isCheckable(j))
68 setChecked(i, source->checked(j));
69 break;
70 } else if (((stateFile.first & UntrackedFile)
71 == (sourceStateFile.first & UntrackedFile))
72 && (stateFile < sourceStateFile)) {
73 break;
74 }
75 }
76 }
77 }
78
79 private:
stateFilePair(int row) const80 CommitData::StateFilePair stateFilePair(int row) const
81 {
82 return CommitData::StateFilePair(static_cast<FileStates>(extraData(row).toInt()), file(row));
83 }
84 };
85
fetch(CommitType commitType,const QString & workingDirectory)86 CommitDataFetchResult CommitDataFetchResult::fetch(CommitType commitType, const QString &workingDirectory)
87 {
88 CommitDataFetchResult result;
89 result.commitData.commitType = commitType;
90 QString commitTemplate;
91 result.success = GitClient::instance()->getCommitData(
92 workingDirectory, &commitTemplate, result.commitData, &result.errorMessage);
93 return result;
94 }
95
96 /* The problem with git is that no diff can be obtained to for a random
97 * multiselection of staged/unstaged files; it requires the --cached
98 * option for staged files. So, we sort apart the diff file lists
99 * according to a type flag we add to the model. */
100
GitSubmitEditor()101 GitSubmitEditor::GitSubmitEditor() :
102 VcsBaseSubmitEditor(new GitSubmitEditorWidget)
103 {
104 connect(this, &VcsBaseSubmitEditor::diffSelectedRows, this, &GitSubmitEditor::slotDiffSelected);
105 connect(submitEditorWidget(), &GitSubmitEditorWidget::showRequested, this, &GitSubmitEditor::showCommit);
106 connect(GitPlugin::versionControl(), &Core::IVersionControl::repositoryChanged,
107 this, &GitSubmitEditor::forceUpdateFileModel);
108 connect(&m_fetchWatcher, &QFutureWatcher<CommitDataFetchResult>::finished,
109 this, &GitSubmitEditor::commitDataRetrieved);
110 }
111
112 GitSubmitEditor::~GitSubmitEditor() = default;
113
submitEditorWidget()114 GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
115 {
116 return static_cast<GitSubmitEditorWidget *>(widget());
117 }
118
submitEditorWidget() const119 const GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget() const
120 {
121 return static_cast<GitSubmitEditorWidget *>(widget());
122 }
123
setCommitData(const CommitData & d)124 void GitSubmitEditor::setCommitData(const CommitData &d)
125 {
126 m_commitEncoding = d.commitEncoding;
127 m_workingDirectory = d.panelInfo.repository;
128 m_commitType = d.commitType;
129 m_amendSHA1 = d.amendSHA1;
130
131 GitSubmitEditorWidget *w = submitEditorWidget();
132 w->initialize(m_commitType, m_workingDirectory, d.panelData, d.panelInfo, d.enablePush);
133 w->setHasUnmerged(false);
134
135 setEmptyFileListEnabled(m_commitType == AmendCommit); // Allow for just correcting the message
136
137 m_model = new GitSubmitFileModel(this);
138 m_model->setRepositoryRoot(d.panelInfo.repository);
139 m_model->setFileStatusQualifier([](const QString &, const QVariant &extraData)
140 -> SubmitFileModel::FileStatusHint
141 {
142 const FileStates state = static_cast<FileStates>(extraData.toInt());
143 if (state & (UnmergedFile | UnmergedThem | UnmergedUs))
144 return SubmitFileModel::FileUnmerged;
145 if (state.testFlag(AddedFile) || state.testFlag(UntrackedFile))
146 return SubmitFileModel::FileAdded;
147 if (state.testFlag(ModifiedFile) || state.testFlag(TypeChangedFile))
148 return SubmitFileModel::FileModified;
149 if (state.testFlag(DeletedFile))
150 return SubmitFileModel::FileDeleted;
151 if (state.testFlag(RenamedFile))
152 return SubmitFileModel::FileRenamed;
153 return SubmitFileModel::FileStatusUnknown;
154 } );
155
156 if (!d.files.isEmpty()) {
157 for (QList<CommitData::StateFilePair>::const_iterator it = d.files.constBegin();
158 it != d.files.constEnd(); ++it) {
159 const FileStates state = it->first;
160 const QString file = it->second;
161 CheckMode checkMode;
162 if (state & UnmergedFile) {
163 checkMode = Uncheckable;
164 w->setHasUnmerged(true);
165 } else if (state & StagedFile) {
166 checkMode = Checked;
167 } else {
168 checkMode = Unchecked;
169 }
170 m_model->addFile(file, CommitData::stateDisplayName(state), checkMode,
171 QVariant(static_cast<int>(state)));
172 }
173 }
174 setFileModel(m_model);
175 }
176
slotDiffSelected(const QList<int> & rows)177 void GitSubmitEditor::slotDiffSelected(const QList<int> &rows)
178 {
179 // Sort it apart into unmerged/staged/unstaged files
180 QStringList unmergedFiles;
181 QStringList unstagedFiles;
182 QStringList stagedFiles;
183 for (int row : rows) {
184 const QString fileName = m_model->file(row);
185 const FileStates state = static_cast<FileStates>(m_model->extraData(row).toInt());
186 if (state & UnmergedFile) {
187 unmergedFiles.push_back(fileName);
188 } else if (state & StagedFile) {
189 if (state & (RenamedFile | CopiedFile)) {
190 const int arrow = fileName.indexOf(" -> ");
191 if (arrow != -1) {
192 stagedFiles.push_back(fileName.left(arrow));
193 stagedFiles.push_back(fileName.mid(arrow + 4));
194 continue;
195 }
196 }
197 stagedFiles.push_back(fileName);
198 } else if (state == UntrackedFile) {
199 Core::EditorManager::openEditor(m_workingDirectory + '/' + fileName);
200 } else {
201 unstagedFiles.push_back(fileName);
202 }
203 }
204 if (!unstagedFiles.empty() || !stagedFiles.empty())
205 GitClient::instance()->diffFiles(m_workingDirectory, unstagedFiles, stagedFiles);
206 if (!unmergedFiles.empty())
207 GitClient::instance()->merge(m_workingDirectory, unmergedFiles);
208 }
209
showCommit(const QString & commit)210 void GitSubmitEditor::showCommit(const QString &commit)
211 {
212 if (!m_workingDirectory.isEmpty())
213 GitClient::instance()->show(m_workingDirectory, commit);
214 }
215
updateFileModel()216 void GitSubmitEditor::updateFileModel()
217 {
218 // Commit data is set when the editor is initialized, and updateFileModel immediately follows,
219 // when the editor is activated. Avoid another call to git status
220 if (m_firstUpdate) {
221 m_firstUpdate = false;
222 return;
223 }
224 GitSubmitEditorWidget *w = submitEditorWidget();
225 if (w->updateInProgress() || m_workingDirectory.isEmpty())
226 return;
227 w->setUpdateInProgress(true);
228 m_fetchWatcher.setFuture(Utils::runAsync(&CommitDataFetchResult::fetch,
229 m_commitType, m_workingDirectory));
230 Core::ProgressManager::addTask(m_fetchWatcher.future(), tr("Refreshing Commit Data"),
231 TASK_UPDATE_COMMIT);
232
233 GitClient::instance()->addFuture(QFuture<void>(m_fetchWatcher.future()));
234 }
235
forceUpdateFileModel()236 void GitSubmitEditor::forceUpdateFileModel()
237 {
238 GitSubmitEditorWidget *w = submitEditorWidget();
239 if (w->updateInProgress())
240 QTimer::singleShot(10, this, [this] { forceUpdateFileModel(); });
241 else
242 updateFileModel();
243 }
244
commitDataRetrieved()245 void GitSubmitEditor::commitDataRetrieved()
246 {
247 CommitDataFetchResult result = m_fetchWatcher.result();
248 GitSubmitEditorWidget *w = submitEditorWidget();
249 if (result.success) {
250 setCommitData(result.commitData);
251 w->refreshLog(m_workingDirectory);
252 w->setEnabled(true);
253 } else {
254 // Nothing to commit left!
255 VcsOutputWindow::appendError(result.errorMessage);
256 m_model->clear();
257 w->setEnabled(false);
258 }
259 w->setUpdateInProgress(false);
260 }
261
panelData() const262 GitSubmitEditorPanelData GitSubmitEditor::panelData() const
263 {
264 return submitEditorWidget()->panelData();
265 }
266
amendSHA1() const267 QString GitSubmitEditor::amendSHA1() const
268 {
269 QString commit = submitEditorWidget()->amendSHA1();
270 return commit.isEmpty() ? m_amendSHA1 : commit;
271 }
272
fileContents() const273 QByteArray GitSubmitEditor::fileContents() const
274 {
275 const QString &text = description();
276
277 // Do the encoding convert, When use user-defined encoding
278 // e.g. git config --global i18n.commitencoding utf-8
279 if (m_commitEncoding)
280 return m_commitEncoding->fromUnicode(text);
281
282 // Using utf-8 as the default encoding
283 return text.toUtf8();
284 }
285
286 } // namespace Internal
287 } // namespace Git
288