-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.cpp
132 lines (120 loc) · 3.11 KB
/
get.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
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
#include <filesystem>
#include <iostream>
#include <regex>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
std::vector<std::regex> ignorePatterns;
std::vector<std::regex> includePatterns;
std::string regexEscape(const std::string& input) {
std::ostringstream escaped;
for (char c : input) {
switch (c) {
// Add more cases if other characters need to be escaped
case '^':
case '$':
case '\\':
case '.':
case '|':
case '?':
case '*':
case '+':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
escaped << '\\' << c;
break;
default:
escaped << c;
break;
}
}
return escaped.str();
}
//Converts gitignore to close enough regex.
//(the file will be reincluded eaven though
//used for a three structure where this is a performace
//optimization for us that do string processing it is
//cheaper to just do pattern matching)
std::regex parsePattern(const std::string& pattern) {
std::string regexPattern;
bool isDirectoryPattern = (pattern.back() == '/');
for(char c : pattern) {
switch (c) {
case '*':
regexPattern += ".*";
break;
case '?':
regexPattern += ".";
break;
case '/':
regexPattern += "\\/";
break;
default:
regexPattern += regexEscape(std::string(1,c));
break;
}
}
if(isDirectoryPattern) {
regexPattern.pop_back();
regexPattern.pop_back();
regexPattern += ".*";
}
return std::regex(regexPattern);
}
void parseGitIgnoreFile(const std::string& filename) {
std::ifstream file(filename);
std::string line;
while(std::getline(file, line)) {
while(!line.empty() && line.back() == ' ') {
if(line.size() > 1 && line[line.size() - 2] == '\\') {
break;
}
line.pop_back();
}
if(line.empty() || line[0] == '#') {
continue;
}
if(line[0] == '!') {
includePatterns.push_back(parsePattern(line.substr(1)));
} else {
ignorePatterns.push_back(parsePattern(line));
}
}
}
bool shouldIgnore(const std::string& path) {
for(const std::regex& pattern : includePatterns) {
if(std::regex_match(path, pattern)) {
return false;
}
}
for(const std::regex& pattern : ignorePatterns) {
if(std::regex_match(path, pattern)) {
return true;
}
}
return false;
}
int main() {
parseGitIgnoreFile(".gitignore");
parseGitIgnoreFile(".permignore");
struct stat st;
std::string path;
for (std::filesystem::recursive_directory_iterator
i("."), end;
i != end; ++i) {
path = i->path().string().substr(2);
if(shouldIgnore(path)){
continue;
}
//lstat to have symlinks not be transparrent
if (lstat(i->path().c_str(), &st) == 0) {
std::cout << std::oct << (st.st_mode & 04777) << std::dec << " "
<< st.st_uid << " " << st.st_gid << " " << path << std::endl;
}
}
}