diff --git a/git/fileviewgitplugin.h b/git/fileviewgitplugin.h --- a/git/fileviewgitplugin.h +++ b/git/fileviewgitplugin.h @@ -56,6 +56,8 @@ void createTag(); void push(); void pull(); + void log(); + void showDiff(const QUrl &link); void slotOperationCompleted(int exitCode, QProcess::ExitStatus exitStatus); void slotOperationError(); @@ -112,6 +114,7 @@ QAction* m_tagAction; QAction* m_pushAction; QAction* m_pullAction; + QAction* m_logAction; QString m_currentDir; QProcess m_process; diff --git a/git/fileviewgitplugin.cpp b/git/fileviewgitplugin.cpp --- a/git/fileviewgitplugin.cpp +++ b/git/fileviewgitplugin.cpp @@ -39,6 +39,7 @@ #include #include #include +#include K_PLUGIN_FACTORY(FileViewGitPluginFactory, registerPlugin();) @@ -106,6 +107,10 @@ connect(m_pullAction, SIGNAL(triggered()), this, SLOT(pull())); + m_logAction = new QAction(this); + m_logAction->setText(xi18nd("@action:inmenu", "Git Log...")); + connect(m_logAction, &QAction::triggered, this, &FileViewGitPlugin::log); + connect(&m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotOperationCompleted(int, QProcess::ExitStatus))); connect(&m_process, SIGNAL(error(QProcess::ProcessError)), @@ -357,6 +362,9 @@ ++it; } + m_logAction->setEnabled(!m_pendingOperation); + actions.append(m_logAction); + m_showLocalChangesAction->setEnabled(!m_pendingOperation && showChanges); actions.append(m_showLocalChangesAction); @@ -407,6 +415,75 @@ KRun::runCommand(QLatin1String("git difftool --dir-diff ."), nullptr, m_contextDir); } +void FileViewGitPlugin::showDiff(const QUrl &link) +{ + if (link.scheme() != QLatin1String("rev")) { + return; + } + KRun::runCommand(QStringLiteral("git difftool --dir-diff %1^ %1").arg(link.path()), nullptr, m_contextDir); +} + +void FileViewGitPlugin::log() +{ + QProcess process; + process.setWorkingDirectory(m_contextDir); + process.start( + QLatin1String("git"), + QStringList { + QStringLiteral("log"), + QStringLiteral("--relative=."), + QStringLiteral("--date=format:%d-%m-%Y"), + QStringLiteral("-n 100"), + QStringLiteral("--pretty=format: %h %ad %s %an ") + } + ); + + if (!process.waitForFinished() || process.exitCode() != 0) { + emit errorMessage(xi18nd("@info:status", "Git Log failed.")); + return; + } + + const QString gitOutput = process.readAllStandardOutput(); + + QPalette palette; + const QString styleSheet = QStringLiteral( + "body { background: %1; color: %2; }" \ + "table.logtable td { padding: 9px 8px 9px; }" \ + "a { color: %3; }" \ + "a:visited { color: %4; } " + ).arg(palette.background().color().name(), + palette.text().color().name(), + palette.link().color().name(), + palette.linkVisited().color().name()); + + auto view = new QTextBrowser(); + view->setAttribute(Qt::WA_DeleteOnClose); + view->setWindowTitle(xi18nd("@title:window", "Git Log")); + view->setOpenLinks(false); + view->setOpenExternalLinks(false); + connect(view, &QTextBrowser::anchorClicked, this, &FileViewGitPlugin::showDiff); + view->setHtml(QStringLiteral( + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "%7" \ + "
%3 %4 %5

%6
" \ + "" + ).arg(styleSheet, + palette.highlight().color().name(), + i18nc("Git commit hash", "Commit"), + i18nc("Git commit date", "Date"), + i18nc("Git commit message", "Message"), + i18nc("Git commit author", "Author"), + gitOutput)); + + view->resize(QSize(720, 560)); + view->show(); +} + void FileViewGitPlugin::checkout() { CheckoutDialog dialog;