Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Last active June 29, 2019 00:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markusfisch/e45b3e46f7fac82b159e3ed4a992412c to your computer and use it in GitHub Desktop.
Save markusfisch/e45b3e46f7fac82b159e3ed4a992412c to your computer and use it in GitHub Desktop.
Synchronize just the contents of a repository with a local directory

Manually embed sub repositories

Synchronize (the contents of) a repository with a local directory. Can be used to manually update "embedded" repositories (aka subtree's).

Does not touch the host repository's stage or history. You have to add and commit the changed files manually. Write a good commit message and have a clean history.

Installation

Simply put git-embed somewhere in your path.

Usage

usage: git embed add    <repository>
   or: git embed update <directory>

Samples

$ git embed add https://github.com/you/project.git

Copy the contents of https://github.com/you/project.git into the local folder project.

$ git embed update project

Updates the local folder project with the contents of the (previously added) remote repository https://github.com/you/project.git.

#!/usr/bin/env bash
# Echo basename without extension of given URI
#
# @param 1 - URI
base()
{
local BASE=${1##*/}
BASE=${BASE%.*}
echo "$BASE"
}
# Echo URI for given basename
#
# @param 1 - basename of repository
resolve()
{
local REPO=${1%/}
while read -r
do
local BASE
BASE=$(base "$REPLY")
[ "$BASE" == "$REPO" ] || continue
echo "$REPLY"
return 0
done < "$STORE"
return 1
}
# Update embedded repository
#
# @param 1 - basename of repository to update
update()
{
local CWD BASE URI
CWD=$(pwd)
BASE=${1%/}
URI=$(resolve "$1") || return $?
local CACHE=${CACHE:-$(mktemp -d "${0##*/}.XXXXXXXXXX")}
(cd "$CACHE" &&
git clone "$URI" "$BASE" &&
rsync --exclude='.git' -a "$BASE" "$CWD/")
rm -rf "$CACHE"
}
# Embed (or update) repository
#
# @param 1 - URI of repository
add()
{
local BASE
BASE=$(base "$1")
resolve "$BASE" &>/dev/null || echo "$1" >> "$STORE"
update "$BASE"
}
# Print help
help()
{
cat <<EOF
usage: git embed add <repository>
or: git embed update <directory>
EOF
}
readonly STORE='.gitembedded'
if [ "$0" == "${BASH_SOURCE[0]}" ]
then
"${@:-help}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment