Skip to content

Instantly share code, notes, and snippets.

@unqueued
Created December 1, 2023 00:47
Show Gist options
  • Save unqueued/06b5a5c14daa8224a659c5610dce3132 to your computer and use it in GitHub Desktop.
Save unqueued/06b5a5c14daa8224a659c5610dce3132 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -o errexit
set -x
# TODO: Want to have it be that TARGET is just the place where the repo will be created...
# TODO: Make it better detect existing repo
# TODO: Should probably just have confirmations before doing anything destrcutive....
# TODO: Do that thing where you make it catch any errors from any command
SOURCE=${SOURCE:-.}
TARGET=${TARGET:-/tmp/"$(basename "$SOURCE")"}
TARGET_UUID=${TARGET_UUID:-""}
# For now, don't let there be defaults...
if [ "$SOURCE" == "." ]; then
echo "MUST SET SOURCE"
exit 1
fi
KEYSTEMP=$(mktemp)
trap "rv=\$?; rm $KEYSTEMP; exit \$rv" EXIT
function init_target {
echo "init_target()"
git init -- "${TARGET}"
# This is so I don't have to deal with nastiness of orphan branches
( cd -- "${TARGET}" && git checkout -b master && git commit --allow-empty -q -m "Initial Commit" && git checkout -b git-annex )
}
function copy_source_keys {
echo "copy_source_keys()"
# TODO: Handle directory stuff better
# TODO: Just pipe keys list directly instead of using tmpfile.
cd "${SOURCE}"
set +x
echo "Building list of keys to copy"
while IFS= read -r line; do
basename -- "$(readlink -- "${line}")"
done > "${KEYSTEMP}" < <(find "${SOURCE}" -type l)
echo "Making directories in ${TARGET}"
while read -r line; do
mkdir -p "${TARGET}/${line}"
done < <(git annex examinekey --batch --format='${hashdirlower}\n' < "${KEYSTEMP}")
echo "Copying keys from ${SOURCE} git-annex branch to ${TARGET}"
git show git-annex:uuid.log > "${TARGET}/uuid.log"
git show git-annex:remote.log > "${TARGET}/remote.log"
git show git-annex:trust.log > "${TARGET}/trust.log"
while read -r line; do
git show git-annex:$line > "${TARGET}/${line}"
git show git-annex:"${line}.cnk" &> /dev/null && git show git-annex:"${line}.cnk" > "${TARGET}/${line}.cnk"
git show git-annex:"${line}.met" &> /dev/null && git show git-annex:"${line}.met" > "${TARGET}/${line}.met"
done < <(git annex examinekey --batch --format='${hashdirlower}${key}.log\n' < "${KEYSTEMP}")
set -x
}
function import_target {
set +x
cd "${TARGET}"
git add . && git commit -q -m "Imported"
git checkout master
# cp -P "${SOURCE}/"* "${TARGET}"
# Deal argument list too long
echo "Copying contents to new repo"
( cd "${SOURCE}" && tar --exclude=.git -cf - . | pv | tar xf - -C "${TARGET}")
git add . && git commit -q -m "IMPORTED"
git annex reinit $TARGET_UUID
echo "Checking for existence of annex-defaults.sh"
which annex-defaults.sh && annex-defaults.sh
set -x
}
echo "Starting"
echo "\$SOURCE: ${SOURCE}"
echo "\$TARGET: ${TARGET}"
echo "\$TARGET_UUID: ${TARGET_UUID}"
init_target
copy_source_keys
import_target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment