Commit 0c9ff4de authored by Kevin Funk's avatar Kevin Funk
Browse files

Feature "Diff From Branch" on BranchManager

Original patch by Carlos Nihelton, thanks! I performed some code cleanups.

Summary:
I was annoyed by the fact that I couldn't create diffs in KDevelop (using git plugin at least) after having committed the changes to any branch. So I added a button on BranchManager UI to allow creation of diffs between the current branch and any branch selected in the list view. In current implementation it uses the command line "git diff --no-color --no-ext-diff --cached <branch> -- <path>", thus, it includes the staging area. If it's clean, it differs from the last commit.

This diff was generated using this feature.

Reviewers: brauch, kfunk, apol

Reviewed By: brauch, kfunk

Subscribers: brauch, kdevelop-devel

Differential Revision: https://phabricator.kde.org/D3073
parent 872ac116
......@@ -366,7 +366,9 @@ VcsJob* GitPlugin::diff(const QUrl& fileOrDirectory, const KDevelop::VcsRevision
&& srcRevision.specialType()==VcsRevision::Base
&& dstRevision.specialType()==VcsRevision::Working)
*job << "HEAD";
else {
else if(dstRevision.specialType()==VcsRevision::Working){
*job << "--cached" << srcRevision.revisionValue().toString();
} else {
QString revstr = revisionInterval(srcRevision, dstRevision);
if(!revstr.isEmpty())
*job << revstr;
......
......@@ -32,6 +32,7 @@
#include <vcs/models/brancheslistmodel.h>
#include "ui_branchmanager.h"
#include "../../debug.h"
#include "widgets/vcsdiffpatchsources.h"
#include <interfaces/icore.h>
#include <interfaces/iruncontroller.h>
......@@ -39,6 +40,7 @@
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <KParts/MainWindow>
using namespace KDevelop;
......@@ -88,6 +90,8 @@ BranchManager::BranchManager(const QString& repository, KDevelop::DistributedVer
m_ui->mergeButton->setIcon(QIcon::fromTheme(QStringLiteral("merge")));
connect(m_ui->mergeButton, &QPushButton::clicked, this, &BranchManager::mergeBranch);
m_ui->diffButton->setIcon(QIcon::fromTheme(QStringLiteral("text-x-patch")));
connect(m_ui->diffButton, &QPushButton::clicked, this, &BranchManager::diffFromBranch);
}
BranchManager::~BranchManager()
......@@ -189,3 +193,48 @@ void BranchManager::mergeBranch()
i18n("You must select a branch to merge into current one from the list."));
}
}
void BranchManager::diffFromBranch()
{
const auto dest = m_model->currentBranch();
const auto src = m_ui->branchView->currentIndex().data().toString();
if (src == dest) {
KMessageBox::messageBox(this, KMessageBox::Information, i18n("Already on branch \"%1\"\n", src));
return;
}
VcsRevision srcRev;
srcRev.setRevisionValue(src, KDevelop::VcsRevision::GlobalNumber);
// We have two options here:
// * create a regular VcsRevision to represent the last commit on the current branch or
// * create a special branch to reflect the staging area. I choosed this one.
// If the staing area is clean it automatically defaults to the first option.
const auto destRev = VcsRevision::createSpecialRevision(KDevelop::VcsRevision::Working);
const auto job = m_dvcPlugin->diff(QUrl::fromLocalFile(m_repository), srcRev, destRev);
connect(job, &VcsJob::finished, this, &BranchManager::diffJobFinished);
m_dvcPlugin->core()->runController()->registerJob(job);
}
void BranchManager::diffJobFinished(KJob* job)
{
auto vcsjob = qobject_cast<KDevelop::VcsJob*>(job);
Q_ASSERT(vcsjob);
if (vcsjob->status() != KDevelop::VcsJob::JobSucceeded) {
KMessageBox::error(ICore::self()->uiController()->activeMainWindow(), vcsjob->errorString(),
i18n("Unable to retrieve diff."));
return;
}
auto diff = vcsjob->fetchResults().value<KDevelop::VcsDiff>();
if(diff.isEmpty()){
KMessageBox::information(ICore::self()->uiController()->activeMainWindow(),
i18n("There are no committed differences."),
i18n("VCS support"));
return;
}
auto patch = new VCSDiffPatchSource(diff);
showVcsDiff(patch);
close();
}
......@@ -49,6 +49,8 @@ private slots:
void renameBranch();
void checkoutBranch();
void mergeBranch();
void diffFromBranch();
void diffJobFinished(KJob* job);
private:
QString m_repository;
......
......@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>438</width>
<height>352</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
......@@ -33,6 +33,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameButton">
<property name="text">
<string>Rename</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
......@@ -50,12 +57,31 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="renameButton">
<widget class="QPushButton" name="diffButton">
<property name="toolTip">
<string>&lt;p&gt;Show changes between the &lt;span style=&quot; font-style:italic;&quot;&gt;staging area&lt;/span&gt; (if not clean or last commit on current branch, otherwise) and the last commit on the selected branch.&lt;/p&gt;</string>
</property>
<property name="text">
<string>Rename</string>
<string>Compare to Branch</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>15</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="checkoutButton">
<property name="toolTip">
......@@ -67,7 +93,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="mergeButton">
<widget class="QPushButton" name="mergeButton">
<property name="text">
<string>Merge</string>
</property>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment