Commit c7bd9acb authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Fix branch change not reflected if projectbase != dotGit



Branch change isn't reflected in the UI if project base dir is different
from dot git path.
Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent beb9f5a5
......@@ -21,6 +21,29 @@ 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;
git.setProgram(QStringLiteral("git"));
git.setWorkingDirectory(repo);
git.setArguments({QStringLiteral("rev-parse"), QStringLiteral("--absolute-git-dir")});
git.start(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
......
......@@ -50,6 +50,15 @@ 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
*/
......
......@@ -151,16 +151,20 @@ void KateProjectView::showFileGitHistory(const QString &file)
void KateProjectView::checkAndRefreshGit()
{
if (GitUtils::isGitRepo(m_project->baseDir())) {
m_branchBtn->setHidden(false);
m_branchBtn->setText(GitUtils::getCurrentBranchName(m_project->baseDir()));
if (m_branchChangedWatcher.files().isEmpty()) {
m_branchChangedWatcher.addPath(m_project->baseDir() + QStringLiteral("/.git/HEAD"));
}
} else {
const auto dotGitPath = GitUtils::getDotGitPath(m_project->baseDir());
/**
* Not in a git repo or git was removed
*/
if (!dotGitPath.has_value()) {
if (!m_branchChangedWatcher.files().isEmpty()) {
m_branchChangedWatcher.removePaths(m_branchChangedWatcher.files());
}
m_branchBtn->setHidden(true);
} else {
m_branchBtn->setHidden(false);
m_branchBtn->setText(GitUtils::getCurrentBranchName(dotGitPath.value()));
if (m_branchChangedWatcher.files().isEmpty()) {
m_branchChangedWatcher.addPath(dotGitPath.value() + QStringLiteral(".git/HEAD"));
}
}
}
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