diff --git a/src/subcommand/init_subcommand.cpp b/src/subcommand/init_subcommand.cpp index 2a3a703..bd1c238 100644 --- a/src/subcommand/init_subcommand.cpp +++ b/src/subcommand/init_subcommand.cpp @@ -1,4 +1,4 @@ -// #include +#include #include "init_subcommand.hpp" #include "../wrapper/repository_wrapper.hpp" @@ -12,11 +12,39 @@ init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app) sub->add_option("directory", m_directory, "info about directory arg") ->check(CLI::ExistingDirectory | CLI::NonexistentPath) ->default_val(get_current_git_path()); + sub->add_option("-b,--initial-branch", m_branch, "Use for the initial branch in the newly created repository. If not specified, fall back to the default name."); sub->callback([this]() { this->run(); }); } void init_subcommand::run() { - repository_wrapper::init(m_directory, m_bare); + std::filesystem::path target_dir = m_directory; + bool reinit = std::filesystem::exists(target_dir / ".git" / "HEAD"); + + repository_wrapper repo = [this]() { + if (m_branch.empty()) + { + return repository_wrapper::init(m_directory, m_bare); + } + else + { + git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; + if (m_bare) { opts.flags |= GIT_REPOSITORY_INIT_BARE; } + opts.initial_head = m_branch.c_str(); + + return repository_wrapper::init_ext(m_directory, &opts); + } + }(); + + std::string path = repo.path(); + + if (reinit) + { + std::cout << "Reinitialized existing Git repository in " << path <git_path(); - std::string shallow_path = git_path + "shallow"; + std::string path = this->path(); + std::string shallow_path = path + "shallow"; std::vector boundaries_list; std::ifstream f(shallow_path); diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 18a25e7..a62f8c2 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -40,10 +40,11 @@ class repository_wrapper : public wrapper_base repository_wrapper& operator=(repository_wrapper&&) noexcept = default; static repository_wrapper init(std::string_view directory, bool bare); + static repository_wrapper init_ext(std::string_view repo_path, git_repository_init_options* opts); static repository_wrapper open(std::string_view directory); static repository_wrapper clone(std::string_view url, std::string_view path, const git_clone_options& opts); - std::string git_path() const; + std::string path() const; git_repository_state_t state() const; void state_cleanup(); diff --git a/test/conftest.py b/test/conftest.py index e6e6003..138f87d 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -52,7 +52,7 @@ def commit_env_config(monkeypatch): @pytest.fixture def repo_init_with_commit(commit_env_config, git2cpp_path, tmp_path, run_in_tmp_path): - cmd_init = [git2cpp_path, "init", "."] + cmd_init = [git2cpp_path, "init", ".", "-b", "main"] p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path, text=True) assert p_init.returncode == 0 @@ -70,7 +70,7 @@ def repo_init_with_commit(commit_env_config, git2cpp_path, tmp_path, run_in_tmp_ @pytest.fixture(scope="session") def private_test_repo(): - # Fixture containing everything needed to access private github repo. + # Fixture containing everything needed to access private github repo. # GIT2CPP_TEST_PRIVATE_TOKEN is the fine-grained Personal Access Token for private test repo. # If this is not available as an environment variable, tests that use this fixture are skipped. token = os.getenv("GIT2CPP_TEST_PRIVATE_TOKEN") @@ -82,5 +82,5 @@ def private_test_repo(): "repo_name": repo_name, "http_url": f"http://github.com/{org_name}/{repo_name}", "https_url": f"https://github.com/{org_name}/{repo_name}", - "token": token + "token": token, } diff --git a/test/test_checkout.py b/test/test_checkout.py index b9ac96a..3383c9a 100644 --- a/test/test_checkout.py +++ b/test/test_checkout.py @@ -6,14 +6,6 @@ def test_checkout(repo_init_with_commit, git2cpp_path, tmp_path): assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - create_cmd = [git2cpp_path, "branch", "foregone"] p_create = subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_create.returncode == 0 @@ -28,27 +20,19 @@ def test_checkout(repo_init_with_commit, git2cpp_path, tmp_path): branch_cmd = [git2cpp_path, "branch"] p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_branch.returncode == 0 - assert p_branch.stdout == f"* foregone\n {default_branch}\n" + assert p_branch.stdout == "* foregone\n main\n" - checkout_cmd[2] = default_branch + checkout_cmd[2] = "main" p_checkout2 = subprocess.run( checkout_cmd, capture_output=True, cwd=tmp_path, text=True ) assert p_checkout2.returncode == 0 - assert f"Switched to branch '{default_branch}'" in p_checkout2.stdout + assert "Switched to branch 'main'" in p_checkout2.stdout def test_checkout_b(repo_init_with_commit, git2cpp_path, tmp_path): assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - checkout_cmd = [git2cpp_path, "checkout", "-b", "foregone"] p_checkout = subprocess.run( checkout_cmd, capture_output=True, cwd=tmp_path, text=True @@ -59,16 +43,16 @@ def test_checkout_b(repo_init_with_commit, git2cpp_path, tmp_path): branch_cmd = [git2cpp_path, "branch"] p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_branch.returncode == 0 - assert p_branch.stdout == f"* foregone\n {default_branch}\n" + assert p_branch.stdout == "* foregone\n main\n" checkout_cmd.remove("-b") - checkout_cmd[2] = default_branch + checkout_cmd[2] = "main" p_checkout2 = subprocess.run(checkout_cmd, cwd=tmp_path, text=True) assert p_checkout2.returncode == 0 p_branch2 = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_branch2.returncode == 0 - assert p_branch2.stdout == f" foregone\n* {default_branch}\n" + assert p_branch2.stdout == " foregone\n* main\n" def test_checkout_B_force_create(repo_init_with_commit, git2cpp_path, tmp_path): @@ -143,14 +127,6 @@ def test_checkout_refuses_overwrite( initial_file = tmp_path / "initial.txt" assert (initial_file).exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create a new branch and switch to it create_cmd = [git2cpp_path, "checkout", "-b", "newbranch"] p_create = subprocess.run(create_cmd, capture_output=True, cwd=tmp_path, text=True) @@ -166,14 +142,14 @@ def test_checkout_refuses_overwrite( subprocess.run(commit_cmd, cwd=tmp_path, text=True) # Switch back to default branch - checkout_default_cmd = [git2cpp_path, "checkout", default_branch] + checkout_default_cmd = [git2cpp_path, "checkout", "main"] p_default = subprocess.run( checkout_default_cmd, capture_output=True, cwd=tmp_path, text=True ) assert p_default.returncode == 0 # Now modify initial.txt locally (unstaged) on default branch - initial_file.write_text(f"Local modification on {default_branch}") + initial_file.write_text("Local modification on main") # Try to checkout newbranch checkout_cmd = [git2cpp_path, "checkout"] @@ -201,7 +177,7 @@ def test_checkout_refuses_overwrite( p_branch = subprocess.run( branch_cmd, capture_output=True, cwd=tmp_path, text=True ) - assert f"* {default_branch}" in p_branch.stdout + assert "* main" in p_branch.stdout else: assert "Switched to branch 'newbranch'" in p_checkout.stdout diff --git a/test/test_init.py b/test/test_init.py index 1ba018e..9a66188 100644 --- a/test/test_init.py +++ b/test/test_init.py @@ -7,10 +7,10 @@ def test_init_in_directory(git2cpp_path, tmp_path): assert list(tmp_path.iterdir()) == [] cmd = [git2cpp_path, "init", "--bare", str(tmp_path)] - p = subprocess.run(cmd, capture_output=True) + p = subprocess.run(cmd, capture_output=True, text=True) assert p.returncode == 0 - assert p.stdout == b"" - assert p.stderr == b"" + assert p.stdout.startswith("Initialized empty Git repository in ") + assert p.stdout.strip().endswith("/") assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [ "HEAD", @@ -31,10 +31,10 @@ def test_init_in_cwd(git2cpp_path, tmp_path, run_in_tmp_path): assert Path.cwd() == tmp_path cmd = [git2cpp_path, "init", "--bare"] - p = subprocess.run(cmd, capture_output=True) + p = subprocess.run(cmd, capture_output=True, text=True) assert p.returncode == 0 - assert p.stdout == b"" - assert p.stderr == b"" + assert p.stdout.startswith("Initialized empty Git repository in ") + assert p.stdout.strip().endswith("/") assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [ "HEAD", @@ -52,12 +52,12 @@ def test_init_in_cwd(git2cpp_path, tmp_path, run_in_tmp_path): def test_init_not_bare(git2cpp_path, tmp_path): # tmp_path exists and is empty. assert list(tmp_path.iterdir()) == [] - + assert not (tmp_path / ".git" / "HEAD").exists() cmd = [git2cpp_path, "init", "."] - p = subprocess.run(cmd, capture_output=True, cwd=tmp_path) + p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) assert p.returncode == 0 - assert p.stdout == b"" - assert p.stderr == b"" + assert p.stdout.startswith("Initialized empty Git repository in ") + assert p.stdout.strip().endswith(".git/") # Directory contains just .git directory. assert sorted(map(lambda path: path.name, tmp_path.iterdir())) == [".git"] @@ -79,6 +79,19 @@ def test_init_not_bare(git2cpp_path, tmp_path): assert b"does not have any commits yet" in p.stdout +def test_init_reinitializes_existing_repo_message(git2cpp_path, tmp_path): + cmd = [git2cpp_path, "init", "."] + + p1 = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) + assert p1.returncode == 0 + + p2 = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) + assert p2.returncode == 0 + assert p2.stderr == "" + assert p2.stdout.startswith("Reinitialized existing Git repository in ") + assert p2.stdout.strip().endswith("/.git/") + + def test_error_on_unknown_option(git2cpp_path): cmd = [git2cpp_path, "init", "--unknown"] p = subprocess.run(cmd, capture_output=True) @@ -93,3 +106,52 @@ def test_error_on_repeated_directory(git2cpp_path): assert p.returncode == 109 assert p.stdout == b"" assert p.stderr.startswith(b"The following argument was not expected: def") + + +def test_init_creates_missing_parent_directories(git2cpp_path, tmp_path): + # Parent "does-not-exist" does not exist yet. + repo_dir = tmp_path / "does-not-exist" / "repo" + assert not repo_dir.parent.exists() + + cmd = [git2cpp_path, "init", "--bare", str(repo_dir)] + p = subprocess.run(cmd, capture_output=True, text=True) + + assert p.returncode == 0 + assert p.stdout.startswith("Initialized empty Git repository in ") + assert p.stdout.strip().endswith("/") + assert ".git" not in p.stdout + + assert repo_dir.exists() + assert sorted(p.name for p in repo_dir.iterdir()) == [ + "HEAD", + "config", + "description", + "hooks", + "info", + "objects", + "refs", + ] + + +def test_init_initial_branch_non_bare(git2cpp_path, tmp_path): + cmd = [git2cpp_path, "init", "-b", "main", "."] + p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) + assert p.returncode == 0 + assert p.stderr == "" + assert p.stdout.startswith("Initialized empty Git repository in ") + assert p.stdout.strip().endswith("/.git/") + + head = (tmp_path / ".git" / "HEAD").read_text() + assert "refs/heads/main" in head + + +def test_init_initial_branch_bare(git2cpp_path, tmp_path): + cmd = [git2cpp_path, "init", "--bare", "-b", "main", str(tmp_path)] + p = subprocess.run(cmd, capture_output=True, text=True) + assert p.returncode == 0 + assert p.stderr == "" + assert p.stdout.startswith("Initialized empty Git repository in ") + assert p.stdout.strip().endswith("/") + + head = (tmp_path / "HEAD").read_text() + assert "refs/heads/main" in head diff --git a/test/test_merge.py b/test/test_merge.py index a805ff3..f6d36a3 100644 --- a/test/test_merge.py +++ b/test/test_merge.py @@ -10,14 +10,6 @@ def test_merge_fast_forward( ): assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - checkout_cmd = [git2cpp_path, "checkout", "-b", "foregone"] p_checkout = subprocess.run( checkout_cmd, capture_output=True, cwd=tmp_path, text=True @@ -35,7 +27,7 @@ def test_merge_fast_forward( p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_commit.returncode == 0 - checkout_cmd_2 = [git2cpp_path, "checkout", default_branch] + checkout_cmd_2 = [git2cpp_path, "checkout", "main"] p_checkout_2 = subprocess.run( checkout_cmd_2, capture_output=True, cwd=tmp_path, text=True ) @@ -64,14 +56,6 @@ def test_merge_fast_forward( def test_merge_commit(repo_init_with_commit, commit_env_config, git2cpp_path, tmp_path): assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - checkout_cmd = [git2cpp_path, "checkout", "-b", "foregone"] p_checkout = subprocess.run( checkout_cmd, capture_output=True, cwd=tmp_path, text=True @@ -89,7 +73,7 @@ def test_merge_commit(repo_init_with_commit, commit_env_config, git2cpp_path, tm p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_commit.returncode == 0 - checkout_cmd_2 = [git2cpp_path, "checkout", default_branch] + checkout_cmd_2 = [git2cpp_path, "checkout", "main"] p_checkout_2 = subprocess.run( checkout_cmd_2, capture_output=True, cwd=tmp_path, text=True ) @@ -135,14 +119,6 @@ def test_merge_conflict( ): assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - checkout_cmd = [git2cpp_path, "checkout", "-b", "foregone"] p_checkout = subprocess.run( checkout_cmd, capture_output=True, cwd=tmp_path, text=True @@ -163,7 +139,7 @@ def test_merge_conflict( p_commit = subprocess.run(commit_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_commit.returncode == 0 - checkout_cmd_2 = [git2cpp_path, "checkout", default_branch] + checkout_cmd_2 = [git2cpp_path, "checkout", "main"] p_checkout_2 = subprocess.run( checkout_cmd_2, capture_output=True, cwd=tmp_path, text=True ) diff --git a/test/test_rebase.py b/test/test_rebase.py index 06d39d9..add183b 100644 --- a/test/test_rebase.py +++ b/test/test_rebase.py @@ -7,14 +7,6 @@ def test_rebase_basic(repo_init_with_commit, commit_env_config, git2cpp_path, tm """Test basic rebase operation with fast-forward""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create a feature branch checkout_cmd = [git2cpp_path, "checkout", "-b", "feature"] p_checkout = subprocess.run( @@ -35,7 +27,7 @@ def test_rebase_basic(repo_init_with_commit, commit_env_config, git2cpp_path, tm assert p_commit.returncode == 0 # Go back to master and create another commit - checkout_master_cmd = [git2cpp_path, "checkout", default_branch] + checkout_master_cmd = [git2cpp_path, "checkout", "main"] p_checkout_master = subprocess.run( checkout_master_cmd, capture_output=True, cwd=tmp_path, text=True ) @@ -61,7 +53,7 @@ def test_rebase_basic(repo_init_with_commit, commit_env_config, git2cpp_path, tm ) assert p_checkout_feature.returncode == 0 - rebase_cmd = [git2cpp_path, "rebase", default_branch] + rebase_cmd = [git2cpp_path, "rebase", "main"] p_rebase = subprocess.run(rebase_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_rebase.returncode == 0 assert "Successfully rebased" in p_rebase.stdout @@ -78,14 +70,6 @@ def test_rebase_multiple_commits( """Test rebase with multiple commits""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch with multiple commits checkout_cmd = [git2cpp_path, "checkout", "-b", "feature"] p_checkout = subprocess.run( @@ -118,7 +102,7 @@ def test_rebase_multiple_commits( subprocess.run(commit_cmd_3, cwd=tmp_path, text=True) # Go to master and add a commit - checkout_master_cmd = [git2cpp_path, "checkout", default_branch] + checkout_master_cmd = [git2cpp_path, "checkout", "main"] subprocess.run(checkout_master_cmd, cwd=tmp_path) master_file = tmp_path / "master_file.txt" @@ -130,7 +114,7 @@ def test_rebase_multiple_commits( checkout_feature_cmd = [git2cpp_path, "checkout", "feature"] subprocess.run(checkout_feature_cmd, cwd=tmp_path) - rebase_cmd = [git2cpp_path, "rebase", default_branch] + rebase_cmd = [git2cpp_path, "rebase", "main"] p_rebase = subprocess.run(rebase_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_rebase.returncode == 0 assert "Rebasing 3 commit(s)" in p_rebase.stdout @@ -149,14 +133,6 @@ def test_rebase_with_conflicts( """Test rebase with conflicts""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch checkout_cmd = [git2cpp_path, "checkout", "-b", "feature"] subprocess.run(checkout_cmd, capture_output=True, cwd=tmp_path, text=True) @@ -168,7 +144,7 @@ def test_rebase_with_conflicts( subprocess.run([git2cpp_path, "commit", "-m", "feature commit"], cwd=tmp_path) # Go to master and create conflicting commit - checkout_master_cmd = [git2cpp_path, "checkout", default_branch] + checkout_master_cmd = [git2cpp_path, "checkout", "main"] subprocess.run(checkout_master_cmd, cwd=tmp_path) conflict_file.write_text("master content") @@ -179,7 +155,7 @@ def test_rebase_with_conflicts( checkout_feature_cmd = [git2cpp_path, "checkout", "feature"] subprocess.run(checkout_feature_cmd, cwd=tmp_path) - rebase_cmd = [git2cpp_path, "rebase", default_branch] + rebase_cmd = [git2cpp_path, "rebase", "main"] p_rebase = subprocess.run(rebase_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_rebase.returncode == 0 assert "Conflicts detected" in p_rebase.stdout @@ -192,14 +168,6 @@ def test_rebase_abort(repo_init_with_commit, commit_env_config, git2cpp_path, tm """Test rebase abort after conflicts""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch checkout_cmd = [git2cpp_path, "checkout", "-b", "feature"] subprocess.run(checkout_cmd, cwd=tmp_path) @@ -211,14 +179,14 @@ def test_rebase_abort(repo_init_with_commit, commit_env_config, git2cpp_path, tm subprocess.run([git2cpp_path, "commit", "-m", "feature commit"], cwd=tmp_path) # Go to master and create conflicting commit - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) conflict_file.write_text("master content") subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) subprocess.run([git2cpp_path, "commit", "-m", "master commit"], cwd=tmp_path) # Rebase and get conflict subprocess.run([git2cpp_path, "checkout", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "rebase", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "rebase", "main"], cwd=tmp_path) # Abort the rebase abort_cmd = [git2cpp_path, "rebase", "--abort"] @@ -236,14 +204,6 @@ def test_rebase_continue( """Test rebase continue after resolving conflicts""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch subprocess.run([git2cpp_path, "checkout", "-b", "feature"], cwd=tmp_path) @@ -254,14 +214,14 @@ def test_rebase_continue( subprocess.run([git2cpp_path, "commit", "-m", "feature commit"], cwd=tmp_path) # Go to master and create conflicting commit - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) conflict_file.write_text("master content") subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) subprocess.run([git2cpp_path, "commit", "-m", "master commit"], cwd=tmp_path) # Rebase and get conflict subprocess.run([git2cpp_path, "checkout", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "rebase", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "rebase", "main"], cwd=tmp_path) # Resolve conflict conflict_file.write_text("resolved content") @@ -283,14 +243,6 @@ def test_rebase_skip(repo_init_with_commit, commit_env_config, git2cpp_path, tmp """Test rebase skip to skip current commit""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch subprocess.run([git2cpp_path, "checkout", "-b", "feature"], cwd=tmp_path) @@ -301,14 +253,14 @@ def test_rebase_skip(repo_init_with_commit, commit_env_config, git2cpp_path, tmp subprocess.run([git2cpp_path, "commit", "-m", "feature commit"], cwd=tmp_path) # Go to master and create conflicting commit - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) conflict_file.write_text("master content") subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) subprocess.run([git2cpp_path, "commit", "-m", "master commit"], cwd=tmp_path) # Rebase and get conflict subprocess.run([git2cpp_path, "checkout", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "rebase", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "rebase", "main"], cwd=tmp_path) # Skip the conflicting commit skip_cmd = [git2cpp_path, "rebase", "--skip"] @@ -321,14 +273,6 @@ def test_rebase_quit(repo_init_with_commit, commit_env_config, git2cpp_path, tmp """Test rebase quit to cleanup state without resetting HEAD""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch subprocess.run([git2cpp_path, "checkout", "-b", "feature"], cwd=tmp_path) @@ -339,14 +283,14 @@ def test_rebase_quit(repo_init_with_commit, commit_env_config, git2cpp_path, tmp subprocess.run([git2cpp_path, "commit", "-m", "feature commit"], cwd=tmp_path) # Create conflict on master - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) conflict_file.write_text("master content") subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) subprocess.run([git2cpp_path, "commit", "-m", "master commit"], cwd=tmp_path) # Start rebase subprocess.run([git2cpp_path, "checkout", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "rebase", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "rebase", "main"], cwd=tmp_path) # Quit rebase quit_cmd = [git2cpp_path, "rebase", "--quit"] @@ -360,14 +304,6 @@ def test_rebase_onto(repo_init_with_commit, commit_env_config, git2cpp_path, tmp """Test rebase with --onto option""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create first branch subprocess.run([git2cpp_path, "checkout", "-b", "branch1"], cwd=tmp_path) file1 = tmp_path / "file1.txt" @@ -383,7 +319,7 @@ def test_rebase_onto(repo_init_with_commit, commit_env_config, git2cpp_path, tmp subprocess.run([git2cpp_path, "commit", "-m", "branch2 commit"], cwd=tmp_path) # Create target branch from master - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) subprocess.run([git2cpp_path, "checkout", "-b", "target"], cwd=tmp_path) target_file = tmp_path / "target.txt" target_file.write_text("target") @@ -433,14 +369,6 @@ def test_rebase_already_in_progress_error( """Test that starting rebase when one is in progress fails""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create feature branch with conflict subprocess.run([git2cpp_path, "checkout", "-b", "feature"], cwd=tmp_path) conflict_file = tmp_path / "conflict.txt" @@ -449,17 +377,17 @@ def test_rebase_already_in_progress_error( subprocess.run([git2cpp_path, "commit", "-m", "feature"], cwd=tmp_path) # Create conflict on master - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) - conflict_file.write_text(default_branch) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) + conflict_file.write_text("main") subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) - subprocess.run([git2cpp_path, "commit", "-m", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "commit", "-m", "main"], cwd=tmp_path) # Start rebase with conflict subprocess.run([git2cpp_path, "checkout", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "rebase", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "rebase", "main"], cwd=tmp_path) # Try to start another rebase - rebase_cmd = [git2cpp_path, "rebase", default_branch] + rebase_cmd = [git2cpp_path, "rebase", "main"] p_rebase = subprocess.run(rebase_cmd, capture_output=True, cwd=tmp_path, text=True) assert p_rebase.returncode != 0 assert ( @@ -491,14 +419,6 @@ def test_rebase_continue_with_unresolved_conflicts( """Test that --continue with unresolved conflicts fails""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Create conflict scenario subprocess.run([git2cpp_path, "checkout", "-b", "feature"], cwd=tmp_path) conflict_file = tmp_path / "conflict.txt" @@ -506,14 +426,14 @@ def test_rebase_continue_with_unresolved_conflicts( subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) subprocess.run([git2cpp_path, "commit", "-m", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "checkout", default_branch], cwd=tmp_path) - conflict_file.write_text(default_branch) + subprocess.run([git2cpp_path, "checkout", "main"], cwd=tmp_path) + conflict_file.write_text("main") subprocess.run([git2cpp_path, "add", "conflict.txt"], cwd=tmp_path) - subprocess.run([git2cpp_path, "commit", "-m", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "commit", "-m", "main"], cwd=tmp_path) # Start rebase subprocess.run([git2cpp_path, "checkout", "feature"], cwd=tmp_path) - subprocess.run([git2cpp_path, "rebase", default_branch], cwd=tmp_path) + subprocess.run([git2cpp_path, "rebase", "main"], cwd=tmp_path) # Try to continue without resolving continue_cmd = [git2cpp_path, "rebase", "--continue"] diff --git a/test/test_status.py b/test/test_status.py index 9a27441..d7a02a0 100644 --- a/test/test_status.py +++ b/test/test_status.py @@ -12,14 +12,6 @@ def test_status_new_file( ): assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - p = tmp_path / "mook_file.txt" # Untracked files p.write_text("") @@ -42,7 +34,7 @@ def test_status_new_file( p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True, check=True) if (long_flag == "--long") or ((long_flag == "") & (short_flag == "")): - assert f"On branch {default_branch}" in p.stdout + assert "On branch main" in p.stdout assert "Changes not staged for commit" in p.stdout assert "Untracked files" in p.stdout assert "deleted" in p.stdout @@ -101,23 +93,14 @@ def test_status_new_repo(git2cpp_path, tmp_path, run_in_tmp_path): # tmp_path exists and is empty. assert list(tmp_path.iterdir()) == [] - cmd = [git2cpp_path, "init"] + cmd = [git2cpp_path, "init", "-b", "main"] p = subprocess.run(cmd, cwd=tmp_path) assert p.returncode == 0 - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - print(default_branch) - cmd_status = [git2cpp_path, "status"] p_status = subprocess.run(cmd_status, capture_output=True, cwd=tmp_path, text=True) assert p_status.returncode == 0 - assert f"On branch {default_branch}" in p_status.stdout + assert "On branch main" in p_status.stdout assert "No commit yet" in p_status.stdout assert "Nothing to commit, working tree clean" in p_status.stdout @@ -126,19 +109,11 @@ def test_status_clean_tree(repo_init_with_commit, git2cpp_path, tmp_path): """Test 'Nothing to commit, working tree clean' message""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - cmd = [git2cpp_path, "status"] p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True) assert p.returncode == 0 - assert f"On branch {default_branch}" in p.stdout + assert "On branch main" in p.stdout assert "Nothing to commit, working tree clean" in p.stdout @@ -308,15 +283,7 @@ def test_status_ahead_of_upstream( repo_path.mkdir() # Initialize repo - subprocess.run([git2cpp_path, "init"], cwd=repo_path, check=True) - - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=repo_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented + subprocess.run([git2cpp_path, "init", "-b", "main"], cwd=repo_path, check=True) # Create initial commit test_file = repo_path / "file.txt" @@ -344,10 +311,10 @@ def test_status_ahead_of_upstream( assert p.returncode == 0 if short_flag == "-s": - assert f"...origin/{default_branch}" in p.stdout + assert "...origin/main" in p.stdout assert "[ahead 1]" in p.stdout else: - assert f"Your branch is ahead of 'origin/{default_branch}'" in p.stdout + assert "Your branch is ahead of 'origin/main'" in p.stdout assert "by 1 commit" in p.stdout assert 'use "git push"' in p.stdout @@ -362,15 +329,7 @@ def test_status_with_branch_and_tracking( repo_path = tmp_path / "repo" repo_path.mkdir() - subprocess.run([git2cpp_path, "init"], cwd=repo_path) - - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=repo_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented + subprocess.run([git2cpp_path, "init", "-b", "main"], cwd=repo_path) test_file = repo_path / "file.txt" test_file.write_text("initial") @@ -395,16 +354,12 @@ def test_status_with_branch_and_tracking( assert p.returncode == 0 if short_flag == "-s": - assert ( - f"## {default_branch}" in p.stdout - ) # "main" locally, but "master" in the CI + assert "## main" in p.stdout # "main" locally, but "master" in the CI assert "[ahead 1]" in p.stdout else: + assert "On branch main" in p.stdout # "main" locally, but "master" in the CI assert ( - f"On branch {default_branch}" in p.stdout - ) # "main" locally, but "master" in the CI - assert ( - f"Your branch is ahead of 'origin/{default_branch}'" in p.stdout + "Your branch is ahead of 'origin/main'" in p.stdout ) # "main" locally, but "master" in the CI assert "1 commit." in p.stdout @@ -413,14 +368,6 @@ def test_status_all_headers_shown(repo_init_with_commit, git2cpp_path, tmp_path) """Test that all status headers can be shown together""" assert (tmp_path / "initial.txt").exists() - default_branch = subprocess.run( - ["git", "branch", "--show-current"], - capture_output=True, - cwd=tmp_path, - text=True, - check=True, - ).stdout.strip() # TODO: use git2cpp when "branch --show-current" is implemented - # Changes to be committed staged = tmp_path / "staged.txt" staged.write_text("staged") @@ -438,7 +385,7 @@ def test_status_all_headers_shown(repo_init_with_commit, git2cpp_path, tmp_path) p = subprocess.run(cmd_status, capture_output=True, cwd=tmp_path, text=True) assert p.returncode == 0 - assert f"On branch {default_branch}" in p.stdout + assert "On branch main" in p.stdout assert "Changes to be committed:" in p.stdout assert 'use "git reset HEAD ..." to unstage' in p.stdout assert "Changes not staged for commit:" in p.stdout