Repository: mesonbuild/meson

Issue:
Automatically generated .gitignore / cvsignore / ? in the build directory
As the build directory is a newly created directory, it could safely contain a `.gitignore` that ignores itself.

I don't know how other vcs work, but meson may/should try to detect which vcs is used (by checking existence of .git ?)

```
# Meson generated .gitignore
*
```


Discussion:
Or, more simply, just write blocker files for all possible vcs:s.

This may be problematic if there are people who want to put their build directories in revision control. I don't know why anyone would, but every single time I have said that someone has come up and claimed that they need it.
Yes, that would probably be cheaper (less code, better performance) than checking which vcs is used.
> This may be problematic if there are people who want to put their build directories in revision control. I don't know why anyone would, but every single time I have said that someone has come up and claimed that they need it.

That's weird.

It can be a meson project configuration : `-Dvcs_ignore=false`

Yes, but one of the main design points of Meson is, and has always been, to minimize the number of toggle options. This issue is not serious enough to warrant an option.
This seems better suited for the `meson init` command. Not the automatic part but a new option for adding `.gitignore`.
@michaelbadcrumble meson init does not create a build directory, right ?
Will if `--build` is passed then yes it does.

On Wed, May 27, 2020 at 12:50 PM Salamandar <notifications@github.com>
wrote:

> @michaelbadcrumble <https://github.com/michaelbadcrumble> meson init does
> not create a build directory, right ?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/mesonbuild/meson/issues/6509#issuecomment-634903551>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ANGEVUHLBQQAQ2UTQEEEMSTRTVVHRANCNFSM4KLM6L7Q>
> .
>

> I don't know why anyone would, but every single time I have said that someone has come up and claimed that they need it.

They should use `git add --force`, which might also remind them of why it's a terrible idea to commit it.

Given the files in question embed absolute paths to the build directory, it's unacceptably wrong to do so either way, and we should not feel afraid to tell them that.
Looks like those people are trying to commit useful generated files. As Meson does not allow to write in the source directory, I can understand the motivation.
For example, Gimp needs to re-generate huge indexes, and generated source files that need to be committed for easier debug. That's really hard to set up with Meson. 
Maybe we could do something like this:

- generate the gitignore file on init
- add a comment to the file that says something like "this file has been autogenerated by Meson, if you don't want it you can delete it and it won't be regenerated"

This should do the right thing 99% of the time and the last 1% can do their own thing without needing to add options or switches to Meson.

Resolving patch:
diff --git a/mesonbuild/msetup.py b/mesonbuild/msetup.py
--- a/mesonbuild/msetup.py
+++ b/mesonbuild/msetup.py
@@ -31,6 +31,16 @@
 from .mconf import make_lower_case
 from .mesonlib import MesonException
 
+git_ignore_file = '''# This file is autogenerated by Meson. If you change or delete it, it won't be recreated.
+*
+'''
+
+hg_ignore_file = '''# This file is autogenerated by Meson. If you change or delete it, it won't be recreated.
+syntax: glob
+**/*
+'''
+
+
 def add_arguments(parser: argparse.ArgumentParser) -> None:
     coredata.register_builtin_arguments(parser)
     parser.add_argument('--native-file',
@@ -131,8 +141,17 @@ def validate_core_dirs(self, dir1: str, dir2: str) -> T.Tuple[str, str]:
             return ndir2, ndir1
         raise MesonException('Neither directory contains a build file {}.'.format(environment.build_filename))
 
+    def add_vcs_ignore_files(self, build_dir: str) -> None:
+        if os.listdir(build_dir):
+            return
+        with open(os.path.join(build_dir, '.gitignore'), 'w') as ofile:
+            ofile.write(git_ignore_file)
+        with open(os.path.join(build_dir, '.hgignore'), 'w') as ofile:
+            ofile.write(hg_ignore_file)
+
     def validate_dirs(self, dir1: str, dir2: str, reconfigure: bool, wipe: bool) -> T.Tuple[str, str]:
         (src_dir, build_dir) = self.validate_core_dirs(dir1, dir2)
+        self.add_vcs_ignore_files(build_dir)
         priv_dir = os.path.join(build_dir, 'meson-private/coredata.dat')
         if os.path.exists(priv_dir):
             if not reconfigure and not wipe:


Test patch: