-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommitimpl.cpp
More file actions
389 lines (311 loc) · 11 KB
/
commitimpl.cpp
File metadata and controls
389 lines (311 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
Description: changes commit dialog
Author: Marco Costalba (C) 2005-2007
Copyright: See COPYING file that comes with this distribution
*/
#include <QTextCodec>
#include <QSettings>
#include <QMenu>
#include <QRegExp>
#include <QDir>
#include <QMessageBox>
#include <QInputDialog>
#include <QToolTip>
#include <QScrollBar>
#include "exceptionmanager.h"
#include "common.h"
#include "git.h"
#include "settingsimpl.h"
#include "commitimpl.h"
using namespace QGit;
QString CommitImpl::lastMsgBeforeError;
CommitImpl::CommitImpl(Git* g, bool amend) : git(g) {
// adjust GUI
setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
textEditMsg->setFont(TYPE_WRITER_FONT);
QVector<QSplitter*> v(1, splitter);
QGit::restoreGeometrySetting(CMT_GEOM_KEY, this, &v);
QSettings settings;
QString templ(settings.value(CMT_TEMPL_KEY, CMT_TEMPL_DEF).toString());
QString msg;
QDir d;
if (d.exists(templ))
readFromFile(templ, msg);
// set-up files list
const RevFile* f = git->getFiles(ZERO_SHA);
for (int i = 0; f && i < f->count(); ++i) { // in case of amend f could be null
bool inIndex = f->statusCmp(i, RevFile::IN_INDEX);
bool isNew = (f->statusCmp(i, RevFile::NEW) || f->statusCmp(i, RevFile::UNKNOWN));
QColor myColor = QPalette().color(QPalette::WindowText);
if (isNew)
myColor = Qt::darkGreen;
else if (f->statusCmp(i, RevFile::DELETED))
myColor = Qt::red;
QTreeWidgetItem* item = new QTreeWidgetItem(treeWidgetFiles);
item->setText(0, git->filePath(*f, i));
item->setText(1, inIndex ? "Updated in index" : "Not updated in index");
item->setCheckState(0, inIndex || !isNew ? Qt::Checked : Qt::Unchecked);
item->setForeground(0, myColor);
}
treeWidgetFiles->resizeColumnToContents(0);
// compute cursor offsets. Take advantage of fixed width font
textEditMsg->setPlainText("\nx\nx"); // cursor doesn't move on empty text
textEditMsg->moveCursor(QTextCursor::Start);
textEditMsg->verticalScrollBar()->setValue(0);
textEditMsg->horizontalScrollBar()->setValue(0);
int y0 = textEditMsg->cursorRect().y();
int x0 = textEditMsg->cursorRect().x();
textEditMsg->moveCursor(QTextCursor::Down);
textEditMsg->moveCursor(QTextCursor::Right);
textEditMsg->verticalScrollBar()->setValue(0);
int y1 = textEditMsg->cursorRect().y();
int x1 = textEditMsg->cursorRect().x();
ofsX = x1 - x0;
ofsY = y1 - y0;
textEditMsg->moveCursor(QTextCursor::Start);
textEditMsg_cursorPositionChanged();
if (lastMsgBeforeError.isEmpty()) {
// setup textEditMsg with old commit message to be amended
QString status("");
if (amend)
status = git->getLastCommitMsg();
// setup textEditMsg with default value if user opted to do so (default)
if (testFlag(USE_CMT_MSG_F, FLAGS_KEY))
status += git->getNewCommitMsg();
// prepend commit msg with template if available
if (!amend)
status.prepend('\n').replace(QRegExp("\\n([^#])"), "\n#\\1"); // comment all the lines
msg = status.trimmed();
} else
msg = lastMsgBeforeError;
textEditMsg->setPlainText(msg);
textEditMsg->setFocus();
// if message is not changed we avoid calling refresh
// to change patch name in stgCommit()
origMsg = msg;
// setup button functions
if (amend) {
if (git->isStGITStack()) {
pushButtonOk->setText("&Add to top");
pushButtonOk->setShortcut(QKeySequence("Alt+A"));
pushButtonOk->setToolTip("Refresh top stack patch");
} else {
pushButtonOk->setText("&Amend");
pushButtonOk->setShortcut(QKeySequence("Alt+A"));
pushButtonOk->setToolTip("Amend latest commit");
}
connect(pushButtonOk, SIGNAL(clicked()),
this, SLOT(pushButtonAmend_clicked()));
} else {
if (git->isStGITStack()) {
pushButtonOk->setText("&New patch");
pushButtonOk->setShortcut(QKeySequence("Alt+N"));
pushButtonOk->setToolTip("Create a new patch");
}
connect(pushButtonOk, SIGNAL(clicked()),
this, SLOT(pushButtonCommit_clicked()));
}
connect(treeWidgetFiles, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(contextMenuPopup(const QPoint&)));
connect(textEditMsg, SIGNAL(cursorPositionChanged()),
this, SLOT(textEditMsg_cursorPositionChanged()));
}
void CommitImpl::closeEvent(QCloseEvent*) {
QVector<QSplitter*> v(1, splitter);
QGit::saveGeometrySetting(CMT_GEOM_KEY, this, &v);
}
void CommitImpl::contextMenuPopup(const QPoint& pos) {
QMenu* contextMenu = new QMenu(this);
QAction* a = contextMenu->addAction("Select All");
connect(a, SIGNAL(triggered()), this, SLOT(checkAll()));
a = contextMenu->addAction("Unselect All");
connect(a, SIGNAL(triggered()), this, SLOT(unCheckAll()));
contextMenu->popup(mapToGlobal(pos));
}
void CommitImpl::checkAll() { checkUncheck(true); }
void CommitImpl::unCheckAll() { checkUncheck(false); }
void CommitImpl::checkUncheck(bool checkAll) {
QTreeWidgetItemIterator it(treeWidgetFiles);
while (*it) {
(*it)->setCheckState(0, checkAll ? Qt::Checked : Qt::Unchecked);
++it;
}
}
bool CommitImpl::getFiles(SList selFiles) {
// check for files to commit
selFiles.clear();
QTreeWidgetItemIterator it(treeWidgetFiles);
while (*it) {
if ((*it)->checkState(0) == Qt::Checked)
selFiles.append((*it)->text(0));
++it;
}
return !selFiles.isEmpty();
}
void CommitImpl::warnNoFiles() {
QMessageBox::warning(this, "Commit changes - QGit",
"Sorry, no files are selected for updating.",
QMessageBox::Ok, QMessageBox::NoButton);
}
bool CommitImpl::checkFiles(SList selFiles) {
if (getFiles(selFiles))
return true;
warnNoFiles();
return false;
}
bool CommitImpl::checkMsg(QString& msg) {
msg = textEditMsg->toPlainText();
msg.remove(QRegExp("(^|\\n)\\s*#[^\\n]*")); // strip comments
msg.replace(QRegExp("[ \\t\\r\\f\\v]+\\n"), "\n"); // strip line trailing cruft
msg = msg.trimmed();
if (msg.isEmpty()) {
QMessageBox::warning(this, "Commit changes - QGit",
"Sorry, I don't want an empty message.",
QMessageBox::Ok, QMessageBox::NoButton);
return false;
}
// split subject from message body
QString subj(msg.section('\n', 0, 0, QString::SectionIncludeTrailingSep));
QString body(msg.section('\n', 1).trimmed());
msg = subj + '\n' + body + '\n';
return true;
}
bool CommitImpl::checkPatchName(QString& patchName) {
bool ok;
patchName = patchName.simplified();
patchName.replace(' ', "_");
patchName = QInputDialog::getText(this, "Create new patch - QGit", "Enter patch name:",
QLineEdit::Normal, patchName, &ok);
if (!ok || patchName.isEmpty())
return false;
QString tmp(patchName.trimmed());
if (patchName != tmp.remove(' '))
QMessageBox::warning(this, "Create new patch - QGit", "Sorry, control "
"characters or spaces\n are not allowed in patch name.");
else if (git->isPatchName(patchName))
QMessageBox::warning(this, "Create new patch - QGit", "Sorry, patch name "
"already exists.\nPlease choose a different name.");
else
return true;
return false;
}
bool CommitImpl::checkConfirm(SCRef msg, SCRef patchName, SCList selFiles, bool amend) {
QTextCodec* tc = QTextCodec::codecForCStrings();
QTextCodec::setCodecForCStrings(0); // set temporary Latin-1
// NOTEME: i18n-ugly
QString whatToDo = amend ?
(git->isStGITStack() ? "refresh top patch with" :
"amend last commit with") :
(git->isStGITStack() ? "create a new patch with" : "commit");
QString text("Do you want to " + whatToDo);
bool const fullList = selFiles.size() < 20;
if (fullList)
text.append(" the following file(s)?\n\n" + selFiles.join("\n") +
"\n\nwith the message:\n\n");
else
text.append(" those " + QString::number(selFiles.size()) +
" files the with the message:\n\n");
text.append(msg);
if (git->isStGITStack())
text.append("\n\nAnd patch name: " + patchName);
QTextCodec::setCodecForCStrings(tc);
QMessageBox msgBox(this);
msgBox.setWindowTitle("Commit changes - QGit");
msgBox.setText(text);
if (!fullList)
msgBox.setDetailedText(selFiles.join("\n"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
return msgBox.exec() != QMessageBox::No;
}
void CommitImpl::pushButtonSettings_clicked() {
SettingsImpl setView(this, git, 3);
setView.exec();
}
void CommitImpl::pushButtonCancel_clicked() {
close();
}
void CommitImpl::pushButtonCommit_clicked() {
QStringList selFiles; // retrieve selected files
if (!checkFiles(selFiles))
return;
QString msg; // check for commit message and strip comments
if (!checkMsg(msg))
return;
QString patchName(msg.section('\n', 0, 0)); // the subject
if (git->isStGITStack() && !checkPatchName(patchName))
return;
// ask for confirmation
if (!checkConfirm(msg, patchName, selFiles, !Git::optAmend))
return;
// ok, let's go
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
EM_PROCESS_EVENTS; // to close message box
bool ok;
if (git->isStGITStack())
ok = git->stgCommit(selFiles, msg, patchName, !Git::optFold);
else
ok = git->commitFiles(selFiles, msg, !Git::optAmend);
lastMsgBeforeError = (ok ? "" : msg);
QApplication::restoreOverrideCursor();
hide();
emit changesCommitted(ok);
close();
}
void CommitImpl::pushButtonAmend_clicked() {
QStringList selFiles; // retrieve selected files
getFiles(selFiles);
// FIXME: If there are no files AND no changes to message, we should not
// commit. Disabling the commit button in such case might be preferable.
QString msg(textEditMsg->toPlainText());
if (msg == origMsg && selFiles.isEmpty()) {
warnNoFiles();
return;
}
if (msg == origMsg && git->isStGITStack())
msg = "";
else if (!checkMsg(msg))
// We are going to replace the message, so it better isn't empty
return;
// ask for confirmation
// FIXME: We don't need patch name for refresh, do we?
if (!checkConfirm(msg, "", selFiles, Git::optAmend))
return;
// ok, let's go
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
EM_PROCESS_EVENTS; // to close message box
bool ok;
if (git->isStGITStack())
ok = git->stgCommit(selFiles, msg, "", Git::optFold);
else
ok = git->commitFiles(selFiles, msg, Git::optAmend);
QApplication::restoreOverrideCursor();
hide();
emit changesCommitted(ok);
close();
}
void CommitImpl::pushButtonUpdateCache_clicked() {
QStringList selFiles;
if (!checkFiles(selFiles))
return;
bool ok = git->updateIndex(selFiles);
QApplication::restoreOverrideCursor();
emit changesCommitted(ok);
close();
}
void CommitImpl::textEditMsg_cursorPositionChanged() {
int col_pos, line_pos;
computePosition(col_pos, line_pos);
QString lineNumber = QString("Line: %1 Col: %2")
.arg(line_pos + 1).arg(col_pos + 1);
textLabelLineCol->setText(lineNumber);
}
void CommitImpl::computePosition(int &col_pos, int &line_pos) {
QRect r = textEditMsg->cursorRect();
int vs = textEditMsg->verticalScrollBar()->value();
int hs = textEditMsg->horizontalScrollBar()->value();
// when in start position r.x() = -r.width() / 2
col_pos = (r.x() + hs + r.width() / 2) / ofsX;
line_pos = (r.y() + vs) / ofsY;
}