mirror of
https://github.com/esphome/esphome.git
synced 2025-04-14 23:00:29 +01:00
Allow cloning/fetching Github PR branches in external_components (#2639)
This commit is contained in:
parent
0d3e6b2c4c
commit
73accf747f
@ -43,19 +43,27 @@ def validate_source_shorthand(value):
|
|||||||
# Regex for GitHub repo name with optional branch/tag
|
# Regex for GitHub repo name with optional branch/tag
|
||||||
# Note: git allows other branch/tag names as well, but never seen them used before
|
# Note: git allows other branch/tag names as well, but never seen them used before
|
||||||
m = re.match(
|
m = re.match(
|
||||||
r"github://([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-\._]+)(?:@([a-zA-Z0-9\-_.\./]+))?",
|
r"github://(?:([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-\._]+)(?:@([a-zA-Z0-9\-_.\./]+))?|pr#([0-9]+))",
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
if m is None:
|
if m is None:
|
||||||
raise cv.Invalid(
|
raise cv.Invalid(
|
||||||
"Source is not a file system path or in expected github://username/name[@branch-or-tag] format!"
|
"Source is not a file system path, in expected github://username/name[@branch-or-tag] or github://pr#1234 format!"
|
||||||
)
|
)
|
||||||
conf = {
|
if m.group(4):
|
||||||
CONF_TYPE: TYPE_GIT,
|
conf = {
|
||||||
CONF_URL: f"https://github.com/{m.group(1)}/{m.group(2)}.git",
|
CONF_TYPE: TYPE_GIT,
|
||||||
}
|
CONF_URL: "https://github.com/esphome/esphome.git",
|
||||||
if m.group(3):
|
CONF_REF: f"pull/{m.group(4)}/head",
|
||||||
conf[CONF_REF] = m.group(3)
|
}
|
||||||
|
else:
|
||||||
|
conf = {
|
||||||
|
CONF_TYPE: TYPE_GIT,
|
||||||
|
CONF_URL: f"https://github.com/{m.group(1)}/{m.group(2)}.git",
|
||||||
|
}
|
||||||
|
if m.group(3):
|
||||||
|
conf[CONF_REF] = m.group(3)
|
||||||
|
|
||||||
return SOURCE_SCHEMA(conf)
|
return SOURCE_SCHEMA(conf)
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,15 +40,23 @@ def clone_or_update(
|
|||||||
) -> Path:
|
) -> Path:
|
||||||
key = f"{url}@{ref}"
|
key = f"{url}@{ref}"
|
||||||
repo_dir = _compute_destination_path(key, domain)
|
repo_dir = _compute_destination_path(key, domain)
|
||||||
|
fetch_pr_branch = ref.startswith("pull/")
|
||||||
if not repo_dir.is_dir():
|
if not repo_dir.is_dir():
|
||||||
_LOGGER.info("Cloning %s", key)
|
_LOGGER.info("Cloning %s", key)
|
||||||
_LOGGER.debug("Location: %s", repo_dir)
|
_LOGGER.debug("Location: %s", repo_dir)
|
||||||
cmd = ["git", "clone", "--depth=1"]
|
cmd = ["git", "clone", "--depth=1"]
|
||||||
if ref is not None:
|
if ref is not None and not fetch_pr_branch:
|
||||||
cmd += ["--branch", ref]
|
cmd += ["--branch", ref]
|
||||||
cmd += ["--", url, str(repo_dir)]
|
cmd += ["--", url, str(repo_dir)]
|
||||||
run_git_command(cmd)
|
run_git_command(cmd)
|
||||||
|
|
||||||
|
if fetch_pr_branch:
|
||||||
|
# We need to fetch the PR branch first, otherwise git will complain
|
||||||
|
# about missing objects
|
||||||
|
_LOGGER.info("Fetching %s", ref)
|
||||||
|
run_git_command(["git", "fetch", "--", "origin", ref], str(repo_dir))
|
||||||
|
run_git_command(["git", "reset", "--hard", "FETCH_HEAD"], str(repo_dir))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Check refresh needed
|
# Check refresh needed
|
||||||
file_timestamp = Path(repo_dir / ".git" / "FETCH_HEAD")
|
file_timestamp = Path(repo_dir / ".git" / "FETCH_HEAD")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user