Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
Kate
Commits
223bade4
Commit
223bade4
authored
Sep 05, 2022
by
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
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
addons/compiler-explorer/compiledbreader.cpp
View file @
223bade4
...
...
@@ -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
(
'/'
)))
{
...
...
addons/git-blame/commitfilesview.cpp
View file @
223bade4
...
...
@@ -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
=
get
DotGit
Path
(
path
);
auto
value
=
get
RepoBase
Path
(
path
);
if
(
value
.
has_value
())
{
m_gitDir
=
value
.
value
();
}
...
...
addons/project/git/gitutils.cpp
View file @
223bade4
...
...
@@ -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
...
...
addons/project/git/gitutils.h
View file @
223bade4
...
...
@@ -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
*/
...
...
addons/project/gitwidget.cpp
View file @
223bade4
...
...
@@ -362,7 +362,7 @@ GitWidget::~GitWidget()
void
GitWidget
::
setDotGitPath
()
{
const
auto
dotGitPath
=
GitUtils
::
getDotGit
Path
(
m_project
->
baseDir
());
const
auto
dotGitPath
=
getRepoBase
Path
(
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
);
...
...
addons/project/kateprojectview.cpp
View file @
223bade4
...
...
@@ -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
::
getDotGit
Path
(
QFileInfo
(
file
).
absolutePath
());
const
auto
dotGitPath
=
getRepoBase
Path
(
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
::
getDotGit
Path
(
m_project
->
baseDir
());
const
auto
dotGitPath
=
getRepoBase
Path
(
m_project
->
baseDir
());
/**
* Not in a git repo or git was removed
*/
...
...
apps/lib/gitprocess.cpp
View file @
223bade4
...
...
@@ -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
;
}
apps/lib/gitprocess.h
View file @
223bade4
...
...
@@ -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
);
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment