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

Re: [PATCH] _git: offer files relative to current directory



Hi Peter,

Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Sun, 13 Dec 2009 03:08:20 +0300
> "Alexey I. Froloff" <raorn@xxxxxxxxxxxx> wrote:
>> On Mon, Dec 07, 2009 at 12:52:33AM +0300, Alexey I. Froloff wrote:
>> > When offering repository files (cached, deleted, changed, etc), make
>> > sure that paths are relative to current directory, as git expects.
>> So, what about this patch?  Will it be accepted or rejected?
>
> Please can someone who uses git decide.

I've did some tests and this functions seems correct. But I would
refuse it for these reasons:

â The function can be written more selfâcontained and generic if the
  prefix would be passed as argument and not determined internally.

â The intention of the function is the make paths relative to a given
  prefix. It should not beautify paths or something. Otherwise name it
  __git_files_relative_and_beatify.

â The function should not take and return a nulâseparated string but an
  array. The code looks as follows:
  call git
  call __git_files_relative
    split output of git
    â
    join output
  split output of __git_files_relative

Here's my test:

#v+
#!/bin/zsh

emulate -R zsh

_call_program () {
    local tmp
    if zstyle -s ":completion:${curcontext}:${1}" command tmp
    then
        if [[ "$tmp" = -* ]]
        then
            eval "$tmp[2,-1]" "$argv[2,-1]"
        else
            eval "$tmp"
        fi
    else
        eval "$argv[2,-1]"
    fi
}
__git_command_successful() { true; }

__git_files_relative () {
  local rawfiles files file f_parts prefix p_parts tmp

  prefix=$(_call_program gitprefix git rev-parse --show-prefix 2>/dev/null)
  __git_command_successful || return

  # Empty prefix, no modifications
  if (( $#prefix == 0 )); then
    print $1
    return
  fi

  rawfiles=(${(ps:\0:)1})
  files=()

  # Now we assume that we've given "absolute" paths list with "root"
  # being repository top directory.  $prefix is also "absolute" path.
  for file in $rawfiles; do
    # Collapse "/./" and "//", strip "/." and "/" from tail (I know,
    # this is a bit paranoid).
    f_parts=(${(s:/:)"${${${${file//\/\///}//\/.\///}%%/.}%%/}"})
    p_parts=(${(s:/:)"${${${${prefix//\/\///}//\/.\///}%%/.}%%/}"})
    tmp=()

    # Strip common path prefix.
    while (( $#f_parts > 0 )) && (( $#p_parts > 0 )) && [[ $f_parts[1] == $p_parts[1] ]]; do
      f_parts[1]=()
      p_parts[1]=()
    done

    # If prefix still not empty, ascend up.
    while (( $#p_parts > 0 )); do
	tmp+=..
	p_parts[1]=()
    done

    # Add remaining path.
    tmp=("$tmp[@]" "$f_parts[@]")

    files+=${(j:/:)tmp}
  done

  print ${(pj:\0:)files}
}

do_test()
{
    local no prefix arg expect
    no=$1
    prefix=$2
    arg=${3//$'\n'/$'\0'}
    expect=${4//$'\n'/$'\0'}

    zstyle ':completion:*:git:gitprefix' command "echo $prefix"
    outp=$(curcontext='git-test:git' __git_files_relative $arg)
    if [[ $outp != $expect ]]
    then
        print "test $no failed"
        print "output:"
        print ${outp//$'\0'/$'\n'}
        print "expected:"
        print ${expect//$'\0'/$'\n'}
        print '===================='
    fi
}

do_test 1 '' "$(print -l 'a/x' 'b')" "$(print -l 'a/x' 'b')"
do_test 2 'a' "$(print -l 'a/x' 'b')" "$(print -l 'x' '../b')"
do_test 3 'b' "$(print -l 'a/x' 'b/y')" "$(print -l '../a/x' 'y')"

do_test 4 'c' "$(print -l a/{1..3} b c/{m/{1..2},n/1,o})" \
  "$(print -l ../a/{1..3} ../b {m/{1..2},n/1,o})"

do_test 5 'c/n' "$(print -l a/{1..3} b c/{m/{1..2},n/1,o})" \
  "$(print -l ../../a/{1..3} ../../b {../m/{1..2},1,../o})"

do_test 6 'a' "$(print -l a/1 b/t/u/../v)" \
  "$(print -l 1 ../b/t/u/../v)"

do_test 7 'a' "$(print -l a/1 b/t/./v)" \
  "$(print -l 1 ../b/t/./v)"

do_test 8 'a' "$(print -l a/1 b/t/.///)" \
  "$(print -l 1 ../b/t/.///)"
#v-

Bye, JÃrg.
-- 
Eine Blume geht Ãber eine Wiese, sieht einen schÃnen Menschen und reiÃt
ihm den Kopf ab.



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