From e132c085d2438fec368bdaf26032037bc7e88d00 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 28 Jun 2018 16:57:37 +0200 Subject: Add compiled files to .gitignore on sibs new and sibs init --- src/Conf.cpp | 1 + src/main.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Conf.cpp b/src/Conf.cpp index 4424551..c71c7bc 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -458,6 +458,7 @@ namespace sibs if(fileContentResult.unwrap().size >= 3 && utf8::is_bom(code)) code += 3; + // Do not free file content (fileContentResult) on purpose, since we are using the data and sibs is short lived return Parser::parse(code, callback); } diff --git a/src/main.cpp b/src/main.cpp index 2dc1364..22198b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "../include/FileUtil.hpp" #include "../include/Conf.hpp" #include "../include/Exec.hpp" @@ -97,6 +98,12 @@ using namespace std::chrono; #define ferr std::wcerr #endif +static string SIBS_GITIGNORE_HEADER = "# Compiled sibs files"; +static string SIBS_GITIGNORE_FILES = + "sibs-build/\n" + "tests/sibs-build/\n" + "compile_commands.json\n"; + void usage() { printf("Usage: sibs COMMAND\n\n"); @@ -505,6 +512,37 @@ Result gitInitProject(const FileString &projectPath) return exec(cmd.c_str()); } +bool gitIgnoreContainsSibs(const FileString &gitIgnoreFilePath) +{ + Result fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); + if(!fileContentResult) return false; + StringView fileContent = fileContentResult.unwrap(); + const char *fileContentEnd = fileContent.data + fileContent.size; + auto it = std::search(fileContent.data, fileContentEnd, SIBS_GITIGNORE_HEADER.begin(), SIBS_GITIGNORE_HEADER.end()); + bool containsSibs = it != fileContentEnd; + free((void*)fileContent.data); + return containsSibs; +} + +void gitIgnoreAppendSibs(const FileString &gitIgnoreFilePath) +{ + Result fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); + string fileContentNew; + if(fileContentResult) + { + StringView fileContent = fileContentResult.unwrap(); + fileContentNew.append(fileContent.data, fileContent.data + fileContent.size); + fileContentNew += "\n\n"; + free((void*)fileContent.data); + } + fileContentNew += SIBS_GITIGNORE_HEADER; + fileContentNew += "\n"; + fileContentNew += SIBS_GITIGNORE_FILES; + Result result = fileOverwrite(gitIgnoreFilePath.c_str(), { fileContentNew.data(), fileContentNew.size() }); + if(!result) + ferr << "Failed to add sibs to .gitignore, reason: " << result.getErrMsg() << endl; +} + int initProject(int argc, const _tinydir_char_t **argv) { if(argc > 2) @@ -650,8 +688,12 @@ int initProject(int argc, const _tinydir_char_t **argv) if(!fileOverwriteResult) fout << "Warning: Failed to create project file: " << toFileString(fileOverwriteResult.getErrMsg()) << endl; auto gitProjDir = projectPath + TINYDIR_STRING("/.git"); - if(getFileType(gitProjDir.c_str()) != FileType::FILE_NOT_FOUND) + if(getFileType(gitProjDir.c_str()) == FileType::FILE_NOT_FOUND) gitInitProject(projectPath); + + auto gitIgnoreFilePath = projectPath + TINYDIR_STRING("/.gitignore"); + if(!gitIgnoreContainsSibs(gitIgnoreFilePath)) + gitIgnoreAppendSibs(gitIgnoreFilePath); return 0; } @@ -750,6 +792,8 @@ int newProject(int argc, const _tinydir_char_t **argv) } // We are ignoring git init result on purpose. If it fails, just ignore it; not important gitInitProject(projectPath); + auto gitIgnoreFilePath = projectPath + TINYDIR_STRING("/.gitignore"); + gitIgnoreAppendSibs(gitIgnoreFilePath); return 0; } -- cgit v1.2.3