From 1c9c2a965e83caf47a93df69cdc49aae35153703 Mon Sep 17 00:00:00 2001 From: Krishnendu Bera Date: Sun, 4 Oct 2020 20:18:23 +0530 Subject: [PATCH 1/4] Added init function in main.cpp --- Makefile | 23 +++++++++++++++ scripts/build.sh | 16 ++++++++++ scripts/imperium.sh | 7 +++++ src/main.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 Makefile create mode 100755 scripts/build.sh create mode 100644 scripts/imperium.sh create mode 100644 src/main.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..16f42ee --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +CXX := g++ +CXX_FLAGS := -std=c++17 -ggdb -L/usr/local/opt/openssl/lib -I /usr/local/opt/openssl/include +BIN := bin +SRC := src +INCLUDE := +LIB := lib +LIBRARIES := +EXECUTABLE := main + + + +all: $(BIN)/$(EXECUTABLE) + +run: clean all + @echo "run called" + ./$(BIN)/$(EXECUTABLE) + +$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp + $(CXX) $(CXX_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $ ~/imperium/$(BIN)/$(EXECUTABLE) $(LIBRARIES) -lssl -lcrypto + +clean: + @echo "clean called" + @rm -rf -d ~/imperium/$(BIN)/* \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..9773cce --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,16 @@ + #!/bin/bash +# brew update +# brew install openssl +# brew install libssl-dev + mkdir -p ~/imperium/bin + cp imperium.sh ~/imperium +cd .. +make +cd ~/imperium/bin || echo "error" +chmod +x main +cd .. +if grep -q "source $PWD/imperium.sh" "$PWD/../.bash_profile" ; then + echo 'already installed bash source'; +else + echo "source $PWD/imperium.sh" >> ~/.bash_profile; +fi diff --git a/scripts/imperium.sh b/scripts/imperium.sh new file mode 100644 index 0000000..aec46cf --- /dev/null +++ b/scripts/imperium.sh @@ -0,0 +1,7 @@ + function imperium(){ + DIR=$PWD + export dir=$DIR + cd ~/imperium/bin || echo "Error" + ./main "$@" + cd "$DIR" || echo "Error" +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..1c321d3 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +std::string root = ""; + +void init(std::string path) +{ + + struct stat buffer; + + if (stat((path + "/.imperium").c_str(), &buffer) == 0) + { + std::cout << "Repository has already been initialized!\n"; + } + else + { + + std::string ignorepath = path + "/.imperiumignore"; + std::ofstream ignore(ignorepath.c_str()); + + ignore << ".gitignore\n.git\n.imperiumignore\n.imperium\n.node_modules\n"; + + ignore.close(); + + path += "/.imperium"; + int created = mkdir(path.c_str(), 0777); + + if (created == 0) + { + + std::string commitlog = path + "/commitlog"; + std::ofstream commit(commitlog.c_str()); + commit.close(); + + std::string addlog = path + "/addlog"; + std::ofstream add(addlog.c_str()); + add.close(); + + std::string conflictlog = path + "/conflictlog"; + std::ofstream conflict(conflictlog.c_str()); + conflict.close(); + + std::cout << " Initialized an empty repository!!\n"; + } + else + { + std::cout << "ERROR\n"; + } + } +} + +int main(int argc, char *argv[]) +{ + + const std::string dir = getenv("dir"); + root = dir; + + if (strcmp(argv[1], "init") == 0) + { + init(root); + } + + return 0; +} \ No newline at end of file From 0f8e6203f4dc8af87840e7488abb75b2a3e272aa Mon Sep 17 00:00:00 2001 From: Krishnendu Bera Date: Sun, 4 Oct 2020 20:41:43 +0530 Subject: [PATCH 2/4] Included project files in a folder named --- Makefile => berakrishnendu36/Makefile | 0 {scripts => berakrishnendu36/scripts}/build.sh | 0 {scripts => berakrishnendu36/scripts}/imperium.sh | 0 {src => berakrishnendu36/src}/main.cpp | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename Makefile => berakrishnendu36/Makefile (100%) rename {scripts => berakrishnendu36/scripts}/build.sh (100%) rename {scripts => berakrishnendu36/scripts}/imperium.sh (100%) rename {src => berakrishnendu36/src}/main.cpp (100%) diff --git a/Makefile b/berakrishnendu36/Makefile similarity index 100% rename from Makefile rename to berakrishnendu36/Makefile diff --git a/scripts/build.sh b/berakrishnendu36/scripts/build.sh similarity index 100% rename from scripts/build.sh rename to berakrishnendu36/scripts/build.sh diff --git a/scripts/imperium.sh b/berakrishnendu36/scripts/imperium.sh similarity index 100% rename from scripts/imperium.sh rename to berakrishnendu36/scripts/imperium.sh diff --git a/src/main.cpp b/berakrishnendu36/src/main.cpp similarity index 100% rename from src/main.cpp rename to berakrishnendu36/src/main.cpp From 59946ed2dd19530046607b0205cbd8254f66baae Mon Sep 17 00:00:00 2001 From: Krishnendu Bera Date: Mon, 5 Oct 2020 19:22:20 +0530 Subject: [PATCH 3/4] Added add functionality --- berakrishnendu36/src/main.cpp | 219 +++++++++++++++++++++++++++++++++- 1 file changed, 217 insertions(+), 2 deletions(-) diff --git a/berakrishnendu36/src/main.cpp b/berakrishnendu36/src/main.cpp index 1c321d3..0d1d3dd 100644 --- a/berakrishnendu36/src/main.cpp +++ b/berakrishnendu36/src/main.cpp @@ -5,11 +5,18 @@ #include #include #include +#include namespace fs = std::filesystem; std::string root = ""; +void init(std::string); +void addToCache(std::string, char); +bool ignoreFolder(std::string, std::vector); +bool toBeIgnored(std::string, int); +void add(char *argv); + void init(std::string path) { @@ -25,7 +32,7 @@ void init(std::string path) std::string ignorepath = path + "/.imperiumignore"; std::ofstream ignore(ignorepath.c_str()); - ignore << ".gitignore\n.git\n.imperiumignore\n.imperium\n.node_modules\n"; + ignore << ".gitignore\n.git\n.imperiumignore\n.imperium/\n.node_modules/\n"; ignore.close(); @@ -47,7 +54,7 @@ void init(std::string path) std::ofstream conflict(conflictlog.c_str()); conflict.close(); - std::cout << " Initialized an empty repository!!\n"; + std::cout << "Initialized an empty repository!!\n"; } else { @@ -56,6 +63,200 @@ void init(std::string path) } } +void addToCache(std::string path, char type) +{ + struct stat buffer; + if (stat((root + "/.imperium/.add").c_str(), &buffer) != 0) + { + mkdir((root + "/.imperium/.add").c_str(), 0777); + } + if (type == 'f') + { + std::string filename = path.substr(root.length()); + std::string filerel = root + "/.imperium/.add" + filename.substr(0, filename.find_last_of("/")); + + struct stat buffer2; + if (stat(filerel.c_str(), &buffer2) != 0) + { + fs::create_directories(filerel); + } + fs::copy_file(path, root + "/.imperium/.add" + filename, fs::copy_options::overwrite_existing); + } + else if (type == 'd') + { + std::string filename = path.substr(root.length()); + std::string filerel = root + "/.imperium/.add" + filename; + + struct stat buffer3; + if (stat(filerel.c_str(), &buffer3) != 0) + { + fs::create_directories(filerel); + } + } +} + +bool ignoreFolder(std::string path, std::vector dirname) +{ + for (auto dir : dirname) + { + dir.pop_back(); + + if (path.find(dir) != std::string::npos) + { + return true; + } + } + return false; +} + +bool toBeIgnored(std::string path, int onlyImperiumIgnore = 0) +{ + std::ifstream addFile, ignoreFile; + std::string file_or_dir; + std::vector filenames; + std::vector> addedFileNames; + std::vector ignoreDir; + + ignoreFile.open(root + "/.imperiumignore"); + addFile.open(root + "/.imperium/add.log"); + + if (ignoreFile.is_open()) + { + + while (!ignoreFile.eof()) + { + std::getline(ignoreFile, file_or_dir); + auto i = file_or_dir.end(); + + i--; + if (*i == '/') + { + ignoreDir.push_back(file_or_dir); + } + else + { + filenames.push_back(root + "/" + file_or_dir); + } + } + } + ignoreFile.close(); + + if (onlyImperiumIgnore == 0) + { + if (addFile.is_open()) + { + while (!addFile.eof()) + { + std::getline(addFile, file_or_dir); + if (file_or_dir.length() > 4) + { + addedFileNames.push_back(std::make_pair(file_or_dir.substr(1, file_or_dir.length() - 4), file_or_dir.at(file_or_dir.length() - 1))); + // std::cout << "test: " << file_or_dir.substr(0, file_or_dir.length() - 2) << "\n"; + } + } + } + addFile.close(); + + for (auto fileEntry : addedFileNames) + { + if (path.compare(fileEntry.first) == 0) + { + addToCache(path, fileEntry.second); + std::cout << "Updated: " << path << std::endl; + return true; + } + } + } + if ((std::find(filenames.begin(), filenames.end(), path)) != filenames.end() || ignoreFolder(path, ignoreDir)) + { + return true; + } + // for (auto filename : filenames) + // { + // std::cout << "Compared .imperiumignore:\n" + // << filename << "\n" + // << path << "\n\n"; + // } + return false; +} + +void add(char *argv) +{ + struct stat buffer; + if (stat((root + "/.imperium").c_str(), &buffer) == 0) + { + std::ofstream addFile; + addFile.open(root + "/.imperium/add.log", std::ios_base::app); + std::string path = root; + if (strcmp(argv, ".") != 0) + { + path = path + "/" + argv; + } + struct stat buffer2; + if (stat(path.c_str(), &buffer2) == 0) + { + if (buffer2.st_mode & S_IFDIR) + { + if (!toBeIgnored(path)) + { + addFile << "\"" << path << "\"" + << "-d\n"; + addToCache(path, 'd'); + std::cout << "Added directory: " + << "\"" << path << "\"" + << "\n"; + } + for (auto &p : fs::recursive_directory_iterator(path)) + { + if (toBeIgnored(p.path())) + continue; + struct stat buffer3; + if (stat(p.path().c_str(), &buffer3) == 0) + { + + if (buffer3.st_mode & S_IFDIR) + { + addToCache(p.path(), 'd'); + addFile << p.path() << "-d\n"; + std::cout << "Added directory: " << p.path() << "\n"; + } + else if (buffer3.st_mode & S_IFREG) + { + addToCache(p.path(), 'f'); + addFile << p.path() << "-f\n"; + std::cout << "Added file: " << p.path() << "\n"; + } + else + { + continue; + } + } + } + } + else if (buffer2.st_mode & S_IFREG) + { + if (!toBeIgnored(path)) + { + addToCache(path, 'f'); + addFile << "\"" << path << "\"" + << "-f\n"; + std::cout << "Added file: " + << "\"" << path << "\"" + << "\n"; + } + } + else + { + std::cout << "Invalid path!\n"; + } + } + } + else + { + std::cout << "Repository has not been initialised yet.\n"; + } +} + int main(int argc, char *argv[]) { @@ -66,6 +267,20 @@ int main(int argc, char *argv[]) { init(root); } + else if (strcmp(argv[1], "add") == 0) + { + if (strcmp(argv[2], ".") != 0) + { + for (int i = 2; i < argc; i++) + { + add(argv[i]); + } + } + else + { + add(argv[2]); + } + } return 0; } \ No newline at end of file From 9ad5e6d5d73656b771ae6654a9400b29cac7b481 Mon Sep 17 00:00:00 2001 From: Krishnendu Bera Date: Thu, 8 Oct 2020 12:52:54 +0530 Subject: [PATCH 4/4] Added commit and commitlog function --- berakrishnendu36/src/main.cpp | 202 +++++++++++++++++++++++++++++++++- 1 file changed, 199 insertions(+), 3 deletions(-) diff --git a/berakrishnendu36/src/main.cpp b/berakrishnendu36/src/main.cpp index 0d1d3dd..209f3da 100644 --- a/berakrishnendu36/src/main.cpp +++ b/berakrishnendu36/src/main.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include namespace fs = std::filesystem; @@ -16,6 +18,13 @@ void addToCache(std::string, char); bool ignoreFolder(std::string, std::vector); bool toBeIgnored(std::string, int); void add(char *argv); +void addCommit(std::string, char, std::string); +void updateCommitLog(std::string, std::string); +void repeatCommit(std::string, char, std::string); +std::string getTime(); +std::string getCommitHash(); +void commit(std::string); +void getCommitLog(); void init(std::string path) { @@ -42,15 +51,15 @@ void init(std::string path) if (created == 0) { - std::string commitlog = path + "/commitlog"; + std::string commitlog = path + "/commit.log"; std::ofstream commit(commitlog.c_str()); commit.close(); - std::string addlog = path + "/addlog"; + std::string addlog = path + "/add.log"; std::ofstream add(addlog.c_str()); add.close(); - std::string conflictlog = path + "/conflictlog"; + std::string conflictlog = path + "/conflict.log"; std::ofstream conflict(conflictlog.c_str()); conflict.close(); @@ -257,6 +266,185 @@ void add(char *argv) } } +void addCommit(std::string abspath, char type, std::string commitHash) +{ + struct stat s; + if (stat((root + "/.imperium/.commit").c_str(), &s) != 0) + { + mkdir((root + "/.imperium/.commit").c_str(), 0777); + } + if (stat((root + "/.imperium/.commit/" + commitHash).c_str(), &s) != 0) + { + mkdir((root + "/.imperium/.commit/" + commitHash).c_str(), 0777); + } + + std::string relpath = abspath.substr(root.length() + 15); + std::string filepath = root + "/.imperium/.commit/" + commitHash + relpath.substr(0, relpath.find_last_of('/')); + + fs::create_directories(filepath); + + if (type == 'f') + { + fs::copy_file(abspath, root + "/.imperium/.commit/" + commitHash + relpath, fs::copy_options::overwrite_existing); + } +} + +void updateCommitLog(std::string commitHash, std::string message) +{ + std::ofstream writeHeadLog; + + writeHeadLog.open(root + "/.imperium/head.log"); + writeHeadLog << commitHash << " -- " << message << std::endl; + writeHeadLog.close(); + + std::ofstream writeCommitLog; + std::ifstream readCommitLog; + + readCommitLog.open(root + "/.imperium/commit.log"); + writeCommitLog.open(root + "/.imperium/new_commit.log"); + + writeCommitLog << commitHash << " -- " << message << " -->HEAD\n"; + + std::string line; + while (std::getline(readCommitLog, line)) + { + if (line.find(" -->HEAD") != std::string::npos) + { + writeCommitLog << line.substr(0, line.length() - 8) << "\n"; + } + else + { + writeCommitLog << line << "\n"; + } + } + + remove((root + "/.imperium/commit.log").c_str()); + rename((root + "/.imperium/new_commit.log").c_str(), (root + "/.imperium/commit.log").c_str()); + + readCommitLog.close(); + writeCommitLog.close(); +} + +void repeatCommit(std::string abspath, char type, std::string commitHash) +{ + mkdir((root + "/.imperium/.commit/" + commitHash).c_str(), 0777); + + std::string relpath = abspath.substr(root.length() + 19 + commitHash.length()); + std::string filepath = root + "/.imperium/.commit/" + commitHash + relpath.substr(0, relpath.find_last_of('/')); + + fs::create_directories(filepath); + + if (type == 'f') + { + fs::copy_file(abspath, root + "/.imperium/.commit/" + commitHash + relpath, fs::copy_options::overwrite_existing); + } +} + +std::string getTime() +{ + auto end = std::chrono::system_clock::now(); + std::time_t end_time = std::chrono::system_clock::to_time_t(end); + std::string time = std::ctime(&end_time); + + return time; +} + +std::string getCommitHash() +{ + std::string commitfilename = getTime(); + std::string commitHash = ""; + + char buf[3]; + int length = commitfilename.length(); + unsigned char hash[20]; + unsigned char *val = new unsigned char[length + 1]; + strcpy((char *)val, commitfilename.c_str()); + + SHA1(val, length, hash); + for (int i = 0; i < 20; i++) + { + sprintf(buf, "%02x", hash[i]); + commitHash += buf; + } + return commitHash; +} + +void commit(std::string message) +{ + struct stat buffer; + if (stat((root + "/.imperium").c_str(), &buffer) != 0) + { + std::cout << "Repository has not been initialised!\n"; + return; + } + + std::string commitHash = getCommitHash(); + + //Copy previous commits + if (stat((root + "/.imperium/head.log").c_str(), &buffer) == 0) + { + std::string headHash; + std::ifstream readCommitLog; + + readCommitLog.open(root + "/.imperium/head.log"); + + std::getline(readCommitLog, headHash); + headHash = headHash.substr(0, headHash.find(" -- ")); + for (auto &p : fs::recursive_directory_iterator(root + "/.imperium/.commit/" + headHash)) + { + + if (stat(p.path().c_str(), &buffer) == 0) + { + if (buffer.st_mode & S_IFREG) + { + repeatCommit(p.path(), 'f', commitHash); + } + else if (buffer.st_mode & S_IFDIR) + { + repeatCommit(p.path(), 'd', commitHash); + } + else + { + continue; + } + } + } + } + + for (auto &p : fs::recursive_directory_iterator(root + "/.imperium/.add")) + { + struct stat s; + if (stat(p.path().c_str(), &s) == 0) + { + if (s.st_mode & S_IFREG) + { + addCommit(p.path(), 'f', commitHash); + } + else if (s.st_mode & S_IFDIR) + { + addCommit(p.path(), 'd', commitHash); + } + } + } + + fs::remove_all((root + "/.imperium/.add").c_str()); + remove((root + "/.imperium/add.log").c_str()); + updateCommitLog(commitHash, message); +} + +void getCommitLog() +{ + std::string commitlogpath = root + "/.imperium/commit.log"; + std::string commitline; + std::ifstream commitlog; + + commitlog.open(commitlogpath); + while (std::getline(commitlog, commitline)) + { + std::cout << commitline << std::endl; + } +} + int main(int argc, char *argv[]) { @@ -281,6 +469,14 @@ int main(int argc, char *argv[]) add(argv[2]); } } + else if (strcmp(argv[1], "commit") == 0) + { + commit(argv[2]); + } + else if (strcmp(argv[1], "log") == 0) + { + getCommitLog(); + } return 0; } \ No newline at end of file