File indexing completed on 2025-01-19 04:22:44
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "branchesmodel.h" 0008 #include "gitmanager.h" 0009 0010 #include <KLocalizedString> 0011 0012 namespace Git 0013 { 0014 0015 BranchesModel::BranchesModel(Manager *git, QObject *parent) 0016 : AbstractGitItemsModel{git, parent} 0017 { 0018 } 0019 0020 int BranchesModel::rowCount(const QModelIndex &parent) const 0021 { 0022 Q_UNUSED(parent) 0023 return mData.size(); 0024 } 0025 0026 int BranchesModel::columnCount(const QModelIndex &parent) const 0027 { 0028 Q_UNUSED(parent) 0029 return 3; 0030 } 0031 0032 QVariant BranchesModel::data(const QModelIndex &index, int role) const 0033 { 0034 if (role != Qt::DisplayRole || !index.isValid() || index.row() < 0 || index.row() >= mData.size()) 0035 return {}; 0036 0037 switch (index.column()) { 0038 case 0: 0039 return mData.at(index.row())->name; 0040 case 1: 0041 return mData.at(index.row())->commitsBehind; 0042 case 2: 0043 return mData.at(index.row())->commitsAhead; 0044 } 0045 0046 return {}; 0047 } 0048 0049 QVariant BranchesModel::headerData(int section, Qt::Orientation orientation, int role) const 0050 { 0051 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) 0052 return {}; 0053 0054 switch (section) { 0055 case 0: 0056 return i18n("Name"); 0057 case 1: 0058 return i18n("Commit(s) behind"); 0059 case 2: 0060 return i18n("Commit(s) ahead"); 0061 } 0062 return {}; 0063 } 0064 0065 BranchesModel::BranchData *BranchesModel::fromIndex(const QModelIndex &index) const 0066 { 0067 if (!index.isValid() || index.row() < 0 || index.row() >= mData.size()) 0068 return nullptr; 0069 0070 return mData.at(index.row()); 0071 } 0072 0073 void BranchesModel::fill() 0074 { 0075 qDeleteAll(mData); 0076 mData.clear(); 0077 0078 QStringList branchesList; 0079 const auto out = mGit->readAllNonEmptyOutput({QStringLiteral("branch"), QStringLiteral("--list")}); 0080 0081 for (const auto &line : out) { 0082 auto b = line.trimmed(); 0083 if (b.isEmpty()) 0084 continue; 0085 if (b.startsWith(QStringLiteral("* "))) { 0086 b = b.mid(2); 0087 mReferenceBranch = mCurrentBranch = b.trimmed(); 0088 } 0089 0090 branchesList.append(b.trimmed()); 0091 0092 auto branch = new BranchData; 0093 branch->name = b.trimmed(); 0094 branch->commitsAhead = branch->commitsBehind = 0; 0095 mData.append(branch); 0096 } 0097 calculateCommitStats(); 0098 } 0099 0100 const QString &BranchesModel::referenceBranch() const 0101 { 0102 return mReferenceBranch; 0103 } 0104 0105 void BranchesModel::calculateCommitStats() 0106 { 0107 for (auto &b : mData) { 0108 const auto commitsInfo = mGit->uniqueCommitsOnBranches(mReferenceBranch, b->name); 0109 b->commitsBehind = commitsInfo.first; 0110 b->commitsAhead = commitsInfo.second; 0111 } 0112 Q_EMIT dataChanged(index(0, 1), index(mData.size() - 1, 2)); 0113 } 0114 0115 void BranchesModel::setReferenceBranch(const QString &newReferenceBranch) 0116 { 0117 mReferenceBranch = newReferenceBranch; 0118 0119 calculateCommitStats(); 0120 } 0121 0122 const QString &BranchesModel::currentBranch() const 0123 { 0124 return mCurrentBranch; 0125 } 0126 0127 } // namespace Git