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 7, 2025
1 parent 5b39146 commit dfe5cbb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
6 changes: 3 additions & 3 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1967,9 +1967,9 @@ def func_vcs_tag(self, node: mparser.BaseNode, args: T.List['TYPE_var'], kwargs:
else:
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()
regex_selector = vcs['rev_regex']
mlog.log('Found {} repository at {}'.format(vcs.name, vcs.wc_dir))
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
# vcstagger.py parameters: infile, outfile, fallback, source_dir, replace_string, regex_selector, command...
Expand Down
83 changes: 48 additions & 35 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pickle
import errno
import json
import dataclasses

from mesonbuild import mlog
from .core import MesonException, HoldableObject
Expand Down Expand Up @@ -756,40 +757,50 @@ 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]]:
@dataclasses.dataclass
class VcsData:
name: str
cmd: str
repo_dir: str
get_rev: T.List[str]
rev_regex: str
dep: str
wc_dir: T.Optional[str] = None

def detect_vcs(source_dir: T.Union[str, Path]) -> T.Optional[VcsData]:
vcs_systems = [
{
'name': 'git',
'cmd': 'git',
'repo_dir': '.git',
'get_rev': 'git describe --dirty=+ --always',
'rev_regex': '(.*)',
'dep': '.git/logs/HEAD'
},
{
'name': 'mercurial',
'cmd': 'hg',
'repo_dir': '.hg',
'get_rev': 'hg id -i',
'rev_regex': '(.*)',
'dep': '.hg/dirstate'
},
{
'name': 'subversion',
'cmd': 'svn',
'repo_dir': '.svn',
'get_rev': 'svn info',
'rev_regex': 'Revision: (.*)',
'dep': '.svn/wc.db'
},
{
'name': 'bazaar',
'cmd': 'bzr',
'repo_dir': '.bzr',
'get_rev': 'bzr revno',
'rev_regex': '(.*)',
'dep': '.bzr'
},
VcsData(
name = 'git',
cmd = 'git',
repo_dir = '.git',
get_rev = ['git', 'describe', '--dirty=+', '--always'],
rev_regex = '(.*)',
dep = '.git/logs/HEAD',
),
VcsData(
name = 'mercurial',
cmd = 'hg',
repo_dir = '.hg',
get_rev = ['hg', 'id', '-i'],
rev_regex = '(.*)',
dep= '.hg/dirstate',
),
VcsData(
name = 'subversion',
cmd = 'svn',
repo_dir = '.svn',
get_rev = ['svn', 'info'],
rev_regex = 'Revision: (.*)',
dep = '.svn/wc.db',
),
VcsData(
name = 'bazaar',
cmd = 'bzr',
repo_dir = '.bzr',
get_rev = ['bzr', 'revno'],
rev_regex = '(.*)',
dep = '.bzr',
),
]
if isinstance(source_dir, str):
source_dir = Path(source_dir)
Expand All @@ -800,8 +811,10 @@ 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']):
vcs['wc_dir'] = str(curdir)
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 dfe5cbb

Please sign in to comment.