Skip to content

Commit f15f1b3

Browse files
authored
mercurial: add a config knob to apply patches to the tip revision (#105)
1 parent 66f44ca commit f15f1b3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

libmozevent/mercurial.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def __init__(self, config, cache_root):
5858
self.try_syntax = config.get("try_syntax")
5959
self.try_name = config.get("try_name", "try")
6060
self.default_revision = config.get("default_revision", "tip")
61+
62+
# Apply patches to the latest revision when `True`.
63+
self.use_latest_revision = config.get("use_latest_revision", False)
64+
6165
if self.try_mode == TryMode.syntax:
6266
assert self.try_syntax, "Missing try syntax"
6367
self._repo = None
@@ -124,6 +128,15 @@ def has_revision(self, revision):
124128
except hglib.error.CommandError:
125129
return False
126130

131+
def get_base_identifier(self, needed_stack: list[PhabricatorPatch]) -> str:
132+
"""Return the base identifier to apply patches against."""
133+
if self.use_latest_revision:
134+
# Use `tip` when `use_latest_revision` is `True`.
135+
return "tip"
136+
137+
# Otherwise use the base/parent revision of first revision in the stack.
138+
return needed_stack[0].base_revision
139+
127140
def apply_build(self, build):
128141
"""
129142
Apply a stack of patches to mercurial repo
@@ -147,8 +160,9 @@ def apply_build(self, build):
147160
logger.info("All the patches are already applied")
148161
return
149162

163+
hg_base = self.get_base_identifier(needed_stack)
164+
150165
# When base revision is missing, update to default revision
151-
hg_base = needed_stack[0].base_revision
152166
build.base_revision = hg_base
153167
build.missing_base_revision = not self.has_revision(hg_base)
154168
if build.missing_base_revision:

tests/test_mercurial.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import responses
1010
from conftest import MockBuild
1111

12+
from libmozdata.phabricator import PhabricatorPatch
1213
from libmozevent.bus import MessageBus
1314
from libmozevent.mercurial import MercurialWorker, Repository
1415

@@ -898,3 +899,21 @@ async def sleep(self, value):
898899
("GET", "http://test.status/try"),
899900
]
900901
assert sleep_history == [42, 42, 2]
902+
903+
904+
def test_get_base_identifier(mock_mc):
905+
stack = [
906+
PhabricatorPatch(1, "PHID-abc", "", "abc", None),
907+
PhabricatorPatch(2, "PHID-def", "", "def", None),
908+
PhabricatorPatch(3, "PHID-ghi", "", "ghi", None),
909+
]
910+
911+
assert (
912+
mock_mc.get_base_identifier(stack) == "abc"
913+
), "The base commit of the stack should be returned."
914+
915+
mock_mc.use_latest_revision = True
916+
917+
assert (
918+
mock_mc.get_base_identifier(stack) == "tip"
919+
), "`tip` commit should be used when `use_latest_revision` is `True`."

0 commit comments

Comments
 (0)