Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement for large repos! (Avoid using "git status") #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ find_git_branch() {
}

find_git_dirty() {
local status=$(git status --porcelain 2> /dev/null)
if [[ "$status" != "" ]]; then
git_dirty='*'
local branch
if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had problems with this before; it didn't work in a detached head state. My solution was to instead use "git name-rev --name-only HEAD" instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That part is just copied from the find_git_branch() function. The actual contribution here is in line 20: "git diff-index ..."

But yes, lines 4 and 16 could probably be improved, too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I just checked the behaviour by checking out an old commit (4 commits ago), and while the traditional output for line 4 is "(detached*)", yours becomes the slightly more verbose "(remotes/origin/HEAD~4)".
But this seems like a matter of taste? Or what were the specific problems?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm confused and it was something else, sorry. I have a Makefile that tries to determine branch name, so "detached" wasn't good enough. For an interactive prompt, it should be.

#local status=$(git status --porcelain 2> /dev/null) # very slow for large repos!
#local has_staged_changes=$(git diff-index --quiet --cached HEAD 2> /dev/null)
#local has_stageable_files=$(git diff-files --quiet 2> /dev/null)
local status=$(git diff-index --quiet HEAD 2> /dev/null ; echo $?)
if [[ "$status" == "0" ]]; then
git_dirty=''
elif [[ "$status" == "1" ]]; then
git_dirty='*'
elif [[ "$status" == "128" ]]; then
git_dirty=' no_worktree'
else
git_dirty=" err_$status" # to show if anything else went wrong.
fi
else
git_dirty=''
fi
Expand Down