5
5
6
6
from .file_finder import is_toplevel_acceptable
7
7
from .file_finder import scm_find_files
8
+ from .utils import do_ex
8
9
from .utils import trace
9
10
10
11
log = logging .getLogger (__name__ )
13
14
def _git_toplevel (path ):
14
15
try :
15
16
cwd = os .path .abspath (path or "." )
16
- with open (os .devnull , "wb" ) as devnull :
17
- out = subprocess .check_output (
18
- ["git" , "rev-parse" , "--show-prefix" ],
19
- cwd = cwd ,
20
- universal_newlines = True ,
21
- stderr = devnull ,
22
- )
17
+ out , err , ret = do_ex (["git" , "rev-parse" , "HEAD" ], cwd = cwd )
18
+ if ret != 0 :
19
+ # BAIL if there is no commit
20
+ log .error ("listing git files failed - pretending there aren't any" )
21
+ return None
22
+ out , err , ret = do_ex (
23
+ ["git" , "rev-parse" , "--show-prefix" ],
24
+ cwd = cwd ,
25
+ )
26
+ if ret != 0 :
27
+ return None
23
28
out = out .strip ()[:- 1 ] # remove the trailing pathsep
24
29
if not out :
25
30
out = cwd
@@ -28,7 +33,7 @@ def _git_toplevel(path):
28
33
# ``cwd`` is absolute path to current working directory.
29
34
# the below method removes the length of ``out`` from
30
35
# ``cwd``, which gives the git toplevel
31
- assert cwd .replace ("\\ " , "/" ).endswith (out )
36
+ assert cwd .replace ("\\ " , "/" ).endswith (out ), f"cwd= { cwd !r } \n out= { out !r } "
32
37
# In windows cwd contains ``\`` which should be replaced by ``/``
33
38
# for this assertion to work. Length of string isn't changed by replace
34
39
# ``\\`` is just and escape for `\`
@@ -59,8 +64,11 @@ def _git_interpret_archive(fd, toplevel):
59
64
def _git_ls_files_and_dirs (toplevel ):
60
65
# use git archive instead of git ls-file to honor
61
66
# export-ignore git attribute
67
+
62
68
cmd = ["git" , "archive" , "--prefix" , toplevel + os .path .sep , "HEAD" ]
63
- proc = subprocess .Popen (cmd , stdout = subprocess .PIPE , cwd = toplevel )
69
+ proc = subprocess .Popen (
70
+ cmd , stdout = subprocess .PIPE , cwd = toplevel , stderr = subprocess .DEVNULL
71
+ )
64
72
try :
65
73
try :
66
74
return _git_interpret_archive (proc .stdout , toplevel )
@@ -70,7 +78,7 @@ def _git_ls_files_and_dirs(toplevel):
70
78
proc .terminate ()
71
79
except Exception :
72
80
if proc .wait () != 0 :
73
- log .exception ("listing git files failed - pretending there aren't any" )
81
+ log .error ("listing git files failed - pretending there aren't any" )
74
82
return (), ()
75
83
76
84
0 commit comments