package gitlet; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static gitlet.Utils.*; /** * Driver class for Gitlet, a subset of the Git version-control system. * * @author Abraham Briones */ public class Main { /** * Usage: java gitlet.Main ARGS, where ARGS contains * <COMMAND> <OPERAND1> <OPERAND2> ... */ public static void main(String[] args) throws IOException { File CWD = new File(System.getProperty("user.dir")); File GITLET_DIR = join(CWD, ".gitlet"); String[] commandArray = new String[]{"add", "commit", "status", "checkoutBranch", "branch", "rm", "log"}; List<String> commandList = new ArrayList<>(Arrays.asList(commandArray)); GitletException error; Repository repo = null; if (args.length == 0) { error = new GitletException("Please enter a command"); System.out.println(error.getMessage()); System.exit(0); } else if (commandList.contains(args[0]) && !GITLET_DIR.exists()) { error = new GitletException("Not in an initialized Gitlet directory."); System.out.println(error.getMessage()); System.exit(0); } else { String firstArg = args[0]; String message; String fileName; String branchName; String commitID; /* If the repository file exists, load that as the repostiory. Otherwise, make a new repo object. */ repo = Repository.REPO_FILE.exists() ? readObject(Repository.REPO_FILE, Repository.class) : new Repository(); switch (firstArg) { case "init": if (GITLET_DIR.exists()) { error = new GitletException("A Gitlet version-control system already " + "exists in the current directory."); System.out.println(error.getMessage()); System.exit(0); } validateNumArgs(args, 1); repo.setUpPersistence(); break; case "add": validateNumArgs(args, 2); fileName = args[1]; repo.addFile(fileName); break; case "commit": validateNumArgs(args, 2); message = args[1]; repo.makeCommit(message); // System.out.print("THE REPOSITORY IS TRACKING THESE COMMITS:\n"); // System.out.println(repo.getRepoCommits().toString().replace("[", "\n\t[")); break; case "rm": validateNumArgs(args, 2); fileName = args[1]; repo.removeFile(fileName); break; case "log": validateNumArgs(args, 1); repo.printRepoLog(); break; case "global-log": validateNumArgs(args, 1); repo.printRepoGlobalLog(); break; case "status": validateNumArgs(args, 1); repo.printRepoStatus(); break; case "branch": validateNumArgs(args, 2); branchName = args[1]; repo.branch(branchName); break; case "checkout": if (args.length == 2) { branchName = args[1]; repo.checkout(branchName); } else if (args.length == 3) { fileName = args[2]; repo.checkoutFile(fileName); } else if (args.length == 4) { commitID = args[1]; fileName = args[3]; repo.checkout(commitID, fileName); } break; default: error = new GitletException("No command with that name exists."); System.out.println(error.getMessage()); } repo.saveRepo(); } } public static void validateNumArgs(String[] args, int n) { if (args.length != n) { GitletException error = new GitletException("Incorrect operands."); System.out.println(error.getMessage()); System.exit(0); } } }