package gitlet; import java.io.File; import java.io.IOException; import java.io.Serial; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static gitlet.Utils.*; /** * The Blob class the will represent the file's contents. */ public class Blob implements Serializable { /** * Serial ID so Java can serialize and deserialize. */ @Serial private static final long serialVersionUID = 1234567L; /** * The file that this particular blob will track. */ private final File trackingFile; /** * The file that this particular blob will track as a string. */ private final String trackingFileName; /** * The file conetnts of the file */ private final byte[] trackingFileContents; /** * The file contents as a string of the tracked file. */ private final String trackingFileContentsString; /** * The SHA-1 representation of the Blob object */ private String blobSHA; /** * Constructor to create a new blob. It records the file and file contents. * * @param fileName THe file that this particular blob should track. */ public Blob(String fileName) { this.trackingFileName = fileName; this.trackingFile = new File(fileName); this.trackingFileContents = Utils.readContents(this.trackingFile); this.trackingFileContentsString = new String(trackingFileContents); this.blobSHA = this.setBlobSHA(); } public String getBlobSHA() { return this.blobSHA; } public byte[] getTrackingFileContents() { return this.trackingFileContents; } public String getTrackingFile() { return this.trackingFileName; } /** * The SHA-1 representation of a blob is the serilization of a list that * contains the variables of the blob. */ private String setBlobSHA() { String blobContents = this.trackingFileName + this.trackingFileContentsString; return Utils.sha1(blobContents, this.trackingFileContents); } public void saveBlob() { File blobFile = join(Repository.OBJECT_DIR, this.blobSHA); Utils.writeObject(blobFile, this); } /** * @return SHA-1 of the blob object as well as the file it is tracking */ @Override public String toString() { return String.format("BlobID:%s\nFile contents:\n\t%s", this.blobSHA, this.trackingFileContentsString); } /** * Compare the SHA-1 representation of the blobs * * @param o the blob to compare * @return true if the blobs are the same, false otherwise */ public boolean compareBlobs(Blob o) { return this.getBlobSHA().equals(o.getBlobSHA()); } }