zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: Converting absolut symlinks to relative ones...?
Date: Sun, 6 Mar 2016 11:38:30 -0800	[thread overview]
Message-ID: <160306113830.ZM2800@torch.brasslantern.com> (raw)
In-Reply-To: <20160306173915.GC10755@lorien.comfychair.org>

On Mar 6,  9:39am, Danek Duvall wrote:
}
} GNU ln has a -r option which does this.

That must be a very recent addition, there's no sign of it in coreutils
8.13 from 2011 (on my Ubuntu box).

ln-r () {
  emulate -L zsh
  local symlink
  local -a here there route common
  for symlink in $@:a
  do
    [[ -h $symlink ]] || continue
    common=()
    route=()
    if [[ -d $symlink ]]
    then here=( ${(s:/:)symlink} )
    else here=( ${(s:/:)symlink:h} )
    fi
    there=( ${(s:/:)symlink:A} )
    while (( $#here && $#there ))
    do
      if (( $#route))
      then route=( .. $route $there[1] )
      elif [[ $here[1] == $there[1] ]]
      then
        common+=( $here[1] )
      else
        if [[ -d $symlink || -z $common ]]
        then route=( $there[1] )
        else route=( .. $there[1] )
        fi
      fi
      shift here
      shift there
    done
    if (( $#common ))
    then route+=( $there )
    else continue # no common prefix, absolute link is best
    fi
    # Remove the "print" and both (qq) to actually do linking
    print ln -fs "${(qq)${(j:/:)route}:-.}" "${(qq)symlink}"
  done
}

To change the "no common prefix" behavior, change the loop to become
"while (( $#here || $#there ))" and remove references to $common.


  reply	other threads:[~2016-03-06 19:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-06 13:20 Meino.Cramer
2016-03-06 17:39 ` Danek Duvall
2016-03-06 19:38   ` Bart Schaefer [this message]
2016-03-06 20:21     ` Danek Duvall
2016-03-06 21:22       ` Vin Shelton

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=160306113830.ZM2800@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@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).