-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
121 lines (97 loc) · 3.01 KB
/
Tree.cpp
File metadata and controls
121 lines (97 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "Tree.h"
#include "Repository.h"
std::string Tree::getHeadRef() const {
return m_head;
}
void Tree::addNewBranch(const std::string& branch_name, const Commit& branch_head) {
if (m_branches.find(branch_name) != m_branches.end()) {
m_branches.erase(branch_name);
}
m_branches.emplace(branch_name, branch_head.getCommitRef());
}
void Tree::setActiveBranchToNULL() {
m_active_branch = "";
}
void Tree::updateBranchHead(const std::string& branch_name, const Commit& branch_head) {
if (branch_name == m_active_branch) {
updateHead(branch_head.getCommitRef());
}
else {
if (m_branches.find(branch_name) != m_branches.end()) {
m_branches.erase(branch_name);
}
m_branches.emplace(branch_name, branch_head.getCommitRef());
}
}
Commit Tree::getGivenBranchHead(const std::string& branch_name) const {
std::string commit_ref = m_branches.at(branch_name);
Commit commit = Commit();
commit.setCommitRef(commit_ref);
commit.readFromFile();
return commit;
}
void Tree::updateHead(const std::string& commit_ref) {
if (m_branches.find(m_active_branch) != m_branches.end()) {
m_branches.erase(m_active_branch);
}
m_branches.emplace(m_active_branch, commit_ref);
m_head = commit_ref;
}
void Tree::removeBranch(const std::string& branch_name) {
if (m_active_branch == branch_name) return;
m_branches.erase(branch_name);
}
void Tree::addNewBranch(const std::string& branch_name) {
if (m_branches.find(branch_name) != m_branches.end()) {
m_branches.erase(branch_name);
}
m_branches.emplace(branch_name, m_head);
}
const std::unordered_map<std::string, std::string>& Tree::getBranches() const {
return m_branches;
}
bool Tree::isBranchExist(const std::string& branch_name) const {
return m_branches.find(branch_name) != m_branches.end();
}
const std::string& Tree::getActiveBranch() const {
return m_active_branch;
}
void Tree::setActiveBranch(const std::string& branch_name) {
if (m_branches.find(branch_name) != m_branches.end()) {
m_active_branch = branch_name;
m_head = m_branches[branch_name];
}
}
void Tree::add(Commit& commit) {
commit.setParentRef(m_head);
if (m_branches.find(m_active_branch) != m_branches.end()) {
m_branches.erase(m_active_branch);
}
if (m_branches.size() == 0) {
m_active_branch = "master";
}
m_branches.emplace(m_active_branch, commit.getCommitRef());
m_head = commit.getCommitRef();
}
Commit Tree::getHead() const {
Commit commit = Commit();
commit.setCommitRef(m_head);
commit.readFromFile();
return commit;
}
// persistence
void Tree::readFromFile() {
if (fs::exists(Repository::TREE) == false) {
return;
}
readObject(Repository::TREE, *this);
}
void Tree::writeToFile() {
writeObject(Repository::TREE, *this);
}
// todo
void Tree::readFromRemoteFile(const fs::path& remote_repo) {
}
// todo
void Tree::writeToRemoteFile(const fs::path& remote_repo) {
}