Skip to content

Commit

Permalink
merge conflict part
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmet-Ray committed Jul 14, 2023
1 parent 954d085 commit 79933a6
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 7 deletions.
44 changes: 38 additions & 6 deletions proj2/gitlet/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,17 +748,21 @@ public static void merge(String branch) throws IOException {
untrackedFiles();

specialMerge(branch);
generalMerge(branch);
boolean conflict = generalMerge(branch);
String secondParent = readBranchHeadCommit(branch);
commitHelper("Merged " + branch + " into " + readContentsAsString(HEAD) + ".", secondParent);
if (conflict) {
System.out.println("Encountered a merge conflict.");
}
}

/**
* for every file in these 3 part : split point, other commit, current commit
* call generalMergeHelper method
* @param branch
* @return return whether this merge encounter conflict
*/
private static void generalMerge(String branch) throws IOException {
private static boolean generalMerge(String branch) throws IOException {
String splitCommit = splitPoint(branch);
String branchHeadCommit = readBranchHeadCommit(branch);
String currentBranchHeadCommit = readHeadCommit();
Expand All @@ -777,9 +781,13 @@ private static void generalMerge(String branch) throws IOException {
}
allFiles.addAll(otherBlobs.keySet());
allFiles.addAll(currentBlobs.keySet());
boolean conflict = false;
for (String s : allFiles) {
generalMergeHelper(branch, s, splitBlobs, otherBlobs, currentBlobs);
if (generalMergeHelper(branch, s, splitBlobs, otherBlobs, currentBlobs)) {
conflict = true;
}
}
return conflict;
}

/**
Expand All @@ -789,8 +797,9 @@ private static void generalMerge(String branch) throws IOException {
* @param splitBlobs
* @param otherBlobs
* @param currentBlobs
* @return return whether this file encountered conflict
*/
private static void generalMergeHelper
private static boolean generalMergeHelper
(String branch, String file, HashMap<String, String> splitBlobs, HashMap<String, String> otherBlobs, HashMap<String, String> currentBlobs) throws IOException {
String splitVersion;
if (splitBlobs != null) {
Expand All @@ -801,6 +810,7 @@ private static void generalMerge(String branch) throws IOException {
String otherVersion = otherBlobs.get(file);
String currentVersion = currentBlobs.get(file);
String otherCommit = readBranchHeadCommit(branch);
boolean conflict = false;
switch (generalMergeType(splitVersion, otherVersion, currentVersion)) {
case 1:
if (otherBlobs.containsKey(file)) {
Expand All @@ -812,7 +822,7 @@ private static void generalMerge(String branch) throws IOException {
break;
}
case 2, 3, 4, 7:
return;
break;
case 5:
checkout2(otherCommit, file);
add(file);
Expand All @@ -821,11 +831,33 @@ private static void generalMerge(String branch) throws IOException {
rm(file);
break;
case 8:
System.out.println("not implemented yet");
conflict = true;
mergeConflict(file, otherVersion, currentVersion);
break;
}
return conflict;
}

private static void mergeConflict(String file, String otherVersion, String currentVersion) throws IOException {
StringBuilder conflictContents = new StringBuilder("<<<<<<< HEAD\n");
if (currentVersion == null) {
conflictContents.append("");
} else {
conflictContents.append(readContentsAsString(join(BLOBS, currentVersion)));
}
conflictContents.append("=======\n");
if (otherVersion == null) {
conflictContents.append("");
} else {
conflictContents.append(readContentsAsString(join(BLOBS, otherVersion)));
}
conflictContents.append(">>>>>>>");
File conflictFile = join(CWD, file);
if (!conflictFile.exists()) {
conflictFile.createNewFile();
}
writeContents(conflictFile, conflictContents.toString());
}
/**
*
* @param splitVersion
Expand Down
3 changes: 3 additions & 0 deletions proj2/testing/merge-conflicts_0/f.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<<<<<<< HEAD
[B@aec6354=======
[B@1c655221>>>>>>>
1 change: 1 addition & 0 deletions proj2/testing/merge-conflicts_0/h.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Another wug.
1 change: 1 addition & 0 deletions proj2/testing/merge-conflicts_0/k.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
And yet another wug.
5 changes: 5 additions & 0 deletions proj2/testing/merge-conflicts_1/f.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<<<<<< HEAD
Another wug.
=======
This is not a wug.
>>>>>>>
1 change: 1 addition & 0 deletions proj2/testing/merge-conflicts_1/h.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Another wug.
1 change: 1 addition & 0 deletions proj2/testing/merge-conflicts_1/k.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
And yet another wug.
5 changes: 5 additions & 0 deletions proj2/testing/merge-conflicts_2/f.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<<<<<< HEAD
Another wug.
=======
This is not a wug.
>>>>>>>
1 change: 1 addition & 0 deletions proj2/testing/merge-conflicts_2/h.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Another wug.
1 change: 1 addition & 0 deletions proj2/testing/merge-conflicts_2/k.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
And yet another wug.
5 changes: 5 additions & 0 deletions proj2/testing/src/conflict1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<<<<<< HEAD
Another wug.
=======
This is not a wug.
>>>>>>>
2 changes: 1 addition & 1 deletion proj2/testing/src/wug2.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Another wug.
Another wug.

0 comments on commit 79933a6

Please sign in to comment.