Skip to content

Commit

Permalink
Store commands as arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed Jan 6, 2025
1 parent 3741a1d commit 1891ce0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,7 @@ def func_vcs_tag(self, node: mparser.BaseNode, args: T.List['TYPE_var'], kwargs:
vcs = mesonlib.detect_vcs(source_dir)
if vcs:
mlog.log('Found {} repository at {}'.format(vcs['name'], vcs['wc_dir']))
vcs_cmd = vcs['get_rev'].split()
vcs_cmd = vcs['get_rev']
regex_selector = vcs['rev_regex']
else:
vcs_cmd = [' '] # executing this cmd will fail in vcstagger.py and force to use the fallback string
Expand Down
26 changes: 19 additions & 7 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

if T.TYPE_CHECKING:
from typing_extensions import Literal, Protocol
from typing import TypedDict

from .._typing import ImmutableListProtocol
from ..build import ConfigurationData
Expand All @@ -46,6 +47,15 @@ class _VerPickleLoadable(Protocol):

version: str

class VcsData(TypedDict):
name: str
cmd: str
repo_dir: str
get_rev: T.List[str]
rev_regex: str
dep: str
wc_dir: str

# A generic type for pickle_load. This allows any type that has either a
# .version or a .environment to be passed.
_PL = T.TypeVar('_PL', bound=T.Union[_EnvPickleLoadable, _VerPickleLoadable])
Expand Down Expand Up @@ -756,37 +766,37 @@ def windows_detect_native_arch() -> str:
raise EnvironmentException('Unable to detect native OS architecture')
return arch

def detect_vcs(source_dir: T.Union[str, Path]) -> T.Optional[T.Dict[str, str]]:
vcs_systems = [
def detect_vcs(source_dir: T.Union[str, Path]) -> 'VcsData':
vcs_systems: 'T.List[VcsData]' = [
{
'name': 'git',
'cmd': 'git',
'repo_dir': '.git',
'get_rev': 'git describe --dirty=+ --always',
'get_rev': ['git describe', '--dirty=+', '--always'],
'rev_regex': '(.*)',
'dep': '.git/logs/HEAD'
},
{
'name': 'mercurial',
'cmd': 'hg',
'repo_dir': '.hg',
'get_rev': 'hg id -i',
'get_rev': ['hg', 'id', '-i'],
'rev_regex': '(.*)',
'dep': '.hg/dirstate'
},
{
'name': 'subversion',
'cmd': 'svn',
'repo_dir': '.svn',
'get_rev': 'svn info',
'get_rev': ['svn', 'info'],
'rev_regex': 'Revision: (.*)',
'dep': '.svn/wc.db'
},
{
'name': 'bazaar',
'cmd': 'bzr',
'repo_dir': '.bzr',
'get_rev': 'bzr revno',
'get_rev': ['bzr', 'revno'],
'rev_regex': '(.*)',
'dep': '.bzr'
},
Expand All @@ -800,7 +810,9 @@ def detect_vcs(source_dir: T.Union[str, Path]) -> T.Optional[T.Dict[str, str]]:
parent_paths_and_self.appendleft(source_dir)
for curdir in parent_paths_and_self:
for vcs in vcs_systems:
if Path.is_dir(curdir.joinpath(vcs['repo_dir'])) and shutil.which(vcs['cmd']):
repodir = vcs['repo_dir']
cmd = vcs['cmd']
if curdir.joinpath(repodir).is_dir() and shutil.which(cmd):
vcs['wc_dir'] = str(curdir)
return vcs
return None
Expand Down

0 comments on commit 1891ce0

Please sign in to comment.