Gitlet / Main.java
Main.java
Raw
package gitlet;

/** Driver class for Gitlet, the tiny stupid version-control system.
 *  @author Ethan Ikegami
 */
public class Main {

    /** Usage: java gitlet.Main ARGS, where ARGS contains
     *  <COMMAND> <OPERAND> ....
     *  java gitlet.Main add */
    public static void main(String... args) {
        if (args.length == 0) {
            System.err.printf("Please enter a command.");
            System.exit(0);
        }
        if (args[0].equals("init")) {
            if (Command.exists()) {
                System.err.printf("A Gitlet version-control "
                        + "system already exists in the current directory.");
                System.exit(0);
            }
            Command.init();
        } else {
            if (!Command.exists()) {
                System.err.printf("Not in an initialized Gitlet directory.");
                System.exit(0);
            }
            switch (args[0]) {
            case "add":
                if (args.length != 2) {
                    error();
                }
                Command.add(args[1]);
                break;
            case "rm":
                if (args.length != 2) {
                    error();
                }
                Command.remove(args[1]);
                break;
            case "commit":
                if (args.length != 2) {
                    error();
                }
                if (args[1].length() == 0) {
                    System.err.printf("Please enter a commit message.");
                    System.exit(0);
                }
                Command.commit(args[1], null, null);
                break;
            case "checkout":
                if (args.length < 2) {
                    error();
                }
                if (args.length == 3 && args[1].equals("--")) {
                    Command.checkoutFile(null, args[2]);
                } else if (args.length == 4 && args[2].equals("--")) {
                    Command.checkoutFile(args[1], args[3]);
                } else if (args.length == 2) {
                    Command.checkoutBranch(args[1]);
                } else {
                    error();
                }
                break;
            default:
                commandHelper(args);
            }
        }
    }

    /** Helper function for the main function. Takes in
     * the original ARGS parameters. */
    public static void commandHelper(String... args) {
        switch (args[0]) {
        case "merge":
            if (args.length != 2) {
                error();
            }
            Command.merge(args[1]);
            break;
        case "log":
            if (args.length != 1) {
                error();
            }
            Command.log();
            break;
        case "global-log":
            if (args.length != 1) {
                error();
            }
            Command.globalLog();
            break;
        case "find":
            if (args.length != 2) {
                error();
            }
            Command.find(args[1]);
            break;
        case "status":
            if (args.length != 1) {
                error();
            }
            Command.status();
            break;
        case "branch":
            if (args.length != 2) {
                error();
            }
            Command.branch(args[1]);
            break;
        case "rm-branch":
            if (args.length != 2) {
                error();
            }
            Command.rmBranch(args[1]);
            break;
        case "reset":
            if (args.length != 2) {
                error();
            }
            Command.reset(args[1]);
            break;
        default:
            System.err.printf("No command with that name exists.");
            System.exit(0);
        }
    }

    /** Just a helper function that errors when called. */
    public static void error() {
        System.err.printf("Incorrect operands.");
        System.exit(0);
    }
}