Skip to content

Commit bf80e96

Browse files
committed
GitCommit: delete untracked files
1 parent 6832486 commit bf80e96

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

src/plugins/git/CommitForm.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <QAbstractListModel>
22
#include <QAbstractTableModel>
3+
#include <QDir>
34
#include <QKeyEvent>
45
#include <QMessageBox>
56
#include <QTemporaryFile>
@@ -413,6 +414,7 @@ void CommitForm::newFileSelected(const QString &filename, GitFileStatus status)
413414
case GitFileStatus::Modified: {
414415
ui->diffLoading->start();
415416
ui->diffLabel->setText("git diff");
417+
ui->revertCurrentButton->setText(tr("Revert current"));
416418
git->runGit({"-C", repoRoot, "diff", filename})
417419
.then(this, [this, updateEditor](const std::tuple<QString, int> &res) {
418420
auto [output2, exitCode] = res;
@@ -429,6 +431,7 @@ void CommitForm::newFileSelected(const QString &filename, GitFileStatus status)
429431
}
430432
case GitFileStatus::Deleted:
431433
ui->diffLabel->setText(tr("Deleted"));
434+
ui->revertCurrentButton->setText(tr("Revert current"));
432435
updateEditor("", highlighter);
433436
break;
434437
case GitFileStatus::Added:
@@ -467,6 +470,7 @@ void CommitForm::newFileSelected(const QString &filename, GitFileStatus status)
467470
if (langInfo.isValid()) {
468471
highlighter = langInfo.id;
469472
}
473+
ui->revertCurrentButton->setText(tr("Delete"));
470474
ui->diffLabel->setText(tr("Content"));
471475
updateEditor(output, highlighter);
472476
break;
@@ -480,10 +484,18 @@ void CommitForm::revertCurrentImpl() {
480484
auto selected = ui->tableView->currentIndex();
481485
auto idx = model->index(selected.row(), 2);
482486
auto fileName = model->data(idx, Qt::DisplayRole).toString();
487+
auto status =
488+
static_cast<GitFileStatus>(model->data(idx, GitStatusTableModel::StatusRole).toInt());
483489

484490
auto msgBox = QMessageBox();
485491
msgBox.setWindowTitle("Revert file");
486-
msgBox.setText(tr("Are you sure you want to revert this file?<br><br><b>%1</b>").arg(fileName));
492+
if (status == GitFileStatus::Modified) {
493+
msgBox.setText(
494+
tr("Are you sure you want to revert this file?<br><br><b>%1</b>").arg(fileName));
495+
} else {
496+
msgBox.setText(
497+
tr("Are you sure you want to delete this file?<br><br><b>%1</b>").arg(fileName));
498+
}
487499
msgBox.setTextFormat(Qt::RichText);
488500
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
489501
msgBox.setDefaultButton(QMessageBox::No);
@@ -493,14 +505,21 @@ void CommitForm::revertCurrentImpl() {
493505
return;
494506
}
495507

496-
auto args = QStringList{"-C", repoRoot, "checkout", fileName};
497-
git->runGit(args).then(this, [this](const std::tuple<QString, int> &res) {
498-
auto [output, exitCode] = res;
499-
ui->gitOutput->setText(output.trimmed());
500-
if (exitCode == 0) {
501-
updateGitStatus();
508+
if (status == GitFileStatus::Modified) {
509+
auto args = QStringList{"-C", repoRoot, "checkout", fileName};
510+
git->runGit(args).then(this, [this](const std::tuple<QString, int> &res) {
511+
auto [output, exitCode] = res;
512+
ui->gitOutput->setText(output.trimmed());
513+
});
514+
} else {
515+
auto fullPath = QDir(repoRoot).filePath(fileName);
516+
if (QFile::remove(fullPath)) {
517+
ui->gitOutput->setText(tr("Deleted %1").arg(fileName));
518+
} else {
519+
ui->gitOutput->setText(tr("Failed deleting %1").arg(fileName));
502520
}
503-
});
521+
}
522+
updateGitStatus();
504523
}
505524

506525
void CommitForm::revertSelectionImpl() {

0 commit comments

Comments
 (0)