-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitScript.cpp
More file actions
54 lines (47 loc) · 2.16 KB
/
gitScript.cpp
File metadata and controls
54 lines (47 loc) · 2.16 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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <filesystem>
static std::string trim(const std::string &s) {
size_t start = s.find_first_not_of(" \t\n\r");
if (start == std::string::npos) return std::string();
size_t end = s.find_last_not_of(" \t\n\r");
return s.substr(start, end - start + 1);
}
void pushToRepo(const std::string& repoUrl, const std::string& folderPath, const std::string& diskFilename) {
std::string tempRepoDir = ".tmp_readme_repo";
// Clone target repo, update README there, then push current branch.
std::string command = "rm -rf \"" + tempRepoDir + "\" && "
"git clone \"" + repoUrl + "\" \"" + tempRepoDir + "\" && "
"cp \"" + folderPath + diskFilename + "\" \"" + tempRepoDir + "/README.md\" && "
"git -C \"" + tempRepoDir + "\" add README.md && "
"(git -C \"" + tempRepoDir + "\" commit -m \"Update README\" || true) && "
"git -C \"" + tempRepoDir + "\" push origin HEAD";
//Executing and handling the result
int result = system(command.c_str());
if (result != 0) {
std::cerr << "Error executing git command: " << command << std::endl;
}
}
int main() {
std::string repoUrl;
std::string filename; // trimmed stem for repo name
std::string diskFilename; // actual on-disk filename (may contain spaces and extension)
//replace with your GitHub username and the path to the folder containing the .md files
std::string user = "bulgadev";
std::string folderPath = "output/";
//Getting the needed variables and pushing for each markdown file
for (const auto& entry : std::filesystem::directory_iterator(folderPath)) {
if (entry.path().extension() == ".md" && entry.is_regular_file()) {
diskFilename = entry.path().filename().string();
filename = trim(entry.path().stem().string());
if (filename.empty()) {
continue;
}
repoUrl = "git@github.com:" + user + "/" + filename + ".git";
pushToRepo(repoUrl, folderPath, diskFilename);
}
}
return 0;
}