Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: git checkout improvement



2008/8/21 Clint Adams <clint@xxxxxxx>:
> On Thu, Aug 21, 2008 at 04:22:08AM +0200, Mikael Magnusson wrote:
>> Another problem is you can do
>> git checkout git.c NEWS
>> but then we try to use git.c as a tree-ish which obviously doesn't work.
>> Probably we should check if words[1] (or whatever) is a valid tree-ish,
>> and if not, complete files from the index instead.
>
> Great, how do you validate a tree-ish?

This seems to work :)

commit b619c9a4c193380d8b974753257c60faf713f6f6
Author: Mikael Magnusson <mikachu@xxxxxxxxx>
Date:   Thu Aug 21 07:18:50 2008 +0200

    _git: Make sure the first argument is a treeish.

    The first rev-parse is needed in case the user wrote something
like HEAD:, HEAD:^{tree}
    is not valid syntax, so get the sha1 first and then append the ^{tree}.

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index 6603371..7e48c19 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -1529,6 +1529,13 @@ _git-branch () {
 }
 __git_zstyle_default
':completion::complete:git-branch:delete-argument-rest:*' ignore-line
yes

+(( $+functions[__git_is_treeish] )) ||
+__git_is_treeish () {
+  local sha1
+  sha1="$(git rev-parse $1)"
+  [[ "$(git cat-file -t "${sha1}^{tree}" 2> /dev/null)" == tree ]]
+}
+
 # TODO: __git_tree_ishs is just stupid.  It should be giving us a list of tags
 # and perhaps also allow all that just with ^{tree} and so on.  Not quite sure
 # how to do that, though.
@@ -1568,7 +1575,7 @@ _git-checkout () {
   #What's the variable that holds the tree-ish argument? Is it even
reliably possible?
   case $state in
     (files)
-      if [[ -n $line[1] ]]; then
+      if [[ -n $line[1] ]] && __git_is_treeish $line[1]; then
         __git_tree_files . $line[1] && ret=0
       else
         __git_cached_files && ret=0


-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author