1212import requests , json
1313from requests .exceptions import HTTPError
1414import time
15+ import re
1516
1617from mkdocs_git_committers_plugin_2 .exclude import exclude
1718
@@ -90,16 +91,18 @@ def on_config(self, config):
9091 return config
9192
9293 # Get unique contributors for a given path
93- def get_contributors_to_file (self , path ):
94+ def get_contributors_to_file (self , path , submodule_repo = None ):
9495 # We already got a 401 (unauthorized) or 403 (rate limit) error, so we don't try again
9596 if self .last_request_return_code == 403 or self .last_request_return_code == 401 :
9697 return []
9798 if self .config ['gitlab_repository' ]:
9899 # REST endpoint is in the form https://gitlab.com/api/v4/projects/[project ID]/repository/commits?path=[uri-encoded-path]&ref_name=[branch]
99100 url = self .gitlaburl + "/projects/" + str (self .config ['gitlab_repository' ]) + "/repository/commits?path=" + requests .utils .quote (path ) + "&ref_name=" + self .branch
100101 else :
102+ # Check git submodule
103+ repository = submodule_repo or self .config ['repository' ]
101104 # REST endpoint is in the form https://api.github.com/repos/[repository]/commits?path=[uri-encoded-path]&sha=[branch]&per_page=100
102- url = self .githuburl + "/repos/" + self . config [ ' repository' ] + "/commits?path=" + requests .utils .quote (path ) + "&sha=" + self .branch + "&per_page=100"
105+ url = self .githuburl + "/repos/" + repository + "/commits?path=" + requests .utils .quote (path ) + "&sha=" + self .branch + "&per_page=100"
103106 authors = []
104107 LOG .info ("git-committers: fetching contributors for " + path )
105108 r = requests .get (url = url , headers = self .auth_header )
@@ -228,6 +231,25 @@ def list_contributors(self, path):
228231 # Use the last commit and get the date
229232 last_commit_date = time .strftime ("%Y-%m-%d" , time .gmtime (c .authored_date ))
230233
234+ # Check if the file is in a git submodule on GitHub
235+ submodule_repo , path_in_submodule = None , None
236+ if last_commit_date == "" and not self .config ['gitlab_repository' ]:
237+ for submodule in self .localrepo .submodules :
238+ if submodule_repo :
239+ break
240+ if not path .startswith (submodule .path ):
241+ continue
242+ match = re .match (r"https:\/\/github\.com\/([^\/]+\/[^\/.]+)" , submodule .url )
243+ if not match :
244+ LOG .warning ("git-committers: Submodule matched but will not be queried, since it isn't a GitHub repo." )
245+ continue
246+ path_in_submodule = path [len (submodule .path )+ 1 :]
247+ for c in Commit .iter_items (submodule .module (), submodule .module ().head , path_in_submodule ):
248+ if not last_commit_date :
249+ # Use the last commit and get the date
250+ submodule_repo = match .group (1 )
251+ last_commit_date = time .strftime ("%Y-%m-%d" , time .gmtime (c .authored_date ))
252+
231253 # File not committed yet
232254 if last_commit_date == "" :
233255 last_commit_date = datetime .now ().strftime ("%Y-%m-%d" )
@@ -241,7 +263,12 @@ def list_contributors(self, path):
241263 return self .cache_page_authors [path ]['authors' ], self .cache_page_authors [path ]['last_commit_date' ]
242264
243265 authors = []
244- authors = self .get_contributors_to_file (path )
266+ if not submodule_repo :
267+ authors = self .get_contributors_to_file (path )
268+ else :
269+ LOG .info ("git-committers: fetching submodule info for " + path + " from repository " + submodule_repo + " with path " + path_in_submodule )
270+ authors = self .get_contributors_to_file (path_in_submodule , submodule_repo = submodule_repo )
271+
245272 if path not in self .cache_page_authors or self .cache_page_authors [path ] != {'last_commit_date' : last_commit_date , 'authors' : authors }:
246273 self .should_save_cache = True
247274 self .cache_page_authors [path ] = {'last_commit_date' : last_commit_date , 'authors' : authors }
0 commit comments