Line data Source code
1 : /*! 2 : * \file esys/repo/sharing/context.h 3 : * \brief Shared sync-time sharing state (ADR-0039–0042) 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2026 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "esys/repo/esysrepo_defs.h" 21 : #include "esys/repo/gitbase.h" 22 : #include "esys/repo/result.h" 23 : #include "esys/repo/sharing/inventory.h" 24 : #include "esys/repo/sharing/providerregistry.h" 25 : 26 : #include <map> 27 : #include <memory> 28 : #include <mutex> 29 : #include <string> 30 : 31 : namespace esys::repo 32 : { 33 : class Manifest; 34 : class ConfigFolder; 35 : } 36 : 37 : namespace esys::log 38 : { 39 : class Logger_if; 40 : } 41 : 42 : namespace esys::repo::sharing 43 : { 44 : 45 : /*! \class Context esys/repo/sharing/context.h 46 : * \brief Per-sync registry, inventory, and per-provider locks 47 : */ 48 : class ESYSREPO_API Context 49 : { 50 : public: 51 27 : Context() = default; 52 : 53 : Result build(const Manifest &manifest); 54 : 55 : ProviderRegistry &get_registry(); 56 : const ProviderRegistry &get_registry() const; 57 : 58 : Inventory &get_inventory(); 59 : const Inventory &get_inventory() const; 60 : 61 : //! Serialize worktree admin for one provider path 62 : std::mutex &provider_lock(const std::string &provider_abs_path); 63 : 64 : bool has_providers() const; 65 : 66 : private: 67 : ProviderRegistry m_registry; 68 : Inventory m_inventory; 69 : std::mutex m_locks_mutex; 70 : std::map<std::string, std::unique_ptr<std::mutex>> m_provider_locks; 71 : }; 72 : 73 : /*! Apply sharing for one consumer after its project sync (ADR-0039 §4). 74 : * 75 : * Discovers submodule occurrences, materializes matches via provider worktrees, 76 : * records inventory. Unmatched deps are left for ``update_submodules``. 77 : * No-op (OK) when context has no providers or consumer has no ``.gitmodules``. 78 : * 79 : * ``git_generator`` must create independent ``GitBase`` instances (consumer + 80 : * provider opens must not share one handle). 81 : */ 82 : ESYSREPO_API Result apply_for_consumer(Context &ctx, const std::string &workspace_abs, 83 : const std::string &consumer_rel_path, const std::string &consumer_abs_path, 84 : GitBase::GeneratorType git_generator, 85 : std::shared_ptr<log::Logger_if> logger); 86 : 87 : /*! Emit unregistered-duplicate warnings and write private inventory file. */ 88 : ESYSREPO_API Result finalize_inventory(Context &ctx, ConfigFolder &config_folder, 89 : std::shared_ptr<log::Logger_if> logger); 90 : 91 : } // namespace esys::repo::sharing