Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/git
*.DS_Store
.txt
*.class
Binary file removed Blob.class
Binary file not shown.
94 changes: 62 additions & 32 deletions Blob.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.zip.GZIPOutputStream;

import mickeybutil.Terminate;

public class Blob {
private String blobName;
Expand All @@ -21,20 +22,23 @@ public class Blob {
private static boolean compressionAuthorization = false;

public Blob(String fileName, boolean compressionAuthorization) throws IOException {
if (fileName.substring(0, 2).equals("./"))
throw new Error("./ is not allowed");

Blob.compressionAuthorization = compressionAuthorization;

// Check if the git/objects directory exists
if (!Files.exists(Paths.get("git/objects"))) {
throw new FileNotFoundException("No git or objects directories");
}

File ogFile = new File(fileName);

// Check if the file exists
if (!ogFile.exists()) {
throw new FileNotFoundException("File does not exist: " + fileName);
}

// Determine if the input is a file or a directory
if (ogFile.isFile()) {
isBlob = true;
Expand All @@ -48,80 +52,90 @@ public Blob(String fileName, boolean compressionAuthorization) throws IOExceptio
System.err.println("Compression failed, proceeding with uncompressed data.");
}
}

// Create a SHA1 hash and save the file in the objects directory
String blobSHA1 = newFileName(fileContents); // Get SHA1 based on file content
File backupFile = new File("git/objects", blobSHA1);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(backupFile))) {
bw.write(fileContents);
}

// Store original fileName and SHA1 in the index file
String str = "blob " + backupFile.getName() + " " + ogFile.getName() + "\n";
try (BufferedWriter bw2 = new BufferedWriter(new FileWriter("git/index", true))) {
bw2.append(str);
}

blobName = backupFile.getName(); // Set the blobName to the SHA1 of the blob
}

}

else if (ogFile.isDirectory()) {
isTree = true;
StringBuilder treeContent = new StringBuilder();

StringBuilder indexContent = new StringBuilder();

// Iterate over the contents of the directory
for (File file : ogFile.listFiles()) {
if (file.isFile()) {
String fileContents = readFileAsString(file);
String createdHash = encryptThisString(fileContents);
File objectFile = new File ("git/objects", createdHash);
File objectFile = new File("git/objects", createdHash);
BufferedWriter bw = new BufferedWriter(new FileWriter(objectFile));
bw.write(fileContents);
bw.close();
treeContent.append("blob ").append(createdHash).append(" ").append(file.getPath().replace("/Users/avivpilipski/Desktop/12/HTCS_Projects/git-project-AVIV", "")).append("\n");
treeContent.append("blob ").append(createdHash).append(" ")
.append(file.getName())
.append("\n");
indexContent.append("blob ").append(createdHash).append(" ")
.append(file.getPath())
.append("\n");
} else if (file.isDirectory()) {
// Recursively create a tree for each subdirectory
String [] dirContents = file.list();
String[] dirContents = file.list();
String toHash = "";
for (String c : dirContents) {
toHash += c;
}
String newObjectSHA1 = encryptThisString(toHash);
File objectFile = new File ("git/objects", newObjectSHA1);
File objectFile = new File("git/objects", newObjectSHA1);
try (BufferedWriter tempBR = new BufferedWriter(new FileWriter(objectFile))) {
tempBR.write(toHash);
}
Blob subTree = new Blob(file.getAbsolutePath(), compressionAuthorization);
Blob subTree = new Blob(file.getPath(), compressionAuthorization);
String treeSHA1 = subTree.getBlobName(); // Get the tree's SHA1
treeContent.append("tree ").append(treeSHA1).append(" ").append(file.getPath().replace("/Users/avivpilipski/Desktop/12/HTCS_Projects/git-project-AVIV", "")).append("\n");
treeContent.append("tree ").append(treeSHA1).append(" ")
.append(file.getName())
.append("\n");
}
}

// Save the tree content to a file
String treeSHA1 = encryptThisString(treeContent.toString());
File treeFile = new File("git/objects", treeSHA1);
try (BufferedWriter treeWriter = new BufferedWriter(new FileWriter(treeFile))) {
treeWriter.write(treeContent.toString());
}

// Add the tree to the index
try (BufferedWriter indexWriter = new BufferedWriter(new FileWriter("git/index", true))) {
indexWriter.append(treeContent);
// writes all the blobs in the tree to index
indexWriter.append(indexContent);
// adds the current tree to index
indexWriter.append("tree " + treeSHA1 + " " + fileName + "\n");
// all the other trees are written to index when `new Blob`ed
}

blobName = treeSHA1; // Set the blobName to the SHA1 of the tree
}

}

