-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcgit.cpp
More file actions
87 lines (71 loc) · 2.49 KB
/
Copy pathcgit.cpp
File metadata and controls
87 lines (71 loc) · 2.49 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
#include "cgit.hpp"
#include <memory.h>
#include <iostream>
#include <sstream>
#include "utils.hpp"
#ifdef WIN32
#define DEFAULT_GIT_FILE "C:\\Program Files\\Git\\bin\\git.exe"
#else
#define DEFAULT_GIT_FILE "/usr/bin/git"
#endif
#define DEFAULT_GITHUB_URL "https://github.com/"
#define DEFAULT_MIRROR_URL "https://gitclone.com/github.com/"
namespace cgit {
using namespace std;
static string create_cmd(std::vector<string> &args) {
std::ostringstream ss;
for (const auto &s:args) {
#ifdef WIN32
ss << s << " ";
#else
ss << "\"" << s << "\" ";
#endif
}
return ss.str();
}
string get_git_file(bool q) {
auto git = get_env("GIT", DEFAULT_GIT_FILE);
if (git == nullptr) {
printf("please set the environment variable `GIT`.");
exit(-1);
}
string git_str(git);
if (q && git_str[0] != '"') git_str = '"' + git_str + '"';
return git_str;
}
string get_cgit_mirror() {
string url = get_env("CGIT_MIRROR", DEFAULT_MIRROR_URL);
if (url.length() > 0 && !url.endswith("/"))url += '/';
return url;
}
int cgit_clone(vector<string> &args) {
// 0.get env for GIT and CGIT_MIRROR
string git_file = get_git_file();
string mirror_url = get_cgit_mirror();
// 1.git clone https://gitclone.com/github.com/killf/cgit.git
string old_url, new_url, folder_name;
for (size_t i = 2; i < args.size(); i++) {
if (args[i].startsWith(DEFAULT_GITHUB_URL)) {
old_url = args[i];
new_url = args[i].replace(0, strlen(DEFAULT_GITHUB_URL), mirror_url.c_str());
auto index = old_url.find_last_of('/');
if (index != std::string::npos) {
folder_name = old_url.substr(index + 1);
if (folder_name.endswith(".git")) {
folder_name = folder_name.substr(0, folder_name.size() - 4);
}
}
}
}
args[0] = git_file.c_str();
auto cmd1 = create_cmd(args);
auto err = system(cmd1.c_str());
if (err != 0 || new_url.empty() || folder_name.empty())return err;
// 2.cd cgit
err = cd(folder_name);
if (err != 0)return err;
// 3.git remote set-url origin https://github.com/killf/cgit.git
auto cmd3 = "git remote set-url origin \"" + old_url + "\"";
return system(cmd3.c_str());
}
}