package gitlet; import java.io.File; import java.io.Serial; import java.io.Serializable; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import static gitlet.Utils.*; /** * Represent the staging area. */ public class Stage implements Serializable { /** * Serial ID so Java can serialize and deserialize. */ @Serial private static final long serialVersionUID = 1234567L; /** * The file that represents the staging area. */ public static File STAGE_FILE = join(Repository.GITLET_DIR, "stage"); /** * Files that will get added to the staginga area. * It is composed of FileName(String):Blob (Key:value) pairings. */ private TreeMap<String, Blob> addedFiles; /** * Files that will get deleted in the staging area. */ private TreeMap<String, Blob> removalFiles; /** * Constructor to make the stage. Each stage will be composed of Maps that record File:blob mappings. */ public Stage() { this.addedFiles = new TreeMap<>(); this.removalFiles = new TreeMap<>(); this.saveStage(); } /** * Get the stage file and deserialize the stage object * * @return the stage object */ public static Stage getStage() { return readObject(STAGE_FILE, Stage.class); } /** * Get File:Blob mapping of the files in the addition area of the stage. * * @return mapping of files staged for addition */ public TreeMap<String, Blob> getAddedFiles() { return this.addedFiles; } /** * Get the files that are stages for removal. * * @return mapping of files staged for removal */ public Set<String> getRemovalFiles() { return this.removalFiles.keySet(); } /** * Save the state of the staging area. */ private void saveStage() { writeObject(STAGE_FILE, this); } public void addFile(String fileName) { Blob fileBlob = new Blob(fileName); fileBlob.saveBlob(); this.addedFiles.put(fileName, fileBlob); this.saveStage(); } /** * Mark a file for removal from the staging area and delete it from the current working * directory. * * @param fileName file to stop tracking and delete. */ public void removeFile(String fileName) { Repository repo = readObject(Repository.REPO_FILE, Repository.class); this.addedFiles = repo.getHeadCommitFiles(); Blob fileBlob = new Blob(fileName); this.removalFiles.put(fileName, fileBlob); this.addedFiles.remove(fileName); Utils.restrictedDelete(fileName); this.saveStage(); } /** * Clear the staging area. Saves the stageFile back to just the main data structures. */ public void printStagingArea() { Set<Map.Entry<String, Blob>> additionSet = addedFiles.entrySet(); Iterator<Map.Entry<String, Blob>> i = additionSet.iterator(); System.out.println("=== Staged Files ==="); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); System.out.printf("%s\n%s\n", entry.getKey(),entry.getValue()); } System.out.println("=== Removed Files ==="); Set<Map.Entry<String, Blob>> removalSet = removalFiles.entrySet(); Iterator<Map.Entry<String, Blob>> j = removalSet.iterator(); while (j.hasNext()) { Map.Entry entry = (Map.Entry) j.next(); System.out.printf("%s\n", entry.getKey()); } } }