diff --git a/Sapnanil/Makefile b/Sapnanil/Makefile new file mode 100644 index 0000000..12bf6bc --- /dev/null +++ b/Sapnanil/Makefile @@ -0,0 +1,17 @@ +CXX := g++ +CXX_FLAGS := -std=c++17 -ggdb +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/Sapnanil/scripts/build.sh b/Sapnanil/scripts/build.sh new file mode 100644 index 0000000..6ad91f7 --- /dev/null +++ b/Sapnanil/scripts/build.sh @@ -0,0 +1,16 @@ +#!/bin/bash +sudo apt-get update +sudo apt-get install openssl -y +sudo apt-get install libssl-dev -y +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/../.bashrc" ; then +echo 'already installed bash source'; +else +echo "source $PWD/imperium.sh" >> ~/.bashrc; +fi \ No newline at end of file diff --git a/Sapnanil/scripts/imperium b/Sapnanil/scripts/imperium new file mode 100644 index 0000000..6bbc30f --- /dev/null +++ b/Sapnanil/scripts/imperium @@ -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/Sapnanil/scripts/imperium.sh b/Sapnanil/scripts/imperium.sh new file mode 100644 index 0000000..6bbc30f --- /dev/null +++ b/Sapnanil/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/Sapnanil/src/main.cpp b/Sapnanil/src/main.cpp new file mode 100644 index 0000000..b1270a8 --- /dev/null +++ b/Sapnanil/src/main.cpp @@ -0,0 +1,247 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::experimental::filesystem; + +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.imperium\n.git\n.imperiumignore\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 +"/conflict"; + std::ofstream conflict(conflictlog.c_str()); + conflict<<"False\n"; + conflict.close(); + std::cout<<"Initialized an empty repository\n"; + } + else + { + std::cout<<"ERROR\n"; + } +} +} + +//add function + +void addtoCache(const path ¤tPath) +{ + int len = root.length(); + + string relativePathString = currentPath.string().substr(len); + + path relativePath(relativePathString); + + path rootPath(root); + path rootPathInUserDirectory(root); + + rootPath /= ".imperium"; + rootPath /= ".add"; + + for (const auto &cur : relativePath) + { + if (cur == "/") + continue; + rootPath /= cur; + rootPathInUserDirectory /= cur; + + + if (is_directory(rootPathInUserDirectory)) + { + + if (!exists(rootPath)) + { + create_directory(rootPath.string().c_str()); + } + } + else + { + + const auto copyOptions = copy_options::update_existing; + if (!exists(rootPath)) + { + cout << currentPath << " Initialized.\n"; + } + else + { + cout << currentPath << " Updated.\n"; + } + copy_file(currentPath.string(), rootPath.string(), copyOptions); + } + } + + +} + +bool searchString(ifstream &inputFile, const string &str) +{ + + + string line; + while (getline(inputFile, line)) + { + if (line.find(str) != string::npos) + return true; + } + return false; +} + +void toBeAdded(const path ¤tPath) +{ + + string addLogPath = root + "/.imperium/addlog"; + ifstream addLogFile(addLogPath.c_str()); + + if (!searchString(addLogFile, currentPath.u8string())) + { + + ofstream fout(addLogPath.c_str(), ios::app); + fout << currentPath << "\n"; + fout.close(); + } + else + { + + } + addLogFile.close(); +} + +bool toBeIgnored(const path ¤tPath) +{ + + string ignorePath = root + "/.imperiumIgnore"; + + string currentPathString = currentPath.string(); + string line; + ifstream inputFile(ignorePath); + + while (getline(inputFile, line)) + { + string Directory = root + line; + if (line.back() == '/') + { + Directory.pop_back(); + if (currentPathString.find(Directory) != string::npos) + { + + return true; + } + } + else + { + + if (currentPathString == Directory) + return true; + } + } + inputFile.close(); + return false; +} +void addUtil(const path ¤tPath) +{ + + string imperiumFolder = root + "/.imperium"; + string addFolder = imperiumFolder + "/.add"; + + if (!toBeIgnored(currentPath)) + { + toBeAdded(currentPath); + + + mkdir(addFolder.c_str(), 0777); + addtoCache(currentPath); + } + +} + +void add(vector &argv) +{ + + string imperiumFolder = root + "/.imperium"; + string addFolder = imperiumFolder + "/.add"; + string addLogPath = imperiumFolder + "/addlog"; + string ignorePath = imperiumFolder + "/ignorelog"; + + if (!exists(addLogPath)) + { + cout << "Please initialize the repository first.\n"; + return; + } + + for (auto &str : argv) + { + + if (str == ".") + { + str = ""; + } + path currentPath(root); + currentPath /= str; + if (!exists(currentPath)) + { + cout << "Sorry the file: " << currentPath << " dosen't exist.\n"; + continue; + } + + if (is_regular_file(currentPath)) + { + addUtil(currentPath); + continue; + } + + for (auto &p : recursive_directory_iterator(currentPath)) + { + + addUtil(p.path()); + } + } +} + +int main(int argc, char *argv[]) +{ + root = getenv("dir"); + if (argc == 1) + std::cout << "Hello Welcome to my world of Imperium"; + else if (!strcmp(argv[1], "init")) + { + init(root); + } + else if (!strcmp(argv[1], "add")) + { + + vector Args; + for (int i = 2; i < argc; i++) + { + Args.push_back(argv[i]); + } + add(Args); + } + else if (strcmp(argv[1], "--help")==0) + std::cout<<"Help is always provided just look for it.May the source be with you."; + return 0; +} \ No newline at end of file