diff --git a/soham0-0/Makefile b/soham0-0/Makefile new file mode 100644 index 0000000..30f31f1 --- /dev/null +++ b/soham0-0/Makefile @@ -0,0 +1,21 @@ +CXX := g++ +CXX_FLAGS := -std=c++17 -ggdb -lstdc++fs +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/soham0-0/scripts/build.sh b/soham0-0/scripts/build.sh new file mode 100644 index 0000000..6ad91f7 --- /dev/null +++ b/soham0-0/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/soham0-0/scripts/imperium.sh b/soham0-0/scripts/imperium.sh new file mode 100644 index 0000000..9cdf4e5 --- /dev/null +++ b/soham0-0/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/soham0-0/src/main.cpp b/soham0-0/src/main.cpp new file mode 100644 index 0000000..576b6ce --- /dev/null +++ b/soham0-0/src/main.cpp @@ -0,0 +1,514 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class imperium { + std::string root; + std::string doesExist(std::string); + bool isRepo(); + std::string getTime(); + std::string relativePath(std::string); + bool isIgnored(std::string); + void addToLog(std::string); + void addToCache(std::string); + std::string getHash(std::string); + void purgeAdd(); + void updateCommitLog(std::string, std::string); + bool isSame(std::string, std::string); + public: + /* + Constructor + Sets root to present working directory. + */ + imperium(); + + void getHelp(std::string); + + /* + Sets the root for repo directory if provided. + @param path: path of the repo directory relative to present working directory. + */ + void setRoot(std::string); + + // Initializes empty repository at root directory + void init(); + + /* + Adds current state to the staging area. + @param path: path of file to be added + */ + void add(std::string); + + // Commits the tracked changes + void commit(std::string); + + // Get Log of all commits + void getCommitLog(std::string); + + // Changes current branch + void checkout(std::string); + + // Reverts back to last commit + void revert(); + + // Shows status of the tracked items + void getStatus(); +}; + +int main(int argc, char **argv){ + if(argc<2){ + std::cout << "Too few arguments!\n"; + return -1; + } + + imperium repository; + if(!strcmp(argv[1], "--help")){ + if(argc >= 3) repository.getHelp(argv[2]); + else repository.getHelp(""); + } + else if(!strcmp(argv[1], "init")){ + if(argc==3) repository.setRoot(argv[2]); + repository.init(); + } + else if(!strcmp(argv[1], "add")){ + int currentArgumentNumber = 2; + for(; currentArgumentNumber] [init] [add ] [commit \"\"]" << std::endl; + } + if(allHelp || helpQuery == "init"){ + std::cout << "Start a working area: \n\tinit\tInitialize an Empty repositorty in current or specified folder.\n\tUsage:\timperium init " << std::endl; + } + if(allHelp || helpQuery == "add"){ + std::cout << "Work on current change:\n\tadd\tAdd current state to index.\n\tUsage:\timperium add [.] [list of arguments]" << std::endl; + } + if(allHelp || helpQuery == "commit"){ + std::cout << "Grow, mark and tweak your common history\n\tcommit\tRecord changes to the repository.\n\tUsage:\timperium commit " <(f1.rdbuf()), + std::istreambuf_iterator(), + std::istreambuf_iterator(f2.rdbuf())); +} + +void imperium::getStatus(){ + if(!isRepo()){ + std::cout << "Fatal Error: Not An Imperium Repository" << std::endl; + exit(0); + } + std::vector staged, notStaged; + std::fstream fileReader; + fileReader.open((root+"/.imperium/commit.log" ).c_str(), std::fstream::in); + std::string commitEntry, headHash = "%%%"; + while (std::getline (fileReader, commitEntry)) { + if(commitEntry.substr(48, 4)=="HEAD"){ + headHash = commitEntry.substr(7, 40); + break; + } + } + fileReader.close(); + + fileReader.open((root + "/.imperium/add.log").c_str(), std::fstream::in); + std::string addEntry; + while(std::getline(fileReader, addEntry)){ + addEntry = relativePath(addEntry); + if(doesExist(root + "/.imperium/.commit/" + headHash + "/" + addEntry)=="\0"){ + staged.push_back("created: " + addEntry); + } + else if(!isSame(root + "/.imperium/.add/" + addEntry, root + "/.imperium/.commit/" + headHash + "/" + addEntry)){ + staged.push_back("modified: " + addEntry); + } + + if(!isSame(root + "/.imperium/.add/" + addEntry, root + "/" + addEntry)){ + notStaged.push_back("modified: " + addEntry); + } + } + fileReader.close(); + + if(doesExist(root + "/.imperium/.commit/" + headHash)=="directory"){ + for(auto &subDir : std::filesystem::recursive_directory_iterator(root)){ + if(!isIgnored(subDir.path())){ + if(doesExist(root + "/.imperium/.commit/" + headHash + "/" + relativePath(subDir.path())) != "\0" && doesExist(root + "/.imperium/.add/" + addEntry) == "\0"){ + if(!isSame(subDir.path(), root + "/.imperium/.commit/" + headHash + "/" + relativePath(subDir.path()))){ + notStaged.push_back("modified: " + relativePath(subDir.path())); + } + } + } + } + } + + for(auto &subDir : std::filesystem::recursive_directory_iterator(root)){ + if(!isIgnored(subDir.path())){ + if(doesExist(root + "/.imperium/.add/" + relativePath(subDir.path())) == "\0" && doesExist(root + "/.imperium/.commit/" + headHash + "/" + relativePath(subDir.path())) == "\0"){ + notStaged.push_back("created: " + relativePath(subDir.path())); + } + } + } + + sort(staged.begin(), staged.end()); + sort(notStaged.begin(), notStaged.end()); + + std::cout << "Staged:\n-------" << std::endl; + for(auto s: staged){ + std::cout << s << std::endl; + } + std::cout << std::endl << "Not Staged:\n----------" << std::endl; + for(auto n: notStaged){ + std::cout << n << std::endl; + } + return ; +} \ No newline at end of file