Skip to content

Commit

Permalink
aadfs
Browse files Browse the repository at this point in the history
  • Loading branch information
ENAMINE1 committed Jul 21, 2024
1 parent e518618 commit af4db2b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 29 deletions.
6 changes: 3 additions & 3 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ cd test_repo
echo "This is an update to file1" >> file1.txt
git_2.0 write-tree .

# git_2.0 commit-tree $(git_2.0 write-tree .) -m "Updated file1"
git_2.0 commit-tree $(git_2.0 write-tree .) -m "Updated file1"


# Modify another file and make a third commit
# echo "This is an update to file2" >> file2.txt
# git_2.0 commit-tree $(git_2.0 write-tree .) -m "Updated file2"
echo "This is an update to file2" >> file2.txt
git_2.0 commit-tree $(git_2.0 write-tree .) -m "Updated file2"

# Now you have a repository with multiple commits to test the checkout functionality
echo "Repository setup complete. You can now test other commands."
10 changes: 6 additions & 4 deletions src/CPP/cat_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ int git_cat_file(int argc, char *argv[])
const std::string dir_name = value.substr(0, 2);
// the rest of the sha1 hash value is the actual file name inside the folder
const std::string blob_sha = value.substr(2);
const auto blob_path = std::filesystem::path(git_path) / "objects" / dir_name / blob_sha;
// cout << MAGENTA << __LINE__ << " cat-file.cpp " << git_path << RESET << endl;
// cout << MAGENTA << __LINE__ << " cat-file.cpp " << blob_path << RESET << endl;
string subdir = "objects";
const fs::path blob_path = git_dir / subdir / dir_name / blob_sha;
cout << MAGENTA << __LINE__ << " cat-file.cpp " << git_path << RESET << endl;
cout << MAGENTA << __LINE__ << " cat-file.cpp " << blob_path << RESET << endl;

