@@ -32,15 +32,16 @@ class MercurialRepository(Repository):
3232 def __init__ (self , name , logger , path , project , command , env , hooks , timeout ):
3333 super ().__init__ (name , logger , path , project , command , env , hooks , timeout )
3434
35- self .command = self ._repository_command (command , default = lambda : which ('hg' ))
35+ self .command = self ._repository_command (command , default = lambda : which ("hg" ))
3636
3737 if not self .command :
3838 raise RepositoryException ("Cannot get hg command" )
3939
4040 def get_branch (self ):
4141 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+ )
4445 cmd .execute ()
4546 self .logger .info ("output of {}:" .format (cmd ))
4647 self .logger .info (cmd .getoutputstr ())
@@ -49,12 +50,10 @@ def get_branch(self):
4950 return None
5051 else :
5152 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 ))
5454 return None
5555 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 ))
5857 return None
5958 return cmd .getoutput ()[0 ].strip ()
6059
@@ -68,8 +67,9 @@ def reposync(self):
6867 if branch != "default" :
6968 hg_command .append ("-b" )
7069 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+ )
7373 cmd .execute ()
7474 self .logger .info ("output of {}:" .format (cmd ))
7575 self .logger .info (cmd .getoutputstr ())
@@ -90,10 +90,11 @@ def reposync(self):
9090 # biggest index as this is likely the correct one.
9191 #
9292 hg_command .append ("-r" )
93- hg_command .append (" max(head() and branch(\" . \ " ))" )
93+ hg_command .append (' max(head() and branch(". "))' )
9494
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+ )
9798 cmd .execute ()
9899 self .logger .info ("output of {}:" .format (cmd ))
99100 self .logger .info (cmd .getoutputstr ())
@@ -107,25 +108,83 @@ def incoming_check(self):
107108 branch = self .get_branch ()
108109 if not branch :
109110 # 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+ )
112114
113- hg_command = [self .command , ' incoming' ]
115+ hg_command = [self .command , " incoming" ]
114116 if branch != "default" :
115117 hg_command .append ("-b" )
116118 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+ )
119122 cmd .execute ()
120123 self .logger .info ("output of {}:" .format (cmd ))
121124 self .logger .info (cmd .getoutputstr ())
122125 retcode = cmd .getretcode ()
123126 if cmd .getstate () != Command .FINISHED or retcode not in [0 , 1 ]:
124127 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+ )
127131
128132 if retcode == 0 :
129133 return True
130134 else :
131135 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