-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
267 lines (244 loc) · 8.79 KB
/
Copy pathmain.cpp
File metadata and controls
267 lines (244 loc) · 8.79 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <iostream>
#include <fstream>
#include <filesystem>
#include <vector>
#include <string>
#include <regex>
#include <sstream>
#include <optional>
#include <algorithm>
namespace fs = std::filesystem;
// A structure representing a single .gitignore rule.
struct GitIgnoreRule {
std::regex patternRegex; // The pattern converted to a regex.
bool negate = false; // True if the rule starts with '!'
bool directoryOnly = false; // True if the pattern ends with '/'
bool anchored = false; // True if the pattern starts with '/'
std::string originalPattern; // The original pattern text.
};
// Utility: trim whitespace.
std::string trim(const std::string &s) {
const char* ws = " \t\r\n";
size_t start = s.find_first_not_of(ws);
if (start == std::string::npos)
return "";
size_t end = s.find_last_not_of(ws);
return s.substr(start, end - start + 1);
}
/**
* Convert a .gitignore pattern into a regex string.
* This implementation handles:
* - Leading '/' (anchored) by prefixing the regex with '^'
* - Trailing '/' (directoryOnly) by forcing a match to the end.
* - '*' matches any characters except '/'
* - '**' matches any characters (including '/')
* - '?' matches any single character except '/'
*
* This conversion is a best‐effort approximation and does not cover all edge cases.
*/
std::string patternToRegex(const std::string &pattern, bool anchored) {
std::ostringstream oss;
if (anchored)
oss << "^";
for (size_t i = 0; i < pattern.size(); ++i) {
char c = pattern[i];
if (c == '*') {
// Check for a double star.
if (i + 1 < pattern.size() && pattern[i+1] == '*') {
oss << ".*";
++i;
} else {
oss << "[^/]*";
}
} else if (c == '?') {
oss << "[^/]";
} else if (c == '.') {
oss << "\\.";
} else if (c == '+') {
oss << "\\+";
} else if (c == '(' || c == ')' || c == '|' || c == '^' || c == '$' || c == '{' || c == '}' || c == '[' || c == ']') {
oss << "\\" << c;
} else {
oss << c;
}
}
// If the pattern was for a directory, ensure it matches a trailing slash or end.
// (Git’s behavior is more nuanced, but here we force end-of-string.)
// You might want to modify this if needed.
if (!pattern.empty() && pattern.back() == '/')
oss << ".*";
return oss.str();
}
/**
* Parse a single line of a .gitignore file into a GitIgnoreRule.
* Returns std::nullopt for blank lines or comments.
*/
std::optional<GitIgnoreRule> parseGitIgnoreLine(const std::string &line) {
std::string trimmedLine = trim(line);
if (trimmedLine.empty() || trimmedLine[0] == '#')
return std::nullopt;
GitIgnoreRule rule;
rule.originalPattern = trimmedLine;
// Check for negation.
if (trimmedLine[0] == '!') {
rule.negate = true;
trimmedLine = trim(trimmedLine.substr(1));
}
// Check for a trailing slash.
if (!trimmedLine.empty() && trimmedLine.back() == '/') {
rule.directoryOnly = true;
}
// Check for anchored rule (pattern starts with '/')
if (!trimmedLine.empty() && trimmedLine[0] == '/') {
rule.anchored = true;
trimmedLine = trimmedLine.substr(1); // remove the leading slash
}
std::string regexStr = patternToRegex(trimmedLine, rule.anchored);
// For our purposes, match the entire string.
regexStr += "$";
try {
rule.patternRegex = std::regex(regexStr, std::regex::ECMAScript);
} catch (std::regex_error &e) {
std::cerr << "Regex error for pattern \"" << trimmedLine << "\": " << e.what() << "\n";
return std::nullopt;
}
return rule;
}
/**
* Parse a .gitignore file and return a vector of rules.
*/
std::vector<GitIgnoreRule> parseGitIgnore(const fs::path &gitignorePath) {
std::vector<GitIgnoreRule> rules;
std::ifstream file(gitignorePath);
if (!file.is_open())
return rules;
std::string line;
while (std::getline(file, line)) {
auto ruleOpt = parseGitIgnoreLine(line);
if (ruleOpt.has_value()) {
rules.push_back(ruleOpt.value());
}
}
return rules;
}
/**
* Determine if the given relative path (with '/' as separator) matches a single rule.
*/
bool matchesRule(const GitIgnoreRule &rule, const std::string &relPath, bool isDir) {
// If rule is directory-only but this is not a directory, it does not match.
if (rule.directoryOnly && !isDir)
return false;
return std::regex_match(relPath, rule.patternRegex);
}
/**
* Given a list of GitIgnoreRule objects (ordered in the order they appear),
* determine if the file with the given relative path should be ignored.
*
* Git’s behavior is that the last matching rule wins.
*/
bool isIgnored(const std::vector<GitIgnoreRule> &rules, const fs::path &baseDir, const fs::path &filePath) {
// Compute the relative path from baseDir, using '/' as separator.
fs::path rel = fs::relative(filePath, baseDir);
std::string relPath = rel.generic_string(); // always uses '/'
bool ignored = false;
for (const auto &rule : rules) {
if (matchesRule(rule, relPath, fs::is_directory(filePath))) {
ignored = !rule.negate; // last match wins
}
}
return ignored;
}
/**
* Recursively gather .gitignore rules from the given directory and its parents.
* (For a fully correct implementation you would also need to merge rules from nested .gitignore files.)
*/
std::vector<GitIgnoreRule> gatherGitIgnoreRules(const fs::path &startDir) {
std::vector<GitIgnoreRule> rules;
fs::path current = fs::canonical(startDir);
while (true) {
fs::path gitignoreFile = current / ".gitignore";
if (fs::exists(gitignoreFile) && fs::is_regular_file(gitignoreFile)) {
auto fileRules = parseGitIgnore(gitignoreFile);
// Append the rules (Git applies .gitignore files in order from the root downward)
rules.insert(rules.end(), fileRules.begin(), fileRules.end());
}
if (!current.has_parent_path() || current == current.parent_path())
break;
current = current.parent_path();
}
return rules;
}
/**
* A heuristic to check if a file is binary.
*/
bool isBinaryFile(const fs::path &filePath) {
std::ifstream in(filePath, std::ios::binary);
if (!in)
return false;
const size_t sampleSize = 512;
char buffer[sampleSize];
in.read(buffer, sampleSize);
std::streamsize bytesRead = in.gcount();
if (bytesRead == 0)
return false;
int nonPrintable = 0;
for (int i = 0; i < bytesRead; ++i) {
unsigned char c = static_cast<unsigned char>(buffer[i]);
if (!((c >= 32 && c <= 126) || c == 9 || c == 10 || c == 13))
nonPrintable++;
}
return (static_cast<double>(nonPrintable) / bytesRead) > 0.30;
}
/**
* Recursively process the directory and write text file contents into combined.txt.
*/
void processDirectory(const fs::path &dir,
std::ofstream &out,
const std::vector<GitIgnoreRule> &rules,
const fs::path &baseDir)
{
for (const auto &entry : fs::directory_iterator(dir)) {
if (!entry.exists())
continue;
fs::path path = entry.path();
if (path.filename() == ".gitignore" || path.filename() == "combined.txt")
continue;
if (isIgnored(rules, baseDir, path))
continue;
if (fs::is_directory(path)) {
processDirectory(path, out, rules, baseDir);
} else {
if (isBinaryFile(path)) {
std::cerr << "Skipping binary file: " << path << "\n";
continue;
}
out << "# File: " << path.string() << "\n\n";
std::ifstream inFile(path);
if (inFile)
out << inFile.rdbuf() << "\n\n";
else
std::cerr << "Failed to open file: " << path << "\n";
}
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <directory_path>\n";
return 1;
}
fs::path targetDir(argv[1]);
if (!fs::exists(targetDir) || !fs::is_directory(targetDir)) {
std::cerr << "Invalid directory: " << targetDir << "\n";
return 1;
}
std::ofstream outFile("combined.txt", std::ios::out | std::ios::binary);
if (!outFile) {
std::cerr << "Failed to create output file combined.txt\n";
return 1;
}
// Gather .gitignore rules from the directory and its parents.
auto rules = gatherGitIgnoreRules(targetDir);
processDirectory(targetDir, outFile, rules, targetDir);
std::cout << "Files have been combined into combined.txt\n";
return 0;
}