SVCS - English Documentation
Loading...
Searching...
No Matches
BranchManager.hxx
Go to the documentation of this file.
1
20
21#pragma once
22
24
25#include <string>
26#include <utility>
27#include <vector>
28#include <memory>
29#include <unordered_map>
30
41 * @details Содержит фундаментальные типы объектов СКВ, такие как Blob, Tree, Commit,
42 * которые формируют строительные блоки системы контроля версий.
43 */
44namespace svcs::core {
45
46using namespace svcs::services;
47
48/**
49 * @english
50 * @class BranchManager
51 * @brief Core service for managing version control branches (creation, deletion, switching).
52 * @details This class abstracts the details of branch storage and provides a
53 * high-level interface for branch manipulation. It uses an internal map to
54 * track branches and communicates status/errors via the ISubject event bus.
55 *
56 * @russian
57 * @class BranchManager
58 * @brief Основной сервис для управления ветвями системы контроля версий (создание, удаление, переключение).
59 * @details Этот класс абстрагирует детали хранения ветвей и предоставляет высокоуровневый
60 * интерфейс для манипулирования ветвями. Он использует внутреннюю карту для отслеживания ветвей
61 * и передает статус/ошибки через шину событий ISubject.
62 *
63 * @ingroup Core
64 */
66public:
76 struct Branch {
80 *
81 * @russian
82 * @brief Уникальное имя ветви.
83 */
84 std::string name;
85
93 std::string head_commit;
94
102 bool is_current;
103
111 Branch() : name(""), head_commit(""), is_current(false) {}
112
126 Branch(std::string name, std::string head_commit, bool is_current = false)
127 : name(std::move(name)), head_commit(std::move(head_commit)), is_current(is_current) {}
128 };
129
139 explicit BranchManager(std::shared_ptr<ISubject> event_bus);
140
149
150 // --- Public Branch Operations / Публичные операции с ветвями ---
151
163 bool createBranch(const std::string& name);
164
178 bool createBranchFromCommit(const std::string& name, const std::string& commit_hash);
179
189 std::string getHeadCommit();
190
206 bool deleteBranch(const std::string& name, bool force = false);
207
221 bool renameBranch(const std::string& old_name, const std::string& new_name);
222
238 bool switchBranch(const std::string& name);
239
240 // --- Public Branch Queries / Публичные запросы о ветвях ---
241
251 std::vector<Branch> getAllBranches() const;
252
262 std::string getCurrentBranch() const;
263
277 bool updateBranchHead(const std::string& branch_name, const std::string& commit_hash);
278
290 bool branchExists(const std::string& name) const;
291
303 std::string getBranchHead(const std::string& branch_name) const;
304
305 // --- Public Utility Methods / Публичные служебные методы ---
306
318 static bool isValidBranchName(const std::string& name);
319
328
331 static bool commitExists(const std::string& commit_hash);
332
333private:
334 // --- Private Persistence Methods / Приватные методы сохранения состояния ---
335
343 void loadBranches();
344
352 void saveBranches();
353
365 static void saveBranchToFile(const std::string &branch_name, const std::string &commit_hash);
366
376 static void deleteBranchFile(const std::string &branch_name);
377
385 void loadCurrentBranch();
386
396 bool saveCurrentBranch() const;
397
398 // --- Private File System Abstraction Methods / Приватные методы абстракции файловой системы ---
399
411 static std::string readFile(const std::string& path);
412
424 static void writeFile(const std::string& path, const std::string& content);
425
437 static bool fileExists(const std::string& path);
438
448 static void createDirectory(const std::string& path);
449
450 // --- Private Path Generation Methods / Приватные методы генерации путей ---
451
461 static std::string getBranchesFilePath();
462
472 static std::string getHeadFilePath();
473
483 static std::string getBranchesDirectory();
484
492 std::unordered_map<std::string, Branch> branches;
493
501 std::string current_branch;
502
510 std::shared_ptr<ISubject> event_bus;
511};
512
513}
Defines the interface for the Subject (Publisher) component of the Observer pattern.
std::string getBranchHead(const std::string &branch_name) const
Retrieves the commit hash that a specific branch points to.
Definition BranchManager.cxx:274
bool renameBranch(const std::string &old_name, const std::string &new_name)
Renames an existing branch.
Definition BranchManager.cxx:138
BranchManager(std::shared_ptr< ISubject > event_bus)
Constructs a BranchManager.
Definition BranchManager.cxx:22
static bool isValidBranchName(const std::string &name)
Checks if the given string is a valid name for a new branch.
Definition BranchManager.cxx:291
std::vector< Branch > getAllBranches() const
Retrieves a list of all known branches, including their state.
Definition BranchManager.cxx:234
bool createBranchFromCommit(const std::string &name, const std::string &commit_hash)
Creates a new branch from a specific commit.
Definition BranchManager.cxx:57
bool branchExists(const std::string &name) const
Checks if a branch with the given name exists.
Definition BranchManager.cxx:270
std::string getCurrentBranch() const
Retrieves the name of the currently active branch.
Definition BranchManager.cxx:266
std::string getHeadCommit()
Get the current HEAD commit hash.
Definition BranchManager.cxx:250
void createDefaultBranches()
Creates the initial set of default branches (e.g., 'main').
Definition BranchManager.cxx:33
static bool commitExists(const std::string &commit_hash)
Checks if a commit exists (implementation assumed to check the object store).
Definition BranchManager.cxx:314
bool deleteBranch(const std::string &name, bool force=false)
Deletes an existing branch.
Definition BranchManager.cxx:105
bool switchBranch(const std::string &name)
Switches the active branch to the specified name.
Definition BranchManager.cxx:215
bool updateBranchHead(const std::string &branch_name, const std::string &commit_hash)
Updates the commit hash that a specific branch points to.
Definition BranchManager.cxx:192
bool createBranch(const std::string &name)
Creates a new branch pointing to a specific commit (usually HEAD).
Definition BranchManager.cxx:44
Core VCS data structures and object model.
Service layer components and infrastructure services.
Branch()
Default constructor.
Definition BranchManager.hxx:71
std::string head_commit
The hash of the latest commit on this branch.
Definition BranchManager.hxx:59
bool is_current
Flag indicating if this is the currently active branch.
Definition BranchManager.hxx:65
std::string name
The unique name of the branch.
Definition BranchManager.hxx:53