Skip to content

Commit a4889e6

Browse files
szedergitster
authored andcommitted
bash prompt: test the prompt with newline in repository path
Newlines in the path to a git repository were not an issue for the git-specific bash prompt before commit efaa0c1 (bash prompt: combine 'git rev-parse' executions in the main code path, 2013-06-17), because the path returned by 'git rev-parse --git-dir' was directly stored in a variable, and this variable was later always accessed inside double quotes. Newlines are not an issue after commit efaa0c1 either, but it's more subtle. Since efaa0c1 we use the following single 'git rev-parse' execution to query various info about the repository: git rev-parse --git-dir --is-inside-git-dir \ --is-bare-repository --is-inside-work-tree The results to these queries are separated by a newline character in the output, e.g.: /home/szeder/src/git/.git false false true A newline in the path to the git repository could potentially break the parsing of these results and ultimately the bash prompt, unless the parsing is done right. Commit efaa0c1 got it right, as I consciously started parsing 'git rev-parse's output from the end, where each record is a single line containing either 'true' or 'false' or, after e3e0b93 (bash prompt: combine 'git rev-parse' for detached head, 2013-06-24), the abbreviated commit object name, and all what remains at the beginning is the path to the git repository, no matter how many lines it is. This subtlety really warrants its own test, especially since I didn't explain it in the log message or in an in-code comment back then, so add a test to excercise the prompt with newline characters in the path to the repository. Guard this test with the FUNNYNAMES prerequisite, because not all filesystems support newlines in filenames. Note that 'git rev-parse --git-dir' prints '.git' or '.' when at the top of the worktree or the repository, respectively, and only prints the full path to the repository when in a subdirectory, hence the need for changing into a subdir in the test. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c2b664 commit a4889e6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

t/t9903-bash-prompt.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ test_expect_success 'prompt - unborn branch' '
6161
test_cmp expected "$actual"
6262
'
6363

64+
repo_with_newline='repo
65+
with
66+
newline'
67+
68+
if mkdir "$repo_with_newline" 2>/dev/null
69+
then
70+
test_set_prereq FUNNYNAMES
71+
else
72+
say 'Your filesystem does not allow newlines in filenames.'
73+
fi
74+
75+
test_expect_success FUNNYNAMES 'prompt - with newline in path' '
76+
printf " (master)" >expected &&
77+
git init "$repo_with_newline" &&
78+
test_when_finished "rm -rf \"$repo_with_newline\"" &&
79+
mkdir "$repo_with_newline"/subdir &&
80+
(
81+
cd "$repo_with_newline/subdir" &&
82+
__git_ps1 >"$actual"
83+
) &&
84+
test_cmp expected "$actual"
85+
'
86+
6487
test_expect_success 'prompt - detached head' '
6588
printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
6689
test_config core.abbrev 13 &&

0 commit comments

Comments
 (0)