3

I've been researching the fastest way to check for changes in a repo to update the PS1 prompt. For reasons not worth elaborating, no access to the __git_ps1() macro, and anyway other posts have suggested various commands, including those used in __git_ps1().

Reason: I'm accessing repos on a remote server over VPN & ssh, with the remote repo tunnelled back through my connection to our in-house git server. I'm guessing (but really not sure if this is true) that this may be the reason that on a fresh connection on a new day, cd'ing into a repo on this remote server can cause git ls-files -m to take 3 or 4 seconds to complete, and still take up to 1 second after file caching has helped.

To improve this I found git diff-index --quiet HEAD is almost instant, but the next day it starts returning '1' even though there are actually no changes (no output from a very slow run of git status). I've found that git update-index --refresh -q seems to fix the issue (quickly) and also returns '0' if in fact there are no changes.

From what I've read I think it should be safe to run from PS1 prompt, but I understand this is a "lower level" git command and I may not be understanding all the implications. So, to be clear, I've currently got the following to check for changes, which always seems to be quick, but after a day starts outputting '*' in clean repos.

if git diff-index --quiet HEAD 2>/dev/null && [[ -z "$(git ls-files --others --exclude-standard --directory --no-empty-directory)" ]]
    then echo -n " "
    else echo -n " *"
fi

Can any "git gurus" out there see a problem with changing 1st test to git update-index --refresh -q?

Steve J
  • 31
  • 2

1 Answers1

1

It should work, except when use in a Git LFS (Large File Storage) where, as explained here, it can trigger racy timestamps.

That is why, with Git 2.36 (Q2 2022), "git update-index --refresh"(man) has been taught to deal better with racy timestamps (just like "git status"(man) already does).

See commit 2ede073, commit 9b71efd, commit 0275e4d, commit ab6245b (07 Jan 2022) by Marc Strapetz (mstrap).
(Merged by Junio C Hamano -- gitster -- in commit ee52b35, 05 Feb 2022)

update-index: refresh should rewrite index in case of racy timestamps

Signed-off-by: Marc Strapetz

'git update-index --refresh'(man) and '--really-refresh' should force writing of the index file if racy timestamps have been encountered, as 'git status'(man) already does.

Note that calling 'git update-index --refresh' still does not guarantee that there will be no more racy timestamps afterwards (the same holds true for 'git status'): - calling 'git update-index --refresh' immediately after touching and adding a file may still leave racy timestamps if all three operations occur within the racy-tolerance (usually 1 second unless USE_NSEC has been defined) - calling 'git update-index --refresh' for timestamps which are set into the future will leave them racy

To guarantee that such racy timestamps will be resolved would require to wait until the system clock has passed beyond these timestamps and only then write the index file.
Especially for future timestamps, this does not seem feasible because of possibly long delays/hangs.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250