-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitignore.cpp
66 lines (56 loc) · 2.06 KB
/
gitignore.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
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <set>
void add_to_gitignore(const std::string& filename) {
const std::string gitignore_filename = ".gitignore";
std::set<std::string> ignore_entries;
// Check if .gitignore exists, if not create it and warn the user
if (!std::filesystem::exists(gitignore_filename)) {
std::ofstream gitignore_file(gitignore_filename);
if (!gitignore_file) {
std::cerr << "Error: Could not create .gitignore file." << std::endl;
return;
}
std::cout << "Warning: .gitignore file created as it did not exist." << std::endl;
}
// Read existing .gitignore entries
std::ifstream gitignore_file(gitignore_filename);
if (gitignore_file) {
std::string line;
while (std::getline(gitignore_file, line)) {
ignore_entries.insert(line);
}
} else {
std::cerr << "Error: Could not read .gitignore file." << std::endl;
return;
}
gitignore_file.close();
// Check if the file to be ignored is already in .gitignore
if (ignore_entries.find(filename) != ignore_entries.end()) {
std::cout << "Info: The file \"" << filename << "\" is already in .gitignore." << std::endl;
return;
}
// Add the file to .gitignore
std::ofstream gitignore_file_out(gitignore_filename, std::ios::app);
if (gitignore_file_out) {
gitignore_file_out << filename << std::endl;
std::cout << "Info: The file \"" << filename << "\" has been added to .gitignore." << std::endl;
} else {
std::cerr << "Error: Could not write to .gitignore file." << std::endl;
}
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: gitignore <filename>" << std::endl;
return 1;
}
std::string filename = argv[1];
if (!std::filesystem::exists(filename)) {
std::cerr << "Error: The file \"" << filename << "\" does not exist." << std::endl;
return 1;
}
add_to_gitignore(filename);
return 0;
}