diff --git a/src/algokit/cli/init.py b/src/algokit/cli/init.py index 72b71344..ab845f77 100644 --- a/src/algokit/cli/init.py +++ b/src/algokit/cli/init.py @@ -380,10 +380,11 @@ def git(*args: str, bad_exit_warn_message: str) -> bool: logger.warning(bad_exit_warn_message) return success - if git("init", "--initial-branch=main", bad_exit_warn_message="Failed to initialise git repository"): - if git("add", "--all", bad_exit_warn_message="Failed to add generated project files"): - if git("commit", "-m", commit_message, bad_exit_warn_message="Initial commit failed"): - logger.info("🎉 Performed initial git commit successfully! 🎉") + if git("init", bad_exit_warn_message="Failed to initialise git repository"): + if git("checkout", "-b", "main", bad_exit_warn_message="Failed to name initial branch"): + if git("add", "--all", bad_exit_warn_message="Failed to add generated project files"): + if git("commit", "-m", commit_message, bad_exit_warn_message="Initial commit failed"): + logger.info("🎉 Performed initial git commit successfully! 🎉") def _get_run_bootstrap() -> bool: diff --git a/tests/init/test_init.py b/tests/init/test_init.py index ea8d1b4d..26ed18ac 100644 --- a/tests/init/test_init.py +++ b/tests/init/test_init.py @@ -18,10 +18,11 @@ GIT_BUNDLE_PATH = PARENT_DIRECTORY / "copier-helloworld.bundle" -def make_output_scrubber(**extra_tokens: str) -> Scrubber: +def make_output_scrubber(*extra_scrubbers: Callable[[str], str], **extra_tokens: str) -> Scrubber: default_tokens = {"test_parent_directory": str(PARENT_DIRECTORY)} tokens = default_tokens | extra_tokens return combine_scrubbers( + *extra_scrubbers, click.unstyle, TokenScrubber(tokens=tokens), TokenScrubber(tokens={"test_parent_directory": str(PARENT_DIRECTORY).replace("\\", "/")}), @@ -167,7 +168,7 @@ def test_init_minimal_interaction_required_yes_git_no_network( git_initial_commit_hash = git_rev_list.stdout[:7] verify( result.output, - scrubber=make_output_scrubber(git_initial_commit_hash=git_initial_commit_hash), + scrubber=make_output_scrubber(_remove_git_hints, git_initial_commit_hash=git_initial_commit_hash), ) @@ -371,7 +372,7 @@ def test_init_ask_about_git(tmp_path_factory: TempPathFactory, mock_questionary_ git_initial_commit_hash = git_rev_list.stdout[:7] verify( result.output, - scrubber=make_output_scrubber(git_initial_commit_hash=git_initial_commit_hash), + scrubber=make_output_scrubber(_remove_git_hints, git_initial_commit_hash=git_initial_commit_hash), ) @@ -516,3 +517,9 @@ def test_init_with_custom_env(tmp_path_factory: TempPathFactory): ), scrubber=make_output_scrubber(), ) + + +def _remove_git_hints(output: str) -> str: + git_init_hint_prefix = "DEBUG: git: hint:" + lines = [line for line in output.splitlines() if not line.startswith(git_init_hint_prefix)] + return "\n".join(lines) diff --git a/tests/init/test_init.test_init_ask_about_git.approved.txt b/tests/init/test_init.test_init_ask_about_git.approved.txt index b98496a0..d2a195f6 100644 --- a/tests/init/test_init.test_init_ask_about_git.approved.txt +++ b/tests/init/test_init.test_init_ask_about_git.approved.txt @@ -6,8 +6,10 @@ DEBUG: Template initialisation complete, final clone URL = {test_parent_director Executed `algokit bootstrap all` in {current_working_directory}/myapp DEBUG: Running 'git rev-parse --show-toplevel' in '{current_working_directory}/myapp' DEBUG: git: fatal: not a git repository (or any of the parent directories): .git -DEBUG: Running 'git init --initial-branch=main' in '{current_working_directory}/myapp' +DEBUG: Running 'git init' in '{current_working_directory}/myapp' DEBUG: git: Initialized empty Git repository in {current_working_directory}/myapp/.git/ +DEBUG: Running 'git checkout -b main' in '{current_working_directory}/myapp' +DEBUG: git: Switched to a new branch 'main' DEBUG: Running 'git add --all' in '{current_working_directory}/myapp' DEBUG: Running 'git commit -m Project initialised with AlgoKit CLI using template: {test_parent_directory}/copier-helloworld.bundle' in '{current_working_directory}/myapp' DEBUG: git: [main (root-commit) {git_initial_commit_hash}] Project initialised with AlgoKit CLI using template: {test_parent_directory}/copier-helloworld.bundle diff --git a/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt b/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt index b98496a0..d2a195f6 100644 --- a/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt +++ b/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt @@ -6,8 +6,10 @@ DEBUG: Template initialisation complete, final clone URL = {test_parent_director Executed `algokit bootstrap all` in {current_working_directory}/myapp DEBUG: Running 'git rev-parse --show-toplevel' in '{current_working_directory}/myapp' DEBUG: git: fatal: not a git repository (or any of the parent directories): .git -DEBUG: Running 'git init --initial-branch=main' in '{current_working_directory}/myapp' +DEBUG: Running 'git init' in '{current_working_directory}/myapp' DEBUG: git: Initialized empty Git repository in {current_working_directory}/myapp/.git/ +DEBUG: Running 'git checkout -b main' in '{current_working_directory}/myapp' +DEBUG: git: Switched to a new branch 'main' DEBUG: Running 'git add --all' in '{current_working_directory}/myapp' DEBUG: Running 'git commit -m Project initialised with AlgoKit CLI using template: {test_parent_directory}/copier-helloworld.bundle' in '{current_working_directory}/myapp' DEBUG: git: [main (root-commit) {git_initial_commit_hash}] Project initialised with AlgoKit CLI using template: {test_parent_directory}/copier-helloworld.bundle