Snap for 13618210 from 2855398416d6b9ad835622ab3bad2fa02a466a26 to 25Q3-release

Change-Id: I032364c4d90bf5e13868ee41e9828e77aca1b097
diff --git a/git_updater.py b/git_updater.py
index e52f4ac..208abdd 100644
--- a/git_updater.py
+++ b/git_updater.py
@@ -13,6 +13,7 @@
 # limitations under the License.
 """Module to check updates from Git upstream."""
 
+from pathlib import Path
 from string import Template
 
 import metadata_pb2  # type: ignore
@@ -42,6 +43,16 @@
     """Updater for Git upstream."""
     UPSTREAM_REMOTE_NAME: str = "update_origin"
 
+    def __init__(self, proj_path: Path, old_identifier: metadata_pb2.Identifier,
+        old_ver: str) -> None:
+        non_default_branch = git_utils.find_non_default_branch(old_identifier.value)
+        if non_default_branch is not None:
+            self.upstream_branch = non_default_branch
+            old_identifier.value = old_identifier.value.strip(f'tree/{self.upstream_branch}')
+        else:
+            self.upstream_branch = git_utils.detect_default_branch(proj_path, self.UPSTREAM_REMOTE_NAME)
+        super().__init__(proj_path, old_identifier, old_ver)
+
     def is_supported_url(self) -> bool:
         return git_utils.is_valid_url(self._proj_path, self._old_identifier.value)
 
@@ -94,7 +105,8 @@
         """Checks upstream and returns whether a new version is available."""
         self.setup_remote()
 
-        latest_sha = self.current_head_of_upstream_default_branch()
+        latest_sha = git_utils.get_sha_for_revision(
+            self._proj_path, self.UPSTREAM_REMOTE_NAME + '/' + self.upstream_branch)
         latest_tag = self.latest_tag_of_upstream()
 
         if git_utils.is_commit(self._old_identifier.version):
diff --git a/git_utils.py b/git_utils.py
index d7b0372..2b6bba0 100644
--- a/git_utils.py
+++ b/git_utils.py
@@ -17,6 +17,7 @@
 import re
 import subprocess
 from pathlib import Path
+from urllib.parse import urlparse
 
 import fileutils
 import hashtags
@@ -25,6 +26,12 @@
 
 UNWANTED_TAGS = ["*alpha*", "*Alpha*", "*beta*", "*Beta*", "*rc*", "*RC*", "*test*"]
 
+COMMIT_PATTERN = r'^[a-f0-9]{40}$'
+COMMIT_RE = re.compile(COMMIT_PATTERN)
+
+GITHUB_NETLOC = 'github.com'
+GITHUB_BRANCH_DIVIDER = '/tree/'
+
 
 def fetch(proj_path: Path, remote_name: str, branch: str | None = None) -> None:
     """Runs git fetch.
@@ -145,10 +152,6 @@
     return lines
 
 
-COMMIT_PATTERN = r'^[a-f0-9]{40}$'
-COMMIT_RE = re.compile(COMMIT_PATTERN)
-
-
 # pylint: disable=redefined-outer-name
 def is_commit(commit: str) -> bool:
     """Whether a string looks like a SHA1 hash."""
@@ -353,3 +356,11 @@
     root = fileutils.find_tree_containing(proj_path)
     manifest = Manifest.for_tree(root)
     return manifest.remote
+
+
+def find_non_default_branch(identifier_value: str) -> str | None:
+    parsed_url = urlparse(identifier_value)
+    _, divider, branch = parsed_url.path.partition(GITHUB_BRANCH_DIVIDER)
+    if parsed_url.netloc == GITHUB_NETLOC and divider == GITHUB_BRANCH_DIVIDER:
+        return branch
+    return None
diff --git a/tests/test_git_utils.py b/tests/test_git_utils.py
index 7f305ef..320577a 100644
--- a/tests/test_git_utils.py
+++ b/tests/test_git_utils.py
@@ -213,5 +213,26 @@
         assert first_commit == out
 
 
+class FindNonDefaultBranchTest(unittest.TestCase):
+    """Tests for git_utils.find_non_default_branch"""
+    def test_branch_in_github_url(self) -> None:
+        """Tests if the branch attached to the url is found."""
+        url = 'https://github.com/robolectric/robolectric/tree/google'
+        non_default_branch = git_utils.find_non_default_branch(url)
+        self.assertEqual(non_default_branch, "google")
+
+    def test_no_branch_in_url(self) -> None:
+        """Tests if None is returned when the url doesn't have a branch."""
+        url = 'https://github.com/GNOME/libxml2/'
+        non_default_branch = git_utils.find_non_default_branch(url)
+        self.assertIsNone(non_default_branch)
+
+    def test_branch_in_gitlab_url(self) -> None:
+        """Tests if None is returned when the url is non-GitHub git."""
+        url = 'https://gitlab.xiph.org/xiph/opus/-/tree/whitespace'
+        non_default_branch = git_utils.find_non_default_branch(url)
+        self.assertIsNone(non_default_branch)
+
+
 if __name__ == "__main__":
     unittest.main(verbosity=2)