@@ -32,15 +32,16 @@ class MercurialRepository(Repository):
32
32
def __init__ (self , name , logger , path , project , command , env , hooks , timeout ):
33
33
super ().__init__ (name , logger , path , project , command , env , hooks , timeout )
34
34
35
- self .command = self ._repository_command (command , default = lambda : which ('hg' ))
35
+ self .command = self ._repository_command (command , default = lambda : which ("hg" ))
36
36
37
37
if not self .command :
38
38
raise RepositoryException ("Cannot get hg command" )
39
39
40
40
def get_branch (self ):
41
41
hg_command = [self .command , "branch" ]
42
- cmd = self .get_command (hg_command , work_dir = self .path ,
43
- env_vars = self .env , logger = self .logger )
42
+ cmd = self .get_command (
43
+ hg_command , work_dir = self .path , env_vars = self .env , logger = self .logger
44
+ )
44
45
cmd .execute ()
45
46
self .logger .info ("output of {}:" .format (cmd ))
46
47
self .logger .info (cmd .getoutputstr ())
@@ -49,12 +50,10 @@ def get_branch(self):
49
50
return None
50
51
else :
51
52
if not cmd .getoutput ():
52
- self .logger .error ("no output from {}" .
53
- format (hg_command ))
53
+ self .logger .error ("no output from {}" .format (hg_command ))
54
54
return None
55
55
if len (cmd .getoutput ()) == 0 :
56
- self .logger .error ("empty output from {}" .
57
- format (hg_command ))
56
+ self .logger .error ("empty output from {}" .format (hg_command ))
58
57
return None
59
58
return cmd .getoutput ()[0 ].strip ()
60
59
@@ -68,8 +67,9 @@ def reposync(self):
68
67
if branch != "default" :
69
68
hg_command .append ("-b" )
70
69
hg_command .append (branch )
71
- cmd = self .get_command (hg_command , work_dir = self .path ,
72
- env_vars = self .env , logger = self .logger )
70
+ cmd = self .get_command (
71
+ hg_command , work_dir = self .path , env_vars = self .env , logger = self .logger
72
+ )
73
73
cmd .execute ()
74
74
self .logger .info ("output of {}:" .format (cmd ))
75
75
self .logger .info (cmd .getoutputstr ())
@@ -90,10 +90,11 @@ def reposync(self):
90
90
# biggest index as this is likely the correct one.
91
91
#
92
92
hg_command .append ("-r" )
93
- hg_command .append (" max(head() and branch(\" . \ " ))" )
93
+ hg_command .append (' max(head() and branch(". "))' )
94
94
95
- cmd = self .get_command (hg_command , work_dir = self .path ,
96
- env_vars = self .env , logger = self .logger )
95
+ cmd = self .get_command (
96
+ hg_command , work_dir = self .path , env_vars = self .env , logger = self .logger
97
+ )
97
98
cmd .execute ()
98
99
self .logger .info ("output of {}:" .format (cmd ))
99
100
self .logger .info (cmd .getoutputstr ())
@@ -107,25 +108,83 @@ def incoming_check(self):
107
108
branch = self .get_branch ()
108
109
if not branch :
109
110
# Error logged already in get_branch().
110
- raise RepositoryException ('cannot get branch for repository {}' .
111
- format (self ))
111
+ raise RepositoryException (
112
+ "cannot get branch for repository {}" .format (self )
113
+ )
112
114
113
- hg_command = [self .command , ' incoming' ]
115
+ hg_command = [self .command , " incoming" ]
114
116
if branch != "default" :
115
117
hg_command .append ("-b" )
116
118
hg_command .append (branch )
117
- cmd = self .get_command (hg_command , work_dir = self .path ,
118
- env_vars = self .env , logger = self .logger )
119
+ cmd = self .get_command (
120
+ hg_command , work_dir = self .path , env_vars = self .env , logger = self .logger
121
+ )
119
122
cmd .execute ()
120
123
self .logger .info ("output of {}:" .format (cmd ))
121
124
self .logger .info (cmd .getoutputstr ())
122
125
retcode = cmd .getretcode ()
123
126
if cmd .getstate () != Command .FINISHED or retcode not in [0 , 1 ]:
124
127
cmd .log_error ("failed to perform incoming" )
125
- raise RepositoryException ('failed to perform incoming command '
126
- 'for repository {}' .format (self ))
128
+ raise RepositoryException (
129
+ "failed to perform incoming command " "for repository {}" .format (self )
130
+ )
127
131
128
132
if retcode == 0 :
129
133
return True
130
134
else :
131
135
return False
136
+
137
+ def strip_outgoing (self ):
138
+ """
139
+ Check for outgoing changes and if found, strip them.
140
+ :return: True if there were any changes stripped, False otherwise.
141
+ """
142
+ #
143
+ # Avoid _run_command() as it complains to the log about failed command
144
+ # when 'hg out' returns 1 which is legitimate return value.
145
+ #
146
+ cmd = self .get_command (
147
+ [self .command , "out" , "-q" , "-b" , "." , "--template={rev}\\ n" ],
148
+ work_dir = self .path ,
149
+ env_vars = self .env ,
150
+ logger = self .logger ,
151
+ )
152
+ cmd .execute ()
153
+ status = cmd .getretcode ()
154
+
155
+ #
156
+ # If there are outgoing changes, 'hg out' returns 0, otherwise returns 1.
157
+ # If the 'hg out' command fails for some reason, it will return 255.
158
+ # Hence, check for positive value as bail out indication.
159
+ #
160
+ if status > 0 :
161
+ return False
162
+
163
+ revisions = list (filter (None , cmd .getoutputstr ().split ("\n " )))
164
+ if len (revisions ) == 0 :
165
+ return False
166
+
167
+ #
168
+ # The revision specification will produce all outgoing changesets.
169
+ # The 'hg strip' command will remove them all. Also, the 'strip'
170
+ # has become part of core Mercurial, however use the --config to
171
+ # enable the extension for backward compatibility.
172
+ #
173
+ self .logger .debug (
174
+ f"Removing outgoing changesets in repository { self } : { revisions } "
175
+ )
176
+ status , out = self ._run_command (
177
+ [
178
+ self .command ,
179
+ "--config" ,
180
+ "extensions.strip=" ,
181
+ "strip" ,
182
+ 'outgoing() and branch(".")' ,
183
+ ]
184
+ )
185
+ if status != 0 :
186
+ raise RepositoryException (
187
+ f"failed to strip outgoing changesets from { self } "
188
+ )
189
+
190
+ return True
0 commit comments