Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ffanyt authored Jan 27, 2023
1 parent 445001f commit 5fdacfe
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions proj2/gitlet/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ private static Commit merge2NewCommit(Commit split, Commit head, Commit other, S
Commit mergeCommit = new Commit(ms, parent);
return mergeCommit;
}
private static void updateStage(Commit split, Commit head, Commit other, List allFileName) {
private static void updateStage(Commit split,
Commit head, Commit other, List allFileName) {
HashMap splitBlob = split.getBlob();
HashMap headBlob = head.getBlob();
HashMap otherBlob = other.getBlob();
Expand Down Expand Up @@ -521,7 +522,7 @@ private static void updateStage(Commit split, Commit head, Commit other, List al
System.out.println("Encountered a merge conflict.");
flag = true;
}
conflict(headFileHash.toString(), otherFileHash.toString());
conflict(headFileHash.toString(), otherFileHash.toString(), true, true);
}
} else {
if (splitFileHash.equals(headFileHash)) {
Expand All @@ -531,6 +532,7 @@ private static void updateStage(Commit split, Commit head, Commit other, List al
System.out.println("Encountered a merge conflict.");
flag = true;
}
conflict(headFileHash.toString(), "", true, false);
}
}
} else { //case3 5
Expand All @@ -541,6 +543,7 @@ private static void updateStage(Commit split, Commit head, Commit other, List al
if (!flag) {
System.out.println("Encountered a merge conflict.");
flag = true;
conflict("", otherFileHash.toString(), false, true);
}
stage(otherFileHash.toString());
}
Expand All @@ -560,7 +563,7 @@ private static void updateStage(Commit split, Commit head, Commit other, List al
System.out.println("Encountered a merge conflict.");
flag = true;
}
conflict(headFileHash.toString(), otherFileHash.toString());
conflict(headFileHash.toString(), otherFileHash.toString(), true, true);
}
} else {
continue;
Expand All @@ -582,17 +585,35 @@ private static void rmStage(String blobHash) {
Stage stage = new Stage(blob);
stage.saveRemove();
}
private static void conflict(String headBlobHash, String otherBlobHash) {
Blob headBlob = Blob.readBlob(headBlobHash);
Blob otherBlob = Blob.readBlob(otherBlobHash);
byte[] head = headBlob.getContent();
byte[] other = otherBlob.getContent();
String headContent = new String(head, StandardCharsets.UTF_8);
String otherContent = new String(other, StandardCharsets.UTF_8);
private static void conflict(String headBlobHash, String otherBlobHash, boolean head, boolean other) {
String headContent;
String otherContent;
Blob headBlob = null;
Blob otherBlob = null;
if (head) {
headBlob = Blob.readBlob(headBlobHash);
byte[] headBytes = headBlob.getContent();
headContent = new String(headBytes, StandardCharsets.UTF_8);
} else {
headContent = headBlobHash;
}
if (other) {
otherBlob = Blob.readBlob(otherBlobHash);
byte[] otherBytes = otherBlob.getContent();
otherContent = new String(otherBytes, StandardCharsets.UTF_8);
} else {
otherContent = otherBlobHash;
}
String result = "<<<<<<< HEAD\n" + headContent + "=======\n"
+ otherContent + ">>>>>>>\n";
Stage a = new Stage(headBlob.getFileName(), result);
a.save();
if (head) {
Stage a = new Stage(headBlob.getFileName(), result);
a.save();
} else {
Stage a = new Stage(otherBlob.getFileName(), result);
a.save();
}

}
private static List calAllFile(Commit split, Commit head, Commit other) {
List allFile = new ArrayList<String>();
Expand Down

0 comments on commit 5fdacfe

Please sign in to comment.