aboutsummaryrefslogtreecommitdiff
path: root/git_updater.py
diff options
context:
space:
mode:
Diffstat (limited to 'git_updater.py')
-rw-r--r--git_updater.py56
1 files changed, 46 insertions, 10 deletions
diff --git a/git_updater.py b/git_updater.py
index ef320cb..483f3fc 100644
--- a/git_updater.py
+++ b/git_updater.py
@@ -23,21 +23,56 @@ import updater_utils
class GitUpdater(base_updater.Updater):
"""Updater for Git upstream."""
UPSTREAM_REMOTE_NAME: str = "update_origin"
- android_remote_name: str
def is_supported_url(self) -> bool:
- return self._old_url.type == metadata_pb2.URL.GIT
+ return git_utils.is_valid_url(self._proj_path, self._old_url.value)
+
+ @staticmethod
+ def _is_likely_android_remote(url: str) -> bool:
+ """Returns True if the URL is likely to be the project's Android remote."""
+ # There isn't a strict rule for finding the correct remote for upstream-master,
+ # so we have to guess. Be careful to filter out things that look almost right
+ # but aren't. Here's an example of a project that has a lot of false positives:
+ #
+ # aosp /usr/local/google/home/danalbert/src/mirrors/android/refs/aosp/toolchain/rr.git (fetch)
+ # aosp persistent-https://android.git.corp.google.com/toolchain/rr (push)
+ # origin https://github.com/DanAlbert/rr.git (fetch)
+ # origin https://github.com/DanAlbert/rr.git (push)
+ # unmirrored persistent-https://android.git.corp.google.com/toolchain/rr (fetch)
+ # unmirrored persistent-https://android.git.corp.google.com/toolchain/rr (push)
+ # update_origin https://github.com/rr-debugger/rr (fetch)
+ # update_origin https://github.com/rr-debugger/rr (push)
+ # upstream https://github.com/rr-debugger/rr.git (fetch)
+ # upstream https://github.com/rr-debugger/rr.git (push)
+ #
+ # unmirrored is the correct remote here. It's not a local path, and contains
+ # either /platform/external/ or /toolchain/ (the two common roots for third-
+ # party Android imports).
+ if '://' not in url:
+ # Skip anything that's likely a local GoB mirror.
+ return False
+ if '/platform/external/' in url:
+ return True
+ if '/toolchain/' in url:
+ return True
+ return False
def _setup_remote(self) -> None:
remotes = git_utils.list_remotes(self._proj_path)
current_remote_url = None
+ android_remote_name: str | None = None
for name, url in remotes.items():
if name == self.UPSTREAM_REMOTE_NAME:
current_remote_url = url
- # Guess android remote name.
- if '/platform/external/' in url:
- self.android_remote_name = name
+ if self._is_likely_android_remote(url):
+ android_remote_name = name
+
+ if android_remote_name is None:
+ remotes_formatted = "\n".join(f"{k} {v}" for k, v in remotes.items())
+ raise RuntimeError(
+ f"Could not determine android remote for {self._proj_path}. Tried:\n"
+ f"{remotes_formatted}")
if current_remote_url is not None and current_remote_url != self._old_url.value:
git_utils.remove_remote(self._proj_path, self.UPSTREAM_REMOTE_NAME)
@@ -48,7 +83,7 @@ class GitUpdater(base_updater.Updater):
self._old_url.value)
git_utils.fetch(self._proj_path,
- [self.UPSTREAM_REMOTE_NAME, self.android_remote_name])
+ [self.UPSTREAM_REMOTE_NAME, android_remote_name])
def check(self) -> None:
"""Checks upstream and returns whether a new version is available."""
@@ -66,15 +101,16 @@ class GitUpdater(base_updater.Updater):
self._new_ver = updater_utils.get_latest_version(self._old_ver, tags)
def _check_head(self) -> None:
- branch = git_utils.get_default_branch(self._proj_path,
- self.UPSTREAM_REMOTE_NAME)
+ branch = git_utils.detect_default_branch(self._proj_path,
+ self.UPSTREAM_REMOTE_NAME)
self._new_ver = git_utils.get_sha_for_branch(
self._proj_path, self.UPSTREAM_REMOTE_NAME + '/' + branch)
- def update(self) -> None:
+ def update(self, skip_post_update: bool) -> None:
"""Updates the package.
-
Has to call check() before this function.
"""
print(f"Running `git merge {self._new_ver}`...")
git_utils.merge(self._proj_path, self._new_ver)
+ if not skip_post_update:
+ updater_utils.run_post_update(self._proj_path, self._proj_path) \ No newline at end of file