1
1
from __future__ import annotations
2
2
3
3
import os
4
+ from contextlib import suppress
4
5
from datetime import date
5
6
from datetime import datetime
6
7
@@ -21,7 +22,7 @@ class GitWorkdirHgClient(GitWorkdir, HgWorkdir):
21
22
@classmethod
22
23
def from_potential_worktree (cls , wd : _t .PathT ) -> GitWorkdirHgClient | None :
23
24
require_command (cls .COMMAND )
24
- root , _ , ret = do_ex ("hg root" , wd )
25
+ root , _ , ret = do_ex ([ "hg" , " root"] , wd )
25
26
if ret :
26
27
return None
27
28
return cls (root )
@@ -58,13 +59,13 @@ def get_hg_node(self) -> str | None:
58
59
return None
59
60
60
61
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
68
69
69
70
def node (self ) -> str | None :
70
71
hg_node = self .get_hg_node ()
@@ -90,7 +91,7 @@ def node(self) -> str | None:
90
91
return git_node [:7 ]
91
92
92
93
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" , "." ] )
94
95
return len (revs )
95
96
96
97
def default_describe (self ) -> _t .CmdResult :
@@ -105,30 +106,28 @@ def default_describe(self) -> _t.CmdResult:
105
106
"hg" ,
106
107
"log" ,
107
108
"-r" ,
108
- "(reverse(ancestors(.)) and tag(r're:[0-9]'))" ,
109
+ "(reverse(ancestors(.)) and tag(r're:v? [0-9].* '))" ,
109
110
"-T" ,
110
111
"{tags}{if(tags, ' ', '')}" ,
111
112
]
112
113
)
113
114
if ret :
114
115
return _FAKE_GIT_DESCRIBE_ERROR
115
- hg_tags : set [str ] = set ( hg_tags_str .split () )
116
+ hg_tags : list [str ] = hg_tags_str .split ()
116
117
117
118
if not hg_tags :
118
119
return _FAKE_GIT_DESCRIBE_ERROR
119
120
120
- node : str | None = None
121
-
122
121
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 )
123
123
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 )
132
131
return _FAKE_GIT_DESCRIBE_ERROR
133
132
134
133
out , _ , ret = self .do_ex (["hg" , "log" , "-r" , f"'{ tag } '::." , "-T" , "." ])
@@ -142,5 +141,5 @@ def default_describe(self) -> _t.CmdResult:
142
141
143
142
if self .is_dirty ():
144
143
desc += "-dirty"
145
-
144
+ trace ( "desc" , desc )
146
145
return _t .CmdResult (desc , "" , 0 )
0 commit comments