Hihi, I have some questions here and I hope that you all can help me out with them. So I was trying to create a Java program that mimics some of the functions that git has. But I need some guidance on the suitable ways to do it. Let's call the program not-git. It has the similar basic functions that git has, including the 1. status command showing the status of files in the respective folder (tracked/not tracked), 2. add command adding a file to the repository while tracking it (e.g. add "file.txt"), 3. commit command to commit the files being tracked with an increasing counter (commit id). 4. revert command to reverse the version of current file into the previous version with a new commit ID (e.g. revert 1 will revert current version into version of commitID 1 but now the new version will have a new commitID, if before reverting the commitID is 2 now the commitID will be 3 for this version). I have attached some images below (I tried uploading ppt, docs and pdf but none of them works) if the explanation above isn't clear enough. I have some ideas in mind but not sure whether it is feasible, so I will create a LinkedList that accepts file as its parameter and store it in a node when an add command is issued, and there will be a counter with a default value of 1 that increases per commit. Whenever a revert command is issued, the list will be used to lookup the content of the node index. The functions mentioned above were just the basic requirements to achieve the passing marks, to achieve better grades we were suggested to add extra features like modifying the status command into showing not only the tracked files but also the new files, untracked files, and the modified files. The other extra features to improve the program are also included in the file uploaded. Thank you for taking your time reading this and I appreciate any help provided!
https://ibb.co/9qXyTRZ
https://ibb.co/qyF5yLp
https://ibb.co/5MZd71G
https://ibb.co/tJ3hmc7
https://ibb.co/9vdjYSN
https://ibb.co/hB7FhSW
The main concern here is how do I collect a pile of files and store them into one node of LinkedList. And also when I revert how do obtain the files from the prev commit and replace the current ones. Thank you in advance for any help incoming!
And also how to know when the content of the file is altered (modified)? Since the assignment requires to display the status of files not only tracked/untracked but also modified/new.
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); notGit newGit = new notGit(); String s; System.out.println("Enter \"not-git\" to initialise the program:"); do { s = br.readLine(); if (!"not-git".equals(s)) { System.out.println("Please enter the instructed command:"); } } while (!"not-git".equals(s)); newGit.check(s); while ((s = br.readLine()) != null) { if ("not-git".equals(s)) { newGit = new notGit(); } newGit.check(s); } } }
import java.util.*; import java.io.*; public class notGit { LinkedList list; //stores file history LinkedList list1; //stores list for each commit int commitID; //increases per commit public notGit() { //initialization this.list = new LinkedList(); this.list1 = new LinkedList(); this.commitID = 1; } void add(File file) { list.add(file); System.out.printf("\"%s\" is now being tracked\n", file.getName()); } void commit() { list1.add(list); list.clear(); System.out.printf("files committed with commit id \"%d\"\n", commitID++); } void revert(int i) { list = (LinkedList) list1.get(i - 1); System.out.printf("revert to commit-id \"%d\" and committed with commit-id \"%d\"", --commitID, commitID += 2); } void status() { if (list.isEmpty()) { System.out.println("No file is being tracked."); } else { System.out.println("tracked files:"); for (int i = 0; i < list.size(); i++) { File n = (File) list.get(i); System.out.println(n.getName()); } } } void check(String s) throws IOException { if ("not-git".equals(s)) { System.out.println("Welcome to not-git!"); } else if ("status".equals(s)) { status(); } else if (s.startsWith("add")) { String[] arr = s.split("\""); File newF = new File(arr[1]); add(newF); } else if ("commit".equals(s)) { commit(); } else if (s.startsWith("revert")) { String[] arr = s.split(" "); revert(Integer.parseInt(arr[1])); } else { System.out.println("Please enter the correct command: "); } } }