package gitlet; import java.io.File; import static gitlet.Utils.*; import java.io.Serializable; import java.util.HashMap; // TODO: any imports you need here /** Represents a gitlet repository. * 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 Repository implements Serializable { /** * TODO: add instance variables here. * * List all instance variables of the Repository class here with a useful * comment above them describing what that variable represents and how that * variable is used. We've provided two examples for you. */ /** The current working directory. */ public static final File CWD = new File(System.getProperty("user.dir")); /** The .gitlet directory. */ public static final File GITLET_DIR = join(CWD, ".gitlet"); /* TODO: fill in the rest of this class. */ public static String HEAD; public static String MASTER; private File blob; public static final File staged = Utils.join(GITLET_DIR, "staged");; private File commit; public static final File Head = Utils.join(GITLET_DIR, "Head"); public static final File Master = Utils.join(GITLET_DIR, "Master"); public static final File stagedForRemoval = Utils.join(staged, "stagedForRemoval"); public static final File stagedForAddition = Utils.join(staged, "stagedForAddition"); public static final HashMap addFiles = new HashMap(); //filename -- sha1 public static final HashMap blobs = new HashMap(); //blobname -- sha1 public void init(){ if (GITLET_DIR.exists()){ System.out.println("A Gitlet version-control system already exists in the current directory."); return; } GITLET_DIR.mkdir(); staged.mkdir(); stagedForRemoval.mkdir(); stagedForAddition.mkdir(); blob = Utils.join(GITLET_DIR, "blob"); blob.mkdir(); blob = Utils.join(GITLET_DIR, "blob"); blob.mkdir(); commit = Utils.join(GITLET_DIR, "commit"); commit.mkdir(); //head file point to current branch Commit init = new Commit("initial commit", null, null); MASTER = Utils.sha1(Utils.serialize(init)); String sha1 = Utils.sha1(Utils.serialize(init)); Utils.writeContents(Head, sha1); Utils.writeContents(Master, sha1); } //check if commit exists of same added file //check added file is already in commits & and check if they are identical //sha1 head commit //check if file name checks in commit //if file exists -- see if file is identical in CWD //compare cwd file sha1 to sha1 of commit //make sure most recent comment public void add(File FileName) { //check if file even exists if (!FileName.exists()){ throw new IllegalArgumentException("File does not exist."); } //sha1 of CWD file String sha1first = sha1(Utils.readContents(FileName)); //create commit object of most current commit Commit commitobj = Utils.readObject(Utils.join(commit, HEAD), Commit.class); //get sha1 of blob String sha1second = commitobj.getblob(FileName.toString()); //check if the commit blob sha1 is same as CWD file (aka the contents) if (sha1second.equals(sha1first)) { throw new IllegalArgumentException("No changes have been made to the file."); } //sha1 of added file if it already exists String temp = addFiles.get(FileName); //sha1 of added most recent added version addFiles.put(FileName.toString(), sha1(FileName)); String temp2 = addFiles.get(FileName); //check if the files are not equal -- so we know to writeContent (Serialze) if (!temp.equals(temp2)) { addFiles.remove(temp); FileName = Utils.join(stagedForAddition, temp2); //get contents of file byte[] contents = Utils.serialize(FileName); //write contents into file Utils.writeContents(FileName, contents); } } }