2
2
3
3
from __future__ import annotations
4
4
5
+ import logging
5
6
import os
6
7
import pathlib
7
8
import shutil
22
23
from tox .tox_env .api import ToxEnv
23
24
24
25
25
- MSG_GIT_DIRTY = (
26
- "exit code 1 due to 'git status -s' reporting dirty. "
27
- "That should not happen regardless if status is passed, failed or aborted. "
28
- "Modify .gitignore file to avoid this."
26
+ logger = logging .getLogger (__name__ )
27
+ WARNING_MSG_GIT_DIRTY = (
28
+ "'git status -s' reported dirty. "
29
+ "Modify .gitignore file as this will cause an error under CI/CD pipelines."
30
+ )
31
+
32
+ ERROR_MSG_GIT_DIRTY = (
33
+ "::error title=tox-extra detected git dirty status:: " + WARNING_MSG_GIT_DIRTY
29
34
)
30
35
31
36
@@ -37,7 +42,9 @@ def is_git_dirty(path: str) -> bool:
37
42
try :
38
43
repo = git .Repo (pathlib .Path .cwd ())
39
44
if repo .is_dirty (untracked_files = True ):
40
- os .system (f"{ git_path } status -s" ) # noqa: S605
45
+ # stderr is hidden to avoid noise like occasional:
46
+ # warning: untracked cache is disabled on this system or location
47
+ os .system (f"{ git_path } status -s 2>/dev/null" ) # noqa: S605
41
48
# We want to display long diff only on non-interactive shells,
42
49
# like CI/CD pipelines because on local shell, the user can
43
50
# decide to run it himself if the status line was not enogh.
@@ -95,4 +102,6 @@ def tox_after_run_commands(
95
102
"""Hook that runs after test commands."""
96
103
allow_dirty = getattr (tox_env .options , "allow_dirty" , False )
97
104
if not allow_dirty and is_git_dirty ("." ):
98
- raise Fail (MSG_GIT_DIRTY )
105
+ if os .environ .get ("CI" ) == "true" :
106
+ raise Fail (ERROR_MSG_GIT_DIRTY )
107
+ logger .error (WARNING_MSG_GIT_DIRTY )
0 commit comments