-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clangd] gitignore index directories #90305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang-tools-extra Author: Sumit Sahrawat (sumit-sahrawat) ChangesThis PR makes This is similar to the behaviour of tools like meson, which adds a gitignore file to the build directories. Full diff: https://github.com/llvm/llvm-project/pull/90305.diff 2 Files Affected:
diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..2931d5999ee5cd 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,20 @@ std::string getShardPathFromFilePath(llvm::StringRef ShardRoot,
return std::string(ShardRootSS);
}
+std::string getGitignorePathForShardRoot(llvm::StringRef ShardRoot) {
+ llvm::SmallString<128> ShardRootSS(ShardRoot);
+ llvm::sys::path::append(ShardRootSS, ".gitignore");
+ return ShardRootSS.str().str();
+}
+
+llvm::Error addGitignore(llvm::StringRef Directory) {
+ auto FilePath = getGitignorePathForShardRoot(Directory);
+ return llvm::writeFileAtomically(
+ FilePath + ".tmp.%%%%%%%%", FilePath,
+ "# This file is autogenerated by clangd. If you change or delete it, "
+ "it won't be recreated.\n*");
+}
+
// Uses disk as a storage for index shards.
class DiskBackedIndexStorage : public BackgroundIndexStorage {
std::string DiskShardRoot;
@@ -40,12 +54,22 @@ class DiskBackedIndexStorage : public BackgroundIndexStorage {
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, std::move(error));
+ }
+ }
}
std::unique_ptr<IndexFileIn>
diff --git a/clang-tools-extra/clangd/test/background-index.test b/clang-tools-extra/clangd/test/background-index.test
index 1983f0957dccf1..c937443a79b77e 100644
--- a/clang-tools-extra/clangd/test/background-index.test
+++ b/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 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
|
|
@llvm/pr-subscribers-clangd Author: Sumit Sahrawat (sumit-sahrawat) ChangesThis PR makes This is similar to the behaviour of tools like meson, which adds a gitignore file to the build directories. Full diff: https://github.com/llvm/llvm-project/pull/90305.diff 2 Files Affected:
diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..2931d5999ee5cd 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,20 @@ std::string getShardPathFromFilePath(llvm::StringRef ShardRoot,
return std::string(ShardRootSS);
}
+std::string getGitignorePathForShardRoot(llvm::StringRef ShardRoot) {
+ llvm::SmallString<128> ShardRootSS(ShardRoot);
+ llvm::sys::path::append(ShardRootSS, ".gitignore");
+ return ShardRootSS.str().str();
+}
+
+llvm::Error addGitignore(llvm::StringRef Directory) {
+ auto FilePath = getGitignorePathForShardRoot(Directory);
+ return llvm::writeFileAtomically(
+ FilePath + ".tmp.%%%%%%%%", FilePath,
+ "# This file is autogenerated by clangd. If you change or delete it, "
+ "it won't be recreated.\n*");
+}
+
// Uses disk as a storage for index shards.
class DiskBackedIndexStorage : public BackgroundIndexStorage {
std::string DiskShardRoot;
@@ -40,12 +54,22 @@ class DiskBackedIndexStorage : public BackgroundIndexStorage {
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, std::move(error));
+ }
+ }
}
std::unique_ptr<IndexFileIn>
diff --git a/clang-tools-extra/clangd/test/background-index.test b/clang-tools-extra/clangd/test/background-index.test
index 1983f0957dccf1..c937443a79b77e 100644
--- a/clang-tools-extra/clangd/test/background-index.test
+++ b/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 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
|
|
@kadircet @sam-mccall kindly review |
This PR makes
clangdadd a.gitignorefile that ignores*when creating index directories (.cache/clangd/index). This makes git repos automatically ignore this directory.This is similar to the behaviour of tools like meson, which adds a gitignore file to the build directories.