git clone --reference,但有硬链接

时间:2017-08-15 16:04:42

标签: git clone

我喜欢git clone --reference如何保存网络容量和磁盘空间。但是,如果删除了引用repo,则新的repo会被破坏。

据我所知--reference使用.git/objects/info/alternates来分享对象。但是,如果它使用了硬链接,就像--local那样,则不存在这样的问题 - 它可以节省网络容量,磁盘空间,并且在删除引用存储库时不会被破坏。

有没有办法达到这样的效果?

1 个答案:

答案 0 :(得分:0)

我编写了以下脚本来实现这一目标。第一次运行时,它将创建一个缓存仓库,它是远程仓库的镜像。因此,它将使用此缓存来创建--local克隆(具有硬链接和节省网络容量的所有好处),然后它将origin覆盖为远程仓库,而不是本地缓存仓库

#!/usr/bin/env bash
#
# Usage: clone <branch> [<target-directory-name>]
#
set -ueo pipefail

readonly BRANCH=$1
readonly TARGET=${2:-$BRANCH}

readonly ORIGIN_REPO='git@...'
readonly CACHE_REPO='/tmp/cache-repo.git'

if [[ -e "$CACHE_REPO" ]]; then
  echo "Updating existing cache repo at: $CACHE_REPO"
  git --git-dir="$CACHE_REPO" remote update --prune
else
  echo "Creating cache repo from scratch"
  git clone --bare --mirror "$ORIGIN_REPO" "$CACHE_REPO"
fi

git clone --local "$CACHE_REPO" "$TARGET" --branch="$BRANCH"
git --git-dir="$TARGET/.git" remote set-url origin "$ORIGIN_REPO"