-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.cpp
More file actions
160 lines (140 loc) · 5 KB
/
patch.cpp
File metadata and controls
160 lines (140 loc) · 5 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
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "patch",
.version = CFBOX_VERSION_STRING,
.one_line = "apply a diff file to an original",
.usage = "patch [FILE]",
.options = "",
.extra = "",
};
struct Hunk {
int old_start;
int old_count;
std::vector<std::string> lines; // '+', '-', ' ' prefixed
};
static auto parse_hunk_header(const std::string& line) -> Hunk {
Hunk h{};
h.old_start = 1;
h.old_count = 0;
// @@ -old_start[,old_count] +new_start[,new_count] @@
auto p = line.c_str();
while (*p && *p != '-') ++p;
if (*p == '-') ++p;
h.old_start = static_cast<int>(std::strtol(p, &const_cast<char*&>(p), 10));
h.old_count = 1;
if (*p == ',') { ++p; h.old_count = static_cast<int>(std::strtol(p, &const_cast<char*&>(p), 10)); }
return h;
}
static auto is_diff_line(const std::string& s) -> bool {
if (s.empty()) return false;
return s[0] == ' ' || s[0] == '-' || s[0] == '+';
}
} // namespace
auto patch_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
auto patch_result = cfbox::io::read_all_stdin();
if (!patch_result) {
CFBOX_ERR("patch", "%s", patch_result.error().msg.c_str());
return 1;
}
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("patch", "missing file operand");
return 1;
}
std::string filepath{pos[0]};
auto file_result = cfbox::io::read_lines(filepath);
if (!file_result) {
CFBOX_ERR("patch", "%s", file_result.error().msg.c_str());
return 1;
}
auto& lines = *file_result;
auto patch_data = cfbox::io::split_lines(*patch_result);
// Detect format: unified has @@, normal diff doesn't
bool has_hunk_headers = false;
for (const auto& l : patch_data) {
if (l.starts_with("@@")) { has_hunk_headers = true; break; }
}
std::vector<Hunk> hunks;
if (has_hunk_headers) {
std::size_t i = 0;
while (i < patch_data.size() && !patch_data[i].starts_with("@@")) ++i;
while (i < patch_data.size()) {
if (!patch_data[i].starts_with("@@")) { ++i; continue; }
Hunk h = parse_hunk_header(patch_data[i]);
++i;
int old_seen = 0;
while (i < patch_data.size() && old_seen < h.old_count) {
auto& pline = patch_data[i];
if (pline.starts_with("@@")) break;
if (!pline.empty()) {
h.lines.push_back(pline);
if (pline[0] == '-' || pline[0] == ' ') ++old_seen;
}
++i;
}
// Also consume remaining '+' lines from this hunk
while (i < patch_data.size() && !patch_data[i].starts_with("@@") &&
patch_data[i].starts_with("+")) {
h.lines.push_back(patch_data[i]);
++i;
}
hunks.push_back(std::move(h));
}
} else {
// Normal diff: treat entire input as one hunk at line 1
Hunk h{};
h.old_start = 1;
for (const auto& pline : patch_data) {
if (is_diff_line(pline)) {
h.lines.push_back(pline);
}
}
if (!h.lines.empty()) {
h.old_count = 0;
for (const auto& l : h.lines) {
if (!l.empty() && (l[0] == '-' || l[0] == ' ')) ++h.old_count;
}
hunks.push_back(std::move(h));
}
}
// Apply hunks in reverse order to preserve line numbers
for (auto hi = hunks.rbegin(); hi != hunks.rend(); ++hi) {
auto& h = *hi;
int start = h.old_start - 1;
if (start < 0) start = 0;
int remove_count = 0;
for (auto& l : h.lines) if (!l.empty() && l[0] != '+') ++remove_count;
if (start + remove_count > static_cast<int>(lines.size())) {
CFBOX_ERR("patch", "hunk at line %d doesn't match", h.old_start);
return 1;
}
std::vector<std::string> replacement;
for (auto& l : h.lines) {
if (l.empty()) continue;
if (l[0] == ' ') replacement.emplace_back(l.substr(1));
else if (l[0] == '+') replacement.emplace_back(l.substr(1));
}
lines.erase(lines.begin() + start, lines.begin() + start + remove_count);
lines.insert(lines.begin() + start, replacement.begin(), replacement.end());
}
// Write result
std::string output;
for (const auto& l : lines) output += l + "\n";
auto wresult = cfbox::io::write_all(filepath, output);
if (!wresult) {
CFBOX_ERR("patch", "%s", wresult.error().msg.c_str());
return 1;
}
return 0;
}