Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
109 lines (103 loc) · 4.54 KB
/
Copy pathMain.java
File metadata and controls
109 lines (103 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gitlet;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
*/
public class Main {
/** the repository */
private static Repository repo;
private static final List<String> commands = new LinkedList<>(Arrays.asList("init", "add", "commit", "rm", "log", "global-log", "find", "status", "checkout", "branch", "rm-branch", "reset", "merge"));
private static final List<String> commands_one = new LinkedList<>(Arrays.asList("init", "log", "global-log", "status", "commit"));
private static final List<String> commands_two = new LinkedList<>(Arrays.asList("add", "commit", "rm", "find", "checkout", "branch", "rm-branch", "reset", "merge"));
public static void main(String... args) {
try {
if (args.length == 0) {
Utils.message("Please enter a command.");
System.exit(0);
}
if (!commands.contains(args[0])) {
Utils.message("No command with that name exists.");
System.exit(0);
}
// print out error message if init is called and there's already a repo that's been instantiated
if (args[0].equals("init") && Files.exists(Paths.get(".gitlet"))) {
Utils.message("A Gitlet version-control system already exists in the current directory.");
System.exit(0);
}
if (!args[0].equals("init") && commands.contains(args[0]) && !Files.exists(Paths.get(".gitlet"))) {
Utils.message("Not in an initialized Gitlet directory.");
System.exit(0);
}
if (args.length == 1 && args[0].equals("init") && !Files.exists(Paths.get(".gitlet"))) {
repo = new Repository();
File hist = new File(".gitlet/myrepo");
Utils.writeObject(hist, repo);
}
// check whether a repo exists already or not
if (Files.exists(Paths.get(".gitlet"))) {
repo = Utils.readObject(new File(".gitlet/myrepo"), Repository.class);
if (args.length == 1) {
switch (args[0]) {
case "commit":
Utils.message("Please enter a commit message.");
System.exit(0);
break;
case "log":
repo.log(repo.getHead());
break;
case "global-log":
repo.globalLog();
break;
case "status":
repo.status();
break;
default:
}
} else {
switch (args[0]) {
case "add":
repo.add(args[1]);
break;
case "commit":
repo.commit(args[1]);
break;
case "rm":
repo.rm(args[1]);
break;
case "rm-branch":
repo.rmBranch(args[1]);
break;
case "reset":
repo.reset(args[1]);
break;
case "find":
repo.find(args[1]);
break;
case "branch":
repo.branch(args[1]);
break;
case "checkout":
if (args.length == 3 && args[1].equals("--"))
repo.checkout1(args[2], repo.getHead());
else if (args.length == 4 && args[2].equals("--"))
repo.checkout2(args[1], args[3]);
else if (args.length == 2)
repo.checkout3(args[1]);
else {
Utils.message("Incorrect operands.");
return;
}
break;
default:
}
}
File f = new File(".gitlet/myrepo");
Utils.writeObject(f, repo);
}
} catch (GitletException e) {
System.exit(0);
}
}
}