package gitlet; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.io.Serializable; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Map; import java.util.Map.Entry; import java.util.Iterator; import java.io.File; // TODO: any imports you need here import java.util.Date; // TODO: You'll likely use this in this class /** Represents a gitlet commit object. * TODO: It's a good idea to give a description here of what else this Class * does at a high level. * * @author TODO */ public class Commit implements Serializable { /** * TODO: add instance variables here. * * List all instance variables of the Commit class here with a useful * comment above them describing what that variable represents and how that * variable is used. We've provided one example for `message`. */ /** The message of this Commit. */ private String message; /* TODO: fill in the rest of this class. */ private String timeStamp; private Commit parent; private HashMap blobcommit; //blob name and file content public Commit(String msg, Commit parent, HashMap blobcommit) { this.message = msg; this.parent = parent; this.blobcommit = blobcommit; if (this.parent == null) { this.timeStamp = "00:00:00 UTC, Thursday, 1 January 1970"; } else { LocalDateTime timeRn = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z"); timeStamp = timeRn.format(formatter); } } public String getMessage(){ return this.message; } public String getDate(){ return this.timeStamp; } public Commit getCommit(){ return this; } public String getblob(String name) { return blobcommit.get(name); } public HashMap getBlobHash() { return blobcommit; } public Commit getParent(){ return this.parent; } public void commit(String message) { Repository c = new Repository(); //check if the staged files are empty if (Utils.plainFilenamesIn(Repository.stagedForAddition).size() == 0 && Utils.plainFilenamesIn(Repository.stagedForRemoval).size() == 0) { System.out.print("No changes added to the commit."); return; } if (message == "") { System.out.print("Please enter a commit message."); return; } //get current commit Commit currentCommit = getCommit(); //copy all blobs into new hashmap Iterator> iteratorclone = getBlobHash().entrySet().iterator(); HashMap cloneOfBlobs = new HashMap(); while (iteratorclone.hasNext()){ Entry entry = iteratorclone.next(); String key = entry.getKey(); String value = entry.getValue(); cloneOfBlobs.put(key, value); } //add new blobs for each new file Iterator> iterator = c.addFiles.entrySet().iterator(); while(iterator.hasNext()){ Entry entry = iterator.next(); String key = entry.getKey(); //somehow get the contents of the file given the sha1 -- I have no idea String value = entry.getValue(); //check if blob already exists in blobcopy if (cloneOfBlobs.containsKey(key)) { //remove old blob cloneOfBlobs.remove(key); } //add new blob cloneOfBlobs.put(key, value); } //create new commit Commit newestCommit = new Commit(message, currentCommit, cloneOfBlobs); //make blob persistent -- write content of blob into blob folder //remove contents of MASTER and HEAD file //replace with new commit sha1 id String sha1 = Utils.sha1(Utils.serialize(newestCommit)); c.MASTER = Utils.sha1(Utils.serialize(newestCommit)); c.HEAD = c.MASTER; //write into Utils.writeObject(c.Head, sha1); Utils.writeObject(c.Master, sha1); //clear both staging areas //stagedForAddition Utils.restrictedDelete(c.stagedForAddition); //add commit to commit file } }