else {
throw new IOException("The specified path is neither a file nor a directory: " + fileName);
}
}



public String getBlobName(){
public String getBlobName() {
return blobName;
}

Expand All @@ -135,12 +149,12 @@ public static byte[] compress(String data) throws IOException {
return compressed;
}

public boolean isCompressed(){
public boolean isCompressed() {
return compressionAuthorization;
}
public String newFileName (String str) throws IOException{
//encrypts a String using the SHA1 function

public String newFileName(String str) throws IOException {
// encrypts a String using the SHA1 function
String SHA1 = encryptThisString(str);
return SHA1;
}
Expand Down Expand Up @@ -176,7 +190,7 @@ public static String encryptThisString(String input) {
}
}

public String readFileAsString(File file) throws IOException {
public static String readFileAsString(File file) throws IOException {
try {
Path path = file.toPath();
return new String(Files.readAllBytes(path));
Expand All @@ -185,4 +199,20 @@ public String readFileAsString(File file) throws IOException {
throw e;
}
}

public static void writeFileAsString(File file, String data) {
try {
Path path = file.toPath();
var parent = file.getParentFile();
if (parent != null && !parent.exists())
parent.mkdirs();
if (!Files.exists(path))
Files.createFile(path);

Files.write(path, data.getBytes());
} catch (IOException e) {
System.err.println("Failed to write to file: " + file.getPath());
Terminate.exception(e);
}
}
}
70 changes: 70 additions & 0 deletions CommitFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import mickeybutil.Terminate;

public class CommitFile {
public String tree;
public String parent;
public String author;
public String date;
public String message;

public CommitFile(String tree, String parent, String author, String message, String date) {
this.tree = tree;
this.parent = parent;
this.author = author;
this.date = date;
this.message = message;
}

static CommitFile read(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
var treeLine = reader.readLine();
var parentLine = reader.readLine();
var authorLine = reader.readLine();
var dateLine = reader.readLine();
var messageLine = reader.readLine();

Terminate.assertEq(treeLine.substring(0, 5), "tree ");
var tree = treeLine.substring(5);
Terminate.assertEq(parentLine.substring(0, 7), "parent ");
var parent = parentLine.substring(7);
Terminate.assertEq(authorLine.substring(0, 7), "author ");
var author = authorLine.substring(7);
Terminate.assertEq(dateLine.substring(0, 5), "date ");
var date = dateLine.substring(9);
Terminate.assertEq(messageLine.substring(0, 8), "message ");
var message = messageLine.substring(8);
reader.close();
return new CommitFile(tree, parent, author, message, date);
} catch (Exception e) {
Terminate.exception(e);
}
return null;
}

public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("tree " + tree + "\n");
sb.append("parent " + parent + "\n");
sb.append("author " + author + "\n");
sb.append("date " + date + "\n");
sb.append("message " + message + "\n");
return sb.toString();
}

static void write(File file, CommitFile commit) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(commit.toString());
writer.close();
} catch (Exception e) {
Terminate.exception(e);
}
}
}
24 changes: 24 additions & 0 deletions DirectoryTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DirectoryTester {
public static void main(String[] args) throws IOException {
Git.deleteDirectory(new File("./git"));
Git.initGit();
Git.stageEverything();
var git = new Git();
var commit = git.commit("me", "test commit");
git.checkout(commit);
}

// Helper method to create a file with specified content
private static void createFile(File file, String content) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(content);
} catch (IOException e) {
System.err.println("Failed to create file: " + file.getAbsolutePath());
}
}
}
Binary file removed Git.class
Binary file not shown.
Loading