From 5e32d71705ad81b216125938c020ac3ef3e0d20b Mon Sep 17 00:00:00 2001 From: Sujal Modanwal <68843492+sujal-ux@users.noreply.github.com> Date: Wed, 7 Oct 2020 00:57:42 +0530 Subject: [PATCH 1/2] Add functionality added --- sujal-ux/Makefile | 18 +++ sujal-ux/scripts/build.sh | 16 +++ sujal-ux/scripts/imperium.sh | 7 ++ sujal-ux/src/main.cpp | 225 +++++++++++++++++++++++++++++++++++ 4 files changed, 266 insertions(+) create mode 100644 sujal-ux/Makefile create mode 100644 sujal-ux/scripts/build.sh create mode 100644 sujal-ux/scripts/imperium.sh create mode 100644 sujal-ux/src/main.cpp diff --git a/sujal-ux/Makefile b/sujal-ux/Makefile new file mode 100644 index 0000000..f608cc7 --- /dev/null +++ b/sujal-ux/Makefile @@ -0,0 +1,18 @@ +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 -lstdc++fs + +clean: + @echo "clean called" + @rm -rf -d ~/imperium/$(BIN)/* \ No newline at end of file diff --git a/sujal-ux/scripts/build.sh b/sujal-ux/scripts/build.sh new file mode 100644 index 0000000..344509a --- /dev/null +++ b/sujal-ux/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 diff --git a/sujal-ux/scripts/imperium.sh b/sujal-ux/scripts/imperium.sh new file mode 100644 index 0000000..a592faa --- /dev/null +++ b/sujal-ux/scripts/imperium.sh @@ -0,0 +1,7 @@ +function imperium(){ +DIR=$PWD +export dir=$DIR +cd ~/imperium/bin || echo "Error" +./main "$@" +cd "$DIR" || echo "Error" +} diff --git a/sujal-ux/src/main.cpp b/sujal-ux/src/main.cpp new file mode 100644 index 0000000..ca6603d --- /dev/null +++ b/sujal-ux/src/main.cpp @@ -0,0 +1,225 @@ +#include +#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<<"Repo has already been initialised\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 + "/conflictlog"; + std::ofstream conflict(conflictlog.c_str()); + conflict<<"false\n"; + conflict.close(); + + std::cout<<"Initialised an empty repository\n"; + } + else{ + std::cout<<"Error\n"; + } + } +} + +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; +} + +void addToCatche(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 toBeIgnored(std::string path, int onlyImperiumIgnore=0) +{ + + std::ifstream addFile, ignoreFile; + std::vector filenames; + std::vector> addedFileNames; + std::string file_or_dir; + std::vector ignoreDir; + + ignoreFile.open((root+"/.imperiumignore")); + addFile.open(root + "/.imperium/addlog"); + + 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(file_or_dir.length()-4), file_or_dir.at(file_or_dir.length()-1))); + } + } + } + addFile.close(); + + for(auto fileEntry : addedFileNames) + { + if(path.compare(fileEntry.first)==0){ + addToCatche(path, fileEntry.second); + std::cout<<"Updated: "<< path<<"\n"; + return true; + } + } + } + if(std::find(filenames.begin(), filenames.end(),path)!= filenames.end() or ignoreFolder(path,ignoreDir)) + return true; + return false; + +} + +void add(char **argv){ + + struct stat buffer; + if(stat((root+"/.imperium").c_str(),&buffer)==0){ + std::ofstream addFile; + addFile.open(root+"/.imperium/addlog", std::ios_base::app); + std::string path = root; + if(strcmp(argv[2],".")!=0){ + path+="/"; + path+=argv[2]; + } + struct stat buffer2; + if(stat(path.c_str(), &buffer2)==0){ + if(buffer2.st_mode & S_IFDIR){ + if(!toBeIgnored(path)){ + addFile<<"\""<2){ + if(strcmp(argv[2],"add")==0){ + add(argv); + } + } + return 0; +} \ No newline at end of file From 76f396042660be700b1ad566c416d664f19effbb Mon Sep 17 00:00:00 2001 From: Sujal Modanwal <68843492+sujal-ux@users.noreply.github.com> Date: Fri, 9 Oct 2020 01:17:24 +0530 Subject: [PATCH 2/2] Commit and add functionality --- sujal-ux/goorm.manifest | 1 + sujal-ux/src/main.cpp | 190 ++++++++++++++++++++++++++++++++++++++-- sujal-ux/test/check.txt | 1 + 3 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 sujal-ux/goorm.manifest create mode 100644 sujal-ux/test/check.txt diff --git a/sujal-ux/goorm.manifest b/sujal-ux/goorm.manifest new file mode 100644 index 0000000..e0db0b0 --- /dev/null +++ b/sujal-ux/goorm.manifest @@ -0,0 +1 @@ +{"storage":"container","type":"cpp","detailedtype":"c","author":"68843492_lqd7c_github","name":"10DaysofCode","description":"Project","date":"2020/10/3 5:8:22","plugins":{"goorm.plugin.cpp":[{"plugin.cpp.compiler_type":"gcc","plugin.cpp.main":"main","plugin.cpp.source_path":"src/","plugin.cpp.makefile_path":"make","plugin.cpp.build_path":"bin/","plugin.cpp.build_option":"-g","plugin.cpp.makefile_option":"false","plugin.cpp.run_option":""}]},"is_user_plugin":false,"author_email":"sujalmodanwal9@gmail.com","author_name":"sujal-ux","ignore_patterns":[],"project_domain":[],"visibility":1} \ No newline at end of file diff --git a/sujal-ux/src/main.cpp b/sujal-ux/src/main.cpp index ca6603d..940a5ed 100644 --- a/sujal-ux/src/main.cpp +++ b/sujal-ux/src/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace fs= std::filesystem; @@ -24,7 +25,7 @@ void init(std::string path) std::string ignorepath= path + "/.imperiumignore"; std::ofstream ignore(ignorepath.c_str()); - ignore<<".gitignore\n.imperium\n.git\n.imperiumignore\n.node_modules\n"; + ignore<<".gitignore\n.imperium/\n.git/\n.imperiumignore\nnode_modules/\n"; ignore.close(); path+="/.imperium"; @@ -68,7 +69,7 @@ bool ignoreFolder(std::string path, std::vector dirname) void addToCatche(std::string path, char type) { struct stat buffer; - if(stat((root + "./imperium/.add").c_str(), &buffer)!=0) + if(stat((root + "/.imperium/.add").c_str(), &buffer)!=0) mkdir((root + "/.imperium/.add").c_str(),0777); if(type == 'f'){ @@ -102,7 +103,7 @@ bool toBeIgnored(std::string path, int onlyImperiumIgnore=0) std::string file_or_dir; std::vector ignoreDir; - ignoreFile.open((root+"/.imperiumignore")); + ignoreFile.open(root+"/.imperiumignore"); addFile.open(root + "/.imperium/addlog"); if(ignoreFile.is_open()){ @@ -180,7 +181,7 @@ void add(char **argv){ addFile< 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/commitlog").c_str()); + rename((root+ "/.imperium/new_commitlog").c_str(),(root+ "/.imperium/commitlog").c_str()); + + readCommitLog.close(); + writeCommitLog.close(); +} + +void commit(std::string message) +{ + + struct stat buffer; + if(stat((root + "/.imperium").c_str(),&buffer)!=0){ + std::cout<<"Repo hasn't been analysed\n"; + } + + std::string commitHash = getcommitHash(); + + //copying from the prevous commit + + if(stat((root+"/.imperium/headlog").c_str(),&buffer)==0) + { + std::string headHash; + std::ifstream readCommitLog; + + readCommitLog.open(root+"/.imperium/headlog"); + + 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; + } + } + } + + //copying from staging + 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/addlog").c_str()); + updateCommitLog(commitHash,message); + +} int main(int argc, char **argv) { const std::string dir = getenv("dir"); @@ -216,10 +386,14 @@ int main(int argc, char **argv) { if(strcmp(argv[1],"init")==0){ init(root); } - if(argc >2){ - if(strcmp(argv[2],"add")==0){ - add(argv); - } + else if(strcmp(argv[1],"add")==0){ + add(argv); + } + 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 diff --git a/sujal-ux/test/check.txt b/sujal-ux/test/check.txt new file mode 100644 index 0000000..f8f8c3a --- /dev/null +++ b/sujal-ux/test/check.txt @@ -0,0 +1 @@ +Hello my name is sujal modanwal. I am here to learn something. \ No newline at end of file