Re: [PATCH] scripts/setlocalversion: Improve -dirty check with git-status --no-optional-locks

Genki Sky
2018-11-10T08:58:59Z

Hi, thanks very much for doing this. But, this patch always prints
-dirty for me, even with no untracked changes in git. I think this is
because:

On Fri, 9 Nov 2018 10:34:37 -0800, Brian Norris <briannorris@chromium.org> wrote:
> diff --git a/scripts/setlocalversion b/scripts/setlocalversion
> index 71f39410691b..eab1f90de50d 100755
> --- a/scripts/setlocalversion
> +++ b/scripts/setlocalversion
> @@ -73,8 +73,19 @@ scm_version()
> printf -- '-svn%s' "`git svn find-rev $head`"
> fi
>
> - # Check for uncommitted changes
> - if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
> + # Check for uncommitted changes.
> + # First, with git-status, but --no-optional-locks is only
> + # supported in git >= 2.14, so fall back to git-diff-index if
> + # it fails. Note that git-diff-index does not refresh the
> + # index, so it may give misleading results. See
> + # git-update-index(1), git-diff-index(1), and git-status(1).
> + local git_status
> + git_status="$(git --no-optional-locks status -uno --porcelain 2>/dev/null)"
> + if [ $? -eq 0 ]; then
> + if echo "$git_status" | grep -qv '^.. scripts/package'; then

Shouldn't this be:

if printf '%s' "$git_status" | grep -qv '^.. scripts/package'; then

I.e., use printf not echo? Because of echo introducing a newline.

With echo:
$ x=$(printf ''); if echo "$x" | grep -qv 'ignore'; then echo dirty; fi
dirty
$ x=$(printf '\n'); if echo "$x" | grep -qv 'ignore'; then echo dirty; fi
dirty
$ x=$(printf 'ignore\n'); if echo "$x" | grep -qv 'ignore'; then echo dirty; fi
$ x=$(printf 'untracked\n'); if echo "$x" | grep -qv 'ignore'; then echo dirty; fi
dirty

With printf:
$ x=$(printf ''); if printf '%s' "$x" | grep -qv 'ignore'; then echo dirty; fi
$ x=$(printf '\n'); if printf '%s' "$x" | grep -qv 'ignore'; then echo dirty; fi
$ x=$(printf 'ignore\n'); if printf '%s' "$x" | grep -qv 'ignore'; then echo dirty; fi
$ x=$(printf 'untracked\n'); if printf '%s' "$x" | grep -qv 'ignore'; then echo dirty; fi
dirty

(Hopefully I'm not missing something.)