-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch.cpp
More file actions
379 lines (187 loc) · 7.11 KB
/
branch.cpp
File metadata and controls
379 lines (187 loc) · 7.11 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "Commit.h"
#include <Windows.h>
#include <list>
namespace fs = std::filesystem;
#include <sstream>
#include "branch.h"
// Helpers declared in Commit.h and defined in Commit.cpp
int branch(std::string path)
{
std::ifstream file;
//check for a project, cause we don't want to commit "aka save" without a project
file.open(path + "\\proj");
if (!file) //if a project is there already
{
std::cout << "ERROR, There is no project at " + path + " that exist. " << std::endl; return -1;
}
std::string name, message;
std::cout << "What would you like to call this branch of project? " + path + "\n>";
std::getline(std::cin >> std::ws, name); std::cout << std::endl;
std::cout << "Making Branch, what should the info message be? \n>";
std::getline(std::cin >> std::ws, message); std::cout << std::endl;
//same thing as the commits, but with some extra stuff
//we create a dir to store the branch, branches will not have any compression,
//they should be like another version of the project, with everything else copied.
std::cout << "Creating The Branch" << std::endl;
std::cout << "Checking if VERSIONS Directory Exists." << std::endl;
if (CommitPathWorks(path + "\\.VERSIONS"))
{
std::cout << "VERSIONS Directory Exists." << std::endl;
}
else
{
std::cout << "The VERSIONS Directory Does not exist." << std::endl;
std::cout << "Creating it now in " + path << std::endl;
std::string commitpos = (path + "\\" + ".VERSIONS");
_mkdir(commitpos.c_str());
}
//set the list of files
std::list<std::string> filesToCommit;
//find all the files in the project.
for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{ path })
{
//we need everything in this project so we comment this out.
//if (!((dir_entry.path().string().find(".COMMIT") != std::string::npos) || (dir_entry.path().string().find("proj") != std::string::npos)))
//{
std::cout << dir_entry << '\n';
filesToCommit.push_front(dir_entry.path().string());
//}
}
//now we check if the files are the same as the most recent branch version
std::list<std::string> lastCommit;
//find the latest folder to make sure we are checking the 2 files
std::string oldest_name;
bool first = true;
for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{ path + "\\" + ".VERSIONS" })
{
std::string foldername = dir_entry.path().filename().string();
if (first || std::atoi(foldername.c_str()) > std::atoi(oldest_name.c_str()))
{
oldest_name = foldername;
first = false;
}
}
for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{ path + "\\" + ".VERSIONS" + "\\" + oldest_name })
{
std::cout << dir_entry << '\n';
lastCommit.push_front(dir_entry.path().string());
}
//Disabled cause user might want multiple branches of the same main.
///////////
/*
//now we need to check the hash contents of that file vs the main commit stream and see if there is any changes, if there isnt, we stop.
//we can do this though combining the file data, then hashing it
//the commit hash
std::string COMMITHASH;
for (auto const& pathssaved : filesToCommit)
{
std::ifstream file(pathssaved);
if (file.is_open())
{
std::stringstream buffer;
buffer << file.rdbuf();
COMMITHASH = COMMITHASH + buffer.str();
}
}
std::hash<std::string> comH;
size_t commitcompare = comH(COMMITHASH);
std::cout << commitcompare << std::endl;
std::string OLDCOMMITHASH;
for (auto const& pathssaved : lastCommit)
{
std::ifstream file(pathssaved);
std::cout << pathssaved << std::endl;
if (file.is_open())
{
std::stringstream buffer;
buffer << file.rdbuf();
OLDCOMMITHASH = OLDCOMMITHASH + buffer.str();
}
}
std::hash<std::string> oldcomH;
size_t oldcommitcompare = oldcomH(OLDCOMMITHASH);
std::cout << oldcommitcompare << std::endl;
if (oldcommitcompare == commitcompare)
{
std::cout << std::endl; std::cout << std::endl;
std::cout << "Nothing has changed since last commit, ending." << std::endl;
return 0;
}
*/
//std::cout << "Commiting: " << std::endl;
//final confirm.
bool loop = true;
while (loop)
{
std::string answer;
std::cout << "Are you sure you want to Create This Branch? Y/N" << std::endl;
std::getline(std::cin >> std::ws, answer); std::cout << std::endl;
if (answer == "N" || answer == "n")
{
std::cout << "Stopping" << std::endl;
return 0;
}
if (answer == "Y" || answer == "y")
{
loop = false;
}
}
//create the commit file in the .COMMIT and upload data to it.
//lets use the date as its special signature, cause its never gonna happen again.
//first clear old commits
/*for (const auto& entry : std::filesystem::directory_iterator(path + "\\" + ".COMMIT"))
std::filesystem::remove_all(entry.path());*/
_mkdir((path + "\\" + ".VERSIONS" + "\\" + name).c_str());
std::cout << "Creating Branch." << std::endl;
copyFiles(filesToCommit, (path + "\\" + ".VERSIONS" + "\\" + name), path);
std::cout << "Branch Made." << std::endl;
//Make description
std::ofstream Discript(path + "\\" + ".VERSIONS" + "\\" + name + "\\" + "Discript.branch");
if (Discript.is_open()) {
Discript << message << std::endl;
}
Discript.close();
//WRITE TO MAIN ABOUT THE BRANCH
//Create a buffer to hold the Path
char exePathBuf[MAX_PATH] = { 0 };
//Get the path of the executable
DWORD len = GetModuleFileNameA(NULL, exePathBuf, MAX_PATH);
//Convert to std::filesystem::path and remove the filename to get the directory
std::filesystem::path exeDir = std::filesystem::path(std::string(exePathBuf)).remove_filename();
//add the branches.list file type
std::filesystem::path branchesPath = exeDir / "branches.list";
std::ofstream pathfile(branchesPath.string(), std::ios::app);
if (pathfile.is_open())
{
pathfile << path << "\\" << ".VERSIONS" << "\\" << name << " | ";
pathfile.close();
}
system("cls");
std::cout << "Branch " + name + " Created Successfully at " + path + "\\" + ".VERSIONS" + "\\" + name << std::endl;
std::cout << "Branch Info: " + message << std::endl;
std::cout << std::endl;
std::cout << "If you want to see all branches check - " + exeDir.string() + "branches.list" << std::endl;
std::cout << "Or you can check with: git branch list" << std::endl;
//std::cout << "THE PATH IS: " + system("echo $PWD") << std::endl;
return 0;
}////////////////////
int listbranch()
{
//Create a buffer to hold the Path
char exePathBuf[MAX_PATH] = { 0 };
//Get the path of the running program (so our git)
DWORD len = GetModuleFileNameA(NULL, exePathBuf, MAX_PATH);
//Convert to std::filesystem::path and remove the filename to get the directory
std::filesystem::path exeDir = std::filesystem::path(std::string(exePathBuf)).remove_filename();
//add the branches.list file type
std::filesystem::path branchesPath = exeDir / "branches.list";
std::cout << "BRANCH LIST: " << std::endl;
std::ifstream branch;
branch.open(branchesPath);
std::string line;
while (std::getline(branch, line)) {
std::cout << ">> " << line << std::endl;
std::cout << "" << std::endl;
}
return 0;
}