1
1
"""Utilities for verifying git integrity."""
2
2
3
+ from __future__ import print_function
4
+
3
5
# Used also from setup.py, so don't pull in anything additional here (like mypy or typing):
4
6
import os
5
7
import pipes
6
8
import subprocess
7
9
import sys
8
10
9
11
10
- def is_git_repo (dir : str ) -> bool :
12
+ # NOTE: Cannot use typing.TYPE_CHECKING guard here because cannot import
13
+ # "typing" in this file at all.
14
+ if False :
15
+ from typing import Iterator
16
+
17
+
18
+ def is_git_repo (dir ):
19
+ # type: (str) -> bool
11
20
"""Is the given directory version-controlled with git?"""
12
21
return os .path .exists (os .path .join (dir , ".git" ))
13
22
14
23
15
- def have_git () -> bool :
24
+ def have_git ():
25
+ # type: () -> bool
16
26
"""Can we run the git executable?"""
17
27
try :
18
28
subprocess .check_output (["git" , "--help" ])
@@ -23,7 +33,8 @@ def have_git() -> bool:
23
33
return False
24
34
25
35
26
- def get_submodules (dir : str ):
36
+ def get_submodules (dir ):
37
+ # type: (str) -> Iterator[str]
27
38
"""Return a list of all git top-level submodules in a given directory."""
28
39
# It would be nicer to do
29
40
# "git submodule foreach 'echo MODULE $name $path $sha1 $toplevel'"
@@ -41,67 +52,77 @@ def get_submodules(dir: str):
41
52
yield name .decode (sys .getfilesystemencoding ())
42
53
43
54
44
- def git_revision (dir : str ) -> bytes :
55
+ def git_revision (dir ):
56
+ # type: (str) -> bytes
45
57
"""Get the SHA-1 of the HEAD of a git repository."""
46
58
return subprocess .check_output (["git" , "rev-parse" , "HEAD" ], cwd = dir ).strip ()
47
59
48
60
49
- def submodule_revision (dir : str , submodule : str ) -> bytes :
61
+ def submodule_revision (dir , submodule ):
62
+ # type: (str, str) -> bytes
50
63
"""Get the SHA-1 a submodule is supposed to have."""
51
64
output = subprocess .check_output (["git" , "ls-files" , "-s" , submodule ], cwd = dir ).strip ()
52
65
# E.g.: "160000 e4a7edb949e0b920b16f61aeeb19fc3d328f3012 0 typeshed"
53
66
return output .split ()[1 ]
54
67
55
68
56
- def is_dirty (dir : str ) -> bool :
69
+ def is_dirty (dir ):
70
+ # type: (str) -> bool
57
71
"""Check whether a git repository has uncommitted changes."""
58
72
output = subprocess .check_output (["git" , "status" , "-uno" , "--porcelain" ], cwd = dir )
59
73
return output .strip () != b""
60
74
61
75
62
- def has_extra_files (dir : str ) -> bool :
76
+ def has_extra_files (dir ):
77
+ # type: (str) -> bool
63
78
"""Check whether a git repository has untracked files."""
64
79
output = subprocess .check_output (["git" , "clean" , "--dry-run" , "-d" ], cwd = dir )
65
80
return output .strip () != b""
66
81
67
82
68
- def warn_no_git_executable () -> None :
83
+ def warn_no_git_executable ():
84
+ # type: () -> None
69
85
print ("Warning: Couldn't check git integrity. "
70
86
"git executable not in path." , file = sys .stderr )
71
87
72
88
73
- def warn_dirty (dir : str ) -> None :
89
+ def warn_dirty (dir ):
90
+ # type: (str) -> None
74
91
print ("Warning: git module '{}' has uncommitted changes." .format (dir ),
75
92
file = sys .stderr )
76
93
print ("Go to the directory" , file = sys .stderr )
77
94
print (" {}" .format (dir ), file = sys .stderr )
78
95
print ("and commit or reset your changes" , file = sys .stderr )
79
96
80
97
81
- def warn_extra_files (dir : str ) -> None :
98
+ def warn_extra_files (dir ):
99
+ # type: (str) -> None
82
100
print ("Warning: git module '{}' has untracked files." .format (dir ),
83
101
file = sys .stderr )
84
102
print ("Go to the directory" , file = sys .stderr )
85
103
print (" {}" .format (dir ), file = sys .stderr )
86
104
print ("and add & commit your new files." , file = sys .stderr )
87
105
88
106
89
- def chdir_prefix (dir : str ) -> str :
107
+ def chdir_prefix (dir ):
108
+ # type: (str) -> str
90
109
"""Return the command to change to the target directory, plus '&&'."""
91
110
if os .path .relpath (dir ) != "." :
92
111
return "cd " + pipes .quote (dir ) + " && "
93
112
else :
94
113
return ""
95
114
96
115
97
- def error_submodule_not_initialized (name : str , dir : str ) -> None :
116
+ def error_submodule_not_initialized (name , dir ):
117
+ # type: (str, str) -> None
98
118
print ("Submodule '{}' not initialized." .format (name ), file = sys .stderr )
99
119
print ("Please run:" , file = sys .stderr )
100
120
print (" {}git submodule update --init {}" .format (
101
121
chdir_prefix (dir ), name ), file = sys .stderr )
102
122
103
123
104
- def error_submodule_not_updated (name : str , dir : str ) -> None :
124
+ def error_submodule_not_updated (name , dir ):
125
+ # type: (str, str) -> None
105
126
print ("Submodule '{}' not updated." .format (name ), file = sys .stderr )
106
127
print ("Please run:" , file = sys .stderr )
107
128
print (" {}git submodule update {}" .format (
@@ -110,7 +131,8 @@ def error_submodule_not_updated(name: str, dir: str) -> None:
110
131
print (" then run \" git add {}\" to silence this check)" .format (name ), file = sys .stderr )
111
132
112
133
113
- def verify_git_integrity_or_abort (datadir : str ) -> None :
134
+ def verify_git_integrity_or_abort (datadir ):
135
+ # type: (str) -> None
114
136
"""Verify the (submodule) integrity of a git repository.
115
137
116
138
Potentially output warnings/errors (to stderr), and exit with status 1
0 commit comments