From 49fe24ef1af838494da646b5ee9ab056fed78e0e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 10 May 2021 09:48:41 +0200 Subject: Generate .gitignore in .bdep/ to prevent accidentally committing it --- bdep/init.cxx | 36 ++++++++++++++++++++++++++++++------ bdep/init.hxx | 6 ++++++ bdep/new.cxx | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/bdep/init.cxx b/bdep/init.cxx index dbb85cb..a3f0859 100644 --- a/bdep/init.cxx +++ b/bdep/init.cxx @@ -15,6 +15,35 @@ using namespace std; namespace bdep { + void + mk_bdep_dir (const dir_path& d) + { + dir_path sd (d / bdep_dir); + + if (!exists (sd)) + { + mk (sd); + + // Create the .gitignore file that ignores everything under .bdep/ + // effectively making git ignore it (this prevents people from + // accidentally adding this directory to a git repository). + // + path f (sd / ".gitignore"); + try + { + ofdstream os (f); + os << "# This directory should not be version-controlled." << '\n' + << "#" << '\n' + << "*" << '\n'; + os.close (); + } + catch (const io_error& e) + { + fail << "unable to write to " << f << ": " << e; + } + } + } + shared_ptr cmd_init_config (const configuration_name_options& o, const configuration_add_options& ao, @@ -192,12 +221,7 @@ namespace bdep // Create .bdep/. // - { - dir_path d (prj / bdep_dir); - - if (!exists (d)) - mk (prj / bdep_dir); - } + mk_bdep_dir (prj); // Open the database creating it if necessary. // diff --git a/bdep/init.hxx b/bdep/init.hxx index 475c87e..6a2afd2 100644 --- a/bdep/init.hxx +++ b/bdep/init.hxx @@ -49,6 +49,12 @@ namespace bdep cmd_init_options merge_options (const default_options&, const cmd_init_options&); + + // Create the .bdep/ subdirectory in the specified directory if it does not + // already exist. + // + void + mk_bdep_dir (const dir_path&); } #endif // BDEP_INIT_HXX diff --git a/bdep/new.cxx b/bdep/new.cxx index 51a5308..aac8221 100644 --- a/bdep/new.cxx +++ b/bdep/new.cxx @@ -3123,7 +3123,7 @@ cmd_new (cmd_new_options&& o, cli::group_scanner& args) // Create .bdep/. // - mk (prj / bdep_dir); + mk_bdep_dir (prj); // Initialize tmp directory. // -- cgit v1.1