1- from typing import Optional , Tuple , Union
1+ from typing import Optional , Union
22
33import re
44
55from gitlab .v4 .objects import Project , ProjectCommit
6+ from gitlab .exceptions import GitlabGetError
67
78from gitlab_submodule .objects import Submodule , Commit
89
910
1011def get_submodule_commit (
1112 submodule : Submodule ,
1213 submodule_project : Optional [Project ] = None ,
13- * args ,
14- ** kwargs
15- ) -> Tuple [Union [ProjectCommit , Commit ], bool ]:
16- commit_id , is_exact = _get_submodule_commit_id (
14+ ) -> Union [ProjectCommit , Commit ]:
15+ commit_id = _get_submodule_commit_id (
1716 submodule .parent_project ,
1817 submodule .path ,
1918 submodule .parent_ref ,
20- submodule_project ,
21- * args ,
22- ** kwargs
2319 )
2420 if submodule_project is not None :
2521 commit = submodule_project .commits .get (commit_id )
2622 else :
2723 commit = Commit (commit_id )
28- return commit , is_exact
24+ return commit
2925
3026
3127def _get_submodule_commit_id (
3228 project : Project ,
3329 submodule_path : str ,
3430 ref : Optional [str ] = None ,
35- submodule_project : Optional [Project ] = None ,
36- get_latest_commit_possible_if_not_found : bool = False ,
37- get_latest_commit_possible_ref : Optional [str ] = None
38- ) -> Tuple [str , bool ]:
31+ ) -> str :
3932 """This uses a trick:
4033 - The .gitmodules files doesn't contain the actual commit sha that the
4134 submodules points to.
@@ -46,16 +39,17 @@ def _get_submodule_commit_id(
4639 => We use that info to get the diff of the last commit that updated the
4740 submodule commit
4841 => We parse the diff to get the new submodule commit sha
49-
50- NOTE: in some weird cases I observed without really understanding,
51- a commit which created a .gitmodules file can contain zero submodule
52- commit sha in its entire diff.
53- In that case, we can only try to guess which was the latest commit in
54- the submodule project at the datetime of the commit.
5542 """
56- submodule_dir = project .files .get (
57- submodule_path ,
58- ref = ref if ref else project .default_branch )
43+ try :
44+ submodule_dir = project .files .get (
45+ submodule_path ,
46+ ref = ref if ref else project .default_branch )
47+ except GitlabGetError :
48+ raise FileNotFoundError (
49+ f'Local submodule path "{ submodule_path } " was not found for '
50+ f'project at url "{ project .web_url } " - check if your .gitmodules '
51+ f'file is up-to-date.' )
52+
5953 last_commit_id = submodule_dir .last_commit_id
6054 update_submodule_commit = project .commits .get (last_commit_id )
6155
@@ -68,26 +62,11 @@ def _get_submodule_commit_id(
6862 matches = re .findall (submodule_commit_regex , diff_file ['diff' ])
6963 # submodule commit id was updated
7064 if len (matches ) == 2 :
71- return matches [1 ], True
65+ return matches [1 ]
7266 # submodule was added
7367 if len (matches ) == 1 :
74- return matches [0 ], True
75-
76- # If the commit diff doesn't contain the submodule commit info, we still
77- # know the date of the last commit in the project that updated the
78- # submodule, so we can fallback to the last commit in the submodule that
79- # was created before this date.
80- # This requires a Project object for the submodule so if it wasn't
81- # passed we cannot guess anything.
82- if not (get_latest_commit_possible_if_not_found
83- and submodule_project is not None ):
84- raise ValueError (
85- f'Could not find commit id for submodule { submodule_path } of '
86- f'project { project .path_with_namespace } .' )
68+ return matches [0 ]
8769
88- last_subproject_commits = submodule_project .commits .list (
89- ref_name = (get_latest_commit_possible_ref
90- if get_latest_commit_possible_ref
91- else submodule_project .default_branch ),
92- until = update_submodule_commit .created_at )
93- return last_subproject_commits [0 ].id , False
70+ # should never happen
71+ raise RuntimeError (f'Did not find any commit id for submodule '
72+ f'"{ submodule_path } " at url "{ project .web_url } "' )
0 commit comments