Skip to content

Instantly share code, notes, and snippets.

@liuliangsir
Forked from geedew/symlink.sh
Created November 21, 2022 10:00
Show Gist options
  • Save liuliangsir/cbb5d36af2bd9f33d397144816c0d3f1 to your computer and use it in GitHub Desktop.
Save liuliangsir/cbb5d36af2bd9f33d397144816c0d3f1 to your computer and use it in GitHub Desktop.

Revisions

  1. @geedew geedew revised this gist Mar 14, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions symlink.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Pulled from https://stackoverflow.com/questions/18641864/git-bash-shell-fails-to-create-symbolic-links
    # https://stackoverflow.com/users/124119/camilo-martin

    # Detect windows (assumes we are in 'msysgit' or similar).
    windows() { [[ -n "$WINDIR" ]]; }
  2. @geedew geedew created this gist Mar 14, 2018.
    33 changes: 33 additions & 0 deletions symlink.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # Pulled from https://stackoverflow.com/questions/18641864/git-bash-shell-fails-to-create-symbolic-links

    # Detect windows (assumes we are in 'msysgit' or similar).
    windows() { [[ -n "$WINDIR" ]]; }

    # Cross-platform symlink function.
    # With one parameter, it will check whether the parameter is a symlink.
    # With two parameters, it will create a symlink to a file or directory,
    # with syntax: link $linkname $target
    link() {
    if [[ -z "$2" ]]; then
    # Link-checking mode.
    if windows; then
    fsutil reparsepoint query "$1" > /dev/null
    else
    [[ -h "$1" ]]
    fi
    else
    # Link-creation mode.
    if windows; then
    # Windows needs to be told if it's a directory or not. Infer that.
    # Also: note that we convert `/` to `\`. In this case it's necessary.
    if [[ -d "$2" ]]; then
    cmd <<< "mklink /D \"$1\" \"${2//\//\\}\"" > /dev/null
    else
    cmd <<< "mklink \"$1\" \"${2//\//\\}\"" > /dev/null
    fi
    else
    # You know what? I think ln's parameters are backwards.
    ln -s "$2" "$1"
    fi
    fi
    }