Commit e366e135 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh
Browse files

Git: Eradicate Q_FOREACH loops



Change-Id: I29b6071ea244d1b3ae0701d36c90b1e93cf21fbb
Reviewed-by: default avatarAndré Hartmann <aha_1980@gmx.de>
Reviewed-by: default avatarhjk <hjk@qt.io>
parent e3eee3b2
......@@ -33,11 +33,13 @@
#include "gitconstants.h"
#include "ui_branchdialog.h"
#include <utils/qtcassert.h>
#include <utils/execmenu.h>
#include <vcsbase/vcsoutputwindow.h>
#include <coreplugin/documentmanager.h>
#include <utils/asconst.h>
#include <utils/execmenu.h>
#include <utils/qtcassert.h>
#include <QAction>
#include <QItemSelectionModel>
#include <QMessageBox>
......@@ -217,7 +219,7 @@ void BranchDialog::checkout()
QList<Stash> stashes;
client->synchronousStashList(m_repository, &stashes);
foreach (const Stash &stash, stashes) {
for (const Stash &stash : Utils::asConst(stashes)) {
if (stash.message.startsWith(popMessageStart)) {
branchCheckoutDialog.foundStashForNextBranch();
break;
......@@ -245,7 +247,7 @@ void BranchDialog::checkout()
QString stashName;
client->synchronousStashList(m_repository, &stashes);
foreach (const Stash &stash, stashes) {
for (const Stash &stash : Utils::asConst(stashes)) {
if (stash.message.startsWith(popMessageStart)) {
stashName = stash.name;
break;
......
......@@ -27,10 +27,12 @@
#include "gitclient.h"
#include "gitconstants.h"
#include <utils/qtcassert.h>
#include <vcsbase/vcsoutputwindow.h>
#include <vcsbase/vcscommand.h>
#include <utils/asconst.h>
#include <utils/qtcassert.h>
#include <QDateTime>
#include <QFont>
......@@ -137,7 +139,7 @@ public:
fn.append(nodes.first()->sha);
nodes.removeFirst();
foreach (const BranchNode *n, nodes)
for (const BranchNode *n : Utils::asConst(nodes))
fn.append(n->name);
return fn;
......@@ -167,7 +169,7 @@ public:
{
if (children.count() > 0) {
QStringList names;
foreach (BranchNode *n, children) {
for (BranchNode *n : children) {
names.append(n->childrenNames());
}
return names;
......@@ -340,9 +342,10 @@ Qt::ItemFlags BranchModel::flags(const QModelIndex &index) const
void BranchModel::clear()
{
foreach (BranchNode *root, m_rootNode->children)
for (BranchNode *root : Utils::asConst(m_rootNode->children)) {
while (root->count())
delete root->children.takeLast();
}
if (hasTags())
m_rootNode->children.takeLast();
......@@ -368,7 +371,7 @@ bool BranchModel::refresh(const QString &workingDirectory, QString *errorMessage
m_workingDirectory = workingDirectory;
const QStringList lines = output.split('\n');
foreach (const QString &l, lines)
for (const QString &l : lines)
parseOutputLine(l);
if (m_currentBranch) {
......@@ -558,8 +561,8 @@ bool BranchModel::branchIsMerged(const QModelIndex &idx)
VcsOutputWindow::appendError(errorMessage);
}
QStringList lines = output.split('\n', QString::SkipEmptyParts);
foreach (const QString &l, lines) {
const QStringList lines = output.split('\n', QString::SkipEmptyParts);
for (const QString &l : lines) {
QString currentBranch = l.mid(2); // remove first letters (those are either
// " " or "* " depending on whether it is
// the currently checked out branch or not)
......
......@@ -24,6 +24,7 @@
****************************************************************************/
#include "commitdata.h"
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
......@@ -164,7 +165,7 @@ bool CommitData::parseFilesFromStatus(const QString &output)
{
const QStringList lines = output.split('\n');
foreach (const QString &line, lines) {
for (const QString &line : lines) {
if (line.isEmpty())
continue;
......@@ -187,7 +188,7 @@ bool CommitData::parseFilesFromStatus(const QString &output)
QStringList CommitData::filterFiles(const FileStates &state) const
{
QStringList result;
foreach (const StateFilePair &p, files) {
for (const StateFilePair &p : files) {
if (state == (p.first & ~(UnmergedFile | UnmergedUs | UnmergedThem)))
result.append(p.second);
}
......
......@@ -48,8 +48,8 @@ void BranchComboBox::init(const QString &repository)
m_repository, { "--format=%(refname)", branchPrefix }, &output)) {
return;
}
QStringList branches = output.trimmed().split('\n');
foreach (const QString &ref, branches) {
const QStringList branches = output.trimmed().split('\n');
for (const QString &ref : branches) {
const QString branch = ref.mid(branchPrefix.size());
addItem(branch);
}
......
......@@ -31,6 +31,8 @@
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
#include <vcsbase/vcsoutputwindow.h>
#include <utils/asconst.h>
#include <utils/synchronousprocess.h>
#include <QJsonArray>
......@@ -106,7 +108,7 @@ QString GerritPatchSet::approvalsToHtml() const
QString result;
QTextStream str(&result);
QString lastType;
foreach (const GerritApproval &a, approvals) {
for (const GerritApproval &a : approvals) {
if (a.type != lastType) {
if (!lastType.isEmpty())
str << "</tr>\n";
......@@ -147,7 +149,7 @@ QString GerritPatchSet::approvalsColumn() const
return result;
TypeReviewMap reviews; // Sort approvals into a map by type character
foreach (const GerritApproval &a, approvals) {
for (const GerritApproval &a : approvals) {
if (a.type != "STGN") { // Qt-Project specific: Ignore "STGN" (Staged)
const QChar typeChar = a.type.at(0);
TypeReviewMapIterator it = reviews.find(typeChar);
......@@ -169,7 +171,7 @@ QString GerritPatchSet::approvalsColumn() const
bool GerritPatchSet::hasApproval(const QString &userName) const
{
foreach (const GerritApproval &a, approvals)
for (const GerritApproval &a : approvals)
if (a.reviewer == userName)
return true;
return false;
......@@ -178,7 +180,7 @@ bool GerritPatchSet::hasApproval(const QString &userName) const
int GerritPatchSet::approvalLevel() const
{
int value = 0;
foreach (const GerritApproval &a, approvals)
for (const GerritApproval &a : approvals)
applyApproval(a.approval, &value);
return value;
}
......@@ -189,7 +191,7 @@ QString GerritChange::filterString() const
QString result = QString::number(number) + blank + title + blank
+ owner + blank + project + blank
+ branch + blank + status;
foreach (const GerritApproval &a, currentPatchSet.approvals) {
for (const GerritApproval &a : currentPatchSet.approvals) {
result += blank;
result += a.reviewer;
}
......@@ -613,7 +615,7 @@ static bool parseOutput(const QSharedPointer<GerritParameters> &parameters,
result.clear();
result.reserve(lines.size());
foreach (const QByteArray &line, lines) {
for (const QByteArray &line : lines) {
if (line.isEmpty())
continue;
QJsonParseError error;
......@@ -791,7 +793,7 @@ void GerritModel::queryFinished(const QByteArray &output)
std::stable_sort(changes.begin(), changes.end(), gerritChangeLessThan);
numberIndexHash.clear();
foreach (const GerritChangePtr &c, changes) {
for (const GerritChangePtr &c : Utils::asConst(changes)) {
// Avoid duplicate entries for example in the (unlikely)
// case people do self-reviews.
if (!itemForNumber(c->number)) {
......
......@@ -337,7 +337,7 @@ void GerritPlugin::push(const QString &topLevel)
QStringList options;
const QStringList reviewers = m_reviewers.split(',', QString::SkipEmptyParts);
foreach (const QString &reviewer, reviewers)
for (const QString &reviewer : reviewers)
options << "r=" + reviewer;
if (!options.isEmpty())
......@@ -412,8 +412,8 @@ void GerritPlugin::fetch(const QSharedPointer<GerritChange> &change, int mode)
// Check if remote from a working dir is the same as remote from patch
QMap<QString, QString> remotesList = GitPlugin::client()->synchronousRemotesList(repository);
if (!remotesList.isEmpty()) {
QStringList remotes = remotesList.values();
foreach (QString remote, remotes) {
const QStringList remotes = remotesList.values();
for (QString remote : remotes) {
if (remote.endsWith(".git"))
remote.chop(4);
if (remote.contains(m_server->host) && remote.endsWith(change->project)) {
......@@ -423,8 +423,8 @@ void GerritPlugin::fetch(const QSharedPointer<GerritChange> &change, int mode)
}
if (!verifiedRepository) {
SubmoduleDataMap submodules = GitPlugin::client()->submoduleList(repository);
foreach (const SubmoduleData &submoduleData, submodules) {
const SubmoduleDataMap submodules = GitPlugin::client()->submoduleList(repository);
for (const SubmoduleData &submoduleData : submodules) {
QString remote = submoduleData.url;
if (remote.endsWith(".git"))
remote.chop(4);
......@@ -501,7 +501,7 @@ QString GerritPlugin::findLocalRepository(QString project, const QString &branch
if (!branchRegexp->isValid())
branchRegexp.reset(); // Oops.
}
foreach (const QString &repository, gitRepositories) {
for (const QString &repository : gitRepositories) {
const QString fileName = Utils::FileName::fromString(repository).fileName();
if ((!branchRegexp.isNull() && branchRegexp->exactMatch(fileName))
|| fileName == project) {
......
......@@ -70,14 +70,14 @@ QString GerritPushDialog::determineRemoteBranch(const QString &localBranch)
return QString();
}
const QString head = "/HEAD";
QStringList refs = output.split('\n');
const QStringList refs = output.split('\n');
QString remoteTrackingBranch;
if (localBranch != "HEAD")
remoteTrackingBranch = GitPlugin::client()->synchronousTrackingBranch(m_workingDir, localBranch);
QString remoteBranch;
foreach (const QString &reference, refs) {
for (const QString &reference : refs) {
const QString ref = reference.trimmed();
if (ref.contains(head) || ref.isEmpty())
continue;
......@@ -104,7 +104,7 @@ void GerritPushDialog::initRemoteBranches()
}
const QStringList refs = output.split("\n");
foreach (const QString &reference, refs) {
for (const QString &reference : refs) {
QStringList entries = reference.split('\t');
if (entries.count() < 2 || entries.first().endsWith(head))
continue;
......@@ -237,7 +237,7 @@ void GerritPushDialog::setRemoteBranches(bool includeOld)
if (!m_remoteBranches.contains(remoteName)) {
const QStringList remoteBranches =
GitPlugin::client()->synchronousRepositoryBranches(remoteName, m_workingDir);
foreach (const QString &branch, remoteBranches)
for (const QString &branch : remoteBranches)
m_remoteBranches.insertMulti(remoteName, qMakePair(branch, QDate()));
if (remoteBranches.isEmpty()) {
m_ui->targetBranchComboBox->setEditable(true);
......@@ -250,7 +250,8 @@ void GerritPushDialog::setRemoteBranches(bool includeOld)
int i = 0;
bool excluded = false;
foreach (const BranchDate &bd, m_remoteBranches.values(remoteName)) {
const QList<BranchDate> remoteBranches = m_remoteBranches.values(remoteName);
for (const BranchDate &bd : remoteBranches) {
const bool isSuggested = bd.first == m_suggestedRemoteBranch;
if (includeOld || isSuggested || !bd.second.isValid()
|| bd.second.daysTo(QDate::currentDate()) <= Git::Constants::OBSOLETE_COMMIT_AGE_IN_DAYS) {
......
include(../../qtcreatorplugin.pri)
DEFINES += QT_NO_FOREACH
HEADERS += gitplugin.h \
gitconstants.h \
gitclient.h \
......
......@@ -11,6 +11,8 @@ QtcPlugin {
Depends { name: "VcsBase" }
Depends { name: "DiffEditor" }
cpp.defines: base.concat(["QT_NO_FOREACH"])
files: [
"annotationhighlighter.cpp",
"annotationhighlighter.h",
......
......@@ -45,6 +45,7 @@
#include <coreplugin/vcsmanager.h>
#include <utils/algorithm.h>
#include <utils/asconst.h>
#include <utils/checkablemessagebox.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
......@@ -158,7 +159,7 @@ void BaseController::runCommand(const QList<QStringList> &args, QTextCodec *code
connect(m_command.data(), &VcsCommand::finished, this, &BaseController::reloadFinished);
m_command->addFlags(diffExecutionFlags());
foreach (const QStringList &arg, args) {
for (const QStringList &arg : args) {
QTC_ASSERT(!arg.isEmpty(), continue);
m_command->addJob(GitPlugin::client()->vcsBinary(), arg, GitPlugin::client()->vcsTimeoutS());
......@@ -1002,7 +1003,8 @@ QStringList GitClient::setupCheckoutArguments(const QString &workingDirectory,
QString remoteBranch;
const QString head("/HEAD");
foreach (const QString &singleRef, output.split('\n')) {
const QStringList refs = output.split('\n');
for (const QString &singleRef : refs) {
if (singleRef.startsWith(refSha)) {
// branch name might be origin/foo/HEAD
if (!singleRef.endsWith(head) || singleRef.count('/') > 1) {
......@@ -1343,7 +1345,7 @@ QString GitClient::synchronousTopic(const QString &workingDirectory) const
const QString dereference("^{}");
QString remoteBranch;
foreach (const QString &ref, references) {
for (const QString &ref : Utils::asConst(references)) {
int derefInd = ref.indexOf(dereference);
if (ref.startsWith(tagStart))
return ref.mid(tagStart.size(), (derefInd == -1) ? -1 : derefInd - tagStart.size());
......@@ -1406,7 +1408,7 @@ void GitClient::synchronousTagsForCommit(const QString &workingDirectory, const
QStringList parents;
QString errorMessage;
synchronousParentRevisions(workingDirectory, revision, &parents, &errorMessage);
foreach (const QString &p, parents) {
for (const QString &p : Utils::asConst(parents)) {
const SynchronousProcessResponse resp2 = vcsFullySynchronousExec(
workingDirectory, { "describe", "--tags", "--abbrev=0", p }, silentFlags);
QString pf = resp2.stdOut();
......@@ -1553,7 +1555,7 @@ bool GitClient::stashNameFromMessage(const QString &workingDirectory,
QList<Stash> stashes;
if (!synchronousStashList(workingDirectory, &stashes, errorMessage))
return false;
foreach (const Stash &s, stashes) {
for (const Stash &s : Utils::asConst(stashes)) {
if (s.message == message) {
*name = s.name;
return true;
......@@ -1640,9 +1642,9 @@ QMap<QString,QString> GitClient::synchronousRemotesList(const QString &workingDi
msgCannotRun(error, errorMessage);
return result;
}
QStringList remotes = output.split("\n");
foreach (const QString &remote, remotes) {
const QStringList remotes = output.split("\n");
for (const QString &remote : remotes) {
if (!remote.endsWith(" (push)"))
continue;
......@@ -1682,10 +1684,9 @@ SubmoduleDataMap GitClient::submoduleList(const QString &workingDirectory) const
if (cachedSubmoduleData.contains(workingDirectory))
return cachedSubmoduleData.value(workingDirectory);
QStringList allConfigs = readConfigValue(workingDirectory, "-l")
.split('\n');
const QStringList allConfigs = readConfigValue(workingDirectory, "-l").split('\n');
const QString submoduleLineStart = "submodule.";
foreach (const QString &configLine, allConfigs) {
for (const QString &configLine : allConfigs) {
if (!configLine.startsWith(submoduleLineStart))
continue;
......@@ -1712,7 +1713,8 @@ SubmoduleDataMap GitClient::submoduleList(const QString &workingDirectory) const
if (!result.isEmpty()) {
QSettings gitmodulesFile(gitmodulesFileName, QSettings::IniFormat);
foreach (const QString &submoduleName, result.keys()) {
const QList<QString> submodules = result.keys();
for (const QString &submoduleName : submodules) {
gitmodulesFile.beginGroup("submodule \"" + submoduleName + '"');
const QString path = gitmodulesFile.value("path").toString();
if (path.isEmpty()) { // invalid submodule entry in config
......@@ -1782,8 +1784,8 @@ bool GitClient::synchronousCleanList(const QString &workingDirectory, const QStr
bool res = cleanList(workingDirectory, modulePath, "-df", files, errorMessage);
res &= cleanList(workingDirectory, modulePath, "-dXf", ignoredFiles, errorMessage);
SubmoduleDataMap submodules = submoduleList(workingDirectory + '/' + modulePath);
foreach (const SubmoduleData &submodule, submodules) {
const SubmoduleDataMap submodules = submoduleList(workingDirectory + '/' + modulePath);
for (const SubmoduleData &submodule : submodules) {
if (submodule.ignore != "all"
&& submodule.ignore != "dirty") {
const QString submodulePath = modulePath.isEmpty() ? submodule.dir
......@@ -1872,12 +1874,12 @@ void GitClient::updateSubmodulesIfNeeded(const QString &workingDirectory, bool p
if (!m_updatedSubmodules.isEmpty() || submoduleList(workingDirectory).isEmpty())
return;
QStringList submoduleStatus = synchronousSubmoduleStatus(workingDirectory);
const QStringList submoduleStatus = synchronousSubmoduleStatus(workingDirectory);
if (submoduleStatus.isEmpty())
return;
bool updateNeeded = false;
foreach (const QString &status, submoduleStatus) {
for (const QString &status : submoduleStatus) {
if (status.startsWith('+')) {
updateNeeded = true;
break;
......@@ -1892,7 +1894,7 @@ void GitClient::updateSubmodulesIfNeeded(const QString &workingDirectory, bool p
return;
}
foreach (const QString &statusLine, submoduleStatus) {
for (const QString &statusLine : submoduleStatus) {
// stash only for lines starting with +
// because only they would be updated
if (!statusLine.startsWith('+'))
......@@ -1918,7 +1920,7 @@ void GitClient::updateSubmodulesIfNeeded(const QString &workingDirectory, bool p
void GitClient::finishSubmoduleUpdate()
{
foreach (const QString &submoduleDir, m_updatedSubmodules)
for (const QString &submoduleDir : Utils::asConst(m_updatedSubmodules))
endStashScope(submoduleDir);
m_updatedSubmodules.clear();
}
......@@ -2112,7 +2114,8 @@ QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryUR
// split "82bfad2f51d34e98b18982211c82220b8db049b<tab>refs/heads/master"
bool headFound = false;
bool branchFound = false;
foreach (const QString &line, resp.stdOut().split('\n')) {
const QStringList lines = resp.stdOut().split('\n');
for (const QString &line : lines) {
if (line.endsWith("\tHEAD")) {
QTC_CHECK(headSha.isNull());
headSha = line.left(line.indexOf('\t'));
......@@ -2988,7 +2991,8 @@ bool GitClient::synchronousStashList(const QString &workingDirectory, QList<Stas
return false;
}
Stash stash;
foreach (const QString &line, splitLines(resp.stdOut())) {
const QStringList lines = splitLines(resp.stdOut());
for (const QString &line : lines) {
if (stash.parseStashLine(line))
stashes->push_back(stash);
}
......
......@@ -35,6 +35,7 @@
#include <vcsbase/vcscommand.h>
#include <vcsbase/vcsbaseconstants.h>
#include <utils/asconst.h>
#include <utils/fancylineedit.h>
#include <utils/filesearch.h>
#include <utils/fileutils.h>
......@@ -119,7 +120,7 @@ public:
QRegularExpressionMatch regexpMatch = regexp.match(line);
single.regexpCapturedTexts = regexpMatch.capturedTexts();
}
foreach (auto match, matches) {
for (auto match : Utils::asConst(matches)) {
single.matchStart = match.first;
single.matchLength = match.second;
resultList->append(single);
......
......@@ -25,6 +25,7 @@
#include <texteditor/texteditorconstants.h>
#include <utils/asconst.h>
#include <utils/qtcassert.h>
#include "githighlighters.h"
......@@ -130,7 +131,7 @@ void GitRebaseHighlighter::highlightBlock(const QString &text)
return;
}
foreach (const RebaseAction &action, m_actions) {
for (const RebaseAction &action : Utils::asConst(m_actions)) {
if (action.exp.indexIn(text) != -1) {
const int len = action.exp.matchedLength();
setFormat(0, len, formatForCategory(action.formatCategory));
......
......@@ -58,6 +58,7 @@
#include <coreplugin/vcsmanager.h>
#include <coreplugin/messagebox.h>
#include <utils/asconst.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
#include <utils/parameteraction.h>
......@@ -1336,15 +1337,15 @@ void GitPlugin::updateActions(VcsBasePlugin::ActionState as)
// Note: This menu is visible if there is no repository. Only
// 'Create Repository'/'Show' actions should be available.
const QString fileName = state.currentFileName();
foreach (ParameterAction *fileAction, m_fileActions)
for (ParameterAction *fileAction : Utils::asConst(m_fileActions))
fileAction->setParameter(fileName);
// If the current file looks like a patch, offer to apply
m_applyCurrentFilePatchAction->setParameter(state.currentPatchFileDisplayName());
const QString projectName = state.currentProjectName();
foreach (ParameterAction *projectAction, m_projectActions)
for (ParameterAction *projectAction : Utils::asConst(m_projectActions))
projectAction->setParameter(projectName);
foreach (QAction *repositoryAction, m_repositoryActions)
for (QAction *repositoryAction : Utils::asConst(m_repositoryActions))
repositoryAction->setEnabled(repositoryEnabled);
m_submoduleUpdateAction->setVisible(repositoryEnabled
......
......@@ -182,7 +182,7 @@ void GitSubmitEditor::slotDiffSelected(const QList<int> &rows)
QStringList unmergedFiles;
QStringList unstagedFiles;
QStringList stagedFiles;
foreach (int row, rows) {
for (int row : rows) {
const QString fileName = m_model->file(row);
const FileStates state = static_cast<FileStates>(m_model->extraData(row).toInt());
if (state & UnmergedFile) {
......
......@@ -161,7 +161,8 @@ bool LogChangeWidget::populateLog(const QString &repository, const QString &comm
QString output;
if (!GitPlugin::client()->synchronousLog(repository, arguments, &output, 0, VcsCommand::NoOutput))
return false;
foreach (const QString &line, output.split('\n')) {
const QStringList lines = output.split('\n');
for (const QString &line : lines) {
const int colonPos = line.indexOf(':');
if (colonPos != -1) {
QList<QStandardItem *> row;
......
......@@ -186,7 +186,8 @@ bool RemoteModel::refresh(const QString &workingDirectory, QString *errorMessage
beginResetModel();
m_remotes.clear();
foreach (const QString &remoteName, remotesList.keys()) {
const QList<QString> remotes = remotesList.keys();
for (const QString &remoteName : remotes) {
Remote newRemote;
newRemote.name = remoteName;
newRemote.url = remotesList.value(remoteName);
......
......@@ -85,7 +85,7 @@ void StashModel::setStashes(const QList<Stash> &stashes)
m_stashes = stashes;
if (const int rows = rowCount())
removeRows(0, rows);
foreach (const Stash &s, stashes)
for (const Stash &s : stashes)
appendRow(stashModelRowItems(s));
}
......@@ -354,7 +354,8 @@ int StashDialog::currentRow() const
QList<int> StashDialog::selectedRows() const
{
QList<int> rc;
foreach (const QModelIndex &proxyIndex, ui->stashView->selectionModel()->selectedRows()) {
const QModelIndexList rows = ui->stashView->selectionModel()->selectedRows();
for (const QModelIndex &proxyIndex : rows) {
const QModelIndex index = m_proxyModel->mapToSource(proxyIndex);
if (index.isValid())
rc.push_back(index.row());
......
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