[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat created 
https://github.com/llvm/llvm-project/pull/90305

This PR makes `clangd` add a `.gitignore` file when creating index directories 
(`.cache/clangd/index`), with the content `*`. 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.

>From b1788cf6b39889c9d0221c7e05a279fece95d5f8 Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 06:09:36 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 24 +++
 .../clangd/test/background-index.test | 12 ++
 2 files changed, 36 insertions(+)

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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat updated 
https://github.com/llvm/llvm-project/pull/90305

>From b713f37e4cb9f53e66904890d0207c7b4349f2a8 Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 06:23:43 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 24 +++
 .../clangd/test/background-index.test | 12 ++
 2 files changed, 36 insertions(+)

diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..fb526d613d9e85 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::writeToOutput(
+  FilePath, 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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat edited 
https://github.com/llvm/llvm-project/pull/90305
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat updated 
https://github.com/llvm/llvm-project/pull/90305

>From 32039a6494c36dbd875d038ebb66196d4013c156 Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 06:28:07 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 23 +++
 .../clangd/test/background-index.test | 12 ++
 2 files changed, 35 insertions(+)

diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..028b8cb4e2595b 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,19 @@ 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::writeToOutput(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 +53,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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat updated 
https://github.com/llvm/llvm-project/pull/90305

>From 8cde06cc546950eb9f9c6a807129d18ef5da882e Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 06:33:17 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 27 +++
 .../clangd/test/background-index.test | 12 +
 2 files changed, 39 insertions(+)

diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..04e27c4db9b719 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,23 @@ 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::writeToOutput(FilePath, [](llvm::raw_ostream &OS) {
+OS << "# This file is autogenerated by clangd.";
+OS << "\n";
+OS << "# If you change or delete it, it won't be recreated.";
+OS << "\n";
+OS << "*";
+  });
+}
+
 // Uses disk as a storage for index shards.
 class DiskBackedIndexStorage : public BackgroundIndexStorage {
   std::string DiskShardRoot;
@@ -40,12 +57,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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat updated 
https://github.com/llvm/llvm-project/pull/90305

>From e62ba302157d7f3576f7d2d24d90e702700c2a4d Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 06:52:10 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 28 +++
 .../clangd/test/background-index.test | 12 
 2 files changed, 40 insertions(+)

diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..3ec26e36d50ebe 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,24 @@ 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::writeToOutput(FilePath, [](llvm::raw_ostream &OS) {
+OS << "# This file is autogenerated by clangd.";
+OS << "\n";
+OS << "# If you change or delete it, it won't be recreated.";
+OS << "\n";
+OS << "*";
+return llvm::Error::success();
+  });
+}
+
 // Uses disk as a storage for index shards.
 class DiskBackedIndexStorage : public BackgroundIndexStorage {
   std::string DiskShardRoot;
@@ -40,12 +58,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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat updated 
https://github.com/llvm/llvm-project/pull/90305

>From d4c734b59e6c3184d42af2b26ef50d57db662796 Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 06:57:29 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 26 +++
 .../clangd/test/background-index.test | 12 +
 2 files changed, 38 insertions(+)

diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..a4c2a4bf072fd4 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,22 @@ 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::writeToOutput(FilePath, [](llvm::raw_ostream &OS) {
+OS << "# This file is autogenerated by clangd.\n"
+   << "# If you change or delete it, it won't be recreated.\n"
+   << "*\n";
+return llvm::Error::success();
+  });
+}
+
 // Uses disk as a storage for index shards.
 class DiskBackedIndexStorage : public BackgroundIndexStorage {
   std::string DiskShardRoot;
@@ -40,12 +56,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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat updated 
https://github.com/llvm/llvm-project/pull/90305

>From ca152a90259afa79ea413b8e4a21165cebcd7d5a Mon Sep 17 00:00:00 2001
From: Sumit Sahrawat <139506625+sumit-sahra...@users.noreply.github.com>
Date: Sat, 27 Apr 2024 07:02:58 +0530
Subject: [PATCH] [clangd] gitignore index directories

---
 .../clangd/index/BackgroundIndexStorage.cpp   | 22 +++
 .../clangd/test/background-index.test | 12 ++
 2 files changed, 34 insertions(+)

diff --git a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp 
b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
index d887b09482a959..fcc3d89744e067 100644
--- a/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+++ b/clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
@@ -33,6 +33,18 @@ std::string getShardPathFromFilePath(llvm::StringRef 
ShardRoot,
   return std::string(ShardRootSS);
 }
 
+llvm::Error addGitignore(llvm::StringRef Directory) {
+  llvm::SmallString<128> FilePath(Directory);
+  llvm::sys::path::append(FilePath, ".gitignore");
+
+  return llvm::writeToOutput(FilePath.str(), [](llvm::raw_ostream &OS) {
+OS << "# This file is autogenerated by clangd.\n"
+   << "# If you change or delete it, it won't be recreated.\n"
+   << "*\n";
+return llvm::Error::success();
+  });
+}
+
 // Uses disk as a storage for index shards.
 class DiskBackedIndexStorage : public BackgroundIndexStorage {
   std::string DiskShardRoot;
@@ -40,12 +52,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
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

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2024-04-26 Thread Sumit Sahrawat via cfe-commits

sumit-sahrawat wrote:

@kadircet @sam-mccall kindly review

https://github.com/llvm/llvm-project/pull/90305
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] gitignore index directories (PR #90305)

2025-03-05 Thread Sumit Sahrawat via cfe-commits

https://github.com/sumit-sahrawat closed 
https://github.com/llvm/llvm-project/pull/90305
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits