-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.cpp
More file actions
37 lines (35 loc) · 1.46 KB
/
diff.cpp
File metadata and controls
37 lines (35 loc) · 1.46 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
void diff(const string& commit1, const string& commit2) {
ifstream in1(COMMITS_DIR + "/" + commit1);
ifstream in2(COMMITS_DIR + "/" + commit2);
if (!in1 || !in2) {
cout << "One of the commits or both are not found." << endl;
return;
}
unordered_map<string, string> map1, map2;
string line;
while (getline(in1, line)) {
if (line.find(":") != string::npos && line.find("message:") != 0 && line.find("timestamp:") != 0 && line.find("parent:") != 0 && line.find("branch:") != 0) {
size_t sep = line.find(":");
map1[line.substr(0, sep)] = line.substr(sep + 1);
}
}
while (getline(in2, line)) {
if (line.find(":") != string::npos && line.find("message:") != 0 && line.find("timestamp:") != 0 && line.find("parent:") != 0 && line.find("branch:") != 0) {
size_t sep = line.find(":");
map2[line.substr(0, sep)] = line.substr(sep + 1);
}
}
cout << "### Diff between " << commit1 << " and " << commit2 << " ###" << endl;
for (const auto& [file, hash1] : map1) {
if (map2.find(file) == map2.end()) {
cout << file << " was removed in " << commit2 << endl;
} else if (map2[file] != hash1) {
cout << file << " was modified." << endl;
}
}
for (const auto& [file, hash2] : map2) {
if (map1.find(file) == map1.end()) {
cout << file << " was added in " << commit2 << endl;
}
}
}