11from __future__ import annotations
22
33import os
4+ from contextlib import suppress
45from datetime import date
56from datetime import datetime
67
@@ -21,7 +22,7 @@ class GitWorkdirHgClient(GitWorkdir, HgWorkdir):
2122 @classmethod
2223 def from_potential_worktree (cls , wd : _t .PathT ) -> GitWorkdirHgClient | None :
2324 require_command (cls .COMMAND )
24- root , _ , ret = do_ex ("hg root" , wd )
25+ root , _ , ret = do_ex ([ "hg" , " root"] , wd )
2526 if ret :
2627 return None
2728 return cls (root )
@@ -58,13 +59,13 @@ def get_hg_node(self) -> str | None:
5859 return None
5960
6061 def _hg2git (self , hg_node : str ) -> str | None :
61- git_node = None
62- with open (os .path .join (self .path , ".hg/git-mapfile" )) as file :
63- for line in file :
64- if hg_node in line :
65- git_node , hg_node = line .split ()
66- break
67- return git_node
62+ with suppress ( FileNotFoundError ):
63+ with open (os .path .join (self .path , ".hg/git-mapfile" )) as map_items :
64+ for item in map_items :
65+ if hg_node in item :
66+ git_node , hg_node = item .split ()
67+ return git_node
68+ return None
6869
6970 def node (self ) -> str | None :
7071 hg_node = self .get_hg_node ()
@@ -90,7 +91,7 @@ def node(self) -> str | None:
9091 return git_node [:7 ]
9192
9293 def count_all_nodes (self ) -> int :
93- revs , _ , _ = self .do_ex ("hg log -r ' ancestors(.)' -T '.'" )
94+ revs , _ , _ = self .do_ex ([ "hg" , " log" , "-r" , " ancestors(.)" , "-T" , "." ] )
9495 return len (revs )
9596
9697 def default_describe (self ) -> _t .CmdResult :
@@ -105,30 +106,28 @@ def default_describe(self) -> _t.CmdResult:
105106 "hg" ,
106107 "log" ,
107108 "-r" ,
108- "(reverse(ancestors(.)) and tag(r're:[0-9]'))" ,
109+ "(reverse(ancestors(.)) and tag(r're:v? [0-9].* '))" ,
109110 "-T" ,
110111 "{tags}{if(tags, ' ', '')}" ,
111112 ]
112113 )
113114 if ret :
114115 return _FAKE_GIT_DESCRIBE_ERROR
115- hg_tags : set [str ] = set ( hg_tags_str .split () )
116+ hg_tags : list [str ] = hg_tags_str .split ()
116117
117118 if not hg_tags :
118119 return _FAKE_GIT_DESCRIBE_ERROR
119120
120- node : str | None = None
121-
122121 with open (os .path .join (self .path , ".hg/git-tags" )) as fp :
122+ git_tags : dict [str , str ] = dict (line .split ()[::- 1 ] for line in fp )
123123
124- git_tags : dict [str , str ] = dict (line .split () for line in fp )
125-
126- tag : str | None = next (
127- # find the first hg tag which is also a git tag
128- (tag for tag in hg_tags if tag in git_tags ),
129- None ,
130- )
131- if tag is None :
124+ tag : str
125+ for hg_tag in hg_tags :
126+ if hg_tag in git_tags :
127+ tag = hg_tag
128+ break
129+ else :
130+ trace ("tag not found" , hg_tags , git_tags )
132131 return _FAKE_GIT_DESCRIBE_ERROR
133132
134133 out , _ , ret = self .do_ex (["hg" , "log" , "-r" , f"'{ tag } '::." , "-T" , "." ])
@@ -142,5 +141,5 @@ def default_describe(self) -> _t.CmdResult:
142141
143142 if self .is_dirty ():
144143 desc += "-dirty"
145-
144+ trace ( "desc" , desc )
146145 return _t .CmdResult (desc , "" , 0 )
0 commit comments