diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-06-28 16:57:37 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-06 07:39:32 +0200 |
commit | e132c085d2438fec368bdaf26032037bc7e88d00 (patch) | |
tree | 83047b4a5731d392019ee1c5a17a8531b1e293e3 /src | |
parent | 5bb7ee28bc4f5a0ee119de46165c507d6db995a3 (diff) |
Add compiled files to .gitignore on sibs new and sibs init
Diffstat (limited to 'src')
-rw-r--r-- | src/Conf.cpp | 1 | ||||
-rw-r--r-- | src/main.cpp | 46 |
2 files changed, 46 insertions, 1 deletions
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 <iostream> #include <unordered_set> #include <chrono> +#include <algorithm> #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<ExecResult> gitInitProject(const FileString &projectPath) return exec(cmd.c_str()); } +bool gitIgnoreContainsSibs(const FileString &gitIgnoreFilePath) +{ + Result<StringView> 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<StringView> 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<bool> 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; } |