std::ifstream input = std::ifstream(blob_path);
if (!input.is_open())
Expand All @@ -43,7 +44,8 @@ int git_cat_file(int argc, char *argv[])
const std::string blob_data = std::string(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
// std::cout << blob_data;
// i have the compressed data of the file which needs to be decompressed using zlib
auto buf = std::string();
string buf = string();
cout<<RED<<buf<<RESET<<endl;
buf.resize(blob_data.size());
while (true)
{
Expand Down
26 changes: 20 additions & 6 deletions src/CPP/commit_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,29 @@ std::string getHomeDirectory()

std::string show_commit(const std::string &commit_hash)
{
char object_path[64];
snprintf(object_path, sizeof(object_path), git_path.c_str(), "/objects/%.2s/%s", commit_hash.c_str(), commit_hash.c_str() + 2);
FILE *object_file = fopen(object_path, "rb");
std::string object_path = git_path + "/objects/" + commit_hash.substr(0, 2) + "/" + commit_hash.substr(2);
FILE *object_file = fopen(object_path.c_str(), "rb");
if (object_file == nullptr)
{
std::cerr << "Invalid object hash: " << commit_hash << "\n";
exit(EXIT_FAILURE);
}

std::cout << RED << __LINE__ << " commit_tree.cpp " << object_path << " " << commit_hash << RESET << endl;
FILE *output_file = tmpfile();
if (output_file == nullptr)
{
std::cerr << "Failed to create output file.\n";
fclose(object_file); // Close the object_file before exiting
exit(EXIT_FAILURE);
}

// Assuming decompress function works correctly
if (decompress(object_file, output_file) != EXIT_SUCCESS)
{
std::cerr << "Failed to decompress object file.\n";
fclose(object_file); // Close the object_file before exiting
fclose(output_file); // Close the output_file before exiting
exit(EXIT_FAILURE);
}

Expand All @@ -153,9 +156,20 @@ std::string show_commit(const std::string &commit_hash)
char buffer[4096]; // Buffer for reading lines
while (fgets(buffer, sizeof(buffer), output_file) != nullptr)
{
// the first line will be of the form commit <length>'\0'tree <tree_hash> so we replace the '\0' with '\n'

commit_content += buffer;
std::string temp = buffer;
if (temp.substr(0, 6) == "commit")
{
cout << GREEN << temp << RESET << endl;
int idx = temp.find("tree");
if (idx != string::npos)
{
commit_content += temp.substr(0, idx);
commit_content += "\n";
commit_content += temp.substr(idx);
}
}
else
commit_content += temp;
}

// Close the files
Expand Down
22 changes: 13 additions & 9 deletions src/CPP/git_add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ void add_index_entry(std::ofstream &index_file, const std::string &mode, const s
void git_add(const std::string &filepath)
{
// Open the index file in append mode
std::ofstream index_file(".git/index", std::ios::binary | std::ios::app);
std::string index_path = git_path + "/index";
std::ofstream index_file("index_path", std::ios::binary | std::ios::app);
if (!index_file)
{
std::cerr << "Error: could not open .git/index" << endl;
Expand All @@ -63,15 +64,18 @@ void git_add(const std::string &filepath)
// Print the filepath for debugging
std::cout << "Opening file: " << filepath << endl;

// Retrieve the file name
std::string filename = std::filesystem::path(filepath).filename().string();
std::cerr<<filename<<endl;
// Calculate the SHA-1 hash of the file content
std::string content = read_file_contents(filepath); // Implement this function to read the file content
std::string hash = sha1_hex(content);
string tree_hash = write_tree(filepath);
index_file<<tree_hash<<endl;

// Add the entry to the index
add_index_entry(index_file, "100644", hash, filename);
// // Retrieve the file name
// std::string filename = std::filesystem::path(filepath).filename().string();
// std::cerr<<filename<<endl;
// // Calculate the SHA-1 hash of the file content
// std::string content = read_file_contents(filepath);
// std::string hash = sha1_hex(content);

// // Add the entry to the index
// add_index_entry(index_file, "100644", hash, filename);

// Close the index file
index_file.close();
Expand Down
4 changes: 2 additions & 2 deletions src/CPP/hash_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ std::string hash_object(const std::string &filepath)
std::ifstream t(filepath, std::ios::binary);
if (!t.is_open())
{
std::cerr << RED << "Could not open file " << filepath << RESET << endl;
// std::cerr << RED << "Could not open file " << filepath << RESET << endl;
return "";
}
std::stringstream data;
data << t.rdbuf();
if (data.str().empty())
{
std::cerr << RED << "fatal: Unable to hash, File is empty: " << filepath << RESET << endl;
// std::cerr << RED << "fatal: Unable to hash, File is empty: " << filepath << RESET << endl;
return "";
}
// if the filepath points to a directory
Expand Down
9 changes: 9 additions & 0 deletions src/CPP/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ int main(int argc, char *argv[])
}
git_checkout(argc, argv);
}
else if(command == "add")
{
if(argc < 3)
{
std::cerr << "No file or folder provided\n";
return EXIT_FAILURE;
}
git_add(argv[2]);
}
else
{
std::cerr << "Unknown command " << command << '\n';
Expand Down
6 changes: 2 additions & 4 deletions src/CPP/write_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ string write_tree(const std::string &directory)
continue;
}
// find the path to this file or dicretory relative to the directory where the git repository is located
fs::path gitDir = locateGitFolder(curr_dir);
string git_path = gitDir.string();
// cout << GREEN << __LINE__ << " write_tree.cpp " << git_path << RESET << endl;
// ignore hidden files
if (std::regex_match(entry.path().filename().string(), hidden_file_regex))
Expand Down Expand Up @@ -67,14 +65,14 @@ string write_tree(const std::string &directory)
}
std::string relative_path = path.substr(path.find(directory) + directory.length() + 1);
std::string hash = std::filesystem::is_directory(path, ec) ? write_tree(path.c_str()) : hash_object(path.c_str());
std::string object_type;
// std::string object_type;
// if (entry_type == "040000 ")
// object_type = "tree";
// else
// object_type = "blob";
// std::cerr << entry_type + ' ' << object_type << ' ' + hash + '\t' + relative_path << endl;

// tree_entries.emplace_back(path + '\0' + entry_type + relative_path + '\0' + hash);
tree_entries.emplace_back(path + '\0' + entry_type + relative_path + '\0' + hash);
}
// sort the entries based on the absolute path O(nlogn)
std::sort(tree_entries.begin(), tree_entries.end());
Expand Down
3 changes: 2 additions & 1 deletion src/Headers/git_add.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

// https://git-scm.com/docs/index-format

#include <string>
#include"utils.h"
#include"write_tree.h"

void git_add(const std::string &file);

Expand Down

0 comments on commit af4db2b

Please sign in to comment.