-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.java
799 lines (711 loc) · 31.5 KB
/
Command.java
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
package gitlet;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayDeque;
public class Command {
/** Current Working Directory. */
static final File CWD = new File(System.getProperty("user.dir"));
/** Gitlet. */
static final File GIT = new File(CWD + "/" + ".gitlet");
/** Staging Area file. */
static final File INDEX = new File(CWD + "/" + ".gitlet/index.txt");
/** Logs. */
static final File LOGS = new File(CWD + "/" + ".gitlet/logs");
/** Head commit. */
static final File HEAD = new File(CWD + "/" + ".gitlet/head.txt");
/** Master branch. */
static final File MASTER = new File(CWD + "/" + ".gitlet/master.txt");
/** Blobs. */
static final File BLOBS = new File(CWD + "/" + ".gitlet/blobs");
/** Commits. */
static final File COMMITS = new File(CWD + "/" + ".gitlet/commits");
/** Current branch. */
static final File CURRENT_BRANCH = new File(CWD
+ "/" + ".gitlet/currbranch.txt");
/** Staging Area. */
private static Index stagingArea = new Index();
public static Commit getCommit() {
String commitName = Utils.readContentsAsString(HEAD);
File commitFile = new File(CWD + "/" + ".gitlet/commits/" + commitName);
Commit currentCommit = Utils.readObject(commitFile, Commit.class);
return currentCommit;
}
public static void init() {
if (GIT.exists()) {
System.out.println("A Gitlet version-control system already "
+ "exists in the current directory.");
} else {
GIT.mkdir();
BLOBS.mkdir();
COMMITS.mkdir();
LOGS.mkdir();
stagingArea = new Index();
ZoneId zone = ZoneId.systemDefault();
Commit initial = new Commit("initial commit", null,
"1970-01-01 00:00:00", null);
String commitName = Utils.sha1(Utils.serialize(initial));
File initialCommit = new File(
CWD + "/" + ".gitlet/commits/" + commitName);
try {
HEAD.createNewFile();
MASTER.createNewFile();
INDEX.createNewFile();
initialCommit.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
String currentBranch = "master";
Utils.writeContents(CURRENT_BRANCH, currentBranch);
Utils.writeObject(INDEX, stagingArea);
Utils.writeContents(initialCommit, Utils.serialize(initial));
Utils.writeContents(MASTER, commitName);
Utils.writeContents(HEAD, commitName);
}
}
public static void add(String filename) {
stagingArea = Utils.readObject(INDEX, Index.class);
File newFile = new File(CWD + "/" + filename);
boolean fileInDir = new File(CWD, filename).exists();
if (!fileInDir) {
System.out.println("File does not exist.");
} else {
String blob = Utils.sha1(Utils.readContentsAsString(newFile));
Commit currentCommit = getCommit();
if (currentCommit.getBlob(filename).equals(blob)
&& stagingArea.fileExists(filename)) {
stagingArea.remove((filename));
} else if (stagingArea.fileExistsToDelete(filename)) {
stagingArea.remove(filename);
} else if (currentCommit.getBlob(filename).equals(blob)) {
return;
} else {
stagingArea.toStage(filename, blob);
File newBlob = new File(
CWD + "/.gitlet/blobs/" + blob + ".txt");
try {
newBlob.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Utils.writeContents(newBlob, Utils.readContents(newFile));
}
}
Utils.writeObject(INDEX, stagingArea);
}
public static void commit(String message) {
DateTimeFormatter dateTime = DateTimeFormatter.
ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
if (message.equals("")) {
System.out.println("Please enter a commit message.");
return;
}
Commit newCommit = new Commit(message,
getCommit(), dateTime.format(now), null);
stagingArea = Utils.readObject(INDEX, Index.class);
if (stagingArea.empty()) {
System.out.println("No changes added to the commit.");
return;
}
@SuppressWarnings({"unchecked"})
TreeMap<String, String> trackedFiles = (TreeMap<String, String>)
getCommit().getContents().clone();
TreeMap<String, String> commitFiles = getCommit().getContents();
@SuppressWarnings({"unchecked"})
TreeMap<String, String> add = (TreeMap<String, String>)
stagingArea.getAdd().clone();
TreeMap<String, String> remove = stagingArea.getRemove();
for (Map.Entry<String, String> pair : commitFiles.entrySet()) {
String filename = pair.getKey();
if (add.containsKey(filename)) {
trackedFiles.remove(filename);
trackedFiles.put(pair.getKey(), add.get(filename));
add.remove(filename);
} else if (remove.containsKey(filename)) {
trackedFiles.remove(filename);
}
}
if (!add.isEmpty()) {
for (Map.Entry<String, String> pair : add.entrySet()) {
trackedFiles.put(pair.getKey(), pair.getValue());
}
}
newCommit.setContents(trackedFiles);
String commitName = Utils.sha1(Utils.serialize(newCommit));
File commitFile = new File(CWD + "/.gitlet/commits/" + commitName);
String currentBranch = Utils.readContentsAsString(CURRENT_BRANCH);
File branchPointer = new File(CWD + "/.gitlet/"
+ currentBranch + ".txt");
try {
commitFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Utils.writeObject(commitFile, newCommit);
stagingArea.reset();
Utils.writeObject(INDEX, stagingArea);
Utils.writeContents(HEAD, commitName);
Utils.writeContents(branchPointer, commitName);
}
public static void rm(String filename) {
stagingArea = Utils.readObject(INDEX, Index.class);
if (!stagingArea.fileExists(filename)
&& !getCommit().getContents().containsKey(filename)) {
System.out.println("No reason to remove the file.");
return;
}
if (stagingArea.fileExistsToAdd(filename)) {
stagingArea.remove(filename);
}
if (getCommit().getContents().containsKey(filename)) {
String blob = getCommit().getBlob(filename);
stagingArea.toRemove(filename, blob);
File removeFile = new File(CWD + "/" + filename);
Utils.restrictedDelete(removeFile);
}
Utils.writeObject(INDEX, stagingArea);
}
public static void log() throws ParseException {
File dir = new File(CWD + "/" + ".gitlet");
if (!dir.exists()) {
System.out.println("Not in an initialized Gitlet directory.");
return;
}
Commit currentCommit = getCommit();
while (currentCommit != null) {
String commitName = Utils.sha1(Utils.serialize(currentCommit));
currentCommit.printCommit(commitName);
currentCommit = currentCommit.getParent();
}
}
public static void globalLog() throws ParseException {
File dir = new File(CWD + "/" + ".gitlet/commits");
List<String> filenames = Utils.plainFilenamesIn(dir);
for (String filename : filenames) {
File commitFile = new File(CWD + "/"
+ ".gitlet/commits/" + filename);
Commit commit = Utils.readObject(commitFile, Commit.class);
commit.printCommit(filename);
}
}
public static void find(String commitMessage) {
File dir = new File(CWD + "/" + ".gitlet/commits");
List<String> filenames = Utils.plainFilenamesIn(dir);
boolean commitExists = false;
for (String filename : filenames) {
File commitFile = new File(CWD + "/"
+ ".gitlet/commits/" + filename);
Commit commit = Utils.readObject(commitFile, Commit.class);
if (commit.getMessage().equals(commitMessage)) {
System.out.println(filename);
commitExists = true;
}
}
if (!commitExists) {
System.out.println("Found no commit with that message.");
}
}
public static void status() {
File dir = new File(CWD + "/" + ".gitlet");
if (!dir.exists()) {
System.out.println("Not in an initialized Gitlet directory.");
return;
}
String currentBranch = Utils.readContentsAsString(CURRENT_BRANCH);
String branchName = currentBranch + ".txt";
List<String> filenames = Utils.plainFilenamesIn(dir);
System.out.println("=== Branches ===");
for (String filename : filenames) {
if (branchName.equals(filename)) {
System.out.println("*" + currentBranch);
} else if (!filename.equals("currbranch.txt")
&& !filename.equals("head.txt")
&& !filename.equals("index.txt")) {
File file = new File(GIT + "/" + filename);
String contents = Utils.readContentsAsString(file);
if (!contents.equals("")) {
System.out.println(filename.
substring(0, filename.length() - 4));
}
}
}
System.out.println();
System.out.println("=== Staged Files ===");
stagingArea = Utils.readObject(INDEX, Index.class);
TreeMap<String, String> filesToAddition = stagingArea.getAdd();
for (Map.Entry<String, String> pair : filesToAddition.entrySet()) {
System.out.println(pair.getKey());
}
System.out.println();
System.out.println("=== Removed Files ===");
TreeMap<String, String> filesToRemove = stagingArea.getRemove();
for (Map.Entry<String, String> pair : filesToRemove.entrySet()) {
System.out.println(pair.getKey());
}
System.out.println();
System.out.println("=== Modifications Not Staged For Commit ===");
System.out.println();
System.out.println("=== Untracked Files ===");
System.out.println();
}
public static void checkoutFile(String filename) {
Commit currentCommit = getCommit();
checkout(Utils.readContentsAsString(HEAD), filename);
}
public static void checkout(String commit, String filename) {
File commitFile = new File(CWD + "/" + ".gitlet/commits/" + commit);
Commit currentCommit = Utils.readObject(commitFile, Commit.class);
if (!currentCommit.getContents().containsKey(filename)) {
System.out.println("File does not exist in that commit.");
return;
} else {
File file = new File(CWD + "/" + filename);
if (file.exists()) {
Utils.restrictedDelete(file);
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
String blob = currentCommit.getContents().get(filename);
Utils.writeContents(file, Utils.readContentsAsString
(new File(CWD + "/.gitlet/blobs/" + blob + ".txt")));
}
}
public static void checkoutCommit(String commit, String filename) {
commit = abbreviatedUID(commit);
File commitFile = new File(CWD + "/.gitlet/commits/" + commit);
if (!commitFile.exists()) {
System.out.println("No commit with that id exists.");
return;
}
checkout(commit, filename);
}
public static void checkoutBranch(String branchName) {
String currentBranch = Utils.readContentsAsString(CURRENT_BRANCH);
if (currentBranch.equals(branchName)) {
System.out.println("No need to checkout the current branch.");
return;
}
File branchFile = new File(CWD + "/.gitlet/" + branchName + ".txt");
if (!branchFile.exists()) {
System.out.println("No such branch exists.");
return;
}
Commit currentCommit = getCommit();
String commitName = Utils.readContentsAsString(branchFile);
File fCommit = new File(CWD + "/.gitlet/commits/" + commitName);
Commit finalCommit = Utils.readObject(fCommit, Commit.class);
TreeMap<String, String> currentFiles = currentCommit.getContents();
TreeMap<String, String> finalFiles = finalCommit.getContents();
for (Map.Entry<String, String> toOverwrite : finalFiles.entrySet()) {
File fileInCWD = new File(CWD + "/" + toOverwrite.getKey());
if (!currentFiles.containsKey(toOverwrite.getKey())
&& fileInCWD.exists()) {
System.out.println("There is an untracked file in the way; "
+ "delete it, or add and commit it first.");
return;
}
}
Utils.writeContents(CURRENT_BRANCH, branchName);
Utils.writeContents(HEAD, commitName);
for (Map.Entry<String, String> toDelete : currentFiles.entrySet()) {
if (!finalFiles.containsKey(toDelete.getKey())) {
File delete = new File(CWD + "/" + toDelete.getKey());
Utils.restrictedDelete(delete);
}
}
for (Map.Entry<String, String> toOverwrite : finalFiles.entrySet()) {
File newFile = new File(CWD + "/" + toOverwrite.getKey());
if (!newFile.exists()) {
try {
newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File blob = new File(CWD + "/.gitlet/blobs/"
+ toOverwrite.getValue() + ".txt");
Utils.writeContents(newFile, Utils.readContents(blob));
}
stagingArea = Utils.readObject(INDEX, Index.class);
stagingArea.reset();
Utils.writeObject(INDEX, stagingArea);
}
public static void branch(String name) {
File newBranch = new File(CWD + "/.gitlet/" + name + ".txt");
if (newBranch.exists()) {
System.out.println("A branch with that name already exists.");
return;
}
try {
newBranch.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Utils.writeContents(newBranch, Utils.readContentsAsString(HEAD));
}
public static void removeBranch(String name) {
File branch = new File(CWD + "/.gitlet/" + name + ".txt");
if (!branch.exists()) {
System.out.println("A branch with that name does not exist.");
return;
}
String currentBranch = Utils.readContentsAsString(CURRENT_BRANCH);
if (currentBranch.equals(name)) {
System.out.println("Cannot remove the current branch.");
return;
}
branch.delete();
}
public static void reset(String commit) {
File commitFile = new File(CWD + "/.gitlet/commits/" + commit);
if (!commitFile.exists()) {
System.out.println("No commit with that id exists.");
return;
}
Commit currentCommit = getCommit();
Commit finalCommit = Utils.readObject(commitFile, Commit.class);
TreeMap<String, String> currentFiles = currentCommit.getContents();
TreeMap<String, String> finalFiles = finalCommit.getContents();
for (Map.Entry<String, String> toOverwrite : finalFiles.entrySet()) {
File fileInCWD = new File(CWD + "/" + toOverwrite.getKey());
if (!currentFiles.containsKey(toOverwrite.getKey())
&& fileInCWD.exists()) {
System.out.println("There is an untracked file in the way; "
+ "delete it, or add and commit it first.");
return;
}
}
for (Map.Entry<String, String> toDelete : currentFiles.entrySet()) {
if (!finalFiles.containsKey(toDelete.getKey())) {
File delete = new File(CWD + "/" + toDelete.getKey());
Utils.restrictedDelete(delete);
}
}
for (Map.Entry<String, String> toOverwrite : finalFiles.entrySet()) {
File newFile = new File(CWD + "/" + toOverwrite.getKey());
if (!newFile.exists()) {
try {
newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File blob = new File(CWD + "/.gitlet/blobs/"
+ toOverwrite.getValue() + ".txt");
Utils.writeContents(newFile, Utils.readContents(blob));
}
String branch = Utils.readContentsAsString(CURRENT_BRANCH);
File branchFile = new File(CWD + "/.gitlet/" + branch + ".txt");
Utils.writeContents(branchFile, commit);
Utils.writeContents(HEAD, commit);
stagingArea = Utils.readObject(INDEX, Index.class);
stagingArea.reset();
Utils.writeObject(INDEX, stagingArea);
}
public static void mergeHelper1(TreeMap<String, String> filesHeadCommit,
TreeMap<String, String> filesOtherCommit,
TreeMap<String, String> filesSplitPoint,
ArrayList<Boolean> conflict,
TreeMap<String, String> trackedFiles,
String commitID) {
for (Map.Entry<String, String> files : filesSplitPoint.entrySet()) {
String name = files.getKey();
String blob = files.getValue();
File file = new File(CWD + "/" + name);
if (filesHeadCommit.containsKey(name)
&& filesOtherCommit.containsKey(name)) {
if (!filesOtherCommit.get(name).equals(blob)
&& filesHeadCommit.get(name).equals(blob)) {
checkout(commitID, name);
stagingArea.toStage(name, filesOtherCommit.get(name));
trackedFiles.put(name, filesOtherCommit.get(name));
} else if (filesOtherCommit.get(name).equals(blob)
&& !filesHeadCommit.get(name).equals(blob)) {
checkoutFile(name);
trackedFiles.put(name, filesHeadCommit.get(name));
} else if (!filesOtherCommit.get(name).equals(blob)
&& !filesHeadCommit.get(name).equals(blob)) {
if (!filesOtherCommit.get(name).
equals(filesHeadCommit.get(name))) {
conflict.add(true);
overwriteConflict(name, filesHeadCommit,
filesOtherCommit);
String newBlob = Utils.sha1(Utils.
readContentsAsString(file));
trackedFiles.put(name, newBlob);
} else {
trackedFiles.put(name, filesHeadCommit.get(name));
}
}
} else if (filesHeadCommit.containsKey(name)
&& filesHeadCommit.get(name).equals(blob)
&& !filesOtherCommit.containsKey(name)) {
stagingArea.remove(name);
Utils.restrictedDelete(name);
} else if (filesOtherCommit.containsKey(name)
&& !filesHeadCommit.containsKey(name)
&& filesOtherCommit.get(name).equals(blob)) {
Utils.restrictedDelete(name);
} else if (filesHeadCommit.containsKey(name)
&& !filesHeadCommit.get(name).equals(blob)
&& !filesOtherCommit.containsKey(name)) {
conflict.add(true);
TreeMap<String, String> removed = new TreeMap<>();
removed.put(name, "");
overwriteConflict(name, filesHeadCommit, removed);
String newBlob = Utils.sha1(Utils.readContentsAsString(file));
trackedFiles.put(name, newBlob);
} else if (filesOtherCommit.containsKey(name)
&& !filesOtherCommit.get(name).equals(blob)
&& !filesHeadCommit.containsKey(name)) {
conflict.add(true);
TreeMap<String, String> removed = new TreeMap<>();
removed.put(name, "");
overwriteConflict(name, removed, filesOtherCommit);
String newBlob = Utils.sha1(Utils.readContentsAsString(file));
trackedFiles.put(name, newBlob);
}
}
}
public static void mergeHelper2(TreeMap<String, String> filesHeadCommit,
TreeMap<String, String> filesOtherCommit,
TreeMap<String, String> filesSplitPoint,
ArrayList<Boolean> conflict,
TreeMap<String, String> trackedFiles,
String commitID) {
for (Map.Entry<String, String> files : filesHeadCommit.entrySet()) {
String name = files.getKey();
if (!filesSplitPoint.containsKey(name)
&& !filesOtherCommit.containsKey(name)) {
checkoutFile(name);
trackedFiles.put(name, files.getValue());
}
}
for (Map.Entry<String, String> files : filesOtherCommit.entrySet()) {
String name = files.getKey();
if (!filesSplitPoint.containsKey(name)
&& !filesHeadCommit.containsKey(name)) {
checkoutCommit(commitID, name);
trackedFiles.put(name, files.getValue());
}
}
}
public static void mergeHelper3(TreeMap<String, String> trackedFiles,
String commitID, String branchName,
String currentBranch, Commit otherCommit) {
DateTimeFormatter dateTime = DateTimeFormatter.
ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String message = "Merged " + branchName
+ " into " + currentBranch + ".";
Commit newCommit = new Commit(message, getCommit(),
dateTime.format(now), otherCommit);
newCommit.setContents(trackedFiles);
String commitName = Utils.sha1(Utils.serialize(newCommit));
File newCommitFile = new File(CWD + "/.gitlet/commits/" + commitName);
File branchPointer = new File(CWD
+ "/.gitlet/" + currentBranch + ".txt");
try {
newCommitFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Utils.writeObject(newCommitFile, newCommit);
Utils.writeObject(INDEX, stagingArea);
Utils.writeContents(HEAD, commitName);
Utils.writeContents(branchPointer, commitName);
}
public static void merge(String branchName) {
Commit headCommit = getCommit();
File branch = new File(CWD + "/.gitlet/" + branchName + ".txt");
String currentBranch = Utils.readContentsAsString(CURRENT_BRANCH);
if (!branch.exists()) {
System.out.println("A branch with that name does not exist.");
return;
}
if (branchName.equals(currentBranch)) {
System.out.println("Cannot merge a branch with itself.");
return;
}
String commitID = Utils.readContentsAsString(branch);
File commitFile = new File(CWD + "/" + ".gitlet/commits/" + commitID);
Commit otherCommit = Utils.readObject(commitFile, Commit.class);
Commit splitPoint = findSplitPointAdvanced(headCommit, otherCommit);
String splitPointName = Utils.sha1(Utils.serialize(splitPoint));
String headCommitName = Utils.readContentsAsString(HEAD);
if (splitPointName.equals(commitID)) {
System.out.println("Given branch is an "
+ "ancestor of the current branch.");
return;
}
if (splitPointName.equals(headCommitName)) {
checkoutBranch(branchName);
System.out.println("Current branch fast-forwarded.");
return;
}
stagingArea = Utils.readObject(INDEX, Index.class);
if (!stagingArea.empty()) {
System.out.println("You have uncommitted changes.");
return;
}
TreeMap<String, String> filesHeadCommit = headCommit.getContents();
TreeMap<String, String> filesOtherCommit = otherCommit.getContents();
TreeMap<String, String> filesSplitPoint = splitPoint.getContents();
List<String> filesInCWD = Utils.plainFilenamesIn(CWD);
for (String file : filesInCWD) {
File newFile = new File(CWD + "/" + file);
String blob = Utils.sha1(Utils.readContentsAsString(newFile));
if (!filesHeadCommit.containsKey(file)
&& !filesOtherCommit.get(file).equals(blob)) {
System.out.println("There is an untracked file in the way; "
+ "delete it, or add and commit it first.");
return;
}
}
TreeMap<String, String> trackedFiles = new TreeMap<>();
ArrayList<Boolean> conflict = new ArrayList<>();
conflict.add(false);
mergeHelper1(filesHeadCommit, filesOtherCommit,
filesSplitPoint, conflict, trackedFiles, commitID);
mergeHelper2(filesHeadCommit, filesOtherCommit,
filesSplitPoint, conflict, trackedFiles, commitID);
mergeHelper3(trackedFiles, commitID, branchName,
currentBranch, otherCommit);
if (conflict.get(conflict.size() - 1)) {
System.out.println("Encountered a merge conflict.");
}
}
public static Commit findSplitPoint(Commit headCommit, Commit otherCommit) {
ArrayList<Commit> headCommitTree = new ArrayList<>();
ArrayList<Commit> otherCommitTree = new ArrayList<>();
Commit currCommit = headCommit;
while (currCommit != null) {
headCommitTree.add(currCommit);
currCommit = currCommit.getParent();
}
currCommit = otherCommit;
while (currCommit != null) {
otherCommitTree.add(currCommit);
currCommit = currCommit.getParent();
}
for (Commit commit1 : headCommitTree) {
for (Commit commit2 : otherCommitTree) {
String commit1Name = Utils.sha1(Utils.serialize(commit1));
String commit2Name = Utils.sha1(Utils.serialize(commit2));
if (commit1Name.equals(commit2Name)) {
return commit1;
}
}
}
return getCommit();
}
public static Commit findSplitPointAdvanced(Commit headCommit,
Commit otherCommit) {
ArrayDeque<String> commits = new ArrayDeque<>();
HashSet<String> ancestors = new HashSet<>();
String otherCommitName = Utils.sha1(Utils.serialize(otherCommit));
commits.add(otherCommitName);
while (!commits.isEmpty()) {
String currCommit = commits.remove();
ancestors.add(currCommit);
File currCommitFile = new File(COMMITS + "/" + currCommit);
Commit commit = Utils.readObject(currCommitFile, Commit.class);
if (commit.getParent() != null) {
String parentName = Utils.sha1(
Utils.serialize(commit.getParent()));
commits.add(parentName);
}
if (commit.getMergeParent() != null) {
String mergeParentName = Utils.sha1(
Utils.serialize(commit.getMergeParent()));
commits.add(mergeParentName);
}
}
String headCommitName = Utils.sha1(Utils.serialize(headCommit));
commits.add(headCommitName);
while (!commits.isEmpty()) {
String currCommit = commits.remove();
File currCommitFile = new File(COMMITS + "/" + currCommit);
Commit commit = Utils.readObject(currCommitFile, Commit.class);
if (ancestors.contains(currCommit)) {
return commit;
}
if (commit.getParent() != null) {
String parentName = Utils.sha1(
Utils.serialize(commit.getParent()));
commits.add(parentName);
}
if (commit.getMergeParent() != null) {
String mergeParentName = Utils.sha1(
Utils.serialize(commit.getMergeParent()));
commits.add(mergeParentName);
}
}
return getCommit();
}
public static String abbreviatedUID(String commit) {
File dir = new File(CWD + "/.gitlet/commits");
List<String> files = Utils.plainFilenamesIn(dir);
for (String filename : files) {
if (commit.length() < filename.length()) {
if (filename.startsWith(commit)) {
commit = filename;
}
}
}
return commit;
}
public static void overwriteConflict(String filename,
TreeMap<String, String> currentCommit,
TreeMap<String, String> givenCommit) {
File newFile = new File(CWD + "/" + filename);
String currentContents;
String givenContents;
if (currentCommit.get(filename).equals("")) {
currentContents = "";
} else {
currentContents = Utils.readContentsAsString(new File(CWD
+ "/.gitlet/blobs/"
+ currentCommit.get(filename) + ".txt"));
}
if (givenCommit.get(filename).equals("")) {
givenContents = "";
} else {
givenContents = Utils.readContentsAsString(new File(CWD
+ "/.gitlet/blobs/" + givenCommit.get(filename) + ".txt"));
}
String line1 = "<<<<<<< HEAD\n";
String line2 = currentContents;
String line3 = "=======\n";
String line4 = givenContents;
String line5 = ">>>>>>>\n";
String newContents;
if (line2.equals("")) {
newContents = line1 + line3 + line4 + line5;
} else if (line4.equals("")) {
newContents = line1 + line2 + line3 + line5;
} else {
newContents = line1 + line2 + line3 + line4 + line5;
}
Utils.writeContents(newFile, newContents);
String newBlob = Utils.sha1(Utils.readContentsAsString(newFile));
File newBlobFile = new File(CWD + "/.gitlet/blobs/" + newBlob + ".txt");
try {
newBlobFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}