Skip to content

Commit 14a253c

Browse files
Mirror repo name (#215)
* Extract name of repo from source * Account for working copies when extracting from mirrors * fix merge conflict * unit tests working --------- Co-authored-by: James Bruten <109733895+james-bruten-mo@users.noreply.github.com>
1 parent 4387949 commit 14a253c

4 files changed

Lines changed: 22 additions & 30 deletions

File tree

github_scripts/get_git_sources.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,16 @@ def datetime_str() -> str:
9191

9292

9393
def clone_and_merge(
94-
dependency: str,
95-
opts: Union[list, dict],
96-
loc: Path,
97-
use_mirrors: bool,
98-
mirror_loc: Path,
94+
opts: Union[list, dict], loc: Path, use_mirrors: bool, mirror_loc: Path
9995
) -> None:
10096
"""
10197
Wrapper script for calling get_source and merge_source for a single dependency
10298
103-
dependency: name of the dependency
10499
opts: dict or list of dicts for a dependency in the dependencies file
105100
loc: path to location to clone to
106101
use_mirrors: bool, use local git mirrors if true
107102
mirror_loc: path to local git mirrors
108103
"""
109-
110104
if not isinstance(opts, list):
111105
opts = [opts]
112106

@@ -120,7 +114,6 @@ def clone_and_merge(
120114
values["source"],
121115
values["ref"],
122116
loc,
123-
dependency,
124117
use_mirrors,
125118
mirror_loc,
126119
)
@@ -130,7 +123,6 @@ def clone_and_merge(
130123
values["source"],
131124
values["ref"],
132125
loc,
133-
dependency,
134126
use_mirrors,
135127
mirror_loc,
136128
)
@@ -140,7 +132,6 @@ def get_source(
140132
source: str,
141133
ref: str,
142134
dest: Path,
143-
repo: str,
144135
use_mirrors: bool = False,
145136
mirror_loc: Path = Path(""),
146137
) -> None:
@@ -151,26 +142,25 @@ def get_source(
151142
if ".git" in source:
152143
if use_mirrors:
153144
logger.info(
154-
f"[{datetime_str()}] Cloning {repo} from {mirror_loc} at ref {ref}"
145+
f"[{datetime_str()}] Cloning {dest.name} from {mirror_loc} at ref {ref}"
155146
)
156-
mirror_repo = repo
157-
if "jules-internal" in source:
158-
mirror_repo = "jules-internal"
147+
mirror_repo = re.split("[:/]", source)[-1]
159148
mirror_loc = Path(mirror_loc) / "MetOffice" / mirror_repo
160149
clone_repo_mirror(source, ref, mirror_loc, dest)
161150
else:
162-
logger.info(f"[{datetime_str()}] Cloning {repo} from {source} at ref {ref}")
151+
logger.info(
152+
f"[{datetime_str()}] Cloning {dest.name} from {source} at ref {ref}"
153+
)
163154
clone_repo(source, ref, dest)
164155
else:
165-
logger.info(f"[{datetime_str()}] Syncing {repo} at ref {ref}")
156+
logger.info(f"[{datetime_str()}] Syncing {dest.name} at ref {ref}")
166157
sync_repo(source, ref, dest)
167158

168159

169160
def merge_source(
170161
source: Union[Path, str],
171162
ref: str,
172163
dest: Path,
173-
repo: str,
174164
use_mirrors: bool = False,
175165
mirror_loc: Path = Path(""),
176166
) -> None:
@@ -181,17 +171,12 @@ def merge_source(
181171

182172
logger.info(
183173
f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Merging "
184-
f"{source} at ref {ref} into {repo}"
174+
f"{source} at ref {ref} into {dest.name}"
185175
)
186176

187-
if ".git" in str(source):
188-
if use_mirrors:
189-
remote_path = Path(mirror_loc) / "MetOffice" / repo
190-
fetch = determine_mirror_fetch(source, ref)
191-
else:
192-
remote_path = source
193-
fetch = ref
194-
else:
177+
if not any(
178+
[g for g in ["https:", "git@", "localmirrors:"] if str(source).startswith(g)]
179+
):
195180
if not ref:
196181
raise Exception(
197182
f"Cannot merge local source '{source}' with empty ref.\n"
@@ -200,6 +185,14 @@ def merge_source(
200185
)
201186
remote_path = source
202187
fetch = ref
188+
elif use_mirrors:
189+
mirror_repo = re.split("[:/]", source)[-1]
190+
remote_path = Path(mirror_loc) / "MetOffice" / mirror_repo
191+
fetch = determine_mirror_fetch(source, ref)
192+
else:
193+
# Not using a clone or the mirrors
194+
remote_path = source
195+
fetch = ref
203196

204197
run_command(f"git -C {dest} remote add local {remote_path}")
205198

@@ -210,7 +203,7 @@ def merge_source(
210203
if result.returncode:
211204
unmerged_files = get_unmerged(dest)
212205
if unmerged_files:
213-
handle_merge_conflicts(source, ref, dest, repo)
206+
handle_merge_conflicts(source, ref, dest, dest.name)
214207
else:
215208
raise subprocess.CalledProcessError(
216209
result.returncode, command, result.stdout, result.stderr

github_scripts/merge_sources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def main():
8282

8383
for dependency, sources in dependencies.items():
8484
dest = args.path / dependency
85-
clone_and_merge(dependency, sources, dest, args.mirrors, args.mirror_loc)
85+
clone_and_merge(sources, dest, args.mirrors, args.mirror_loc)
8686

8787

8888
if __name__ == "__main__":

github_scripts/rose_stem_extract_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def main() -> None:
4545

4646
for dependency, sources in dependencies.items():
4747
loc = clone_loc / dependency
48-
clone_and_merge(dependency, sources, loc, use_mirrors, mirror_loc)
48+
clone_and_merge(sources, loc, use_mirrors, mirror_loc)
4949

5050

5151
if __name__ == "__main__":

github_scripts/tests/test_get_git_sources.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ def test_merge_sources(setup_sources):
126126
"https://github.com/MetOffice/SimSys_Scripts.git",
127127
"main",
128128
target_clone,
129-
"SimSys_Scripts",
130129
)
131130
is None
132131
)

0 commit comments

Comments
 (0)