Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
135 lines (125 loc) · 5.15 KB
/
Copy pathmain.cpp
File metadata and controls
135 lines (125 loc) · 5.15 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
#include <iostream>
#include <string>
#include <sstream>
#include "FileVersionManager.hpp"
void printHelp() {
std::cout << "\nVersion Control System - Available Commands:\n"
<< "-------------------------------------------\n"
<< "init <filename> - Initialize version control for a file\n"
<< "save [comment] - Save current state as a new version (optional comment)\n"
<< "list [--preview] - List all versions (--preview shows content previews)\n"
<< "restore <version> - Restore to a specific version number\n"
<< "view <version> - View the full content of a specific version\n"
<< "delete <version> - Delete a specific version\n"
<< "latest - Restore to the latest version\n"
<< "backup - Restore from original backup\n"
<< "help - Show this help message\n"
<< "exit - Quit the program\n"
<< "-------------------------------------------\n";
}
int main() {
std::cout << "Welcome to Version Control System\n";
printHelp();
std::string input;
FileVersionManager* manager = nullptr;
while (true) {
std::cout << "\n> ";
std::getline(std::cin, input);
std::istringstream iss(input);
std::string command;
iss >> command;
try {
if (command == "exit") {
break;
}
else if (command == "help") {
printHelp();
}
else if (command == "init") {
std::string filename;
iss >> filename;
if (filename.empty()) {
std::cout << "Error: Please provide a filename\n";
continue;
}
delete manager; // Clean up any existing manager
try {
manager = new FileVersionManager(filename);
std::cout << "Initialized version control for: " << filename << std::endl;
} catch (const std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
manager = nullptr;
}
}
else if (!manager) {
std::cout << "Error: Please initialize a file first using 'init <filename>'\n";
continue;
}
else if (command == "save") {
std::string comment;
std::getline(iss, comment);
// Trim leading whitespace from comment
comment = comment.substr(comment.find_first_not_of(" \t"));
manager->createVersion(comment);
std::cout << "Created version " << manager->getCurrentVersion()
<< (comment.empty() ? "" : " with comment: " + comment) << std::endl;
}
else if (command == "list") {
std::string arg;
iss >> arg;
bool showPreview = (arg == "--preview");
manager->listVersions(showPreview);
}
else if (command == "restore") {
int version;
if (iss >> version) {
if (manager->restoreVersion(version)) {
std::cout << "File restored to version " << version << std::endl;
}
} else {
std::cout << "Error: Please provide a valid version number\n";
}
}
else if (command == "view") {
int version;
if (iss >> version) {
manager->viewVersion(version);
} else {
std::cout << "Error: Please provide a valid version number\n";
}
}
else if (command == "delete") {
int version;
if (iss >> version) {
if (manager->deleteVersion(version)) {
std::cout << "Version " << version << " deleted successfully\n";
} else {
std::cout << "Failed to delete version " << version << "\n";
}
} else {
std::cout << "Error: Please provide a valid version number\n";
}
}
else if (command == "latest") {
if (manager->restoreLatest()) {
std::cout << "File restored to latest version ("
<< manager->getCurrentVersion() << ")\n";
}
}
else if (command == "backup") {
if (manager->restoreFromBackup()) {
std::cout << "File restored from original backup\n";
}
}
else {
std::cout << "Unknown command. Type 'help' for available commands.\n";
}
}
catch (const std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
delete manager;
std::cout << "Goodbye!\n";
return 0;
}