22import os
33import sys
44import tempfile
5- import urllib .parse
65from pathlib import Path
76from typing import Any , Dict , Optional , Tuple , TypeVar , Union
87
1312from app .gitmastery_config import GitMasteryConfig
1413from app .utils .click import error , get_exercise_root_config , get_gitmastery_root_config
1514from app .utils .general import ensure_str
16- from app .utils .version import Version , get_latest_exercise_version_tag
15+ from app .utils .version import (
16+ Version ,
17+ get_latest_exercise_version_within_pin ,
18+ )
1719
1820GITMASTERY_CONFIG_NAME = ".gitmastery.json"
1921GITMASTERY_EXERCISE_CONFIG_NAME = ".gitmastery-exercise.json"
2224)
2325
2426
27+ def _construct_gitmastery_exercises_url (filepath : str , version : Version ) -> str :
28+ if version .release :
29+ ref = "heads/main"
30+ elif version .development :
31+ latest_development = get_latest_exercise_version_within_pin (version )
32+ ref = f"tags/{ latest_development } "
33+ elif not version .pinned :
34+ ref = f"tags/{ version .to_version_string ()} "
35+ else :
36+ # If pinned, we need to basically search for all the available tags within the
37+ # range
38+ latest_within_pin = get_latest_exercise_version_within_pin (version )
39+ ref = f"tags/{ latest_within_pin } "
40+
41+ url = (
42+ f"https://raw.githubusercontent.com/git-mastery/exercises/refs/{ ref } /{ filepath } "
43+ )
44+ print (url )
45+ return url
46+
47+
2548def _find_root (filename : str ) -> Optional [Tuple [Path , int ]]:
2649 current = Path .cwd ()
2750 steps = 0
@@ -68,8 +91,8 @@ def read_gitmastery_config(gitmastery_config_path: Path, cds: int) -> GitMastery
6891 cds = cds ,
6992 progress_local = raw_config .get ("progress_local" , False ),
7093 progress_remote = raw_config .get ("progress_remote" , False ),
71- exercises_version = raw_config . get (
72- "exercises_version" , Version . parse_version_string ( "latest " )
94+ exercises_version = Version . parse_version_string (
95+ raw_config . get ( "exercises_version" , "release " )
7396 ),
7497 )
7598
@@ -124,7 +147,12 @@ def generate_cds_string(cds: int) -> str:
124147
125148
126149def get_gitmastery_file_path (path : str ):
127- return urllib .parse .urljoin (GITMASTERY_EXERCISES_BASE_URL , path )
150+ config = get_gitmastery_root_config ()
151+ if config is None :
152+ version = Version .RELEASE
153+ else :
154+ version = config .exercises_version
155+ return _construct_gitmastery_exercises_url (path , version )
128156
129157
130158def fetch_file_contents (url : str , is_binary : bool ) -> str | bytes :
@@ -138,7 +166,6 @@ def fetch_file_contents(url: str, is_binary: bool) -> str | bytes:
138166 error (
139167 f"Failed to fetch resource { click .style (url , bold = True , italic = True )} . Inform the Git-Mastery team."
140168 )
141- return ""
142169
143170
144171def fetch_file_contents_or_none (
0 commit comments