1010
1111from app .exercise_config import ExerciseConfig
1212from app .gitmastery_config import GitMasteryConfig
13- from app .utils .click import error , get_exercise_root_config , get_gitmastery_root_config
14- from app .utils .general import ensure_str
15- from app .utils .version import (
16- Version ,
13+ from app .utils .click import (
14+ error ,
15+ get_exercise_root_config ,
16+ get_gitmastery_root_config ,
17+ get_verbose ,
18+ get_web_cache ,
19+ )
20+ from app .utils .exercises import (
21+ get_latest_development_exercise_version ,
1722 get_latest_exercise_version_within_pin ,
23+ get_latest_release_exercise_version ,
1824)
25+ from app .utils .general import ensure_str
26+ from app .utils .version import Version
1927
2028GITMASTERY_CONFIG_NAME = ".gitmastery.json"
2129GITMASTERY_EXERCISE_CONFIG_NAME = ".gitmastery-exercise.json"
2634
2735def _construct_gitmastery_exercises_url (filepath : str , version : Version ) -> str :
2836 if version .release :
29- ref = "heads/main"
37+ latest_release = get_latest_release_exercise_version ()
38+ if latest_release is None :
39+ raise ValueError ("This should not happen. Contact the Git-Mastery team." )
40+ ref = f"tags/v{ latest_release .to_version_string ()} "
3041 elif version .development :
31- latest_development = get_latest_exercise_version_within_pin (version )
32- ref = f"tags/{ latest_development } "
42+ latest_development = get_latest_development_exercise_version ()
43+ if latest_development is None :
44+ raise ValueError ("This should not happen. Contact the Git-Mastery team." )
45+ ref = f"tags/v{ latest_development .to_version_string ()} "
3346 elif not version .pinned :
34- ref = f"tags/{ version .to_version_string ()} "
47+ ref = f"tags/v { version .to_version_string ()} "
3548 else :
3649 # If pinned, we need to basically search for all the available tags within the
3750 # range
3851 latest_within_pin = get_latest_exercise_version_within_pin (version )
39- ref = f"tags/{ latest_within_pin } "
52+ ref = f"tags/v { latest_within_pin } "
4053
4154 url = (
4255 f"https://raw.githubusercontent.com/git-mastery/exercises/refs/{ ref } /{ filepath } "
4356 )
44- print (url )
4557 return url
4658
4759
@@ -156,12 +168,22 @@ def get_gitmastery_file_path(path: str):
156168
157169
158170def fetch_file_contents (url : str , is_binary : bool ) -> str | bytes :
171+ web_cache = get_web_cache ()
172+ if url in web_cache :
173+ if get_verbose ():
174+ print (f"Fetching { url } contents from WEB_CACHE" )
175+ return web_cache [url ]
159176 response = requests .get (url )
160177
178+ if get_verbose ():
179+ print (f"Querying { url } for contents" )
180+
161181 if response .status_code == 200 :
162182 if is_binary :
163- return response .content
164- return response .text
183+ web_cache [url ] = response .content
184+ else :
185+ web_cache [url ] = response .text
186+ return web_cache [url ]
165187 else :
166188 error (
167189 f"Failed to fetch resource { click .style (url , bold = True , italic = True )} . Inform the Git-Mastery team."
@@ -171,12 +193,22 @@ def fetch_file_contents(url: str, is_binary: bool) -> str | bytes:
171193def fetch_file_contents_or_none (
172194 url : str , is_binary : bool
173195) -> Optional [Union [str , bytes ]]:
196+ web_cache = get_web_cache ()
197+ if url in web_cache :
198+ if get_verbose ():
199+ print (f"Fetching { url } contents from WEB_CACHE" )
200+ return web_cache [url ]
174201 response = requests .get (url )
175202
203+ if get_verbose ():
204+ print (f"Querying { url } for contents" )
205+
176206 if response .status_code == 200 :
177207 if is_binary :
178- return response .content
179- return response .text
208+ web_cache [url ] = response .content
209+ else :
210+ web_cache [url ] = response .text
211+ return web_cache [url ]
180212 return None
181213
182214
@@ -208,10 +240,14 @@ def get_variable_from_url(
208240
209241def exercise_exists (exercise : str , timeout : int = 5 ) -> bool :
210242 try :
243+ exercise_url = get_gitmastery_file_path (
244+ f"{ exercise .replace ('-' , '_' )} /.gitmastery-exercise.json"
245+ )
246+ if get_verbose ():
247+ print (exercise_url )
248+
211249 response = requests .head (
212- get_gitmastery_file_path (
213- f"{ exercise .replace ('-' , '_' )} /.gitmastery-exercise.json"
214- ),
250+ exercise_url ,
215251 allow_redirects = True ,
216252 timeout = timeout ,
217253 )
0 commit comments