Skip to content

Commit

Permalink
Changes branches to tmp branches
Browse files Browse the repository at this point in the history
  • Loading branch information
r.jaepel committed Oct 23, 2023
1 parent 6ae9f6c commit 449138f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/push_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: |
date > generated.txt
git config user.name github-actions
git config user.email [email protected]
git add .
python ./workshop_git_tools/process_repo.py
git commit -m "generated"
git push
83 changes: 40 additions & 43 deletions workshop_git_tools/process_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ def run_command(command):
return subprocess.run(command, shell=True, check=True)


def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_restore_dev=False):
def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_restore_dev=False, base_branch="dev",
target_branch="solution"
):
"""Create solution files.
Parameters
Expand All @@ -134,6 +136,10 @@ def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_rest
Number of cpu cores to use for parallelization
on_fail_restore_dev : bool, optional
Reset the repo to dev on failure.
base_branch : str, optional
Branch on which these actions are based. If git is not currently on this branch: abort script.
target_branch : str, optional
Name for the newly created branch.
Returns
-------
Expand All @@ -143,19 +149,16 @@ def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_rest
current_branch = repo.active_branch.name
repo_root = Path(repo.working_tree_dir) # Gets the root directory of the repo

if current_branch != "dev":
print("Not on dev branch. Skipping create_solution script.")
if current_branch != base_branch:
print(f"Not on {base_branch} branch. Skipping create_solution script.")
return

try:
# Stash everything
run_command("git stash")

# Checkout the 'solution' branch
repo.git.checkout("solution")
repo.git.checkout(target_branch)

# Reset to `dev`
run_command("git reset --hard dev")
# Reset to base_branch
run_command(f"git reset --hard {base_branch}")

# Find all myst files recursively
myst_files = list(repo_root.glob("**/*.md"))
Expand Down Expand Up @@ -185,19 +188,14 @@ def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_rest
if commit:
# Commit all changes to ipynb files
repo.git.add(".") # Less error-prone than working with path lists
run_command('git commit -m "Update solution"')
run_command(f'git commit -m "Update {target_branch}"')

if push:
# Push files to remote
run_command("git push --force-with-lease --set-upstream origin solution")

# Switch back to dev
repo.git.checkout("dev")
run_command(f"git push --force --set-upstream origin {target_branch}")

try:
repo.git.stash("pop")
except git.GitCommandError as e:
print(e)
# Switch back to base_branch
repo.git.checkout(base_branch)

except Exception as e:
print(f"An error occurred: {e}")
Expand All @@ -206,8 +204,8 @@ def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_rest
run_command("git restore --staged .")
run_command("git restore .")

# Switch back to dev
repo.git.checkout("dev")
# Switch back to base_branch
repo.git.checkout(base_branch)

try:
repo.git.stash("pop")
Expand All @@ -217,7 +215,8 @@ def create_solution(run=False, commit=False, push=False, n_cores=1, on_fail_rest
raise


def create_teaching(commit=False, push=False, n_cores=1, on_fail_restore_dev=False):
def create_teaching(commit=False, push=False, n_cores=1, on_fail_restore_dev=False, base_branch="dev",
target_branch="teaching"):
"""Create teaching files.
Parameters
Expand All @@ -239,19 +238,16 @@ def create_teaching(commit=False, push=False, n_cores=1, on_fail_restore_dev=Fal
current_branch = repo.active_branch.name
repo_root = Path(repo.working_tree_dir) # Gets the root directory of the repo

if current_branch != "dev":
print("Not on dev branch. Skipping create_solution script.")
if current_branch != base_branch:
print(f"Not on {base_branch} branch. Skipping create_teaching script.")
return

try:
# Stash everything
run_command("git stash")

# Checkout the 'teaching' branch
repo.git.checkout("teaching")
repo.git.checkout(target_branch)

# Reset to `dev`
run_command("git reset --hard dev")
# Reset to base_branch
run_command(f"git reset --hard {base_branch}")

# Find all myst files recursively
myst_files = list(repo_root.glob("**/*.md"))
Expand Down Expand Up @@ -280,23 +276,19 @@ def create_teaching(commit=False, push=False, n_cores=1, on_fail_restore_dev=Fal
if commit:
# Commit all changes to myst files (our source of truth)
repo.git.add(".") # Less error-prone than working with path lists
run_command('git commit -m "Update teaching"')
run_command(f'git commit -m "Update {target_branch}"')

if push:
# Push files to remote
run_command("git push --force-with-lease --set-upstream origin teaching")
run_command(f"git push --force --set-upstream origin {target_branch}")

# Clean up
for ipynb_file in ipynb_files:
os.remove(ipynb_file)

# Switch back to dev
repo.git.checkout("dev")
# Switch back to base_branch
repo.git.checkout(base_branch)

try:
repo.git.stash("pop")
except git.GitCommandError as e:
print(e)

except Exception as e:
print(f"An error occurred: {e}")
Expand All @@ -305,8 +297,8 @@ def create_teaching(commit=False, push=False, n_cores=1, on_fail_restore_dev=Fal
run_command("git restore --staged .")
run_command("git restore .")

# Switch back to dev
repo.git.checkout("dev")
# Switch back to base_branch
repo.git.checkout(base_branch)

try:
repo.git.stash("pop")
Expand Down Expand Up @@ -384,13 +376,15 @@ def convert_ipynb_to_myst_md(ipynb_file_path):
return run_command(f'jupytext --to md:myst "{ipynb_file_path}"')


def setup_teaching_copy(new_repo_dir="CADET-Workshop-teaching"):
def setup_teaching_copy(new_repo_dir="CADET-Workshop-teaching", target_branch="teaching"):
"""Create a copy of this directory and check out the teaching branch in it
Parameters
----------
new_repo_dir : str
Name for the new repo folder.
target_branch : str
Name of the branch to check out in the new repo folder.
Returns
-------
Expand All @@ -414,7 +408,7 @@ def setup_teaching_copy(new_repo_dir="CADET-Workshop-teaching"):
# Clone from this repo to new repo
new_repo = git.Repo.clone_from(repo_root, new_repo_dir)

new_repo.git.checkout("teaching")
new_repo.git.checkout(target_branch)


def delete_dir_contents(dir_path):
Expand Down Expand Up @@ -457,9 +451,12 @@ def main(**kwargs):
args.__setattr__(kwarg_key, kwarg_value)

args.run = False
args.commit = True

create_solution(args.run, args.commit, args.push, args.n_cores, args.on_fail_restore_dev)
create_teaching(args.commit, args.push, args.n_cores, args.on_fail_restore_dev)
create_solution(args.run, args.commit, args.push, args.n_cores, args.on_fail_restore_dev, base_branch="test-ci",
target_branch="tmp_solution")
create_teaching(args.commit, args.push, args.n_cores, args.on_fail_restore_dev, base_branch="test-ci",
target_branch="tmp_teaching")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions workshop_git_tools/setup_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def setup_repo():

repo.heads.dev.checkout()

create_or_replace_branch(repo, 'solution')
create_or_replace_branch(repo, 'teaching')
create_or_replace_branch(repo, 'tmp_solution')
create_or_replace_branch(repo, 'tmp_teaching')


if __name__ == "__main__":
Expand Down

0 comments on commit 449138f

Please sign in to comment.