zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: [PATCH] vcs_info git: Fix stagedstr for empty repos
Date: Sun, 1 Jun 2014 02:26:53 +0000	[thread overview]
Message-ID: <20140601022653.GE1820@tarsus.local2> (raw)

[-- Attachment #1: Type: text/plain, Size: 679 bytes --]

Good morning Frank, *,

vcs_info git false-negatives to detect staged changes in a repository that has
no commits.  Attached a patch for that.

The patch includes two variants for computing the "has staged changes?" bit in
empty repositories: one via 'git ls-files' and one via git's empty tree.

- The output of ls-files isn't O(1), but I tested with a 3000-file tree and
  didn't notice a slowdown, so I suppose it's acceptable.

- 'diff-index --cached --quiet $empty_tree' is an O(1) operation, but I am not
  sure whether it is forwards compatible with future versions of Git, i.e.,
  whether it's a hack that happens to work or a legitimate use of git's API.

WDYT?

Daniel

[-- Attachment #2: 0002-vcs_info-git-Fix-stagedstr-for-empty-repos.patch --]
[-- Type: text/x-patch, Size: 2379 bytes --]

>From 9994132f33c5439fbb5d3c4ef91ba122b942fd7c Mon Sep 17 00:00:00 2001
From: Daniel Shahaf <d.s@daniel.shahaf.name>
Date: Fri, 30 May 2014 23:50:55 +0000
Subject: [PATCH 2/2] vcs_info git: Fix stagedstr for empty repos

In empty repositories, HEAD is an unresolvable symbolic ref.  Start computing
stagedstr/unstagedstr in that case; for the former, use a different method
than the non-empty-repository case.
---
 Functions/VCS_Info/Backends/VCS_INFO_get_data_git |   17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/Functions/VCS_Info/Backends/VCS_INFO_get_data_git b/Functions/VCS_Info/Backends/VCS_INFO_get_data_git
index a48dc39..caf938b 100644
--- a/Functions/VCS_Info/Backends/VCS_INFO_get_data_git
+++ b/Functions/VCS_Info/Backends/VCS_INFO_get_data_git
@@ -128,16 +128,25 @@ elif zstyle -t ":vcs_info:${vcs}:${usercontext}:${rrn}" "check-for-staged-change
     querystaged=1
 fi
 if (( querystaged || queryunstaged )) && \
-   [[ "$(${vcs_comm[cmd]} rev-parse --is-inside-git-dir 2> /dev/null)" != 'true' ]] && \
-   ${vcs_comm[cmd]} rev-parse --quiet --verify HEAD &> /dev/null ; then
+   [[ "$(${vcs_comm[cmd]} rev-parse --is-inside-work-tree 2> /dev/null)" == 'true' ]] ; then
     # Default: off - these are potentially expensive on big repositories
     if (( queryunstaged )) ; then
         ${vcs_comm[cmd]} diff --no-ext-diff --ignore-submodules --quiet --exit-code ||
             gitunstaged=1
     fi
     if (( querystaged )) ; then
-        ${vcs_comm[cmd]} diff-index --cached --quiet --ignore-submodules HEAD 2> /dev/null
-        (( $? && $? != 128 )) && gitstaged=1
+        if ${vcs_comm[cmd]} rev-parse --quiet --verify HEAD &> /dev/null ; then
+            ${vcs_comm[cmd]} diff-index --cached --quiet --ignore-submodules HEAD 2> /dev/null
+            (( $? && $? != 128 )) && gitstaged=1
+        else
+            # empty repository (no commits yet)
+            # TODO: ls-files' output isn't O(1); might be faster to use
+            #         empty_tree_sha1=$(git hash-object -t tree /dev/null)
+            #         git diff-index --cached --quiet $empty_tree_sha1
+            #         (( $? && $? != 128 ))
+            #       to efficiently get the "is the index not empty?" bit.
+            [[ -n "$(${vcs_comm[cmd]} ls-files --cached -z)" ]] && gitstaged=1
+        fi
     fi
 fi
 
-- 
1.7.10.4


             reply	other threads:[~2014-06-01  2:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-01  2:26 Daniel Shahaf [this message]
2014-06-01 12:33 ` Aaron Schrab
2014-06-02  8:14 ` [PATCH] " Frank Terbeck
2014-06-02 13:00   ` Daniel Shahaf
2014-06-02 13:19     ` Frank Terbeck
2014-06-02 18:21       ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140601022653.GE1820@tarsus.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).