Git是否有任何本机方法来规范化(解析)符号链接,比如readlink-f?

Git是否有任何本机方法来规范化(解析)符号链接,比如readlink-f?,git,Git,readlink-f,根据其手册页: “通过递归跟踪给定名称的每个组件中的每个符号链接来规范化[s];除最后一个组件外,所有组件都必须存在” 我希望Git有一个“readlink-f”的等价物。换句话说,一种在存储库中给定提交位置跟踪路径中每个符号链接的方法,最终得到该路径的规范版本 我已经编写了自己的函数来实现这一点(见下文),但我想知道Git是否具有做同样事情的本机功能。而且,如果没有,也许值得考虑在新版本的Git中包含等效功能。谢谢 SYMLINK_FILE_TYPE_OCTAL="12"

readlink-f,根据其手册页:

“通过递归跟踪给定名称的每个组件中的每个符号链接来规范化[s];除最后一个组件外,所有组件都必须存在”

我希望Git有一个“readlink-f”的等价物。换句话说,一种在存储库中给定提交位置跟踪路径中每个符号链接的方法,最终得到该路径的规范版本

我已经编写了自己的函数来实现这一点(见下文),但我想知道Git是否具有做同样事情的本机功能。而且,如果没有,也许值得考虑在新版本的Git中包含等效功能。谢谢

SYMLINK_FILE_TYPE_OCTAL="12"
MAX_SYMLINK_COUNT=10

git-is-symlink() {
  local commit="$1"
  local dir="$2"
  local base="$3"
  git ls-tree "${commit}:$dir" "$base" | grep -q -P "^$SYMLINK_FILE_TYPE_OCTAL"
}

git-get-symlink-target() {
  local commit="$1"
  local dir="$2"
  local base="$3"

  if test -z "$dir"; then
    local path="$base"
  else
    local path="$dir/$base"
  fi
  git cat-file blob "$commit:$path"
}

git-resolve-path-recursive() {
  local commit="$1"
  local resolved_prefix="$2"
  local segment_to_be_resolved="$3"
  local unresolved_suffix="$4"
  local symlink_count="$5"

  if test -z "$segment_to_be_resolved" && test -z "$unresolved_suffix"; then
    echo "$resolved_prefix"
    return
  fi

  if test "$symlink_count" -gt "$MAX_SYMLINK_COUNT"; then
    echo "Exceeded symlink count of $MAX_SYMLINK_COUNT on '$prefix' '$segment_to_be_resolved'" >&2
    exit 1
  fi

  if test -n "$segment_to_be_resolved" && git-is-symlink "$commit" "$resolved_prefix" "$segment_to_be_resolved"; then
    local symlink_target="$(git-get-symlink-target "$commit" "$resolved_prefix" "$segment_to_be_resolved")"
    git-resolve-path-recursive "$commit" "$resolved_prefix" "$symlink_target" "$unresolved_suffix" $((++symlink_count))
  else
    if test -z "$resolved_prefix"; then
      local new_resolved_prefix="$segment_to_be_resolved"
    else
      local new_resolved_prefix="$resolved_prefix/$segment_to_be_resolved"
    fi
    local new_segment_to_be_resolved="$(echo "$unresolved_suffix" | cut -d / -f 1)"
    local new_unresolved_suffix="$(echo "$unresolved_suffix" | cut -s -d / -f 2-)"
    git-resolve-path-recursive "$commit" "$new_resolved_prefix" "$new_segment_to_be_resolved" "$new_unresolved_suffix" 0
  fi
}

## Main entry point
## USAGE: git-resolve-path COMMIT PATH
git-resolve-path() {
  local commit="$1"
  local path="$2"
  git-resolve-path-recursive "$commit" "" "" "$path" 0
}
用法示例:

> mkdir -p targetdir
> ln -s targetdir linkdir
> touch targetdir/targetfile
> ln -s targetfile targetdir/linkfile
> git add targetdir linkdir targetdir/targetfile targetdir/linkfile
> git commit -m "Add files"
> git-resolve-path HEAD linkdir/linkfile
targetdir/targetfile