Skip to content

Commit

Permalink
Roughly finished rm.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsrigo committed Oct 2, 2022
1 parent b390629 commit 0b32276
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
48 changes: 43 additions & 5 deletions gitlet/Commit.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public class Commit implements Serializable {
/**
* The timestamp of the Commit.
*/
private String timestamp;
private final String timestamp;
// /** The SHA-1 of the commit. */
// private String SHA;
/**
* The reference to the files SHA-1.
*/
private HashMap<String, String> ids;
/**
* The reference to parent.
* The reference to parent. SHA-1 of the parent commit are stored in it.
*/
private ArrayList<String> parents;

Expand All @@ -52,16 +52,31 @@ public Commit(String message, String timestamp) {
parents = new ArrayList<>();
}

public Commit(Commit parent, String message, String timestamp) {
public Commit(HashMap<String, String> ids, String message, String timestamp){
this.ids = new HashMap<>(ids);
this.message = message;
this.timestamp = timestamp;
this.ids = new HashMap<>(parent.ids);
}

public Commit(Commit copy, String message, String timestamp) {
this.message = message;
this.timestamp = timestamp;
this.ids = new HashMap<>(copy.ids);
parents = new ArrayList<>();
parents.add(Utils.sha1(serialize(parent)));
// parents.add(Utils.sha1(serialize(parent)));
}

public Commit() {
this.message = "hhh";
this.timestamp = "hhh";
}

public Commit(Commit parent) {
this.ids = new HashMap<>(parent.ids);
this.message = null;
this.timestamp = null;
parents = new ArrayList<>();
parents.add(Utils.sha1(serialize(parent)));
}

public static void main(String[] args) {
Expand All @@ -80,6 +95,29 @@ public void addFile(String filename, String sha) {
ids.put(filename, sha);
}

public void addParent(String parentSha){
parents.add(parentSha);
}

// public void setMessage(String message){
// if (this.message != null) {
// throw new IllegalArgumentException("Message is already set");
// }
// this.message = message;
// }
//
// public void setTimestamp(String timestamp){
// if (this.timestamp != null) {
// throw new IllegalArgumentException("Timestamp is already set");
// }
// this.timestamp = timestamp;
// }


public void removeFile(String filename){
ids.remove(filename);
}

@Override
public String toString() {
return "Time stamp: " + this.timestamp + " Message: " + this.message
Expand Down
2 changes: 1 addition & 1 deletion gitlet/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void main(String[] args) {
System.exit(0);
}
assert repo != null;

repo.rm(args[1]);
// TODO: FILL THE REST IN
}
writeObject(REPO_DIR, repo);
Expand Down
46 changes: 42 additions & 4 deletions gitlet/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Repository implements Serializable {
*/
public static final File STAGING_DIR = join(GITLET_DIR, ".staging");
/**
* Current commit: the new commit's parent.
* Current commit: the commit in front of the currentBranch.
*/
private Commit currentCommit;
/**
Expand All @@ -60,6 +60,10 @@ public class Repository implements Serializable {
* The files in staging area.
*/
private HashSet<File> stagingArea;
/**
* The tracking area.
*/
private HashMap<String, String> trackingArea;
/**
* Treat sha-1 as a reference.
*/
Expand All @@ -69,6 +73,11 @@ public class Repository implements Serializable {
*/
private HashMap<String, Commit> sha2commit;

/**
* This flag is used to notify whether there is an removal of files.
*/
private boolean removalFlag;

public Repository() {
LinkedList<Commit> commits = new LinkedList<>();
currentCommit = new Commit("initial commit", "1970/2/1 00:00:00");
Expand All @@ -80,6 +89,7 @@ public Repository() {
stagingArea = new HashSet<>();
sha2file = new HashMap<>();
sha2commit = new HashMap<>();
trackingArea = new HashMap<>();
}

public static Repository init() {
Expand Down Expand Up @@ -112,17 +122,19 @@ public void add(String filename) {
System.exit(0);
}
stagingArea.add(stagingFile);
trackingArea.put(filename, fileSha);
writeContents(stagingFile, (Object) contents);
System.out.println("The files you have staged are: " + stagingArea.toString());
}

public void commit(String message) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
Commit newCommit = new Commit(currentCommit, message, dtf.format(LocalDateTime.now()));
if (stagingArea.isEmpty()) {
Commit newCommit = new Commit(trackingArea, message, dtf.format(LocalDateTime.now()));
if (stagingArea.isEmpty() && !removalFlag) {
System.out.println("No changes added to the commit.");
System.exit(0);
}
removalFlag = false;
for (File X : stagingArea) {
String S = sha1((Object) readContents(X));
newCommit.addFile(X.getName(), S);
Expand Down Expand Up @@ -152,7 +164,33 @@ private void removeStage(File X) {
}
stagingArea.remove(X);
if (!X.delete()) {
throw new IllegalArgumentException("delete fail!!!");
System.out.println("Warning: Delete not exist file.");
}
}
private void removeTrack(String filename){
File workingFile = join(CWD, filename);
trackingArea.remove(filename);
workingFile.delete();
}

/**
* Unstage the file if it is currently staged for addition.
* Or remove the file in tracking area.
* @param filename The file to be removed.
*/
public void rm(String filename){
File file = join(STAGING_DIR, filename);
if (stagingArea.isEmpty() && trackingArea.isEmpty()){
System.out.println("No reason to remove the file.");
System.exit(0);
}
if (stagingArea.contains(file)){
removeStage(file);
}
if (currentCommit.getFilesha(filename) != null){ // tracking area will always be same with currentCommit.ids
System.out.println("Notice: " + filename + " will be deleted");
removeTrack(filename);
removalFlag = true;
}
}
/* TODO: fill in the rest of this class. */
Expand Down

0 comments on commit 0b32276

Please sign in to comment.