sums created this revision. sums added a reviewer: clang-tools-extra. sums added a project: clang-tools-extra. Herald added subscribers: usaxena95, kadircet, arphaman. Herald added a project: All. sums requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
This change makes clangd add .gitignore files to the index directories that ignore everything using an asterisk (*) value. We only add these gitignore files when creating a new directory, to allow cases where the user doesn't want this behavior (unlikely but not sure). This behavior is similar to meson, which automatically puts a gitignore file in all build directories. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D130863 Files: clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp clang-tools-extra/clangd/test/background-index.test Index: clang-tools-extra/clangd/test/background-index.test =================================================================== --- clang-tools-extra/clangd/test/background-index.test +++ clang-tools-extra/clangd/test/background-index.test @@ -18,6 +18,18 @@ # RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx # RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx +# Test that the the newly created index directories also have a .gitignore file present. +# RUN: ls %/t/.cache/clangd/index/.gitignore +# RUN: ls %/t/sub_dir/.cache/clangd/index/.gitignore + +# Delete .gitignore files to test that they're not recreated when loading an existing index directory. +# RUN: rm %/t/.cache/clangd/index/.gitignore +# RUN: rm %/t/sub_dir/.cache/clangd/index/.gitignore + # Test the index is read from disk: delete code and restart clangd. # RUN: rm %/t/foo.cpp # RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck %/t/definition.jsonrpc --check-prefixes=CHECK,USE + +# Test that the existing index directories do not have recreated .gitignore files. +# RUN: test ! -e %/t/.cache/clangd/index/.gitignore +# RUN: test ! -e %/t/sub_dir/.cache/clangd/index/.gitignore Index: clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp =================================================================== --- clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp +++ clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp @@ -33,6 +33,12 @@ return std::string(ShardRootSS.str()); } +std::string getGitignorePathForShardRoot(llvm::StringRef ShardRoot) { + llvm::SmallString<128> ShardRootSS(ShardRoot); + llvm::sys::path::append(ShardRootSS, llvm::sys::path::filename(".gitignore")); + return std::string(ShardRootSS.str()); +} + // Uses disk as a storage for index shards. class DiskBackedIndexStorage : public BackgroundIndexStorage { std::string DiskShardRoot; @@ -40,12 +46,21 @@ public: // Creates `DiskShardRoot` and any parents during construction. DiskBackedIndexStorage(llvm::StringRef Directory) : DiskShardRoot(Directory) { + bool CreatingNewDirectory = !llvm::sys::fs::is_directory(DiskShardRoot); std::error_code OK; std::error_code EC = llvm::sys::fs::create_directories(DiskShardRoot); if (EC != OK) { elog("Failed to create directory {0} for index storage: {1}", DiskShardRoot, EC.message()); } + + if (CreatingNewDirectory) { + llvm::Error error = addGitignore(DiskShardRoot); + if (error) { + elog("Failed to add .gitignore to directory {0} for index storage: {1}", + DiskShardRoot, error); + } + } } std::unique_ptr<IndexFileIn> @@ -73,6 +88,15 @@ return llvm::Error::success(); }); } + +private: + llvm::Error addGitignore(llvm::StringRef Directory) { + auto FilePath = getGitignorePathForShardRoot(DiskShardRoot); + return llvm::writeFileAtomically( + FilePath + ".tmp", FilePath, + "# This file is autogenerated by clangd. If you change or delete it, " + "it won't be recreated.\n*"); + } }; // Doesn't persist index shards anywhere (used when the CDB dir is unknown).
Index: clang-tools-extra/clangd/test/background-index.test =================================================================== --- clang-tools-extra/clangd/test/background-index.test +++ clang-tools-extra/clangd/test/background-index.test @@ -18,6 +18,18 @@ # RUN: ls %/t/.cache/clangd/index/foo.cpp.*.idx # RUN: ls %/t/sub_dir/.cache/clangd/index/foo.h.*.idx +# Test that the the newly created index directories also have a .gitignore file present. +# RUN: ls %/t/.cache/clangd/index/.gitignore +# RUN: ls %/t/sub_dir/.cache/clangd/index/.gitignore + +# Delete .gitignore files to test that they're not recreated when loading an existing index directory. +# RUN: rm %/t/.cache/clangd/index/.gitignore +# RUN: rm %/t/sub_dir/.cache/clangd/index/.gitignore + # Test the index is read from disk: delete code and restart clangd. # RUN: rm %/t/foo.cpp # RUN: clangd -background-index -lit-test < %/t/definition.jsonrpc | FileCheck %/t/definition.jsonrpc --check-prefixes=CHECK,USE + +# Test that the existing index directories do not have recreated .gitignore files. +# RUN: test ! -e %/t/.cache/clangd/index/.gitignore +# RUN: test ! -e %/t/sub_dir/.cache/clangd/index/.gitignore Index: clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp =================================================================== --- clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp +++ clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp @@ -33,6 +33,12 @@ return std::string(ShardRootSS.str()); } +std::string getGitignorePathForShardRoot(llvm::StringRef ShardRoot) { + llvm::SmallString<128> ShardRootSS(ShardRoot); + llvm::sys::path::append(ShardRootSS, llvm::sys::path::filename(".gitignore")); + return std::string(ShardRootSS.str()); +} + // Uses disk as a storage for index shards. class DiskBackedIndexStorage : public BackgroundIndexStorage { std::string DiskShardRoot; @@ -40,12 +46,21 @@ public: // Creates `DiskShardRoot` and any parents during construction. DiskBackedIndexStorage(llvm::StringRef Directory) : DiskShardRoot(Directory) { + bool CreatingNewDirectory = !llvm::sys::fs::is_directory(DiskShardRoot); std::error_code OK; std::error_code EC = llvm::sys::fs::create_directories(DiskShardRoot); if (EC != OK) { elog("Failed to create directory {0} for index storage: {1}", DiskShardRoot, EC.message()); } + + if (CreatingNewDirectory) { + llvm::Error error = addGitignore(DiskShardRoot); + if (error) { + elog("Failed to add .gitignore to directory {0} for index storage: {1}", + DiskShardRoot, error); + } + } } std::unique_ptr<IndexFileIn> @@ -73,6 +88,15 @@ return llvm::Error::success(); }); } + +private: + llvm::Error addGitignore(llvm::StringRef Directory) { + auto FilePath = getGitignorePathForShardRoot(DiskShardRoot); + return llvm::writeFileAtomically( + FilePath + ".tmp", FilePath, + "# This file is autogenerated by clangd. If you change or delete it, " + "it won't be recreated.\n*"); + } }; // Doesn't persist index shards anywhere (used when the CDB dir is unknown).
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits