Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to permit to rcheckin a git repository to a newly created TFVC repository with git-tfs
#Script to permit to rcheckin a git repository to a newly created TFVC repository with git-tfs
#adaptation from my answer: http://stackoverflow.com/questions/645450/insert-a-commit-before-the-root-commit-in-git/30558271#30558271
#usage: pass to the script (1) the url of the tfs server, (2) the tfs path in the repository, (3) the changeset id
#../replace_root.sh "https://urlOfYourTfs/DefaultCollection" "$/EmptyTfs/Trunk" 21
# Command to get Changeset ID
# tf history "$/EmptyTfs/Trunk" /collection:"https://urlOfYourTfs/DefaultCollection" /noprompt /stopafter:1
# Clean up any previous attempt
rm -rf ./.git/refs/replace
git checkout master
git branch -D new-root
tfsServer=$1
tfsPath=$2
tfsChangesetId=$3
gitTfsMetadata="git-tfs-id: [$tfsServer]$tfsPath;C$tfsChangesetId"
echo "Creating a root commit with git-tfs metadatas:"
echo $gitTfsMetadata
root_commit_sha=$(git rev-list --max-parents=0 HEAD)
echo "root_commit_sha=" $root_commit_sha
git checkout --force --orphan new-root
"C:\Program Files\Git\usr\bin\find.exe" . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null
git add -A
GIT_COMMITTER_DATE="2000-01-01T12:00:00" git commit --date==2000-01-01T12:00:00 --allow-empty -m "$gitTfsMetadata"
new_root_commit_sha=$(git rev-parse HEAD)
echo "The commit '$new_root_commit_sha' will be added before existing root commit '$root_commit_sha'..."
parent="parent $new_root_commit_sha"
replacement_commit=$(
git cat-file commit $root_commit_sha | sed "s/author/$parent\nauthor/" | git hash-object -t commit -w --stdin
) || return 3
git replace "$root_commit_sha" "$replacement_commit"
read -r -p "Apply changes? [y/N] " response
response=${response,,} #tolower
if [[ $response =~ ^(yes|y)$ ]]
then
git filter-branch -- --all
rm -rf ./.git/refs/original
rm -rf ./.git/refs/replace
git checkout -f master
git branch -D new-root
git tfs bootstrap
else
echo "Aborting! Feel free to run it again after you've verified your changes."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.