Git fork

completion: add helper to count path components

A follow-up commit will use this with for-each-ref to strip the right
number of path components from refnames.

Signed-off-by: David Mandelberg <david@mandelberg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

David Mandelberg and committed by
Junio C Hamano
5637bdc3 6a64ac7b

+37
+11
contrib/completion/git-completion.bash
··· 234 234 done 235 235 } 236 236 237 + # Prints the number of slash-separated components in a path. 238 + # 1: Path to count components of. 239 + __git_count_path_components () 240 + { 241 + local path="$1" 242 + local relative="${path#/}" 243 + relative="${relative%/}" 244 + local slashes="/${relative//[^\/]}" 245 + echo "${#slashes}" 246 + } 247 + 237 248 # The following function is based on code from: 238 249 # 239 250 # bash_completion - programmable completion functions for bash 3.2+
+26
t/t9902-completion.sh
··· 455 455 ' 456 456 457 457 458 + test_expect_success '__git_count_path_components - no slashes' ' 459 + echo 1 >expected && 460 + __git_count_path_components a >"$actual" && 461 + test_cmp expected "$actual" 462 + ' 463 + 464 + test_expect_success '__git_count_path_components - relative' ' 465 + echo 3 >expected && 466 + __git_count_path_components a/b/c >"$actual" && 467 + test_cmp expected "$actual" 468 + 469 + ' 470 + 471 + test_expect_success '__git_count_path_components - absolute' ' 472 + echo 3 >expected && 473 + __git_count_path_components /a/b/c >"$actual" && 474 + test_cmp expected "$actual" 475 + ' 476 + 477 + test_expect_success '__git_count_path_components - trailing slash' ' 478 + echo 3 >expected && 479 + __git_count_path_components a/b/c/ >"$actual" && 480 + test_cmp expected "$actual" 481 + ' 482 + 483 + 458 484 test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' ' 459 485 sed -e "s/Z$//g" >expected <<-EOF && 460 486 with-trailing-space Z