Skip to content
GitLab
Menu
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
9d0615f0
Commit
9d0615f0
authored
Feb 07, 2021
by
Waqar Ahmed
Browse files
Improved git get all branches
parent
d3569a71
Changes
4
Hide whitespace changes
Inline
Side-by-side
addons/project/CMakeLists.txt
View file @
9d0615f0
...
...
@@ -59,10 +59,14 @@ target_sources(
kateprojectconfigpage.cpp
kateprojectcodeanalysistool.cpp
branchesdialog.cpp
tools/kateprojectcodeanalysistoolcppcheck.cpp
tools/kateprojectcodeanalysistoolflake8.cpp
tools/kateprojectcodeanalysistoolshellcheck.cpp
tools/kateprojectcodeanalysisselector.cpp
gitutils.cpp
plugin.qrc
)
...
...
addons/project/branchesdialog.cpp
View file @
9d0615f0
...
...
@@ -181,13 +181,13 @@ BranchesDialog::BranchesDialog(QWidget *parent, KTextEditor::MainWindow *mainWin
void
BranchesDialog
::
openDialog
()
{
const
auto
branches
=
GitUtils
::
getAllBranches
(
m_projectPath
);
const
QVector
<
GitUtils
::
Branch
>
branches
=
GitUtils
::
getAllBranches
(
m_projectPath
);
m_model
->
clear
();
static
const
QIcon
branchIcon
=
QIcon
(
QStringLiteral
(
":/kxmlgui5/kateproject/sc-apps-git.svg"
));
for
(
const
auto
&
branch
:
branches
)
{
m_model
->
appendRow
(
new
QStandardItem
(
branchIcon
,
branch
));
m_model
->
appendRow
(
new
QStandardItem
(
branchIcon
,
branch
.
name
));
}
reselectFirst
();
...
...
addons/project/gitutils.cpp
0 → 100644
View file @
9d0615f0
#include "gitutils.h"
bool
GitUtils
::
isGitRepo
(
const
QString
&
repo
)
{
QProcess
git
;
git
.
setWorkingDirectory
(
repo
);
QStringList
args
{
QStringLiteral
(
"rev-parse"
),
QStringLiteral
(
"--is-inside-work-tree"
)};
git
.
start
(
QStringLiteral
(
"git"
),
args
);
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
return
git
.
readAll
().
trimmed
()
==
"true"
;
}
return
false
;
}
QString
GitUtils
::
getCurrentBranchName
(
const
QString
&
repo
)
{
QProcess
git
;
git
.
setWorkingDirectory
(
repo
);
QStringList
args
{
QStringLiteral
(
"rev-parse"
),
QStringLiteral
(
"--abbrev-ref"
),
QStringLiteral
(
"HEAD"
)};
git
.
start
(
QStringLiteral
(
"git"
),
args
);
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
return
QString
::
fromUtf8
(
git
.
readAllStandardOutput
().
trimmed
());
}
return
QString
();
}
int
GitUtils
::
checkoutBranch
(
const
QString
&
repo
,
const
QString
&
branch
)
{
QProcess
git
;
git
.
setWorkingDirectory
(
repo
);
QStringList
args
{
QStringLiteral
(
"checkout"
),
branch
};
git
.
start
(
QStringLiteral
(
"git"
),
args
);
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
return
git
.
exitCode
();
}
return
-
1
;
}
QVector
<
GitUtils
::
Branch
>
GitUtils
::
getAllBranches
(
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)"
)};
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})?$"
));
branches
.
reserve
(
out
.
size
());
QRegularExpressionMatch
m
;
// clang-format off
for
(
const
auto
&
o
:
out
)
{
if
(
ref
&
Head
&&
(
m
=
headRe
.
match
(
o
)).
hasMatch
())
{
branches
.
append
({
m
.
captured
(
1
),
QString
(),
// no remote
m
.
captured
(
2
),
RefType
::
Head
});
}
else
if
(
ref
&
Remote
&&
(
m
=
remoteRe
.
match
(
o
)).
hasMatch
())
{
branches
.
append
({
m
.
captured
(
1
).
append
(
QLatin1Char
(
'/'
)
+
m
.
captured
(
2
)),
m
.
captured
(
2
),
m
.
captured
(
3
),
RefType
::
Remote
});
}
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
});
}
}
// clang-format on
}
return
branches
;
}
addons/project/gitutils.h
View file @
9d0615f0
...
...
@@ -4,66 +4,34 @@
#include <QDebug>
#include <QDir>
#include <QProcess>
#include <QRegularExpression>
#include <QString>
namespace
GitUtils
{
static
bool
isGitRepo
(
const
QString
&
repo
)
{
QProcess
git
;
git
.
setWorkingDirectory
(
repo
);
QStringList
args
{
QStringLiteral
(
"rev-parse"
),
QStringLiteral
(
"--is-inside-work-tree"
)};
git
.
start
(
QStringLiteral
(
"git"
),
args
);
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
return
git
.
readAll
().
trimmed
()
==
"true"
;
}
return
false
;
}
// clang-format off
enum
RefType
{
Head
=
0x1
,
Remote
=
0x2
,
Tag
=
0x4
,
All
=
0x7
};
// clang-format on
static
QString
getCurrentBranchName
(
const
QString
&
repo
)
{
QProcess
git
;
git
.
setWorkingDirectory
(
repo
);
QStringList
args
{
QStringLiteral
(
"rev-parse"
),
QStringLiteral
(
"--abbrev-ref"
),
QStringLiteral
(
"HEAD"
)};
git
.
start
(
QStringLiteral
(
"git"
),
args
);
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
return
QString
::
fromUtf8
(
git
.
readAllStandardOutput
().
trimmed
());
}
return
QString
();
}
struct
Branch
{
QString
name
;
QString
remote
;
QString
commit
;
RefType
type
;
};
static
int
checkoutBranch
(
const
QString
&
repo
,
const
QString
&
branch
)
{
QProcess
git
;
git
.
setWorkingDirectory
(
repo
);
QStringList
args
{
QStringLiteral
(
"checkout"
),
branch
};
git
.
start
(
QStringLiteral
(
"git"
),
args
);
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
return
git
.
exitCode
();
}
return
-
1
;
}
bool
isGitRepo
(
const
QString
&
repo
);
static
QStringList
getAllBranches
(
const
QString
&
repo
)
{
QProcess
git
;
QStringList
args
{
QStringLiteral
(
"branch"
),
QStringLiteral
(
"--all"
)};
git
.
setWorkingDirectory
(
repo
);
QString
getCurrentBranchName
(
const
QString
&
repo
);
git
.
start
(
QStringLiteral
(
"git"
),
args
);
QStringList
branches
;
if
(
git
.
waitForStarted
()
&&
git
.
waitForFinished
(
-
1
))
{
QList
<
QByteArray
>
out
=
git
.
readAllStandardOutput
().
split
(
'\n'
);
for
(
const
QByteArray
&
br
:
out
)
{
auto
branch
=
br
.
trimmed
();
if
(
!
branch
.
isEmpty
()
&&
!
branch
.
startsWith
(
"* "
))
{
branches
.
append
(
QString
::
fromUtf8
(
branch
.
trimmed
()));
}
}
}
return
branches
;
}
int
checkoutBranch
(
const
QString
&
repo
,
const
QString
&
branch
);
QVector
<
Branch
>
getAllBranches
(
const
QString
&
repo
,
RefType
ref
=
RefType
::
All
);
}
#endif // GITUTILS_H
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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