-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.java
More file actions
834 lines (689 loc) · 29.4 KB
/
Repository.java
File metadata and controls
834 lines (689 loc) · 29.4 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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
package gitlet;
import java.io.File;
import static gitlet.Utils.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/** Represents a gitlet repository.
* [TODO]: It's a good idea to give a description here of what else this Class
* does at a high level.
*
* @author TODO
*/
public class Repository {
/**
*
*
* List all instance variables of the Repository class here with a useful
* comment above them describing what that variable represents and how that
* variable is used. We've provided two examples for you.
*/
/** The current working directory. */
public static final File CWD = new File(System.getProperty("user.dir"));
public static final File GITLET_DIR = join(CWD, ".gitlet");
public static final File COMMITS = join(GITLET_DIR, "Commits");
public static final File BRANCHES = join(GITLET_DIR, "branches");
public static void init() {
if (!GITLET_DIR.exists()) {
GITLET_DIR.mkdir();
COMMITS.mkdir();
BRANCHES.mkdir();
File master = join(BRANCHES, "master");
createFile(master);
/* the file that will store the current commit */
File f = join(GITLET_DIR, "files");
createFile(f);
File cB = join(GITLET_DIR, "Current Branch");
createFile(cB);
writeContents(cB, "master");
Commit zero = new Commit(null, getDateTime(new Date(0)), "initial commit", null, null);
StagingArea stage = new StagingArea();
writeObject(master, zero);
writeObject(f, stage);
commitToMasterInit(zero, sha1(serialize(zero)));
} else {
String a = "A Gitlet version-control system already exists in the current directory.";
System.out.println(a);
}
}
/* Creates a commit Object*/
public static void commit(String message, Commit p2) {
if (message == null) {
System.out.println("Please enter a commit message.");
} else {
StagingArea currentStage = curStage();
currentStage.untracked = ufiles();
writeToCurrentStage(currentStage);
if (currentStage.fileHash.size() == 0 && currentStage.removeFiles.size() == 0) {
System.out.println("No changes added to the commit.");
} else {
Commit head = commitHEAD();
head = new Commit(getFileHash(), getDateTime(new Date()), message, head, p2);
commitToMaster(head, sha1(serialize(head)));
}
}
}
private static void commitToMaster(Commit h, String sha1) {
StagingArea oldFiles = stageHEAD();
oldFiles.associatedCommit = h;
StagingArea currentFiles = curStage();
HashMap<String, String> getKeys = currentFiles.fileHash;
HashMap<String, Blob> getBlobs = currentFiles.files;
for (HashMap.Entry<String, String> entry : getKeys.entrySet()) {
oldFiles.fileHash.put(entry.getKey(), entry.getValue());
}
for (HashMap.Entry<String, Blob> entry : getBlobs.entrySet()) {
oldFiles.files.put(entry.getKey(), entry.getValue());
}
for (int i = 0; i < currentFiles.removeFiles.size(); i++) {
oldFiles.files.remove(oldFiles.fileHash.get(currentFiles.removeFiles.get(i)));
oldFiles.fileHash.remove(currentFiles.removeFiles.get(i));
}
oldFiles.untracked = currentFiles.untracked;
File f = new File(COMMITS, sha1);
createFile(f);
writeObject(f, oldFiles);
StagingArea A = new StagingArea();
A.untracked = oldFiles.untracked;
writeToCurrentStage(A);
writeToHEADcommit(h);
}
private static void commitToMasterInit(Commit h, String sha1) {
File f = join(COMMITS, sha1);
File files = new File(GITLET_DIR, "files");
createFile(f);
StagingArea stage = curStage();
stage.associatedCommit = h;
ArrayList<String> initUntracked = new ArrayList<>();
for (int i = 0; i < plainFilenamesIn(CWD).size(); i++) {
initUntracked.add(plainFilenamesIn(CWD).get(i));
}
stage.untracked = initUntracked;
writeObject(f, stage);
writeObject(files, new StagingArea());
}
public static void add(String fileTxt) {
File fileInCWD = new File(CWD, fileTxt);
if (fileInCWD.exists()) {
StagingArea prevStage = stageHEAD();
StagingArea A = curStage();
Blob b = new Blob(fileTxt, fileInCWD);
Boolean fileInPrevCommit = prevStage.fileHash.containsKey(fileTxt);
Boolean sameHash = sha1(serialize(b)).equals(prevStage.fileHash.get(fileTxt));
Boolean notInRemove = !A.removeFiles.contains(fileTxt);
if (fileInPrevCommit && sameHash && notInRemove) {
return;
}
if (A.untracked.contains(fileTxt)) {
A.untracked.remove(fileTxt);
}
A.stageAddition(fileTxt);
writeToCurrentStage(A);
} else {
System.out.println("File does not exist.");
}
}
public static void log() {
Commit printHead = commitHEAD();
while (printHead != null) {
System.out.println("===");
System.out.println("commit " + sha1(serialize(printHead)));
if (printHead.parentTwo != null) {
String p1 = sha1(serialize(printHead.parentOne));
String p2 = sha1(serialize(printHead.parentTwo));
System.out.print("Merge: ");
System.out.println(p1.substring(0, 7) + " " + p2.substring(0, 7));
}
System.out.println("Date: " + printHead.getDate());
System.out.println(printHead.getMessage());
System.out.println();
printHead = printHead.parentOne;
}
}
public static void globalLog() {
List<String> commits = plainFilenamesIn(COMMITS);
for (int i = 0; i < commits.size(); i++) {
File f = join(COMMITS, commits.get(i));
Commit c = readObject(f, StagingArea.class).associatedCommit;
System.out.println("===");
System.out.println("commit " + sha1(serialize(c)));
if (c.parentTwo != null) {
String p1 = sha1(serialize(c.parentOne));
String p2 = sha1(serialize(c.parentTwo));
System.out.print("Merge: ");
System.out.println(p1.substring(0, 7) + " " + p2.substring(0, 7));
}
System.out.println("Date: " + c.getDate());
System.out.println(c.getMessage());
System.out.println();
}
}
public static void headCheckout(String file) {
StagingArea files = stageHEAD();
HashMap<String, String> keys = files.fileHash;
HashMap<String, Blob> blobs = files.files;
if (keys.containsKey(file)) {
byte[] sFile = blobs.get(keys.get(file)).serializedFile;
File newFile = new File(CWD, file);
createFile(newFile);
writeContents(newFile, sFile);
} else {
System.out.println("File does not exist in that commit.");
}
}
public static void checkoutByCommit(String commitSHA1, String file) {
String fullSHA1 = containsSHA1(commitSHA1);
if (fullSHA1 != null) {
File f = new File(COMMITS, fullSHA1);
File newFile = new File(CWD, file);
StagingArea sA = readObject(f, StagingArea.class);
HashMap<String, String> getKeys = sA.fileHash;
HashMap<String, Blob> getBlobs = sA.files;
if (getKeys.get(file) != null && getBlobs.get(getKeys.get(file)) != null) {
if (!newFile.exists()) {
createFile(newFile);
}
byte[] serializedFile = getBlobs.get(getKeys.get(file)).serializedFile;
writeContents(newFile, serializedFile);
} else {
System.out.println("File does not exist in that commit.");
}
} else {
System.out.println("No commit with that id exists");
}
}
public static void checkoutBranch(String fileTxt) {
String curBranch = currentBranch();
ArrayList<String> unntrackedCWD = ufiles();
StagingArea oldFiles = stageHEAD();
File f = new File(BRANCHES, fileTxt);
if (f.exists() && !currentBranch().equals(fileTxt)) {
changeBranch(fileTxt);
StagingArea newHeadFiles = stageHEAD();
for (int i = 0; i < unntrackedCWD.size(); i++) {
if (newHeadFiles.fileHash.containsKey(unntrackedCWD.get(i))) {
System.out.println("There is an untracked file in the way; "
+ "delete it, or add and commit it first.");
changeBranch(curBranch);
return;
}
}
for (HashMap.Entry<String, String> entry : oldFiles.fileHash.entrySet()) {
if (!newHeadFiles.fileHash.containsKey(entry.getKey())) {
File fDel = new File(CWD, entry.getKey());
fDel.delete();
}
}
HashMap<String, String> keys = newHeadFiles.fileHash;
HashMap<String, Blob> blobs = newHeadFiles.files;
writeToCurrentStage(new StagingArea());
for (HashMap.Entry<String, String> entry : keys.entrySet()) {
File cwdFile = new File(CWD, entry.getKey());
byte[] serializedFile = blobs.get(entry.getValue()).serializedFile;
createFile(cwdFile);
writeContents(cwdFile, serializedFile);
}
} else if (currentBranch().equals(fileTxt)) {
System.out.println("No need to checkout the current branch.");
} else if (!f.exists()) {
System.out.println("No such branch exists.");
}
}
public static void rm(String fileTxt) {
/** see if fileTxt is in staging Add
* if yes, just remove from files staging area
* if no, see if in prev commit
* if yes, add to area stage for removval */
StagingArea curStage = curStage();
StagingArea prevStage = stageHEAD();
if (prevStage.fileHash.containsKey(fileTxt)) {
File f = new File(CWD, fileTxt);
curStage.stageRemove(fileTxt);
writeToCurrentStage(curStage);
f.delete();
} else if (curStage.fileHash.containsKey(fileTxt)) {
curStage.files.remove(curStage.fileHash.get(fileTxt));
curStage.fileHash.remove(fileTxt);
writeToCurrentStage(curStage);
return;
} else {
System.out.println("No reason to remove the file");
}
}
public static void removeBranch(String branchTxt) {
if (branchTxt.equals(currentBranch())) {
System.out.print("Cannot remove the current branch.");
return;
}
File f = new File(BRANCHES, branchTxt);
if (!f.exists()) {
System.out.println("A branch with that name does not exist.");
} else {
f.delete();
}
}
public static void newBranch(String fileTxt) {
File f = new File(BRANCHES, fileTxt);
if (!f.exists()) {
createFile(f);
Commit c = commitHEAD();
StagingArea A = stageHEAD();
writeObject(f, c);
} else {
System.out.println("A branch with that name already exists.");
}
}
public static void status() {
List<String> brnchs = plainFilenamesIn(BRANCHES);
System.out.println("=== Branches ===");
for (int i = 0; i < brnchs.size(); i++) {
if (brnchs.get(i).equals(currentBranch())) {
System.out.println("*" + brnchs.get(i));
} else {
System.out.println(brnchs.get(i));
}
}
System.out.println("\n=== Staged Files ===");
StagingArea stage = curStage();
for (HashMap.Entry<String, String> entry : stage.fileHash.entrySet()) {
System.out.println(entry.getKey());
}
System.out.println("\n=== Removed Files ===");
for (int i = 0; i < stage.removeFiles.size(); i++) {
System.out.println(stage.removeFiles.get(i));
}
System.out.println("\n=== Modifications Not Staged For Commit ===");
for (HashMap.Entry<String, String> entry : stageHEAD().fileHash.entrySet()) {
if (curStage().fileHash.containsKey(entry.getKey())) {
continue;
} else if (plainFilenamesIn(CWD).contains(entry.getKey())) {
File mod = new File(CWD, entry.getKey());
Blob b = new Blob(entry.getKey(), mod);
String modSHA = sha1(serialize(b));
String shaHead = entry.getValue();
if (!modSHA.equals(shaHead)) {
System.out.println(entry.getKey() + " (modified)");
}
} else if (!curStage().removeFiles.contains(entry.getKey())) {
System.out.println(entry.getKey() + " (deleted)");
}
}
System.out.println("\n=== Untracked Files ===");
List<String> filesinCWD = ufiles();
for (int i = 0; i < filesinCWD.size(); i++) {
System.out.println(filesinCWD.get(i));
}
System.out.println();
}
public static void find(String fileTxt) {
boolean oneFound = false;
List<String> commits = plainFilenamesIn(COMMITS);
for (int i = 0; i < commits.size(); i++) {
File allCommits = new File(COMMITS, commits.get(i));
Commit c = readObject(allCommits, StagingArea.class).associatedCommit;
if (c.getMessage().equals(fileTxt)) {
oneFound = true;
System.out.println(sha1(serialize(c)));
}
}
if (!oneFound) {
System.out.println("Found no commit with that message.");
}
}
public static void reset(String fileTxt) {
List<String> cwdFiles = plainFilenamesIn(CWD);
String fullSha1 = containsSHA1(fileTxt);
if (fullSha1 != null) {
File f = new File(COMMITS, fullSha1);
StagingArea newHead = readObject(f, StagingArea.class);
ArrayList<String> untrackedCWD = ufiles();
String s = "There is an untracked "
+ "file in the way; delete it, or add and commit it first.";
for (int i = 0; i < untrackedCWD.size(); i++) {
if (newHead.fileHash.containsKey(untrackedCWD.get(i))) {
System.out.println(s);
return;
}
}
Commit c = newHead.associatedCommit;
writeToHEADcommit(c);
for (int i = 0; i < cwdFiles.size(); i++) {
String fname = cwdFiles.get(i);
if (!(newHead.untracked.contains(fname))) {
File del = new File(CWD, fname);
del.delete();
}
}
for (HashMap.Entry<String, String> entry : newHead.fileHash.entrySet()) {
String fname = entry.getKey();
checkoutByCommit(fileTxt, fname);
}
writeToCurrentStage(new StagingArea());
} else {
System.out.println("No commit with that id exists.");
}
}
public static void merge(String givenBranch) {
if (!new File(BRANCHES, givenBranch).exists()) {
System.out.println("A branch with that name does not exist.");
return;
}
StagingArea curStage = curStage();
if (!(curStage.fileHash.size() == 0 & curStage.removeFiles.size() == 0)) {
System.out.println("You have uncommitted changes.");
return;
}
if (currentBranch().equals(givenBranch)) {
System.out.println("Cannot merge a branch with itself.");
return;
}
String splitSha = returnSplitSHA1(givenBranch);
if (splitSha.equals(sha1(serialize(commitHEAD())))) {
System.out.println("Current branch fast-forwarded.");
checkoutBranch(givenBranch);
return;
}
if (splitSha.equals(sha1(serialize(getBranch(givenBranch))))) {
System.out.println("Given branch is an ancestor of the current branch.");
return;
}
if (checkUFiles(givenBranch)) {
File stage = new File(COMMITS, splitSha);
StagingArea splitStage = readObject(stage, StagingArea.class);
File givenStageFile = new File(COMMITS, sha1(serialize(getBranch(givenBranch))));
StagingArea givenStage = readObject(givenStageFile, StagingArea.class);
ArrayList<String> allFiles = arrayFiles(givenBranch);
boolean mergeConflict = false;
for (int i = 0; i < allFiles.size(); i++) {
String name = allFiles.get(i);
boolean fileInSplit = fileInSplit(name, splitStage);
boolean fileInGiven = fileInGiven(name, givenStage);
boolean fileInHEAD = fileInCurrent(name, stageHEAD());
boolean modifiedInHEAD = modInCur(name, splitStage, stageHEAD());
boolean modifiedInGiven = modInCur(name, splitStage, givenStage);
boolean givenEqHead = curEqGiven(name, stageHEAD(), givenStage);
boolean inSplitAndCur = fileInSplit && fileInHEAD;
boolean inSplitAndGiv = fileInSplit && fileInGiven;
File writeOver = new File(CWD, name);
createFile(writeOver);
if (inSplitAndGiv && !modifiedInHEAD && modifiedInGiven) {
writeContents(writeOver,
givenStage.files.get(givenStage.fileHash.get(name)).serializedFile);
add(name);
} else if (inSplitAndCur && modifiedInHEAD && !modifiedInGiven) {
continue;
} else if ((!fileInSplit && !fileInGiven && fileInHEAD) || givenEqHead) {
continue;
} else if (!fileInSplit && fileInGiven && !fileInHEAD) {
String temp = givenStage.fileHash.get(name);
writeContents(writeOver,
givenStage.files.get(temp).serializedFile);
add(name);
} else if (fileInSplit && fileInHEAD && !fileInGiven && !modifiedInHEAD) {
rm(name);
} else if (fileInSplit && !fileInHEAD && fileInGiven && !modifiedInGiven) {
writeOver.delete();
} else {
skeleton(name, givenBranch, writeOver);
mergeConflict = true;
add(name);
}
}
if (mergeConflict) {
System.out.println("Encountered a merge conflict.");
}
commit("Merged " + givenBranch + " into "
+ currentBranch() + ".", getBranch(givenBranch));
}
}
/**************************************************************************************/
/***************************** HELPER METHODS FOR MERGE *******************************/
/**************************************************************************************/
public static boolean fileInSplit(String fileName, StagingArea split) {
return split.fileHash.containsKey(fileName);
}
public static ArrayList<String> arrayFiles(String givenBranch) {
File givenStageFile = new File(COMMITS, sha1(serialize(getBranch(givenBranch))));
StagingArea givenStage = readObject(givenStageFile, StagingArea.class);
ArrayList<String> allFiles = new ArrayList<>();
for (HashMap.Entry<String, String> entry : stageHEAD().fileHash.entrySet()) {
allFiles.add(entry.getKey());
}
for (HashMap.Entry<String, String> entry : givenStage.fileHash.entrySet()) {
if (!allFiles.contains(entry.getKey())) {
allFiles.add(entry.getKey());
}
}
return allFiles;
}
public static boolean fileInGiven(String fileName, StagingArea given) {
return given.fileHash.containsKey(fileName);
}
public static boolean fileInCurrent(String fileName, StagingArea current) {
return current.fileHash.containsKey(fileName);
}
public static boolean fileInAllThree(String fileName,
StagingArea split,
StagingArea given, StagingArea current) {
boolean inSplit = fileInSplit(fileName, split);
boolean inGiven = fileInGiven(fileName, given);
boolean inCurrent = fileInCurrent(fileName, current);
return inSplit && inGiven && inCurrent;
}
public static boolean modInGiven(String fileName, StagingArea split, StagingArea given) {
if (!fileInGiven(fileName, given) && fileInSplit(fileName, split)) {
return true;
}
if (fileInGiven(fileName, given) && !fileInSplit(fileName, split)) {
return true;
}
if (fileInGiven(fileName, given) && fileInSplit(fileName, split)) {
return !(split.fileHash.get(fileName).equals(given.fileHash.get(fileName)));
}
return false;
}
public static boolean modInCur(String fileName, StagingArea split, StagingArea cur) {
if (fileInCurrent(fileName, cur) && !fileInSplit(fileName, split)) {
return true;
}
if (!fileInCurrent(fileName, cur) && fileInSplit(fileName, split)) {
return true;
}
if (fileInCurrent(fileName, cur) && fileInSplit(fileName, split)) {
return !(split.fileHash.get(fileName).equals(cur.fileHash.get(fileName)));
}
return false;
}
public static boolean curEqGiven(String fileName, StagingArea cur, StagingArea given) {
if (fileInCurrent(fileName, cur) && fileInGiven(fileName, given)) {
return (cur.fileHash.get(fileName).equals(given.fileHash.get(fileName)));
}
return false;
}
public static String splitCommit(String givenBranch) {
Commit given = getBranch(givenBranch);
Commit curHead = getBranch(currentBranch());
ArrayList<String> givenShas = new ArrayList<>();
while (given != null) {
givenShas.add(sha1(serialize(given)));
given = given.parentOne;
}
while (curHead != null) {
if (givenShas.contains(sha1(serialize(curHead)))) {
return sha1(serialize(curHead));
}
curHead = curHead.parentOne;
}
return "";
}
public static void skeleton(String file, String branch, File curFile) {
File givenFile = new File(COMMITS, sha1(serialize(getBranch(branch))));
StagingArea giv = readObject(givenFile, StagingArea.class);
File tempFile = new File(CWD, "temp");
if (giv.fileHash.containsKey(file)) {
createFile(tempFile);
writeContents(tempFile, giv.files.get(giv.fileHash.get(file)).serializedFile);
}
File headTemp = new File(COMMITS, "headTemp");
StagingArea headFile = stageHEAD();
if (headFile.fileHash.containsKey(file)) {
createFile(headTemp);
writeContents(headTemp, headFile.files.get(headFile.fileHash.get(file)).serializedFile);
}
String newFile = "<<<<<<< HEAD\n";
if (headTemp.exists()) {
String ht = readContentsAsString(headTemp);
newFile += ht;
}
newFile += "=======\n";
if (tempFile.exists()) {
String tf = readContentsAsString(tempFile);
newFile += tf;
}
newFile += ">>>>>>>\n";
writeContents(curFile, newFile);
tempFile.delete();
headTemp.delete();
}
/** END OF HELPER METHODS FOR MERGE */
public static String currentBranch() {
File f = new File(GITLET_DIR, "Current Branch");
String fileName = readContentsAsString(f);
return fileName;
}
private static void changeBranch(String fileTxt) {
File f = new File(GITLET_DIR, "Current Branch");
writeContents(f, fileTxt);
}
private static HashMap<String, String> getFileHash() {
StagingArea stage = curStage();
return stage.fileHash;
}
protected static String getDateTime(Date d) {
SimpleDateFormat inForm = new SimpleDateFormat("EEE MMM d HH:mm:ss YYYY Z");
return inForm.format(d);
}
protected static void createFile(File f) {
try {
f.createNewFile();
} catch (IOException var2) {
return;
}
}
protected static StagingArea curStage() {
File f = new File(GITLET_DIR, "files");
StagingArea currentFiles = readObject(f, StagingArea.class);
return currentFiles;
}
protected static void writeToCurrentStage(StagingArea A) {
File f = new File(GITLET_DIR, "files");
writeObject(f, A);
}
protected static Commit commitHEAD() {
File f = new File(BRANCHES, currentBranch());
Commit head = readObject(f, Commit.class);
return head;
}
protected static void writeToHEADcommit(Commit h) {
File f = new File(BRANCHES, currentBranch());
writeObject(f, h);
}
protected static StagingArea stageHEAD() {
Commit c = commitHEAD();
File f = new File(COMMITS, sha1(serialize(c)));
StagingArea headStage = readObject(f, StagingArea.class);
return headStage;
}
protected static void writeToHEADstage(StagingArea A) {
File f = new File(COMMITS, sha1(serialize(commitHEAD())));
writeObject(f, A);
}
protected static Commit getBranch(String branch) {
File f = new File(BRANCHES, branch);
Commit c = readObject(f, Commit.class);
return c;
}
protected static String containsSHA1(String commitSHA1) {
List<String> fileNames = plainFilenamesIn(COMMITS);
int len = commitSHA1.length();
for (int i = 0; i < fileNames.size(); i++) {
if (fileNames.get(i).substring(0, len).equals(commitSHA1)) {
return fileNames.get(i);
}
}
return null;
}
protected static String returnSplitSHA1(String newBranch) {
ArrayList<String> sha1OfNewBranch = allPrevCommmits(getBranch(newBranch));
ArrayList<String> old = allPrevCommmits(getBranch(currentBranch()));
while (sha1OfNewBranch.size() > 0) {
int index = sha1OfNewBranch.size() - 1;
String node = sha1OfNewBranch.get(index);
if (old.contains(node)) {
return node;
} else {
sha1OfNewBranch.remove(index);
}
}
return "";
}
protected static Commit firstCommitInBranch(String branchName) {
Commit newBranch = getBranch(branchName);
while (newBranch.parentOne != null) {
newBranch = newBranch.parentOne;
}
return newBranch;
}
protected static ArrayList<String> allPrevCommmits(Commit c) {
ArrayList<String> cur = new ArrayList<>();
if (c.parentOne != null) {
ArrayList<String> temp = allPrevCommmits(c.parentOne);
for (int i = 0; i < temp.size(); i++) {
if (!cur.contains(temp.get(i))) {
cur.add(temp.get(i));
}
}
}
if (c.parentTwo != null) {
ArrayList<String> temp = allPrevCommmits(c.parentTwo);
for (int i = 0; i < temp.size(); i++) {
if (!cur.contains(temp.get(i))) {
cur.add(temp.get(i));
}
}
}
cur.add(sha1(serialize(c)));
return cur;
}
public static boolean checkUFiles(String givenBranch) {
ArrayList<String> untrackedCWD = ufiles();
Commit commitHeadBranch = getBranch(givenBranch);
File filesinCommitHead = new File(COMMITS, sha1(serialize(commitHeadBranch)));
StagingArea newHeadFiles = readObject(filesinCommitHead, StagingArea.class);
for (int i = 0; i < untrackedCWD.size(); i++) {
if (newHeadFiles.fileHash.containsKey(untrackedCWD.get(i))) {
System.out.println("There is an untracked file in the way; "
+ "delete it, or add and commit it first.");
return false;
}
}
return true;
}
private static ArrayList<String> ufiles() {
List<String> files = plainFilenamesIn(CWD);
ArrayList<String> untracked = new ArrayList<>();
StagingArea stage = curStage();
StagingArea oldStage = stageHEAD();
HashMap<String, String> newKeys = stage.fileHash;
HashMap<String, String> oldKeys = oldStage.fileHash;
for (int i = 0; i < files.size(); i++) {
boolean notInOld = !oldKeys.containsKey(files.get(i));
boolean notInNew = !newKeys.containsKey(files.get(i));
boolean inRemove = stage.removeFiles.contains(files.get(i));
if (inRemove || (notInOld && notInNew)) {
untracked.add(files.get(i));
}
}
return untracked;
}
}