@@ -61,4 +61,99 @@ def test_push_private_repo(
6161 assert p_push .returncode == 0
6262 assert p_push .stdout .count ("Username:" ) == 2
6363 assert p_push .stdout .count ("Password:" ) == 2
64- assert "Pushed to origin" in p_push .stdout
64+ assert " * [new branch] test-" in p_push .stdout
65+
66+
67+ def test_push_branch_private_repo (
68+ git2cpp_path , tmp_path , run_in_tmp_path , private_test_repo , commit_env_config
69+ ):
70+ """Test push with an explicit branch name: git2cpp push <remote> <branch>."""
71+ branch_name = f"test-{ uuid4 ()} "
72+
73+ username = "abc"
74+ password = private_test_repo ["token" ]
75+ input = f"{ username } \n { password } "
76+ repo_path = tmp_path / private_test_repo ["repo_name" ]
77+ url = private_test_repo ["https_url" ]
78+
79+ # Clone the private repo.
80+ clone_cmd = [git2cpp_path , "clone" , url ]
81+ p_clone = subprocess .run (clone_cmd , capture_output = True , text = True , input = input )
82+ assert p_clone .returncode == 0
83+ assert repo_path .exists ()
84+
85+ # Create a new branch and commit on it.
86+ checkout_cmd = [git2cpp_path , "checkout" , "-b" , branch_name ]
87+ p_checkout = subprocess .run (checkout_cmd , cwd = repo_path , capture_output = True , text = True )
88+ assert p_checkout .returncode == 0
89+
90+ (repo_path / "push_branch_file.txt" ).write_text ("push branch test" )
91+ subprocess .run ([git2cpp_path , "add" , "push_branch_file.txt" ], cwd = repo_path , check = True )
92+ subprocess .run ([git2cpp_path , "commit" , "-m" , "branch commit" ], cwd = repo_path , check = True )
93+
94+ # Switch back to main so HEAD is NOT on the branch we want to push.
95+ subprocess .run (
96+ [git2cpp_path , "checkout" , "main" ], capture_output = True , check = True , cwd = repo_path
97+ )
98+
99+ status_cmd = [git2cpp_path , "status" ]
100+ p_status = subprocess .run (status_cmd , cwd = repo_path , capture_output = True , text = True )
101+ assert p_status .returncode == 0
102+ assert "On branch main" in p_status .stdout
103+
104+ # Push specifying the branch explicitly (HEAD is on main, not the test branch).
105+ input = f"{ username } \n { password } "
106+ push_cmd = [git2cpp_path , "push" , "origin" , branch_name ]
107+ p_push = subprocess .run (push_cmd , cwd = repo_path , capture_output = True , text = True , input = input )
108+ assert p_push .returncode == 0
109+ # assert " * [new branch] test-" in p_push.stdout
110+ print ("\n \n " , p_push .stdout )
111+
112+
113+ def test_push_branches_flag_private_repo (
114+ git2cpp_path , tmp_path , run_in_tmp_path , private_test_repo , commit_env_config
115+ ):
116+ """Test push --branches pushes all local branches."""
117+ branch_a = f"test-a-{ uuid4 ()} "
118+ branch_b = f"test-b-{ uuid4 ()} "
119+
120+ username = "abc"
121+ password = private_test_repo ["token" ]
122+ input = f"{ username } \n { password } "
123+ repo_path = tmp_path / private_test_repo ["repo_name" ]
124+ url = private_test_repo ["https_url" ]
125+
126+ # Clone the private repo.
127+ clone_cmd = [git2cpp_path , "clone" , url ]
128+ p_clone = subprocess .run (clone_cmd , capture_output = True , text = True , input = input )
129+ assert p_clone .returncode == 0
130+ assert repo_path .exists ()
131+
132+ # Create two extra branches with commits.
133+ for branch_name in [branch_a , branch_b ]:
134+ subprocess .run (
135+ [git2cpp_path , "checkout" , "-b" , branch_name ],
136+ capture_output = True ,
137+ check = True ,
138+ cwd = repo_path ,
139+ )
140+ (repo_path / f"{ branch_name } .txt" ).write_text (f"content for { branch_name } " )
141+ subprocess .run ([git2cpp_path , "add" , f"{ branch_name } .txt" ], cwd = repo_path , check = True )
142+ subprocess .run (
143+ [git2cpp_path , "commit" , "-m" , f"commit on { branch_name } " ],
144+ cwd = repo_path ,
145+ check = True ,
146+ )
147+
148+ # Go back to main.
149+ subprocess .run (
150+ [git2cpp_path , "checkout" , "main" ], capture_output = True , check = True , cwd = repo_path
151+ )
152+
153+ # Push all branches at once.
154+ input = f"{ username } \n { password } "
155+ push_cmd = [git2cpp_path , "push" , "origin" , "--branches" ]
156+ p_push = subprocess .run (push_cmd , cwd = repo_path , capture_output = True , text = True , input = input )
157+ assert p_push .returncode == 0
158+ # assert " * [new branch] test-" in p_push.stdout
159+ print ("\n \n " , p_push .stdout )
0 commit comments