forked from jreiner16/GitStage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.java
More file actions
389 lines (347 loc) · 17.1 KB
/
Git.java
File metadata and controls
389 lines (347 loc) · 17.1 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
import java.io.*;
import java.lang.reflect.Array;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import javax.print.attribute.HashAttributeSet;
// import org.jcp.xml.dsig.internal.dom.Utils;
public class Git {
private static boolean compression = false;
private static ArrayList<String> workingListAL = new ArrayList<String>();
private static String path;
///
/// INITIALIZES REPOSITORY INCLUDING GIT FOLDER
/// WITH OBJECTS FOLDER AND INDEX FILE
///
public static void initializeRepo(String repoPath) throws IOException {
// initialize main folder, if it doesn't exist already
if (repoPath.equals(""))
System.out.println("Can't Name Repository An Empty String");
Utils.makeDir(repoPath);
path = repoPath;
// initialize git folder inside of main folder, unless one already exists
if (!Utils.makeDir(repoPath + "/git")) {
System.out.println("Git Repository Already Exists");
return;
}
Utils.makeDir(repoPath + "/git/objects");
Utils.makeFile(repoPath + "/git/index");
Utils.makeFile(repoPath + "/git/HEAD");
Utils.makeFile(repoPath, "git/objects/" + Utils.SHA1(""));
System.out.println("Git Repository Created");
}
///
/// DELETES REPOSITORY AT GIVEN PATH
/// RETURNS FALSE IF REPOSITORY NOT FOUND
///
public static void deleteRepo(String repoPath) throws IOException {
if (!Utils.deleteDirectory(repoPath)) {
System.out.println("Repository Does Not Exist");
return;
}
System.out.println("Repository Deleted Successfully");
}
///
/// MAKE A BLOB OF THE FILE AT FILEPATH
/// INSIDE OF THE GIT/OBJECTS FOLDER WHICH IS FOUND IN THE REPO
/// THE NAME OF THE BLOB IS THE SHA1 OF THE CONTENTS
/// THE CONTENTS OF THE BLOB ARE THE COMPRESSED ORIGINAL CONTENTS
///
public static void makeBlob(String filePath, String repoPath) throws IOException {
if (compression) {
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Can't Blob A Nonexistent File!");
return;
}
String fileContents = Utils.readFile(filePath);
Utils.makeFile(repoPath + "/git/objects/" + Utils.SHA1(Utils.compress(fileContents).toString()),
Utils.compress(fileContents).toString());
} else {
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Can't Blob A Nonexistent File!");
return;
}
String fileContents = Utils.readFile(filePath);
Utils.makeFile(repoPath + "/git/objects/" + Utils.SHA1(fileContents),
fileContents);
}
}
///
/// ADD THE FILE AT THE FILE PATH TO THE INDEX FOLDER
///
public static boolean indexFile(String filePath, String repoPath) throws IOException {
File indexFile = new File(repoPath + "/git/index");
// the hash of the file + space + name of file
String indexInfo = Utils.SHA1(Utils.readFile(filePath)) + " " + filePath.substring(filePath.indexOf("/") + 1);
// write it unless it already exists
if (!Utils.readFile(indexFile.getPath()).contains(indexInfo)) {
// add newline unless it's first (moved to fix edge case)
if (indexFile.length() != 0)
indexInfo = "\n" + indexInfo;
Utils.write(repoPath + "/git/index", indexInfo);
return true;
}
return false;
}
public static void refresh(String repoPath) throws IOException {
File mainRepo = new File(repoPath);
File gitRepo = new File(repoPath + "/git");
if (!gitRepo.exists())
initializeRepo(repoPath);
File[] projectFiles = mainRepo.listFiles();
stageFiles(projectFiles);
}
public static void stageFiles(File[] files) throws IOException {
for (File f : files) {
if (f.getName().equals("git"))
continue;
if (f.isDirectory()) {
File[] subFiles = f.listFiles();
stageFiles(subFiles);
} else {
makeBlob(f.getPath(), f.getPath().substring(0, f.getPath().indexOf("/"))); // could this be erroring because pcs use \ and macbooks use /? --> changed "/" to "\\""
indexFile(f.getPath(), f.getPath().substring(0, f.getPath().indexOf("/")));
}
}
}
// Description of makeInitialWorkingList(): This method reads through the index file and makes writes out all the blobs and paths to this working list
public static ArrayList<String> makeInitialWorkingList() throws IOException {
String directoryPath = System.getProperty("user.dir"); // gets root folder
Utils.makeFile(directoryPath + "/workingFile.txt");
String workingFileLOC = directoryPath + "/workingFile.txt";
// read through index file and look for path that has directoryPath in it
//int indexOfSlashBeforeFolderName = directoryPath.indexOf("/");
String indexFileLoc = "ProjectFolder/git/index";
String contentOfIndexFile = Utils.readFile(indexFileLoc);
// System.out.println(contentOfIndexFile);
String hashOFFile = "";
String pathOfFile = "";
String wordsInWorkingFile = "";
int count = 0;
int lastLineBreak = 0;
ArrayList<String> hashNamesOfFile = new ArrayList<String>();
for (int k = 0; k < contentOfIndexFile.length(); k++) {
if (contentOfIndexFile.charAt(k) == 10) { // acii value of 10 equals a new line
System.out.println(k);
count++;
if (count == 1) {
hashOFFile = contentOfIndexFile.substring(0, 40);
hashNamesOfFile.add(hashOFFile);
pathOfFile = contentOfIndexFile.substring(41, k);
//System.out.println("File 1: " + hashOFFile + " PATH: " + pathOfFile);
lastLineBreak = k;
wordsInWorkingFile = "blob " + hashOFFile + " " + pathOfFile;
Utils.write(workingFileLOC, wordsInWorkingFile + "\n");
}
if (count != 1) {
if (k != contentOfIndexFile.length() - 1) {
hashOFFile = contentOfIndexFile.substring(lastLineBreak + 1, lastLineBreak + 41);
if (hashNamesOfFile.contains(hashOFFile) == false) {
pathOfFile = contentOfIndexFile.substring(lastLineBreak + 41, k);
// System.out.println("file " + count + ":" + hashOFFile + " PATH: " + pathOfFile);
lastLineBreak = k;
wordsInWorkingFile = "blob " + hashOFFile + "" + pathOfFile;
Utils.write(workingFileLOC, wordsInWorkingFile + "\n");
hashNamesOfFile.add(hashOFFile);
} else {
lastLineBreak = k;
}
} else {
pathOfFile = contentOfIndexFile.substring(lastLineBreak + 41, k);
hashOFFile = contentOfIndexFile.substring(lastLineBreak + 1, lastLineBreak + 41);
if (hashNamesOfFile.get(0).equals(hashOFFile) == false) {
// System.out.println("file " + count + ":" + hashOFFile + " PATH: " + pathOfFile);
wordsInWorkingFile = "blob " + hashOFFile + "" + pathOfFile;
Utils.write(workingFileLOC, wordsInWorkingFile);
hashNamesOfFile.add(hashOFFile);
}
}
}
}
}
// creates arraylist of paths in workingFile and sets it equal to instance variable
String contentOfWorkinglist = Utils.readFile(workingFileLOC);
ArrayList<String> workingListcontentAL = new ArrayList<String>();
String word = "";
int j = 0;
for (int i = 0; i < contentOfWorkinglist.length(); i++) {
if (contentOfWorkinglist.charAt(i) != 10) {
j++;
word = word + contentOfWorkinglist.charAt(i) + "";
} else {
// System.out.println(word);
workingListcontentAL.add(word);
word = "";
j = 0;
}
}
workingListAL = workingListcontentAL;
return workingListcontentAL;
}
// reads through workingListcontentAL and counts how many slashes are in each element of that array. It then creates a new array called numberOfSlashesArrayList that stores the number of slashes that was found in the first sentence. If numberofSlashesArrayList.get(2) returns 3 then workingListcontentAL.get(2) has 3 slashes
public static ArrayList<Integer> order() throws IOException {
ArrayList<String> workingListPaths = workingListAL;
ArrayList<Integer> numberOfSlashesArrayList = new ArrayList<Integer>();
int numberOfSlashes = 0;
int maxNumberOfSlashes = 0;
int indexOfPathWithMaxNumberOfSlashes = 0;
// int counterOfElementsThatAreNotZero = 0;
// read through workingListcontentAL and count all the slashes in each word, add the number of slashes to a new arraylist
for (int j = 0; j < workingListPaths.size(); j++) {
for (int k = 0; k < workingListPaths.get(j).length(); k++) {
if (workingListPaths.get(j).charAt(k) == '/') {
numberOfSlashes++;
}
}
numberOfSlashesArrayList.add(numberOfSlashes);
numberOfSlashes = 0;
}
// determine the max number of slashes (not really useful tbh)
for (int t = 0; t < numberOfSlashesArrayList.size(); t++) {
if (maxNumberOfSlashes < numberOfSlashesArrayList.get(t)) {
maxNumberOfSlashes = numberOfSlashesArrayList.get(t);
indexOfPathWithMaxNumberOfSlashes = t;
}
}
return numberOfSlashesArrayList;
}
//return number of most slashes by using arraylist from order() method
public static int getMaxSlashes() throws IOException {
ArrayList<Integer> num = order();
int maxValue = 0;
for (int i = 0; i < num.size(); i++) {
if (num.get(i) > maxValue) {
maxValue = num.get(i);
}
}
return maxValue;
}
// creates an array that holds each path that has the most slashes
// to-do: is row zero a possibility here, and should it be?
public static ArrayList<String> maxRows() throws IOException {
ArrayList<String> maxSlashRows = new ArrayList<String>();
for (int i = 0; i < order().size(); i++) {
if (order().get(i) == getMaxSlashes()) {
maxSlashRows.add(workingListAL.get(i));
}
}
return maxSlashRows;
}
// given a string, it getParentPath() will return that string except everything that comes after the last \
public static String getParentPath(String path) throws IOException {
int lastSlash = path.lastIndexOf('/');
if (lastSlash == -1) {
return "";
}
return path.substring(0, lastSlash);
}
// returns a hash map in which the key is the parent folder and the value is all the files in that parent folder
public static HashMap<String, ArrayList<String>> groupByParentPath(ArrayList<String> rows) throws IOException {
HashMap<String, ArrayList<String>> groups = new HashMap<>();
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) { // rows array consists all paths with the max number of slashes
String row = rows.get(rowIndex);
int pathStartIndex = 46; // 46 bc sha1 hash is always 40 characters, blob is 4 characters, and there are 2 spaces
String path = row.substring(pathStartIndex); // getting the path from the line that contains the hash
String parentPath = getParentPath(path); // uses getParentPath() to get only part of path that is after last \
// adding parent folder to hash map if not already there
if (groups.containsKey(parentPath) == false) {
groups.put(parentPath, new ArrayList<String>());
}
// adding any blobs that are in the parent folder to to the value part of the key-value pair where the key is the parent folder
groups.get(parentPath).add(row);
}
return groups;
}
// hashes all the lines that have the same parent folder
public static String createTreeHash(ArrayList<String> rows, String parentPath) throws IOException {
String treeFileContent = "";
for (int i = 0; i < rows.size(); i++) {
String currentRow = rows.get(i);
treeFileContent += currentRow + "\n";
}
String treeHash = Utils.SHA1(treeFileContent);
Utils.makeFile("ProjectFolder/git/objects/" + treeHash, treeFileContent);
return treeHash;
}
public static boolean buildTree() throws IOException {
if (workingListAL.size() == 1) { //
return false;
}
if (maxRows().isEmpty()) {
return false;
}
HashMap<String, ArrayList<String>> folderGroups = groupByParentPath(maxRows());
ArrayList<String> newWorkingList = new ArrayList<>();
for (int i = 0; i < workingListAL.size(); i++) {
String currentRow = workingListAL.get(i);
boolean isDeepRow = false;
int deepestRowSize = maxRows().size(); // row with the most number of slashes
for (int j = 0; j < deepestRowSize; j++) {
if (currentRow.equals(maxRows().get(j))) { // if the current row is the max row it is a row with the most of number of slashes aka it is a deep row
isDeepRow = true;
j = deepestRowSize; // used to stop forloop
}
}
if (!isDeepRow) {
newWorkingList.add(currentRow); // non deep rows added to working list bc they are not being used to make a tree
}
}
ArrayList<String> parentFolders = new ArrayList<>(folderGroups.keySet()); // AL with all the keys from folderGroups as elements
int numGroups = parentFolders.size();
for (int i = 0; i < numGroups; i++) {
String parentFolder = parentFolders.get(i);
ArrayList<String> filesInFolder = folderGroups.get(parentFolder); // gets all the values that go with the key value pair
String treeHash = createTreeHash(filesInFolder, parentFolder);
String treeRow = "tree " + treeHash + " " + parentFolder;
newWorkingList.add(treeRow); // creation of a tree file now added
}
String directoryPath = System.getProperty("user.dir"); // gets root folder
//Utils.makeFile(directoryPath + "/workingFile.txt");
String workingFileLOC = directoryPath + "/workingFile.txt";
Utils.deleteFile(workingFileLOC);
Utils.makeFile(workingFileLOC, ""); // makes a new blank working file
int newListSize = newWorkingList.size();
for (int i = 0; i < newListSize; i++) {
Utils.write(workingFileLOC, newWorkingList.get(i));
if (i < newListSize - 1) {
Utils.write(workingFileLOC, "\n"); // updating working File
}
}
workingListAL = newWorkingList; // updating working list
return true;
}
public static void recursiveBuildTree() throws IOException {
// has to call this top method (makeInitialWorkingLit) first before making tree using (recursiveBuildTreeHelper)
makeInitialWorkingList();
recursiveBuildTreeHelper();
}
public static void recursiveBuildTreeHelper() throws IOException {
if (buildTree()) {
recursiveBuildTreeHelper();
}
}
public static String buildCommit(String author, String message) throws IOException {
Git.recursiveBuildTree();
String treeHash = Utils.readFile("workingFile.txt").split(" ")[1];
StringBuilder commitFileContents = new StringBuilder();
commitFileContents.append("tree: " + treeHash);
String headHash = Utils.readFile(path + "/git/HEAD").strip();
if (!headHash.equals("")) {
String headPath = headHash;
commitFileContents.append("\nparent: " + headPath);
}
commitFileContents.append("\nauthor: " + author);
Instant instant = Instant.now();
long timeStampMillis = instant.toEpochMilli();
commitFileContents.append("\ndate: " + timeStampMillis);
commitFileContents.append("\nmessage: " + message);
String commitHash = Utils.SHA1(commitFileContents.toString());
Utils.makeFile(path + "/git/objects/" + commitHash, commitFileContents.toString());
Utils.write(path + "/git/HEAD", commitHash);
return commitHash;
}
}