Commit 83f38e9a authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Only show local/remote branches, no tags

parent c590fc4e
......@@ -90,16 +90,16 @@ public:
kfts::to_scored_fuzzy_matched_display_string(m_filterString, name, QStringLiteral("<b style=\"color:%1;\">").arg(nameColor), QStringLiteral("</b>"));
auto type = (GitUtils::RefType)index.data(BranchesDialogModel::RefType).toInt();
auto commit = index.data(BranchesDialogModel::Commit).toString();
using RefType = GitUtils::RefType;
const auto fontSz = option.font.pointSize();
if (type == RefType::Head) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">at %2</span>").arg(fontSz).arg(commit));
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">local</span>").arg(fontSz));
} else if (type == RefType::Remote) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">remote at %2</span>").arg(fontSz).arg(commit));
} else if (type == RefType::Tag) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">tag at %2</span>").arg(fontSz).arg(commit));
} else {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">remote</span>").arg(fontSz));
} /*else if (type == RefType::Tag) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">tag at %2</span>").arg(fontSz));
} */
else {
Q_ASSERT(false);
}
......
......@@ -45,8 +45,6 @@ QVariant BranchesDialogModel::data(const QModelIndex &idx, int role) const
} else if (role == Qt::DecorationRole) {
static const auto branchIcon = QIcon(QStringLiteral(":/kxmlgui5/kateproject/sc-apps-git.svg"));
return branchIcon;
} else if (role == Role::Commit) {
return branch.commit.mid(0, 7);
} else if (role == Role::CheckoutName) {
return branch.type == GitUtils::RefType::Remote ? branch.name.mid(branch.remote.size() + 1) : branch.name;
} else if (role == Role::RefType) {
......
......@@ -18,7 +18,7 @@ class BranchesDialogModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Role { Score = Qt::UserRole + 1, DisplayName, Commit, CheckoutName, RefType };
enum Role { Score = Qt::UserRole + 1, DisplayName, CheckoutName, RefType };
explicit BranchesDialogModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent) const override;
......
......@@ -36,21 +36,21 @@ int GitUtils::checkoutBranch(const QString &repo, const QString &branch)
return -1;
}
QVector<GitUtils::Branch> GitUtils::getAllBranches(const QString &repo, RefType ref)
QVector<GitUtils::Branch> GitUtils::getAllBranchesAndTags(const QString &repo, RefType ref)
{
// git for-each-ref --format '%(refname) %(objectname) %(*objectname)'
QProcess git;
git.setWorkingDirectory(repo);
QStringList args{QStringLiteral("for-each-ref"), QStringLiteral("--format"), QStringLiteral("%(refname) %(objectname) %(*objectname)")};
QStringList args{QStringLiteral("for-each-ref"), QStringLiteral("--format"), QStringLiteral("%(refname)")};
git.start(QStringLiteral("git"), args);
QVector<Branch> branches;
if (git.waitForStarted() && git.waitForFinished(-1)) {
QString gitout = QString::fromUtf8(git.readAllStandardOutput());
QVector<QStringRef> out = gitout.splitRef(QLatin1Char('\n'));
static const QRegularExpression headRe(QStringLiteral("^refs/heads/([^ ]+) ([0-9a-f]{40}) ([0-9a-f]{40})?$"));
static const QRegularExpression remoteRe(QStringLiteral("^refs/remotes/([^/]+)/([^ ]+) ([0-9a-f]{40}) ([0-9a-f]{40})?$"));
static const QRegularExpression tagRe(QStringLiteral("^refs/tags/([^ ]+) ([0-9a-f]{40}) ([0-9a-f]{40})?$"));
static const QRegularExpression headRe(QStringLiteral("^refs/heads/([^ ]+)$"));
static const QRegularExpression remoteRe(QStringLiteral("^refs/remotes/([^/]+)/([^ ]+)"));
static const QRegularExpression tagRe(QStringLiteral("^refs/tags/([^ ]+)$"));
branches.reserve(out.size());
QRegularExpressionMatch m;
......@@ -59,19 +59,16 @@ QVector<GitUtils::Branch> GitUtils::getAllBranches(const QString &repo, RefType
if (ref & Head && (m = headRe.match(o)).hasMatch()) {
branches.append({m.captured(1),
QString(), // no remote
m.captured(2),
RefType::Head,
-1});
} else if (ref & Remote && (m = remoteRe.match(o)).hasMatch()) {
branches.append({m.captured(1).append(QLatin1Char('/') + m.captured(2)),
m.captured(1),
m.captured(3),
RefType::Remote,
-1});
} else if (ref & Tag && (m = tagRe.match(o)).hasMatch()) {
branches.append({m.captured(1),
QString(), // no remote
m.captured(3).isEmpty() ? QString() : m.captured(2),
RefType::Tag,
-1});
}
......@@ -81,3 +78,8 @@ QVector<GitUtils::Branch> GitUtils::getAllBranches(const QString &repo, RefType
return branches;
}
QVector<GitUtils::Branch> GitUtils::getAllBranches(const QString &repo)
{
return getAllBranchesAndTags(repo, static_cast<RefType>(RefType::Head | RefType::Remote));
}
......@@ -9,19 +9,16 @@
namespace GitUtils
{
// clang-format off
enum RefType {
Head = 0x1,
Head = 0x1,
Remote = 0x2,
Tag = 0x4,
All = 0x7
Tag = 0x4,
All = 0x7,
};
// clang-format on
struct Branch {
QString name;
QString remote;
QString commit;
RefType type;
int score; // used for scoring when filtering
};
......@@ -32,7 +29,14 @@ QString getCurrentBranchName(const QString &repo);
int checkoutBranch(const QString &repo, const QString &branch);
QVector<Branch> getAllBranches(const QString &repo, RefType ref = RefType::All);
/**
* @brief get all local and remote branches
*/
QVector<Branch> getAllBranches(const QString &repo);
/**
* @brief get all local and remote branches + tags
*/
QVector<Branch> getAllBranchesAndTags(const QString &repo, RefType ref = RefType::All);
}
#endif // GITUTILS_H
......@@ -14,9 +14,9 @@
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
#include <KActionCollection>
#include <KLineEdit>
#include <KLocalizedString>
#include <KActionCollection>
#include <QPushButton>
#include <QSortFilterProxyModel>
......@@ -48,13 +48,12 @@ KateProjectView::KateProjectView(KateProjectPluginView *pluginView, KateProject
setFocusProxy(m_treeView);
// add to actionCollection so that this is available in Kate Command bar
auto chckbr = pluginView->actionCollection()->addAction(QStringLiteral("checkout_branch"), this, [this]{
auto chckbr = pluginView->actionCollection()->addAction(QStringLiteral("checkout_branch"), this, [this] {
m_branchBtn->click();
});
chckbr->setText(QStringLiteral("Checkout Branch"));
m_branchesDialog = new BranchesDialog(this, mainWindow, m_project->baseDir());
/**
* setup filter line edit
*/
......
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