|
| 1 | +from typing import Optional, Tuple |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +from gitlab.v4.objects import Project, ProjectCommit |
| 6 | + |
| 7 | +from gitlab_submodule.objects import GitmodulesSubmodule |
| 8 | + |
| 9 | + |
| 10 | +def get_submodule_commit( |
| 11 | + submodule: GitmodulesSubmodule, |
| 12 | + submodule_project: Project, |
| 13 | + get_latest_commit_possible_if_not_found: bool = True, |
| 14 | + get_latest_commit_possible_ref: Optional[str] = None |
| 15 | + ) -> Tuple[ProjectCommit, bool]: |
| 16 | + commit_id, is_exact = _get_submodule_commit_id( |
| 17 | + submodule.parent_project, |
| 18 | + submodule.path, |
| 19 | + submodule.parent_ref, |
| 20 | + submodule_project, |
| 21 | + get_latest_commit_possible_if_not_found, |
| 22 | + get_latest_commit_possible_ref |
| 23 | + ) |
| 24 | + commit = submodule_project.commits.get(commit_id) |
| 25 | + return commit, is_exact |
| 26 | + |
| 27 | + |
| 28 | +def _get_submodule_commit_id( |
| 29 | + project: Project, |
| 30 | + submodule_path: str, |
| 31 | + ref: Optional[str] = None, |
| 32 | + submodule_project: Optional[Project] = None, |
| 33 | + get_latest_commit_possible_if_not_found: bool = True, |
| 34 | + get_latest_commit_possible_ref: Optional[str] = None |
| 35 | +) -> Tuple[str, bool]: |
| 36 | + """This uses a trick: |
| 37 | + - The .gitmodules files doesn't contain the actual commit sha that the |
| 38 | + submodules points to. |
| 39 | + - Accessing the `<submodule_path>` dir via the ProjectFileManager |
| 40 | + doesn't bring any useful info, EXCEPT: the id of the last commit that |
| 41 | + modified the file (i.e. that updated the submodule commit sha) |
| 42 | +
|
| 43 | + => We use that info to get the diff of the last commit that updated the |
| 44 | + submodule commit |
| 45 | + => We parse the diff to get the new submodule commit sha |
| 46 | +
|
| 47 | + NOTE: in some weird cases I observed without really understanding, |
| 48 | + a commit which created a .gitmodules file can contain zero submodule |
| 49 | + commit sha in its entire diff. |
| 50 | + In that case, we can only try to guess which was the latest commit in |
| 51 | + the submodule project at the datetime of the commit. |
| 52 | + """ |
| 53 | + submodule_dir = project.files.get( |
| 54 | + submodule_path, |
| 55 | + ref=ref if ref else project.default_branch) |
| 56 | + last_commit_id = submodule_dir.last_commit_id |
| 57 | + update_submodule_commit = project.commits.get(last_commit_id) |
| 58 | + |
| 59 | + submodule_commit_regex = r'Submodule commit ([a-zA-Z0-9]+)\n' |
| 60 | + for diff_file in update_submodule_commit.diff(): |
| 61 | + if diff_file['new_path'] == submodule_path: |
| 62 | + # either the commit id was added for the first time, |
| 63 | + # or it was updated -> we can find one or two matches |
| 64 | + # (or 0 in these weird cases) |
| 65 | + matches = re.findall(submodule_commit_regex, diff_file['diff']) |
| 66 | + # submodule commit id was updated |
| 67 | + if len(matches) == 2: |
| 68 | + return matches[1], True |
| 69 | + # submodule was added |
| 70 | + if len(matches) == 1: |
| 71 | + return matches[0], True |
| 72 | + |
| 73 | + # If the commit diff doesn't contain the submodule commit info, we still |
| 74 | + # know the date of the last commit in the project that updated the |
| 75 | + # submodule, so we can fallback to the last commit in the submodule that |
| 76 | + # was created before this date. |
| 77 | + # This requires a Project object for the submodule so if it wasn't |
| 78 | + # passed we cannot guess anything. |
| 79 | + if not get_latest_commit_possible_if_not_found: |
| 80 | + raise ValueError( |
| 81 | + f'Could not find commit id for submodule {submodule_path} of ' |
| 82 | + f'project {project.path_with_namespace}.') |
| 83 | + else: |
| 84 | + last_subproject_commits = submodule_project.commits.list( |
| 85 | + ref_name=(get_latest_commit_possible_ref |
| 86 | + if get_latest_commit_possible_ref |
| 87 | + else submodule_project.default_branch), |
| 88 | + until=update_submodule_commit.created_at |
| 89 | + ) |
| 90 | + return last_subproject_commits[0].id, False |
0 commit comments