Skip to content

Commit

Permalink
Merge 1def8ad into c465191
Browse files Browse the repository at this point in the history
  • Loading branch information
nickderobertis authored Feb 20, 2022
2 parents c465191 + 1def8ad commit 5aecde4
Show file tree
Hide file tree
Showing 14 changed files with 636 additions and 55 deletions.
12 changes: 12 additions & 0 deletions flexlate/adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def add_template_source(
target_version: Optional[str] = None,
out_root: Path = Path("."),
merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
base_merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
base_template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
add_mode: AddMode = AddMode.LOCAL,
config_manager: ConfigManager = ConfigManager(),
):
Expand Down Expand Up @@ -108,7 +110,9 @@ def add_template_source(
commit_message,
out_root,
merged_branch_name=merged_branch_name,
base_merged_branch_name=base_merged_branch_name,
template_branch_name=template_branch_name,
base_template_branch_name=base_template_branch_name,
)

print_styled(
Expand All @@ -124,7 +128,9 @@ def apply_template_and_add(
out_root: Path = Path("."),
add_mode: AddMode = AddMode.LOCAL,
merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
base_merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
base_template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
no_input: bool = False,
config_manager: ConfigManager = ConfigManager(),
updater: Updater = Updater(),
Expand Down Expand Up @@ -182,7 +188,9 @@ def apply_template_and_add(
commit_message,
out_root,
merged_branch_name=merged_branch_name,
base_merged_branch_name=base_merged_branch_name,
template_branch_name=template_branch_name,
base_template_branch_name=base_template_branch_name,
)
template_update = TemplateUpdate(
template=template,
Expand All @@ -198,7 +206,9 @@ def apply_template_and_add(
[template_update],
transaction,
merged_branch_name=merged_branch_name,
base_merged_branch_name=base_merged_branch_name,
template_branch_name=template_branch_name,
base_template_branch_name=base_template_branch_name,
no_input=no_input,
full_rerender=False,
renderer=renderer,
Expand Down Expand Up @@ -256,7 +266,9 @@ def init_project_and_add_to_branches(
"Initialized flexlate project",
path,
merged_branch_name=merged_branch_name,
base_merged_branch_name=merged_branch_name,
template_branch_name=template_branch_name,
base_template_branch_name=template_branch_name,
)

print_styled(
Expand Down
55 changes: 49 additions & 6 deletions flexlate/branch_update.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from pathlib import Path
from typing import Callable, Union, List
from typing import Callable, Union, List, Optional

from git import Repo
from git import Repo, repo, Head

from flexlate.constants import DEFAULT_MERGED_BRANCH_NAME, DEFAULT_TEMPLATE_BRANCH_NAME
from flexlate.ext_git import (
Expand All @@ -11,6 +11,8 @@
fast_forward_branch_without_checkout,
checked_out_template_branch,
merge_branch_into_current,
abort_merge,
reset_branch_to_commit_without_checkout,
)
from flexlate.path_ops import make_func_that_creates_cwd_and_out_root_before_running
from flexlate.transactions.transaction import (
Expand All @@ -25,7 +27,9 @@ def modify_files_via_branches_and_temp_repo(
commit_message: str,
out_root: Path,
merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
base_merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
base_template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
):
cwd = os.getcwd()
current_branch = repo.active_branch
Expand All @@ -36,15 +40,19 @@ def modify_files_via_branches_and_temp_repo(

# Update the template only branch with the new template
with temp_repo_that_pushes_to_branch( # type: ignore
repo, branch_name=template_branch_name
repo,
branch_name=template_branch_name,
base_branch_name=base_template_branch_name,
) as temp_repo:
make_dirs_add_operation(Path(temp_repo.working_dir)) # type: ignore
stage_and_commit_all(temp_repo, commit_message)

# Bring the change into the merged branch
# Update with changes from the main repo
fast_forward_branch_without_checkout(repo, merged_branch_name, current_branch.name)
with checked_out_template_branch(repo, branch_name=merged_branch_name):
with checked_out_template_branch(
repo, branch_name=merged_branch_name, base_branch_name=base_merged_branch_name
):
# Update with the new template
merge_branch_into_current(repo, template_branch_name)

Expand All @@ -67,20 +75,55 @@ def undo_transaction_in_flexlate_branches(
repo: Repo,
transaction: FlexlateTransaction,
merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
base_merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
base_template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
):
# Reset the template only branch to the appropriate commit
with temp_repo_that_pushes_to_branch( # type: ignore
repo, branch_name=template_branch_name, force_push=True
repo,
branch_name=template_branch_name,
base_branch_name=base_template_branch_name,
force_push=True,
) as temp_repo:
reset_last_transaction(
temp_repo, transaction, merged_branch_name, template_branch_name
)

# Reset the merged template branch to the appropriate commit
with temp_repo_that_pushes_to_branch( # type: ignore
repo, branch_name=merged_branch_name, force_push=True
repo,
branch_name=merged_branch_name,
base_branch_name=base_merged_branch_name,
force_push=True,
) as temp_repo:
reset_last_transaction(
temp_repo, transaction, merged_branch_name, template_branch_name
)


def abort_merge_and_reset_flexlate_branches(
repo: Repo,
current_branch: Head,
merged_branch_sha: Optional[str] = None,
template_branch_sha: Optional[str] = None,
merged_branch_name: str = DEFAULT_MERGED_BRANCH_NAME,
template_branch_name: str = DEFAULT_TEMPLATE_BRANCH_NAME,
):
abort_merge(repo)
current_branch.checkout()
reset_branch_to_commit_without_checkout(repo, merged_branch_name, merged_branch_sha)
reset_branch_to_commit_without_checkout(
repo, template_branch_name, template_branch_sha
)


def get_flexlate_branch_name(repo: Repo, base_branch_name: str) -> str:
current_branch = repo.active_branch.name
return get_flexlate_branch_name_for_feature_branch(current_branch, base_branch_name)


def get_flexlate_branch_name_for_feature_branch(
feature_branch: str, base_branch_name: str
) -> str:
return f"{base_branch_name}-{feature_branch}"
42 changes: 35 additions & 7 deletions flexlate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
show_default=False,
)
QUIET_OPTION = typer.Option(False, "--quiet", "-q", show_default=False)
PROJECT_PATH_ARGUMENT = typer.Argument(Path("."), help=PROJECT_PATH_DOC)
PROJECT_PATH_OPTION = typer.Option(Path("."), help=PROJECT_PATH_DOC)


@add_cli.command(name="source")
Expand Down Expand Up @@ -151,7 +153,7 @@ def remove_template_output(

@cli.command(name="init")
def init_project(
path: Path = typer.Argument(Path("."), help=PROJECT_PATH_DOC),
path: Path = PROJECT_PATH_ARGUMENT,
default_add_mode: AddMode = typer.Option(
AddMode.LOCAL,
"--add-mode",
Expand Down Expand Up @@ -193,7 +195,7 @@ def init_project_from(
...,
help=f"A template source path to initialize the project from. {TEMPLATE_SOURCE_EXTRA_DOC}",
),
path: Path = typer.Argument(Path("."), help=PROJECT_PATH_DOC),
path: Path = PROJECT_PATH_ARGUMENT,
version: Optional[str] = VERSION_OPTION,
folder_name: str = typer.Option(
"project",
Expand Down Expand Up @@ -244,7 +246,7 @@ def update_templates(
),
no_input: bool = NO_INPUT_OPTION,
quiet: bool = QUIET_OPTION,
path: Path = typer.Option(Path("."), help=PROJECT_PATH_DOC),
path: Path = PROJECT_PATH_OPTION,
):
"""
Updates applied templates in the project to the newest versions
Expand All @@ -260,23 +262,22 @@ def undo(
1,
help="Number of flexlate operations to undo",
),
path: Path = typer.Option(Path("."), help=PROJECT_PATH_DOC),
path: Path = PROJECT_PATH_OPTION,
quiet: bool = QUIET_OPTION,
):
"""
Undoes the last flexlate operation, like ctrl/cmd + z for flexlate.
Note that this modifies the git history, discarding the last commits.
It has protections against deleting user commits but you should only
use this on a feature branch.
:return:
"""
app = Flexlate(quiet=quiet)
app.undo(num_operations=num_operations, project_path=path)


@cli.command(name="sync")
def sync(
path: Path = typer.Argument(Path("."), help=PROJECT_PATH_DOC),
path: Path = PROJECT_PATH_ARGUMENT,
no_input: bool = NO_INPUT_OPTION,
quiet: bool = QUIET_OPTION,
):
Expand All @@ -288,11 +289,38 @@ def sync(
specific command for it: manually change the version and run sync.
Note: Be sure to commit your changes before running sync
:return:
"""
app = Flexlate(quiet=quiet)
app.sync(no_input=no_input, project_path=path)


@cli.command(name="merge")
def merge(
branch_name: Optional[str] = typer.Argument(
None,
help="Optional name of feature branch for which to merge "
"corresponding flexlate branches. Defaults to current branch",
),
delete: bool = typer.Option(
True,
"--no-delete",
"-n",
help="Pass to prevent deleting feature flexlate branches after merge",
show_default=False,
),
path: Path = PROJECT_PATH_OPTION,
quiet: bool = QUIET_OPTION,
):
"""
Merges feature flexlate branches into the main flexlate branches
Feature flexlate branches should be merged into main flexlate branches when
the corresponding feature branch is merged into the repo's main branch. This
command provides a convenient way to do so.
"""
app = Flexlate(quiet=quiet)
app.merge_flexlate_branches(branch_name, delete=delete, project_path=path)


if __name__ == "__main__":
cli()
Loading

0 comments on commit 5aecde4

Please sign in to comment.