-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitlet.java
More file actions
162 lines (153 loc) · 4.8 KB
/
Gitlet.java
File metadata and controls
162 lines (153 loc) · 4.8 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.io.File;
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
//final
public class Gitlet implements Serializable {
static GitletVCS gitletVCS;
public Gitlet() {
gitletVCS = new GitletVCS();
}
public static void deserialize() {
try {
FileInputStream fileIn = new FileInputStream(".gitlet/serCommits/commit.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
gitletVCS = (GitletVCS) in.readObject();
in.close();
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/*http://cdn2.crunchify.com/wp-content/uploads/2013/07/Serialize-DeSerialize-an-Object.png*/
public static void serialize() {
try {
FileOutputStream fileOut = new FileOutputStream(".gitlet/serCommits/commit.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(gitletVCS);
out.close();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void whichCheckout(String[] args) {
if (args.length == 2) {
gitletVCS.oneCheckout(args[1]);
}
if (args.length == 3) {
gitletVCS.twoCheckout(Integer.parseInt(args[1]), args[2], true);
}
}
public static void addable(String[] args) {
String sToAdd = args[1];
File fToAdd = new File(sToAdd);
if (fToAdd.exists()) {
gitletVCS.stage(sToAdd);
} else {
System.out.println("File does not exist.");
return;
}
}
public static void commitable(String[] args) {
if (args.length < 2) {
System.out.println("You must enter a commit message.");
return;
}
if (args.length > 2) {
System.out.println("You must enter a commit message.");
return;
}
gitletVCS.commit(args[1]);
}
public static void main(String[] args) {
if (args.length < 0) {
System.out.println("Please enter a command");
}
switch (args[0]) {
case "init":
Gitlet newG = new Gitlet();
gitletVCS.gitletInit();
serialize();
break;
case "add":
deserialize();
addable(args);
serialize();
break;
case "commit":
deserialize();
commitable(args);
serialize();
break;
case "rm":
deserialize();
gitletVCS.remove(args[1]);
serialize();
break;
case "log":
deserialize();
gitletVCS.log();
break;
case "global-log":
deserialize();
gitletVCS.globalLog();
break;
case "find":
deserialize();
gitletVCS.find(args[1]);
break;
case "status":
deserialize();
gitletVCS.status();
break;
case "checkout":
deserialize();
whichCheckout(args);
serialize();
break;
case "branch":
deserialize();
gitletVCS.branch(args[1]);
serialize();
break;
case "rm-branch" :
deserialize();
gitletVCS.removeBranch(args[1]);
serialize();
break;
case "reset":
deserialize();
gitletVCS.reset(Integer.parseInt(args[1]));
serialize();
break;
case "merge":
deserialize();
gitletVCS.merge(args[1]);
serialize();
break;
// case "rebase":
// deserialize();
// gitletVCS.rebase(args[1]);
// serialize();
// break;
// case "i-rebase":
// deserialize();
// gitletVCS.iRebase(args[1]);
// serialize();
// break;
default:
System.out.println("Unrecognized command.");
}
}
}