Merge branch 'ct/prompt-untracked-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Sat, 28 Mar 2015 16:33:12 +0000 (09:33 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 28 Mar 2015 16:33:12 +0000 (09:33 -0700)
The prompt script (in contrib/) did not show the untracked sign
when working in a subdirectory without any untracked files.

* ct/prompt-untracked-fix:
git prompt: use toplevel to find untracked files

1  2 
contrib/completion/git-prompt.sh
t/t9903-bash-prompt.sh
index 214e859f99e7d896a6fa45a737b5b3e2a629176f,10c4d0ad74bc072afc86440c8810e2c3faae00ba..f18aedc73be9daf4aab564beecb7bae29ea759c9
  # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
  # the colored output of "git status -sb" and are available only when
  # using __git_ps1 for PROMPT_COMMAND or precmd.
 +#
 +# If you would like __git_ps1 to do nothing in the case when the current
 +# directory is set up to be ignored by git, then set
 +# GIT_PS1_HIDE_IF_PWD_IGNORED to a nonempty value. Override this on the
 +# repository level by setting bash.hideIfPwdIgnored to "false".
  
  # check whether printf supports -v
  __git_printf_supports_v=
@@@ -275,7 -270,7 +275,7 @@@ __git_ps1_colorize_gitstring (
  
  __git_eread ()
  {
 -      f="$1"
 +      local f="$1"
        shift
        test -r "$f" && read "$@" <"$f"
  }
  # In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
  __git_ps1 ()
  {
 +      # preserve exit status
 +      local exit=$?
        local pcmode=no
        local detached=no
        local ps1pc_start='\u@\h:\w '
                        ps1pc_start="$1"
                        ps1pc_end="$2"
                        printf_format="${3:-$printf_format}"
 +                      # set PS1 to a plain prompt so that we can
 +                      # simply return early if the prompt should not
 +                      # be decorated
 +                      PS1="$ps1pc_start$ps1pc_end"
                ;;
                0|1)    printf_format="${1:-$printf_format}"
                ;;
 -              *)      return
 +              *)      return $exit
                ;;
        esac
  
        rev_parse_exit_code="$?"
  
        if [ -z "$repo_info" ]; then
 -              if [ $pcmode = yes ]; then
 -                      #In PC mode PS1 always needs to be set
 -                      PS1="$ps1pc_start$ps1pc_end"
 -              fi
 -              return
 +              return $exit
        fi
  
        local short_sha
        local inside_gitdir="${repo_info##*$'\n'}"
        local g="${repo_info%$'\n'*}"
  
 +      if [ "true" = "$inside_worktree" ] &&
 +         [ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] &&
 +         [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
 +         git check-ignore -q .
 +      then
 +              return $exit
 +      fi
 +
        local r=""
        local b=""
        local step=""
                else
                        local head=""
                        if ! __git_eread "$g/HEAD" head; then
 -                              if [ $pcmode = yes ]; then
 -                                      PS1="$ps1pc_start$ps1pc_end"
 -                              fi
 -                              return
 +                              return $exit
                        fi
                        # is it a symbolic ref?
                        b="${head#ref: }"
                        fi
                fi
                if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] &&
 -                 [ -r "$g/refs/stash" ]; then
 +                 git rev-parse --verify --quiet refs/stash >/dev/null
 +              then
                        s="$"
                fi
  
                if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
                   [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
-                  git ls-files --others --exclude-standard --error-unmatch -- '*' >/dev/null 2>/dev/null
+                  git ls-files --others --exclude-standard --error-unmatch -- ':/*' >/dev/null 2>/dev/null
                then
                        u="%${ZSH_VERSION+%}"
                fi
        else
                printf -- "$printf_format" "$gitstring"
        fi
 +
 +      return $exit
  }
diff --combined t/t9903-bash-prompt.sh
index 51ecd3e4c157ea267557a4484d574748b9757bd4,43cfefd2f8f12b835ff1b755af16ef5886721e24..46d7d37a51023296033eae2e5576bb36f9c38f7b
@@@ -35,8 -35,6 +35,8 @@@ test_expect_success 'setup for prompt t
        git commit -m "another b2" file &&
        echo 000 >file &&
        git commit -m "yet another b2" file &&
 +      mkdir ignored_dir &&
 +      echo "ignored_dir/" >>.gitignore &&
        git checkout master
  '
  
@@@ -397,6 -395,17 +397,17 @@@ test_expect_success 'prompt - untracke
        test_cmp expected "$actual"
  '
  
+ test_expect_success 'prompt - untracked files status indicator - untracked files outside cwd' '
+       printf " (master %%)" >expected &&
+       (
+               mkdir -p ignored_dir &&
+               cd ignored_dir &&
+               GIT_PS1_SHOWUNTRACKEDFILES=y &&
+               __git_ps1 >"$actual"
+       ) &&
+       test_cmp expected "$actual"
+ '
  test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
        printf " (master)" >expected &&
        test_config bash.showUntrackedFiles false &&
@@@ -590,108 -599,4 +601,108 @@@ test_expect_success 'prompt - zsh colo
        test_cmp expected "$actual"
  '
  
 +test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled' '
 +      printf " (master)" >expected &&
 +      test_config bash.hideIfPwdIgnored false &&
 +      (
 +              cd ignored_dir &&
 +              __git_ps1 >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
 +      printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
 +      test_config bash.hideIfPwdIgnored false &&
 +      (
 +              cd ignored_dir &&
 +              __git_ps1 "BEFORE:" ":AFTER" &&
 +              printf "%s" "$PS1" >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset' '
 +      printf " (master)" >expected &&
 +      (
 +              cd ignored_dir &&
 +              __git_ps1 >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
 +      printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
 +      (
 +              cd ignored_dir &&
 +              __git_ps1 "BEFORE:" ":AFTER" &&
 +              printf "%s" "$PS1" >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled' '
 +      printf " (master)" >expected &&
 +      test_config bash.hideIfPwdIgnored false &&
 +      (
 +              cd ignored_dir &&
 +              GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
 +              __git_ps1 >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
 +      printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
 +      test_config bash.hideIfPwdIgnored false &&
 +      (
 +              cd ignored_dir &&
 +              GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
 +              __git_ps1 "BEFORE:" ":AFTER" &&
 +              printf "%s" "$PS1" >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var set, config unset' '
 +      printf "" >expected &&
 +      (
 +              cd ignored_dir &&
 +              GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
 +              __git_ps1 >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
 +      printf "BEFORE::AFTER" >expected &&
 +      (
 +              cd ignored_dir &&
 +              GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
 +              __git_ps1 "BEFORE:" ":AFTER" &&
 +              printf "%s" "$PS1" >"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stdout)' '
 +      printf " (GIT_DIR!)" >expected &&
 +      (
 +              GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
 +              cd .git &&
 +              __git_ps1 >"$actual" 2>/dev/null
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
 +test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stderr)' '
 +      printf "" >expected &&
 +      (
 +              GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
 +              cd .git &&
 +              __git_ps1 >/dev/null 2>"$actual"
 +      ) &&
 +      test_cmp expected "$actual"
 +'
 +
  test_done