-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.cpp
96 lines (73 loc) · 2.17 KB
/
cli.cpp
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
#include "cli.h"
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
#include "repository.h"
namespace fs = std::filesystem;
namespace tog::cli {
using namespace tog;
tog::Repository load_repository() {
const auto path = fs::current_path() / ".tog";
if (!fs::exists(path)) {
throw TogException("Not a tog repository");
}
return Repository{path};
}
void init() {
try {
Repository::init(fs::current_path());
} catch (const TogException &err) {
std::cerr << "Error " << err.what() << std::endl;
}
std::cout << "Initialized tog repository" << std::endl;
}
void commit(const std::string &message) {
try {
auto repo = load_repository();
auto hash = repo.commit(message);
std::cout << "Created commit " << hash << std::endl;
} catch (const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
void checkout(const std::string &hash) {
try {
auto repo = load_repository();
repo.checkout(hash);
std::cout << "Checked out commit " << hash << std::endl;
} catch (const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
void status() {
try {
auto repo = load_repository();
auto head = repo.head();
auto main = repo.main();
std::cout << "On branch main" << std::endl;
if (main) {
std::cout << "Current commit: " << *head << std::endl;
std::cout << "Latest commit: " << *main << std::endl;
} else {
std::cout << "No commits yet" << std::endl;
}
} catch (const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
void log(int history_length) {
try {
auto repo = load_repository();
auto history = repo.history(history_length);
if (history.empty()) {
std::cout << "No commits yet" << std::endl;
}
for (const auto &hash : history) {
std::cout << hash << std::endl;
}
} catch (const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
} // namespace tog::cli