-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoid Merge.cpp
More file actions
49 lines (43 loc) · 1.75 KB
/
Copy pathVoid Merge.cpp
File metadata and controls
49 lines (43 loc) · 1.75 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
void merge(const string& targetBranch) {
string currentBranch = getCurrentBranch();
string currentCommit = getBranchHead(currentBranch);
string targetCommit = getBranchHead(targetBranch);
if (targetCommit == "null") {
cout << "Branch not found or no commits in target branch." << endl;
return;
}
ifstream targetIn(COMMITS_DIR + "/" + targetCommit);
string line, mergedData;
unordered_map<string, string> mergedFiles;
while (getline(targetIn, 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(":");
string file = line.substr(0, sep);
string hash = line.substr(sep + 1);
mergedFiles[file] = hash;
}
}
ifstream currentIn(COMMITS_DIR + "/" + currentCommit);
while (getline(currentIn, 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(":");
string file = line.substr(0, sep);
string hash = line.substr(sep + 1);
if (mergedFiles.find(file) != mergedFiles.end() && mergedFiles[file] != hash) {
cout << "CONFLICT: both modified " << file << endl;
}
mergedFiles[file] = hash; // prefer current
}
}
ofstream indexOut(INDEX_FILE);
for (const auto& [file, hash] : mergedFiles) {
indexOut << file << ":" << hash << endl;
}
stringstream msg;
msg << "Merged branch " << targetBranch;
commit(msg.str());
}