Commit 223bade4 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Use a common method to get git-repo base path

Fixes fileHistory not able to show history for files in submodules
parent 9579f1f7
Pipeline #227670 passed with stage
in 33 minutes and 53 seconds
......@@ -16,30 +16,6 @@
#include <optional>
// TODO: copied from kate project plugin, move to shared/
std::optional<QString> getDotGitPath(const QString &repo)
{
/* This call is intentionally blocking because we need git path for everything else */
QProcess git;
if (!setupGitProcess(git, repo, {QStringLiteral("rev-parse"), QStringLiteral("--absolute-git-dir")})) {
return std::nullopt;
}
startHostProcess(git, QProcess::ReadOnly);
if (git.waitForStarted() && git.waitForFinished(-1)) {
if (git.exitStatus() != QProcess::NormalExit || git.exitCode() != 0) {
return std::nullopt;
}
QString dotGitPath = QString::fromUtf8(git.readAllStandardOutput());
if (dotGitPath.endsWith(QLatin1String("\n"))) {
dotGitPath.remove(QLatin1String(".git\n"));
} else {
dotGitPath.remove(QLatin1String(".git"));
}
return dotGitPath;
}
return std::nullopt;
}
QString CompileDBReader::locateCompileCommands(KTextEditor::MainWindow *mw, const QString &openedFile)
{
Q_ASSERT(mw);
......@@ -61,8 +37,7 @@ QString CompileDBReader::locateCompileCommands(KTextEditor::MainWindow *mw, cons
// For now it only checks for compile_commands in the .git/../ directory
QFileInfo fi(openedFile);
if (fi.exists()) {
QString base = fi.absolutePath();
auto basePathOptional = getDotGitPath(base);
auto basePathOptional = getRepoBasePath(fi.absolutePath());
if (basePathOptional.has_value()) {
auto basePath = basePathOptional.value();
if (basePath.endsWith(QLatin1Char('/'))) {
......
......@@ -276,36 +276,6 @@ static std::optional<QString> getGitCmdOutput(const QString &workDir, const QStr
return {};
}
static std::optional<QString> getDotGitPath(const QString &repo)
{
/* This call is intentionally blocking because we need git path for everything else */
auto dotGitPathRes = getGitCmdOutput(repo, {QStringLiteral("rev-parse"), QStringLiteral("--absolute-git-dir")});
if (!dotGitPathRes.has_value()) {
return {};
}
QString dotGitPath = dotGitPathRes.value();
if (dotGitPath.endsWith(QLatin1String(".git"))) {
dotGitPath.remove(QLatin1String(".git"));
} else if (dotGitPath.contains(QLatin1String(".git/modules"))) {
// maybe this is some submodule
auto topLevelRes = getGitCmdOutput(repo, {QStringLiteral("rev-parse"), QStringLiteral("--show-toplevel")});
if (!topLevelRes.has_value()) {
return {};
}
QString topLevel = QDir::cleanPath(topLevelRes.value());
if (!topLevel.endsWith(QLatin1Char('/'))) {
topLevel += QStringLiteral("/");
}
return topLevel;
} else {
qWarning() << "[blame] Got invalid dot git path: " << dotGitPath;
return {};
}
return dotGitPath;
return std::nullopt;
}
static bool getNum(const QByteArray &numBytes, int *num)
{
bool res = false;
......@@ -393,7 +363,7 @@ void CommitDiffTreeView::createFileTreeForCommit(const QString &filePath, const
{
QFileInfo fi(filePath);
QString path = fi.absolutePath();
auto value = getDotGitPath(path);
auto value = getRepoBasePath(path);
if (value.has_value()) {
m_gitDir = value.value();
}
......
......@@ -26,30 +26,6 @@ bool GitUtils::isGitRepo(const QString &repo)
return false;
}
std::optional<QString> GitUtils::getDotGitPath(const QString &repo)
{
/* This call is intentionally blocking because we need git path for everything else */
QProcess git;
if (!setupGitProcess(git, repo, {QStringLiteral("rev-parse"), QStringLiteral("--absolute-git-dir")})) {
return std::nullopt;
}
startHostProcess(git, QProcess::ReadOnly);
if (git.waitForStarted() && git.waitForFinished(-1)) {
if (git.exitStatus() != QProcess::NormalExit || git.exitCode() != 0) {
return std::nullopt;
}
QString dotGitPath = QString::fromUtf8(git.readAllStandardOutput());
if (dotGitPath.endsWith(QLatin1String("\n"))) {
dotGitPath.remove(QLatin1String(".git\n"));
} else {
dotGitPath.remove(QLatin1String(".git"));
}
return dotGitPath;
}
return std::nullopt;
}
QString GitUtils::getCurrentBranchName(const QString &repo)
{
// clang-format off
......
......@@ -55,15 +55,6 @@ struct StatusEntry {
*/
bool isGitRepo(const QString &repo);
/**
* @brief get the .git folder path
* Returns the path without the .git in the string e.g:
* ~/projects/kate/ instead of ~/projects/kate/.git
*
* Can be used to check whether you are in a git repo as well
*/
std::optional<QString> getDotGitPath(const QString &repo);
/**
* @brief get name of current branch in @p repo
*/
......
......@@ -362,7 +362,7 @@ GitWidget::~GitWidget()
void GitWidget::setDotGitPath()
{
const auto dotGitPath = GitUtils::getDotGitPath(m_project->baseDir());
const auto dotGitPath = getRepoBasePath(m_project->baseDir());
if (!dotGitPath.has_value()) {
QTimer::singleShot(1, this, [this] {
sendMessage(i18n("Failed to find .git directory for '%1', things may not work correctly", m_project->baseDir()), false);
......
......@@ -10,6 +10,7 @@
#include "diffparams.h"
#include "filehistorywidget.h"
#include "git/gitutils.h"
#include "gitprocess.h"
#include "gitwidget.h"
#include "kateprojectfiltermodel.h"
#include "kateprojectpluginview.h"
......@@ -150,7 +151,7 @@ void KateProjectView::setTreeViewAsCurrent()
void KateProjectView::showFileGitHistory(const QString &file)
{
// create on demand and on switch back delete
const auto dotGitPath = GitUtils::getDotGitPath(QFileInfo(file).absolutePath());
const auto dotGitPath = getRepoBasePath(QFileInfo(file).absolutePath());
if (!dotGitPath.has_value()) {
// TODO: Show message in output
return;
......@@ -178,7 +179,7 @@ void KateProjectView::showFileGitHistory(const QString &file)
void KateProjectView::checkAndRefreshGit()
{
const auto dotGitPath = GitUtils::getDotGitPath(m_project->baseDir());
const auto dotGitPath = getRepoBasePath(m_project->baseDir());
/**
* Not in a git repo or git was removed
*/
......
......@@ -74,3 +74,25 @@ std::pair<int, int> getGitVersion(const QString &workingDir)
static const auto cachedVersion = getGitVersionUncached(workingDir);
return cachedVersion;
}
std::optional<QString> getRepoBasePath(const QString &repo)
{
/* This call is intentionally blocking because we need git path for everything else */
QProcess git;
if (!setupGitProcess(git, repo, {QStringLiteral("rev-parse"), QStringLiteral("--show-toplevel")})) {
return std::nullopt;
}
startHostProcess(git, QProcess::ReadOnly);
if (git.waitForStarted() && git.waitForFinished(-1)) {
if (git.exitStatus() != QProcess::NormalExit || git.exitCode() != 0) {
return std::nullopt;
}
QString dotGitPath = QString::fromUtf8(git.readAllStandardOutput().trimmed());
if (!dotGitPath.endsWith(QLatin1Char('/'))) {
dotGitPath.append(QLatin1Char('/'));
}
return dotGitPath;
}
return std::nullopt;
}
......@@ -13,6 +13,8 @@
#include "hostprocess.h"
#include "kateprivate_export.h"
#include <optional>
/**
* small helper function to setup a QProcess based "git" command.
* you pass working directory & arguments
......@@ -32,3 +34,10 @@ KATE_PRIVATE_EXPORT bool setupGitProcess(QProcess &process, const QString &worki
* @return git major and minor version as pair, -1,-1 if infeasible to determine
*/
KATE_PRIVATE_EXPORT std::pair<int, int> getGitVersion(const QString &workingDir);
/**
* @brief get the git repo base path
* Returned path has a `/` at the end
* @param workingDir the dir where
*/
KATE_PRIVATE_EXPORT std::optional<QString> getRepoBasePath(const QString &workingDir);
Supports Markdown
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