File indexing completed on 2025-01-19 05:11:30

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 "remotesmodel.h"
0008 #include "entities/branch.h"
0009 #include "entities/remote.h"
0010 #include "gitmanager.h"
0011 #include "qdebug.h"
0012 #include "types.h"
0013 
0014 namespace Git
0015 {
0016 
0017 RemotesModel::RemotesModel(Manager *git, QObject *parent)
0018     : AbstractGitItemsModel(git, parent)
0019 {
0020 }
0021 
0022 int RemotesModel::columnCount(const QModelIndex &parent) const
0023 {
0024     Q_UNUSED(parent)
0025     return 3;
0026 }
0027 
0028 int RemotesModel::rowCount(const QModelIndex &parent) const
0029 {
0030     Q_UNUSED(parent)
0031     return mData.count();
0032 }
0033 
0034 QVariant RemotesModel::data(const QModelIndex &index, int role) const
0035 {
0036     if (role != Qt::DisplayRole || !index.isValid() || index.row() < 0 || index.row() >= mData.size())
0037         return {};
0038 
0039     auto remote = mData.at(index.row());
0040 
0041     switch (index.column()) {
0042     case 0:
0043         return remote->name();
0044     }
0045     return {};
0046 }
0047 
0048 Remote *RemotesModel::fromIndex(const QModelIndex &index)
0049 {
0050     if (!index.isValid() || index.row() < 0 || index.row() >= mData.size())
0051         return nullptr;
0052 
0053     return mData.at(index.row());
0054 }
0055 
0056 Remote *RemotesModel::findByName(const QString &name)
0057 {
0058     for (const auto &d : std::as_const(mData))
0059         if (d->name() == name)
0060             return d;
0061     return nullptr;
0062 }
0063 
0064 void RemotesModel::rename(const QString &oldName, const QString &newName)
0065 {
0066     mGit->runGit({QStringLiteral("remote"), QStringLiteral("rename"), oldName, newName});
0067     load();
0068 }
0069 
0070 void RemotesModel::setUrl(const QString &remoteName, const QString &newUrl)
0071 {
0072     mGit->runGit({QStringLiteral("remote"), QStringLiteral("set-url"), remoteName, newUrl});
0073     load();
0074 }
0075 
0076 void RemotesModel::fill()
0077 {
0078     qDeleteAll(mData.begin(), mData.end());
0079     mData.clear();
0080 
0081     if (!mGit)
0082         return;
0083 
0084     QMap<QString, Remote *> remotesMap;
0085 
0086     git_strarray list{};
0087     git_remote_list(&list, mGit->mRepo);
0088     auto remotes = convert(&list);
0089     git_strarray_free(&list);
0090 
0091     for (auto &r : remotes) {
0092         git_remote *remote;
0093         if (!git_remote_lookup(&remote, mGit->mRepo, r.toLatin1().data())) {
0094             auto rem = new Remote{remote};
0095             mData << rem;
0096 
0097             remotesMap.insert(rem->name(), rem);
0098         }
0099     }
0100 
0101     git_branch_iterator *it;
0102     git_branch_iterator_new(&it, mGit->mRepo, GIT_BRANCH_ALL);
0103 
0104     git_reference *ref;
0105     git_branch_t b;
0106     qDebug() << remotesMap;
0107     while (!git_branch_next(&ref, &b, it)) {
0108         auto b = new Branch{ref};
0109 
0110         auto remote = remotesMap.value(b->remoteName());
0111 
0112         if (!remote)
0113             qDebug() << "NO REMOTE FOUUND" << b->remoteName();
0114         else {
0115             qDebug() << "REMOTE FOUUND" << b->remoteName();
0116             remote->mBranches << b;
0117         }
0118     }
0119     git_branch_iterator_free(it);
0120 
0121     //    const auto remotes = mGit->remotes();
0122     //    for (const auto &remote : remotes) {
0123     //        auto r = new Remote;
0124     //        r->name = remote;
0125     //        auto ret = QString(mGit->runGit({QStringLiteral("remote"), QStringLiteral("show"), remote}));
0126     //        r->parse(ret);
0127     //        mData.append(r);
0128     //    }
0129 }
0130 
0131 }
0132 
0133 #include "moc_remotesmodel.cpp"