-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitLet.java
More file actions
500 lines (461 loc) · 20.8 KB
/
GitLet.java
File metadata and controls
500 lines (461 loc) · 20.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package gitlet;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
/* Performs the functionality of Main class. Avoids having the Main class become too clustered. */
public class GitLet {
private static File GIT_DIR;
private static File OBJ_DIR;
private static String WORKING_DIR = System.getProperty("user.dir");
public static final String REPO = ".gitlet";
private static boolean merged = true;
/* Creates a new gitlet version-control system in the current directory. */
public void init() {
GIT_DIR = new File(REPO);
if (GIT_DIR.exists()) {
System.out.println("A gitlet version-control system "
+ "already exists in the current directory.");
System.exit(0);
}
GIT_DIR.mkdir();
OBJ_DIR = new File(REPO + "/objects");
OBJ_DIR.mkdir();
CommitTree tree = new CommitTree();
File cTree = new File(REPO + "/commitTree");
FileUtil.serialize(cTree, tree);
StagingArea stage = StagingArea.getStagingArea();
File sArea = new File(REPO + "/stagingArea");
FileUtil.serialize(sArea, stage);
DataPool data = DataPool.getPool();
File dPool = new File(REPO + "/objects/dataPool");
FileUtil.serialize(dPool, data);
}
public void commit(String commitMessage) {
/* Saves a snapshot of certain files in the current commit
and staging area so they can be restored at a later time,
creating a new commit. */
File inFile = new File(REPO + "/stagingArea");
StagingArea stage = (StagingArea) FileUtil.deSerialize(inFile);
if (commitMessage.length() == 0) {
System.out.println("Please enter a commit message.");
System.exit(0);
} else if (stage.getAddedSize() == 0 && stage.getRemovedSize() == 0) {
System.out.println("No changes added to the commit.");
System.exit(0);
}
stage.commit(commitMessage);
File sArea = new File(REPO + "/stagingArea");
FileUtil.serialize(sArea, stage);
}
public void checkout(String[] commands) {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
if (commands.length == 2) {
/* java gitlet.Main checkout [branch name] */
tree.checkoutBranch(commands[1]);
} else if (commands.length == 3) {
/* java gitlet.Main checkout -- [file name] */
tree.checkoutFile(commands[2]);
} else if (commands.length == 4) {
/* java gitlet.Main checkout [commit id] -- [file name] */
tree.checkoutFile(commands[3], commands[1]);
} else {
/* Undefined method */
System.out.println("Check git --help for help");
System.exit(0);
}
FileUtil.serialize(inFile, tree);
}
public void reset(String commitID) {
// may need to check on reset edge case. If rm file.
File inTree = new File(REPO + "/commitTree");
File inStage = new File(REPO + "/stagingArea");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inTree);
StagingArea stage = (StagingArea) FileUtil.deSerialize(inStage);
if (commitID.length() < 40) {
for (String id: tree.nodeIds) {
if (id.contains(commitID)) {
commitID = id;
}
}
}
if (!tree.nodeIds.contains(commitID)) {
System.out.println("No commit with that id exists.");
System.exit(0);
}
CommitNode givenCommitNode = tree.getCommitNode(commitID);
for (String filename : givenCommitNode.fileNames.keySet()) {
String fileId = givenCommitNode.fileNames.get(filename);
CommitNode.Blob commitFile = givenCommitNode.getBlob(fileId);
File userFile = new File(filename);
if (userFile.exists()) {
if (!tree.currPtr.fileNames.containsKey(filename)) {
System.out.println("There is an untracked file "
+ "in the way; delete it or add it first.");
System.exit(0);
} else {
Utils.writeContents(userFile, commitFile.content);
}
} else {
try {
File f = new File(filename);
f.createNewFile();
Utils.writeContents(f, commitFile.content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* for (String trackedFile : trackedFiles) {
if (!givenCommitNode.fileNames.keySet().contains(trackedFile)) {
stage.rm(trackedFile);
} else {
File track = new File(trackedFile);
Utils.writeContents(track,
givenCommitNode.getBlob(givenCommitNode.getFileId(trackedFile)).content);
}
}
*/
tree.branchPointers.put(tree.currBranch, commitID);
tree.currPtr = tree.getCommitNode(commitID);
stage.clearAdd();
// Do i clear removedFiles from stage too??
File cTree = new File(REPO + "/commitTree");
FileUtil.serialize(cTree, tree);
File sArea = new File(REPO + "/stagingArea");
FileUtil.serialize(sArea, stage);
}
public void status() {
/* Displays what branches currently exist, and marks the current branch with a *.
Also displays what files have been staged or marked for untracking. */
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
File inFile2 = new File(REPO + "/stagingArea");
StagingArea stage = (StagingArea) FileUtil.deSerialize(inFile2);
statusHelper("Branches");
String currBranchName = tree.currBranch;
System.out.println("*" + currBranchName);
for (String branchName : tree.branchPointers.keySet()) {
if (!branchName.equals(currBranchName)) {
System.out.println(branchName);
}
}
System.out.println();
statusHelper("Staged Files");
for (String stagedFile : stage.getAddedFiles()) {
System.out.println(stagedFile);
}
System.out.println();
statusHelper("Removed Files");
for (String removedFile : stage.getRemovedFiles()) {
System.out.println(removedFile);
}
System.out.println();
statusHelper("Modifications Not Staged For Commit");
System.out.println();
statusHelper("Untracked Files");
System.out.println();
}
private static void statusHelper(String name) {
System.out.println("=== " + name + " ===");
}
public void log() {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
System.out.println(tree.log());
}
public void globalLog() {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
String globalLog = tree.globalLog();
System.out.println(globalLog);
}
public void find(String commitMessage) {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
tree.find(commitMessage);
}
// public void merge(String branchName) {
// File inFile = new File(REPO + "/commitTree");
// CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
// File inFileStage = new File(REPO + "/stagingArea");
// StagingArea stage = (StagingArea) FileUtil.deSerialize(inFileStage);
//
// if (!tree.branchPointers.containsKey(branchName)) {
// System.out.println("A branch with that name does not exist.");
// System.exit(0);
// }
//
// if ((stage.getAddedSize() > 0) || (stage.getRemovedSize()>0)) {
// System.out.println("You have uncommitted changes");
// System.exit(0);
//
// }
//
// if(tree.currBranch.equals(branchName)) {
// System.out.println("Cannot merge a branch with itself");
// System.exit(0);
// }
//
// String splitPoint = tree.findSplitNode(branchName);
// Boolean merged = true;
// if (splitPoint == null) {
// System.out.println("Given branch is an ancestor of the current branch.");
// System.exit(0);
// }
// if (splitPoint.equals(tree.branchPointers.get(tree.currBranch))) {
// tree.branchPointers.put(tree.currBranch, tree.branchPointers.get(branchName));
// String currPtrId = tree.branchPointers.get(tree.currBranch);
// tree.currPtr = tree.getCommitNode(currPtrId);
// System.out.println("Current branch fast-forwarded.");
// File cTree = new File(REPO + "/commitTree");
// FileUtil.serialize(cTree, tree);
// System.exit(0);
// }
// CommitNode split = tree.getCommitNode(splitPoint);
// CommitNode given = tree.getCommitNode(tree.branchPointers.get(branchName));
// CommitNode curr = tree.getCommitNode(tree.branchPointers.get(tree.currBranch));
// for (String file : split.fileNames.keySet()) {
// byte[] splitFile = split.getBlob(split.fileNames.get(file)).content;
// if (given.fileNames.containsKey(file) && curr.fileNames.containsKey(file)) {
// byte[] currFile = curr.getBlob(curr.fileNames.get(file)).content;
// byte[] givFile = given.getBlob(given.fileNames.get(file)).content;
// if (currFile.equals(splitFile) && !givFile.equals(splitFile)) {
// tree.checkoutFile(file, given.commitID);
// add(file);
// } else if (!currFile.equals(splitFile) && !givFile.equals(splitFile)
// && !currFile.equals(givFile)) {
// mergeHelper(file, currFile, givFile);
// merged = false;
// }
// } else if (!given.fileNames.containsKey(file) && curr.fileNames.containsKey(file)) {
// byte[] currFile = curr.getBlob(curr.fileNames.get(file)).content;
//// if (currFile.equals(splitFile)) {
// if(Arrays.equals(currFile, splitFile)) {
// rm(file);
// } else if (!currFile.equals(splitFile)) {
// mergeHelper(file, currFile, new byte[0]);
// merged = false;
// }
// } else if (given.fileNames.containsKey(file) && !curr.fileNames.containsKey(file)) {
// byte[] givFile = given.getBlob(given.fileNames.get(file)).content;
//// if (!givFile.equals(splitFile)) {
// if (!Arrays.equals(givFile, splitFile)) {
// mergeHelper(file, new byte[0], givFile);
// merged = false;
// }
// }
// }
// for (String gfile : given.fileNames.keySet()) {
// byte[] givFile = given.getBlob(given.fileNames.get(gfile)).content;
// if (!split.fileNames.containsKey(gfile) && !curr.fileNames.containsKey(gfile)) {
// tree.checkoutFile(gfile, given.commitID);
// add(gfile);
// } else if (!split.fileNames.containsKey(gfile)) {
// byte[] currFile = curr.getBlob(curr.fileNames.get(gfile)).content;
// if (!currFile.equals(givFile)) {
// mergeHelper(gfile, currFile, givFile);
// merged = false;
// }
// }
// }
// if (merged) {
// commit("Merged " + tree.currBranch + " with" + branchName + ".");
// } else {
// System.out.println("Encountered a merge conflict.");
// }
// File cTree = new File(REPO + "/commitTree");
// FileUtil.serialize(cTree, tree);
// }
public void merge(String branchName) {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
File inFileStage = new File(REPO + "/stagingArea");
StagingArea stage = (StagingArea) FileUtil.deSerialize(inFileStage);
if (!tree.branchPointers.containsKey(branchName)) {
System.out.println("A branch with that name does not exist.");
System.exit(0);
}
if ((stage.getAddedSize() > 0) || (stage.getRemovedSize() > 0)) {
System.out.println("You have uncommitted changes");
System.exit(0);
}
if (tree.currBranch.equals(branchName)) {
System.out.println("Cannot merge a branch with itself");
System.exit(0);
}
File pwd = new File(System.getProperty("user.dir"));
CommitNode given = tree.getCommitNode(tree.branchPointers.get(branchName));
CommitNode curr = tree.getCommitNode(tree.branchPointers.get(tree.currBranch));
for (File file : pwd.listFiles()) {
if (!tree.currPtr.fileNames.containsKey(file.getName())
&& given.fileNames.containsKey(file.getName())) {
System.out.println("There is an untracked file in the way;"
+ " delete it or add it first.");
System.exit(0);
}
}
String splitPoint = tree.findSplitNode(branchName);
if (splitPoint == null) {
System.out.println("Given branch is an ancestor of the current branch.");
System.exit(0);
}
if (splitPoint.equals(tree.branchPointers.get(tree.currBranch))) {
tree.branchPointers.put(tree.currBranch, tree.branchPointers.get(branchName));
String currPtrId = tree.branchPointers.get(tree.currBranch);
tree.currPtr = tree.getCommitNode(currPtrId);
System.out.println("Current branch fast-forwarded.");
File cTree = new File(REPO + "/commitTree");
FileUtil.serialize(cTree, tree);
System.exit(0);
}
CommitNode split = tree.getCommitNode(splitPoint);
for (String file : split.fileNames.keySet()) {
byte[] splitFile = split.getBlob(split.fileNames.get(file)).content;
if (given.fileNames.containsKey(file) && curr.fileNames.containsKey(file)) {
byte[] currFile = curr.getBlob(curr.fileNames.get(file)).content;
byte[] givFile = given.getBlob(given.fileNames.get(file)).content;
if (Arrays.equals(currFile, splitFile) && !Arrays.equals(givFile, splitFile)) {
tree.checkoutFile(file, given.commitID);
add(file);
} else if (!Arrays.equals(currFile, splitFile) && !Arrays.equals(givFile, splitFile)
&& !Arrays.equals(currFile, givFile)) {
mergeHelper(file, currFile, givFile);
merged = false;
}
} else if (!given.fileNames.containsKey(file) && curr.fileNames.containsKey(file)) {
byte[] currFile = curr.getBlob(curr.fileNames.get(file)).content;
if (Arrays.equals(currFile, splitFile)) {
rm(file);
} else if (!Arrays.equals(currFile, splitFile)) {
mergeHelper(file, currFile, new byte[0]);
merged = false;
}
} else if (given.fileNames.containsKey(file) && !curr.fileNames.containsKey(file)) {
byte[] givFile = given.getBlob(given.fileNames.get(file)).content;
if (!Arrays.equals(givFile, splitFile)) {
mergeHelper(file, new byte[0], givFile);
merged = false;
}
}
}
mergeExtension(split, curr, given);
if (merged) {
commit("Merged " + tree.currBranch + " with " + branchName + ".");
} else {
System.out.println("Encountered a merge conflict.");
}
}
private void mergeHelper(String filename, byte[] currContent, byte[] givContent) {
try {
Files.write(Paths.get(filename), ("<<<<<<< HEAD" + System.lineSeparator()).getBytes());
Files.write(Paths.get(filename), currContent, StandardOpenOption.APPEND);
Files.write(Paths.get(filename), ("=======" + System.lineSeparator()).getBytes(),
StandardOpenOption.APPEND);
Files.write(Paths.get(filename), givContent, StandardOpenOption.APPEND);
Files.write(Paths.get(filename),
(">>>>>>>" + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
private void mergeExtension(CommitNode split,
CommitNode curr, CommitNode given) {
for (String gfile : given.fileNames.keySet()) {
CommitTree tree = (CommitTree) FileUtil.deSerialize(
new File(REPO + "/commitTree"));
byte[] givFile = given.getBlob(given.fileNames.get(gfile)).content;
if (!split.fileNames.containsKey(gfile) && !curr.fileNames.containsKey(gfile)) {
tree.checkoutFile(gfile, given.commitID);
add(gfile);
} else if (!split.fileNames.containsKey(gfile)) {
byte[] currFile = curr.getBlob(curr.fileNames.get(gfile)).content;
//if (!currFile.equals(givFile)) {
if (!Arrays.equals(currFile, givFile)) {
mergeHelper(gfile, currFile, givFile);
merged = false;
}
}
}
}
public void add(String fileName) {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
CommitNode curr = tree.currPtr;
File inFile2 = new File(REPO + "/stagingArea");
StagingArea stage = (StagingArea) FileUtil.deSerialize(inFile2);
if (!checkInWorkingDir(fileName)) {
System.out.println("File does not exist");
System.exit(0);
} else if (curr.fileNames.containsKey(fileName)) {
String newHash = Utils.sha1(Utils.readContents(new File(fileName)));
String currHash = Utils.sha1(curr.getBlob(curr.fileNames.get(fileName)).content);
if (newHash.equals(currHash)) {
if (stage.getRemovedFiles().contains(fileName)) {
stage.getRemovedFiles().remove(fileName);
}
File sArea = new File(REPO + "/stagingArea");
FileUtil.serialize(sArea, stage);
System.exit(0);
}
}
stage.addFile(fileName);
File sArea = new File(REPO + "/stagingArea");
FileUtil.serialize(sArea, stage);
File cTree = new File(REPO + "/commitTree");
FileUtil.serialize(cTree, tree);
}
private boolean checkInWorkingDir(String fileName) {
File folder = new File(WORKING_DIR);
File[] files = folder.listFiles();
for (File file : files) {
if (file.isFile() && file.getName().equals(fileName)) {
return true;
}
}
return false;
}
public void rm(String fileName) {
File inFile = new File(REPO + "/stagingArea");
StagingArea stage = (StagingArea) FileUtil.deSerialize(inFile);
stage.rm(fileName);
File sArea = new File(REPO + "/stagingArea");
FileUtil.serialize(sArea, stage);
}
public void rmBranch(String branchName) {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
tree.rmBranch(branchName);
File cTree = new File(REPO + "/commitTree");
FileUtil.serialize(cTree, tree);
}
public void branch(String branchName) {
File inFile = new File(REPO + "/commitTree");
CommitTree tree = (CommitTree) FileUtil.deSerialize(inFile);
tree.addBranch(branchName);
File cTree = new File(REPO + "/commitTree");
FileUtil.serialize(cTree, tree);
}
public static void main(String[] args) {
GitLet g = new GitLet();
g.init();
g.add("hi.txt");
g.branch("other");
g.add("hey.txt");
g.commit("version 1");
g.rm("hi.txt");
g.checkout(new String[]{"branch ", "other"});
g.add("hey.txt");
g.commit("version 2");
g.checkout(new String[]{"branch ", "master"});
File f = new File("hi.txt");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}