1#include <WipWidget.h>
2#include <ui_CommitChangesWidget.h>
3
4#include <FileWidget.h>
5#include <GitBase.h>
6#include <GitCache.h>
7#include <GitConfig.h>
8#include <GitHistory.h>
9#include <GitLocal.h>
10#include <GitQlientRole.h>
11#include <GitQlientStyles.h>
12#include <GitRepoLoader.h>
13#include <GitWip.h>
14#include <UnstagedMenu.h>
15
16#include <QMessageBox>
17
18#include <QLogger.h>
19
20using namespace QLogger;
21
22WipWidget::WipWidget(const QSharedPointer<GitCache> &cache, const QSharedPointer<GitBase> &git, QWidget *parent)
23 : CommitChangesWidget(cache, git, parent)
24{
25 mCurrentSha = CommitInfo::ZERO_SHA;
26}
27
28void WipWidget::configure(const QString &sha)
29{
30 const auto commit = mCache->commitInfo(sha);
31
32 QScopedPointer<GitWip> git(new GitWip(mGit, mCache));
33 git->updateWip();
34
35 const auto files = mCache->revisionFile(CommitInfo::ZERO_SHA, commit.firstParent());
36
37 QLog_Info("UI", QString("Configuring WIP widget"));
38
39 prepareCache();
40
41 if (files)
42 insertFiles(files.value(), ui->unstagedFilesList);
43
44 clearCache();
45
46 ui->applyActionBtn->setEnabled(ui->stagedFilesList->count());
47}
48
49void WipWidget::commitChanges()
50{
51 QString msg;
52 QStringList selFiles = getFiles();
53
54 if (!selFiles.isEmpty())
55 {
56 if (hasConflicts())
57 QMessageBox::warning(this, tr("Impossible to commit"),
58 tr("There are files with conflicts. Please, resolve "
59 "the conflicts first."));
60 else if (checkMsg(msg))
61 {
62 const auto revInfo = mCache->commitInfo(CommitInfo::ZERO_SHA);
63
64 QScopedPointer<GitWip> git(new GitWip(mGit, mCache));
65 git->updateWip();
66
67 if (const auto files = mCache->revisionFile(CommitInfo::ZERO_SHA, revInfo.firstParent()); files)
68 {
69 const auto lastShaBeforeCommit = mGit->getLastCommit().output.trimmed();
70 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
71 QScopedPointer<GitLocal> gitLocal(new GitLocal(mGit));
72 const auto ret = gitLocal->commitFiles(selFiles, files.value(), msg);
73 QApplication::restoreOverrideCursor();
74
75 if (ret.success)
76 {
77 // Adding new commit in the log
78 const auto currentSha = mGit->getLastCommit().output.trimmed();
79 QScopedPointer<GitConfig> gitConfig(new GitConfig(mGit));
80 auto committer = gitConfig->getLocalUserInfo();
81
82 if (committer.mUserEmail.isEmpty() || committer.mUserName.isEmpty())
83 committer = gitConfig->getGlobalUserInfo();
84
85 const auto message = msg.split("\n\n");
86
87 CommitInfo newCommit { currentSha,
88 { lastShaBeforeCommit },
89 std::chrono::seconds(QDateTime::currentDateTime().toSecsSinceEpoch()),
90 ui->leCommitTitle->text() };
91
92 newCommit.committer = QString("%1<%2>").arg(committer.mUserName, committer.mUserEmail);
93 newCommit.author = QString("%1<%2>").arg(committer.mUserName, committer.mUserEmail);
94 newCommit.longLog = ui->teDescription->toPlainText();
95
96 mCache->insertCommit(newCommit);
97 mCache->deleteReference(lastShaBeforeCommit, References::Type::LocalBranch, mGit->getCurrentBranch());
98 mCache->insertReference(currentSha, References::Type::LocalBranch, mGit->getCurrentBranch());
99
100 QScopedPointer<GitHistory> gitHistory(new GitHistory(mGit));
101 const auto ret = gitHistory->getDiffFiles(currentSha, lastShaBeforeCommit);
102
103 mCache->insertRevisionFiles(currentSha, lastShaBeforeCommit, RevisionFiles(ret.output));
104
105 prepareCache();
106 clearCache();
107
108 ui->stagedFilesList->clear();
109 ui->stagedFilesList->update();
110
111 ui->leCommitTitle->clear();
112 ui->teDescription->clear();
113
114 git->updateWip();
115
116 emit mCache->signalCacheUpdated();
117 emit changesCommitted();
118 }
119 else
120 {
121 QMessageBox msgBox(QMessageBox::Critical, tr("Error when commiting"),
122 tr("There were problems during the commit "
123 "operation. Please, see the detailed "
124 "description for more information."),
125 QMessageBox::Ok, this);
126 msgBox.setDetailedText(ret.output);
127 msgBox.setStyleSheet(GitQlientStyles::getStyles());
128 msgBox.exec();
129 }
130
131 lastMsgBeforeError = (ret.success ? "" : msg);
132 }
133 }
134 }
135}
136