File indexing completed on 2025-03-09 05:11:41

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 "submodule.h"
0008 #include "qdebug.h"
0009 #include "types.h"
0010 
0011 #include <git2/submodule.h>
0012 
0013 namespace Git
0014 {
0015 
0016 Submodule::Submodule() = default;
0017 
0018 Submodule::Submodule(git_submodule *submodule)
0019     : ptr{submodule}
0020 {
0021     mName = QString{git_submodule_name(submodule)};
0022     mPath = QString{git_submodule_path(submodule)};
0023     mUrl = QString{git_submodule_url(submodule)};
0024 
0025     auto headId = git_submodule_head_id(submodule);
0026     mRefName = QString{git_oid_tostr_s(headId)};
0027 }
0028 
0029 Submodule::~Submodule()
0030 {
0031     qDebug() << Q_FUNC_INFO << ptr;
0032     // git_submodule_free(ptr);
0033     ptr = nullptr;
0034 }
0035 
0036 const QString &Submodule::path() const
0037 {
0038     return mPath;
0039 }
0040 
0041 const QString &Submodule::commitHash() const
0042 {
0043     return mCommitHash;
0044 }
0045 
0046 const QString &Submodule::refName() const
0047 {
0048     return mRefName;
0049 }
0050 
0051 QString Submodule::url() const
0052 {
0053     auto s = QString{git_submodule_url(ptr)};
0054     return s;
0055 }
0056 
0057 void Submodule::setUrl(const QString &newUrl)
0058 {
0059     mUrl = newUrl;
0060     BEGIN;
0061     STEP git_submodule_set_url(git_submodule_owner(ptr), toConstChars(mName), toConstChars(newUrl));
0062 }
0063 
0064 QString Submodule::name() const
0065 {
0066     return mName;
0067 }
0068 
0069 QString Submodule::branch()
0070 {
0071     if (mBranch.isNull())
0072         mBranch = git_submodule_branch(ptr);
0073     return mBranch;
0074 }
0075 
0076 Submodule::StatusFlags Submodule::status() const
0077 {
0078     unsigned int status;
0079     if (git_submodule_status(&status, git_submodule_owner(ptr), mName.toLocal8Bit().constData(), GIT_SUBMODULE_IGNORE_UNSPECIFIED))
0080         return Status::Unknown;
0081 
0082     return static_cast<StatusFlags>(status);
0083 }
0084 
0085 bool Submodule::sync() const
0086 {
0087     if (Q_UNLIKELY(!ptr))
0088         return false;
0089     return !git_submodule_sync(ptr);
0090 }
0091 
0092 bool Submodule::reload(bool force) const
0093 {
0094     if (Q_UNLIKELY(!ptr))
0095         return false;
0096     return !git_submodule_reload(ptr, force);
0097 }
0098 
0099 } // namespace Git