Skip to content

Commit 0f68bb3

Browse files
authored
Remove extra output after switching to different branch (#204)
* Remove extra output after switching to different branch * Support quotes around exit code
1 parent 162a89a commit 0f68bb3

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎src/subcommand/checkout_subcommand.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ void checkout_subcommand::run()
150150
update_head(repo, annotated_commit, target_name);
151151

152152
std::cout << "Switched to a new branch '" << target_name << "'" << std::endl;
153+
return;
153154
}
154155
else if (!pathspecs.empty())
155156
{

‎test/conftest_lite.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
STDERR_FILE = "stderr.txt"
3333

3434
# Command line that writes the exit code of the previous command line and the shell's cwd to the
35-
# terminal, as lines such as '?=0' and 'PWD=/drive'.
35+
# terminal, as lines such as '?=0' and 'PWD=/drive'. Some versions of coreutils env quote the
36+
# '?' name as in "'?'=0", so allow an optional character either side of it.
3637
# Quote the grep pattern as cockle treats an unquoted '?' as a glob wildcard.
37-
EXIT_CODE_AND_PWD_LINE = "env | grep -E '^(\\?|PWD)='"
38+
EXIT_CODE_AND_PWD_LINE = "env | grep -E '^(.?\\?.?|PWD)='"
3839

3940
# Collect the output of the open terminal so that we can tell when a command line has finished.
4041
COLLECT_OUTPUT_JS = """
@@ -230,7 +231,8 @@ def read_exit_code_and_pwd(page: Page) -> tuple[int | None, str]:
230231
output = run_line(page, EXIT_CODE_AND_PWD_LINE)
231232
# Remove ANSI escape sequences such as colours, as grep is aliased to 'grep --color=auto'.
232233
output = re.sub(r"\x1b\[[0-9;]*[A-Za-z]", "", output)
233-
returncode = re.search(r"^\?=(-?\d+)\s*$", output, re.MULTILINE)
234+
# The '?' name may or may not be quoted, depending on the version of coreutils env.
235+
returncode = re.search(r"^'?\?'?=(-?\d+)\s*$", output, re.MULTILINE)
234236
pwd = re.search(r"^PWD=(.*?)\s*$", output, re.MULTILINE)
235237
if pwd is None:
236238
raise RuntimeError(f"Unable to read PWD from terminal output: {output!r}")

‎test/test_checkout.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_checkout_b(repo_init_with_commit, git2cpp_path, tmp_path):
3535
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=tmp_path, text=True)
3636
assert p_checkout.returncode == 0
3737
assert "Switched to a new branch 'foregone'" in p_checkout.stdout
38+
assert "Switched to branch 'foregone'" not in p_checkout.stdout
3839

3940
branch_cmd = [git2cpp_path, "branch"]
4041
p_branch = subprocess.run(branch_cmd, capture_output=True, cwd=tmp_path, text=True)
@@ -65,6 +66,7 @@ def test_checkout_B_force_create(repo_init_with_commit, git2cpp_path, tmp_path):
6566
p_checkout = subprocess.run(checkout_cmd, capture_output=True, cwd=tmp_path, text=True)
6667
assert p_checkout.returncode == 0
6768
assert "Switched to a new branch 'resetme'" in p_checkout.stdout
69+
assert "Switched to branch 'resetme'" not in p_checkout.stdout
6870

6971
# Verify we're on the branch
7072
branch_cmd = [git2cpp_path, "branch"]

0 commit comments

Comments
 (0)