Skip to content

Instantly share code, notes, and snippets.

@calebpower
Created January 4, 2025 21:01
Show Gist options
  • Select an option

  • Save calebpower/fba15176282b00ef9a5dd368155952aa to your computer and use it in GitHub Desktop.

Select an option

Save calebpower/fba15176282b00ef9a5dd368155952aa to your computer and use it in GitHub Desktop.
Port tags arranged in SVN directories to annotated tags in GIT
#!/usr/local/bin/bash
# Prerequisites:
# 1. Clone two instances of the same repo into primary_repo and backup_repo
# 2. On primary_repo, rebase where necessary (or git filter-repo)
if [ "$#" -ne 2 ]; then
printf 'Usage: %s <primary_repo> <tags_path>\n' "$0"
printf 'Example: %s /freshports/primary_repo /freshports/backup_repo/path/to/tags\n' "$0"
exit 1
fi
PRIMARY_REPO="$1"
TAGS_PATH="$2"
PARENT_DIR=$(pwd)
cd "${TAGS_PATH}"
declare -A datetimes
declare -A messages
# here, parse out all the tags and their respective timestamps/messages
for tag in $(ls); do
tag_log="$(git log -n 1 --pretty=format:"%ad %s" --date=iso $tag)"
tag_date=$(echo $tag_log | cut -d' ' -f1,2,3)
tag_msg=$(echo $tag_log | cut -d' ' -f4- | sed "s/^'//;s/'$//")
datetimes[$tag]="${tag_date}"
messages[$tag]="${tag_msg}"
done
cd "${PARENT_DIR}" && cd "${PRIMARY_REPO}"
declare -A commits
# here, figure out which commits line up with the tags
for tag in "${!datetimes[@]}"; do
commit=$(git log --until="${datetimes[$tag]}" -n 1 --author-date-order --pretty=format:"%h")
commits[$commit]="${tag}"
printf '%s (%s): %s: %s\n' "${tag}" "${commit}" "${datetimes[$tag]}" "${messages[$tag]}"
done
# go ahead and apply the annotated tag
for commit in "${!commits[@]}"; do
tag="${commits[$commit]}"
git checkout $commit
git tag -a "${tag}" -m "${messages[$tag]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment