Repository: pdm-project/pdm

Issue:
Include .pdm.toml in root .gitignore when running `pdm init`
## Is your feature request related to a problem? Please describe.

When running `pdm --init` add a root level `.gitignore` and write the `.pdm.toml` path to it. Alternatively, if a `.gitignore` already exists append the path to the file.

This would be similar to how `pdm --init` created `__pypackages__/.gitignore`, but for the `.pdm.toml` file. (#446)

This would save people a bit of extra time reading the docs (#1364) to know that you don't want to commit this file.

## Describe the solution you'd like

The `pdm --init` creates (or updates) the project root's `.gitignore` to include the `.pdm.toml` path.

---

If this is accepted I would be happy to open a PR to implement this.

Discussion:
codecov-commenter: ## [Codecov](https://codecov.io/gh/pdm-project/pdm/pull/1785?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) Report
Patch coverage: **`77.77`**% and no project coverage change.
> Comparison is base [(`9461649`)](https://codecov.io/gh/pdm-project/pdm/commit/946164916d506ba33cf8eabbad1c21282147c737?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) 85.18% compared to head [(`e6b4f8b`)](https://codecov.io/gh/pdm-project/pdm/pull/1785?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) 85.19%.

:mega: This organization is not using Codecov’s [GitHub App Integration](https://github.com/apps/codecov). We recommend you install it so Codecov can continue to function properly for your repositories. [Learn more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project)

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #1785   +/-   ##
=======================================
  Coverage   85.18%   85.19%           
=======================================
  Files          95       95           
  Lines        8895     8913   +18     
  Branches     1939     1941    +2     
=======================================
+ Hits         7577     7593   +16     
- Misses        891      892    +1     
- Partials      427      428    +1     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `84.99% <77.77%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/pdm-project/pdm/pull/1785?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Coverage Δ | |
|---|---|---|
| [src/pdm/cli/actions.py](https://codecov.io/gh/pdm-project/pdm/pull/1785?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#diff-c3JjL3BkbS9jbGkvYWN0aW9ucy5weQ==) | `77.82% <77.77%> (-0.01%)` | :arrow_down: |

... and [1 file with indirect coverage changes](https://codecov.io/gh/pdm-project/pdm/pull/1785/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project)

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project)

</details>

[:umbrella: View full report in Codecov by Sentry](https://codecov.io/gh/pdm-project/pdm/pull/1785?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).


Patch:
diff --git a/news/1749.feature.md b/news/1749.feature.md
new file mode 100644
index 00000000..8b67ae31
--- /dev/null
+++ b/news/1749.feature.md
@@ -0,0 +1,1 @@
+Include `.pdm-python` in project root `.gitignore` when running `pdm init`.
diff --git a/src/pdm/cli/actions.py b/src/pdm/cli/actions.py
index 9e42c30b..0c629798 100644
--- a/src/pdm/cli/actions.py
+++ b/src/pdm/cli/actions.py
@@ -12,6 +12,7 @@ import warnings
 from argparse import Namespace
 from collections import defaultdict
 from itertools import chain
+from pathlib import Path
 from typing import Collection, Iterable, Mapping, cast
 
 import tomlkit
@@ -535,9 +536,31 @@ def do_init(
     get_specifier(python_requires)
     project.pyproject._data.update(data)
     project.pyproject.write()
+    _write_gitignore(project.root.joinpath(".gitignore"))
     hooks.try_emit("post_init")
 
 
+def _write_gitignore(path: Path) -> None:
+    import requests
+
+    url = "https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore"
+    if not path.exists():
+        try:
+            resp = requests.get(url)
+            resp.raise_for_status()
+        except requests.exceptions.RequestException:
+            content = "\n".join(["build/", "dist/", "*.egg-info/", "__pycache__/", "*.py[cod]"]) + "\n"
+        else:
+            content = resp.text
+        content += ".pdm-python\n"
+    else:
+        content = path.read_text(encoding="utf-8")
+        if ".pdm-python" in content:
+            return
+        content += ".pdm-python\n"
+    path.write_text(content, encoding="utf-8")
+
+
 def do_use(
     project: Project,
     python: str = "",


Test patch:
diff --git a/tests/cli/test_init.py b/tests/cli/test_init.py
index b864ae40..360a9be5 100644
--- a/tests/cli/test_init.py
+++ b/tests/cli/test_init.py
@@ -103,6 +103,7 @@ def test_init_auto_create_venv(project_no_init, invoke, mocker):
     result = invoke(["init"], input="\n\n\n\n\n\n\n", obj=project_no_init)
     assert result.exit_code == 0
     assert project_no_init.python.executable.parent.parent == project_no_init.root / ".venv"
+    assert ".pdm-python" in (project_no_init.root / ".gitignore").read_text()
 
 
 def test_init_auto_create_venv_specify_python(project_no_init, invoke, mocker):