Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit c956f40

Browse files
OCoppingcoretl
authored andcommitted
Added check for existing skeleton branch
1 parent ddb028e commit c956f40

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

src/dls_python3_skeleton/__main__.py

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -66,57 +66,62 @@ def replace_text(text: str) -> str:
6666
text = text.replace("[email protected]", email)
6767
return text
6868

69-
with GitTemporaryDirectory() as git_tmp:
70-
# Clone existing repo into tmp so we don't mess up if we fail
71-
# half way through
72-
git_tmp("clone", path, git_tmp.name)
73-
# We will use this branch to put the skeleton changes on
74-
git_tmp("checkout", "--orphan", MERGE_BRANCH)
75-
# Delete all the current files if there are any
76-
git_tmp("rm", "-rf", ".", "--ignore-unmatch")
77-
# And make sure src isn't there otherwise the git mv below
78-
# will do the wrong thing
79-
shutil.rmtree(git_tmp / "src", ignore_errors=True)
80-
# Merge in the skeleton commits
81-
git_tmp("pull", SKELETON, "skeleton")
82-
# Move things around
83-
git_tmp("mv", "src/dls_python3_skeleton", f"src/{package}")
84-
git_tmp("mv", "tests/test_dls_python3_skeleton.py", f"tests/test_{package}.py")
85-
# Change contents of all children known to git
86-
for relative_child in git_tmp("ls-files").splitlines():
87-
child = Path(git_tmp.name) / relative_child
88-
if child.suffix in CHANGE_SUFFIXES and child.name not in IGNORE_FILES:
89-
text = child.read_text()
90-
start_search, end_search = IGNORE_RANGES.get(child.name, (None, None))
91-
if start_search:
92-
start_ignore = text.find(start_search)
93-
assert start_ignore > 0, f"{start_search} not in {child.name}"
94-
if end_search:
95-
end_ignore = text.find(end_search, start_ignore) + len(
96-
end_search
97-
)
98-
assert end_ignore > 0, f"{end_search} not in {child.name}"
99-
else:
100-
end_ignore = len(text)
101-
else:
102-
start_ignore = 0
103-
end_ignore = 0
104-
child.write_text(
105-
replace_text(text[:start_ignore])
106-
+ text[start_ignore:end_ignore]
107-
+ replace_text(text[end_ignore:])
108-
)
109-
# Commit what we have and push to the original repo
110-
git_tmp("commit", "-a", "-m", f"Rename dls-python3-skeleton -> {repo}")
111-
git_tmp("push", "origin", MERGE_BRANCH)
112-
try:
113-
git("merge", MERGE_BRANCH, "--allow-unrelated-histories", cwd=path)
114-
except CalledProcessError:
115-
# The merge failed, so ask the user to fix it
116-
print("Please fix the conflicts above, then you can run:")
117-
print(f" git branch -d {MERGE_BRANCH}")
69+
branches = [x[2:] for x in str(git("branch", "--list")).split("\n")]
70+
71+
if "skeleton-merge-branch" in branches:
72+
print("Merge branch already exists. Skipping creation...")
11873
else:
119-
git("branch", "-d", MERGE_BRANCH, cwd=path)
74+
with GitTemporaryDirectory() as git_tmp:
75+
# Clone existing repo into tmp so we don't mess up if we fail
76+
# half way through
77+
git_tmp("clone", path, git_tmp.name)
78+
# We will use this branch to put the skeleton changes on
79+
git_tmp("checkout", "--orphan", MERGE_BRANCH)
80+
# Delete all the current files if there are any
81+
git_tmp("rm", "-rf", ".", "--ignore-unmatch")
82+
# And make sure src isn't there otherwise the git mv below
83+
# will do the wrong thing
84+
shutil.rmtree(git_tmp / "src", ignore_errors=True)
85+
# Merge in the skeleton commits
86+
git_tmp("pull", SKELETON, "skeleton")
87+
# Move things around
88+
git_tmp("mv", "src/dls_python3_skeleton", f"src/{package}")
89+
git_tmp("mv", "tests/test_dls_python3_skeleton.py", f"tests/test_{package}.py")
90+
# Change contents of all children known to git
91+
for relative_child in git_tmp("ls-files").splitlines():
92+
child = Path(git_tmp.name) / relative_child
93+
if child.suffix in CHANGE_SUFFIXES and child.name not in IGNORE_FILES:
94+
text = child.read_text()
95+
start_search, end_search = IGNORE_RANGES.get(child.name, (None, None))
96+
if start_search:
97+
start_ignore = text.find(start_search)
98+
assert start_ignore > 0, f"{start_search} not in {child.name}"
99+
if end_search:
100+
end_ignore = text.find(end_search, start_ignore) + len(
101+
end_search
102+
)
103+
assert end_ignore > 0, f"{end_search} not in {child.name}"
104+
else:
105+
end_ignore = len(text)
106+
else:
107+
start_ignore = 0
108+
end_ignore = 0
109+
child.write_text(
110+
replace_text(text[:start_ignore])
111+
+ text[start_ignore:end_ignore]
112+
+ replace_text(text[end_ignore:])
113+
)
114+
# Commit what we have and push to the original repo
115+
git_tmp("commit", "-a", "-m", f"Rename dls-python3-skeleton -> {repo}")
116+
git_tmp("push", "origin", MERGE_BRANCH)
117+
try:
118+
git("merge", MERGE_BRANCH, "--allow-unrelated-histories", cwd=path)
119+
except CalledProcessError:
120+
# The merge failed, so ask the user to fix it
121+
print("Please fix the conflicts above, then you can run:")
122+
print(f" git branch -d {MERGE_BRANCH}")
123+
else:
124+
git("branch", "-d", MERGE_BRANCH, cwd=path)
120125
print("Instructions on how to develop this module are in CONTRIBUTING.rst")
121126

122127

0 commit comments

Comments
 (0)