0

Given a bare repository, I can use git --work-tree=/path/to/folder --git-dir=/path/to/bare.git checkout -f in the post-receive hook to checkout the head to folder without having the .git folder inside. How can I do achieve this when the folder sits in on a remote machine?

I am aware of git clone --depth=1 but I cannot use this because the remote folder may already exist. rsync [--delete] is not an option neither because I want files that are local to each folder but not in the repo (e.g. all folders contain a different confi.local.ini).

Rolf
  • 1,129
  • 11
  • 27

1 Answers1

0

I finally merged different solutions and now have the following in my post-receive:

function clone_dev() {
    # This function ssh's to a remote machine (or "localhost") and pulls the
    # latest changes from the bare repo without leaving traces of .git.
    # This approach allows to "local" files to not be overwritten.
    DEV_SSH=$1 # ex: "gitusr@localhost"
    DEV_DIR=$2 # ex: "/data/production/development"
    REPO_DIR_SSH=$3 # ex: "ssh://gitusr@localhost/data/bare.git"
    BRANCH=master

    echo -e "\e[1;31mDeploying to '${DEV_SSH}${DEV_DIR}' ...\e[0m"
    echo ""
    ssh "${DEV_SSH}" /bin/bash <<EOF
if [[ ! -d "${DEV_DIR}" ]]; then
    git clone -b $BRANCH --single-branch --depth=1 "${REPO_DIR_SSH}" "${DEV_DIR}"
    cd $DEV_DIR
    git checkout -f $BRANCH
else
    cd "${DEV_DIR}"
    git init
    git remote add -t $BRANCH origin "${REPO_DIR_SSH}"
    git fetch --depth=1 origin
    git checkout -f -t origin/$BRANCH
fi
cd "${DEV_DIR}"
rm -rf "${DEV_DIR}"/.git
EOF
    echo ""
}

...
REPO_DIR_SSH=ssh://gitusr@$HOSTNAME/$REPO_DIR

# the locations of the cutting-edge systems
DEVELOPMENT1_SSH=gitusr@$HOSTNAME
DEVELOPMENT1_DIR=/data/production/development
...

clone_dev "$DEVELOPMENT1_SSH" "$DEVELOPMENT3_DIR" "$REPO_DIR_SSH"
clone_dev "$DEVELOPMENT2_SSH" "$DEVELOPMENT2_DIR" "$REPO_DIR_SSH"

Stolen partly from: https://stackoverflow.com/a/22597919/2923406 and https://stackoverflow.com/a/11498124/2923406

Community
  • 1
  • 1
Rolf
  • 1,129
  • 11
  • 27