-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths-git.cpp
More file actions
96 lines (80 loc) · 2.4 KB
/
s-git.cpp
File metadata and controls
96 lines (80 loc) · 2.4 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
#include "s-git.h"
#include <map>
#include <string>
#include <iostream>
const char *GIT_NAME = "s-git";
const char *GIT_DESC = "s-git"
" is a simple but not stupid version-control system works like git.";
std::optional<std::filesystem::path> GIT_DIR{};
std::optional<std::filesystem::path> ROOT_DIR{};
std::filesystem::path CWD{};
static std::map<std::string, Command> funcTable;
static int help(int, const char*[]);
static int version(int, const char*[]);
static void initFuncTable() {
funcTable.emplace("help", Command{ help, "show this description." });
funcTable.emplace("version", Command{ version, "version info." });
funcTable.emplace("init", InitCommand);
funcTable.emplace("status", StatusCommand);
funcTable.emplace("commit", CommitCommand);
funcTable.emplace("checkout", CheckoutCommand);
funcTable.emplace("tag", TagCommand);
funcTable.emplace("log", LogCommand);
funcTable.emplace("branch", BranchCommand);
funcTable.emplace("ls-tree", LsTreeCommand);
funcTable.emplace("merge", MergeCommand);
}
static void usage() {
std::cout << GIT_DESC << '\n';
std::cout << "usage: " << GIT_NAME << " <command>\n";
std::cout << "These are commands available:\n\n";
for (auto &func : funcTable) {
std::cout << '\t' << func.first << ": " << func.second.description << '\n';
}
std::cout << "\nUse " << GIT_NAME << " <command> --help for command specific usage.\n";
std::cout.flush();
}
static void setupDir() {
CWD = std::filesystem::current_path();
auto root = CWD.root_path();
auto path = CWD;
while (path != root) {
auto git = path / (std::string{ '.' } + GIT_NAME);
if (std::filesystem::exists(git) && std::filesystem::is_directory(git)) {
GIT_DIR = git;
ROOT_DIR = path;
return;
}
path = path.parent_path();
}
}
int main(int argc, const char *argv[]) {
initFuncTable();
if (argc < 2) {
usage();
return 1;
}
auto commandIt = funcTable.find(argv[1]);
if (commandIt != funcTable.end()) {
// drop argv[0] and change argv[1]
std::string exeName(GIT_NAME);
exeName += ' ';
exeName += argv[1];
argv[1] = exeName.c_str();
setupDir();
return commandIt->second.function(argc - 1, argv + 1);
} else {
std::cout << GIT_NAME << ": '" << argv[1] << "' is not a "
<< GIT_NAME << " command.\n";
usage();
return 1;
}
}
int version(int, const char*[]) {
std::cout << GIT_NAME << " versoin 0.7.0" << std::endl;
return 0;
}
int help(int, const char*[]) {
usage();
return 0;
}