Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (145 loc) · 4.33 KB
/
Copy pathmain.cpp
File metadata and controls
156 lines (145 loc) · 4.33 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "tree.cpp"
bool check()
{
for (const auto &enty : fs::directory_iterator(fs::current_path()))
{
if (enty.path().filename() == ".pgit")
return true;
}
return false;
}
void init()
{
if (check())
{
std::cout << "pgit already initialised" << std::endl;
std::cout << fs::current_path();
}
else
{
auto base = fs::current_path() / fs::path(".pgit");
fs::create_directories(base);
fs::create_directories(base / fs::path("diff"));
fs::create_directories(base / fs::path("file"));
fs::create_directories(base / fs::path("stage"));
std::cout << "pgit initialised" << std::endl;
}
}
int main(int argc, char *argv[])
{
if(argc==1){
std::cout<<"enter a command"<<std::endl;
return 1;
}
if (std::strcmp(argv[1], "snapshot") == 0)
{
if (!check())
{
std::cout << "pgit not initialized" << std::endl;
return 0;
}
if (argc == 3)
{
json tree_json;
if (!fs::exists(fs::current_path() / fs::path(".pgit") / fs::path("tree.json")))
{
std::fstream ff;
ff.open(fs::current_path() / fs::path(".pgit") / fs::path("tree.json"), std::ios::out | std::ios::trunc);
ff.close();
}
std::fstream file((fs::current_path() / fs::path(".pgit") / fs::path("tree.json")).string());
file.seekg(0, std::ios::end);
std::streampos size = file.tellg();
file.seekg(0, std::ios::beg);
if (size == 0)
{
Treeobject new_head = Treeobject(argv[2]);
new_head.traverse_make_tree_obj(fs::current_path());
tree_json = new_head.to_json();
file << tree_json.dump(4); // Write formatted JSON to file
file.close();
}
else
{
auto j = loadTreeobject();
j.snapshot(argv[2]);
}
}
else
{
std::cout << "invalid command" << std::endl;
}
}
else if (std::strcmp(argv[1], "init") == 0)
{
if (argc == 2)
{
init();
}
else
{
std::cout << "invalid command" << std::endl;
}
}
else if (std::strcmp(argv[1], "rollback") == 0)
{
if (argc == 3)
{
if (!fs::exists(fs::current_path() / fs::path(".pgit") / fs::path("tree.json")))
{
std::fstream ff;
ff.open(fs::current_path() / fs::path(".pgit") / fs::path("tree.json"), std::ios::out | std::ios::trunc);
ff.close();
}
std::fstream file((fs::current_path() / fs::path(".pgit") / fs::path("tree.json")).string());
file.seekg(0, std::ios::end);
std::streampos size = file.tellg();
file.seekg(0, std::ios::beg);
if (size == 0)
{
std::cout << "no commits made to roll back" << std::endl;
}
else
{
auto j = loadTreeobject();
j.roll_back(argv[2]);
}
}
else
{
std::cout << "invalid command" << std::endl;
}
}
else if (std::strcmp(argv[1], "log") == 0)
{
if (argc == 2)
{
if (!fs::exists(fs::current_path() / fs::path(".pgit") / fs::path("tree.json")))
{
std::fstream ff;
ff.open(fs::current_path() / fs::path(".pgit") / fs::path("tree.json"), std::ios::out | std::ios::trunc);
ff.close();
}
std::fstream file((fs::current_path() / fs::path(".pgit") / fs::path("tree.json")).string());
file.seekg(0, std::ios::end);
std::streampos size = file.tellg();
file.seekg(0, std::ios::beg);
if (size == 0)
{
std::cout << "no commits exists" << std::endl;
}
else
{
auto j = loadTreeobject();
j.log();
}
}
else
{
std::cout << "invalid command" << std::endl;
}
}
else{
std::cout<<"invalid command"<<std::endl;
}
}