1#include <AmendWidget.h>
2#include <ui_CommitChangesWidget.h>
3
4#include <GitBase.h>
5#include <GitCache.h>
6#include <GitHistory.h>
7#include <GitLocal.h>
8#include <GitQlientRole.h>
9#include <GitQlientStyles.h>
10#include <GitWip.h>
11#include <UnstagedMenu.h>
12
13#include <QMessageBox>
14
15#include <QLogger.h>
16
17using namespace QLogger;
18
19AmendWidget::AmendWidget(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git, QWidget *parent)
20 : CommitChangesWidget(cache, git, parent)
21{
22 ui->applyActionBtn->setText(tr("Amend"));
23}
24
25void AmendWidget::configure(const QString &sha)
26{
27 const auto commit = mCache->commitInfo(sha);
28
29 ui->amendFrame->setVisible(true);
30 ui->warningButton->setVisible(true);
31
32 if (commit.parentsCount() <= 0)
33 return;
34
35 QScopedPointer<GitWip> git(new GitWip(mGit, mCache));
36 git->updateWip();
37
38 const auto files = mCache->revisionFile(CommitInfo::ZERO_SHA, sha);
39 auto amendFiles = mCache->revisionFile(sha, commit.firstParent());
40
41 if (!amendFiles)
42 {
43 QScopedPointer<GitHistory> git(new GitHistory(mGit));
44 const auto ret = git->getDiffFiles(mCurrentSha, commit.firstParent());
45
46 if (ret.success)
47 {
48 amendFiles = RevisionFiles(ret.output);
49 mCache->insertRevisionFiles(mCurrentSha, commit.firstParent(), amendFiles.value());
50 }
51 }
52
53 if (mCurrentSha != sha)
54 {
55 QLog_Info("UI", QString("Amending sha {%1}.").arg(mCurrentSha));
56
57 mCurrentSha = sha;
58
59 const auto author = commit.author.split("<");
60 ui->leAuthorName->setText(author.first());
61 ui->leAuthorEmail->setText(author.last().mid(0, author.last().count() - 1));
62 ui->teDescription->setPlainText(commit.longLog.trimmed());
63 ui->leCommitTitle->setText(commit.shortLog);
64
65 blockSignals(true);
66 ui->unstagedFilesList->clear();
67 ui->stagedFilesList->clear();
68 mInternalCache.clear();
69 blockSignals(false);
70
71 if (files)
72 insertFiles(files.value(), ui->unstagedFilesList);
73
74 if (amendFiles)
75 insertFiles(amendFiles.value(), ui->stagedFilesList);
76 }
77 else
78 {
79 QLog_Info("UI", QString("Updating files for SHA {%1}").arg(mCurrentSha));
80
81 prepareCache();
82
83 if (files)
84 insertFiles(files.value(), ui->unstagedFilesList);
85
86 clearCache();
87
88 if (amendFiles)
89 insertFiles(amendFiles.value(), ui->stagedFilesList);
90 }
91
92 ui->applyActionBtn->setEnabled(ui->stagedFilesList->count());
93}
94
95void AmendWidget::commitChanges()
96{
97 QStringList selFiles = getFiles();
98
99 if (!selFiles.isEmpty())
100 {
101 QString msg;
102
103 if (hasConflicts())
104 {
105 QMessageBox::critical(this, tr("Impossible to commit"),
106 tr("There are files with conflicts. Please, resolve the conflicts first."));
107 }
108 else if (checkMsg(msg))
109 {
110 QScopedPointer<GitWip> git(new GitWip(mGit, mCache));
111 git->updateWip();
112
113 const auto files = mCache->revisionFile(CommitInfo::ZERO_SHA, mCurrentSha);
114
115 if (files)
116 {
117 const auto author = QString("%1<%2>").arg(ui->leAuthorName->text(), ui->leAuthorEmail->text());
118 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
119
120 QScopedPointer<GitLocal> gitLocal(new GitLocal(mGit));
121 const auto ret = gitLocal->ammendCommit(selFiles, files.value(), msg, author);
122 QApplication::restoreOverrideCursor();
123
124 emit logReload();
125
126 if (ret.success)
127 {
128 const auto newSha = mGit->getLastCommit().output.trimmed();
129 auto commit = mCache->commitInfo(mCurrentSha);
130 const auto oldSha = commit.sha;
131 commit.sha = newSha;
132 commit.committer = author;
133 commit.author = author;
134
135 const auto log = msg.split("\n\n");
136 commit.shortLog = log.constFirst();
137 commit.longLog = log.constLast();
138
139 mCache->updateCommit(oldSha, std::move(commit));
140
141 QScopedPointer<GitHistory> git(new GitHistory(mGit));
142 const auto ret = git->getDiffFiles(mCurrentSha, commit.firstParent());
143
144 mCache->insertRevisionFiles(mCurrentSha, commit.firstParent(), RevisionFiles(ret.output));
145
146 emit changesCommitted();
147 }
148 else
149 {
150 QMessageBox msgBox(QMessageBox::Critical, tr("Error when amending"),
151 tr("There were problems during the commit "
152 "operation. Please, see the detailed "
153 "description for more information."),
154 QMessageBox::Ok, this);
155 msgBox.setDetailedText(ret.output);
156 msgBox.setStyleSheet(GitQlientStyles::getStyles());
157 msgBox.exec();
158 }
159 }
160 }
161 }
162}
163