From 131c8d74e54492e6ce72ed9b4dfe1f60bb9e8a79 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 7 Jun 2019 21:14:11 +0200 Subject: [PATCH] lib/git/git_is_touched: Speed up (#706) * lib/git/git_is_touched: Speed up This used to use `git status --porcelain`, which by necessity needs to check the entire repo for all kinds of changes, just to figure out if there are any. Instead, we now use git commands that can exit early. In large repos, this can be faster by a factor of 15 or so. Fixes #624. * Fix return status `git diff` also returns 1 if there *is* a diff. --- lib/git/git_is_touched.fish | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/git/git_is_touched.fish b/lib/git/git_is_touched.fish index 172f605..335bed6 100644 --- a/lib/git/git_is_touched.fish +++ b/lib/git/git_is_touched.fish @@ -1,5 +1,8 @@ function git_is_touched -d "Check if repo has any changes" git_is_repo; and begin - test -n (echo (command git status --porcelain)) + # The first checks for staged changes, the second for unstaged ones. + # We put them in this order because checking staged changes is *fast*. + not command git diff-index --cached --quiet HEAD -- >/dev/null 2>&1 + or not command git diff --no-ext-diff --quiet --exit-code >/dev/null 2>&1 end end