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
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)
git checkout --force --orphan new-root
find . -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"
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
@sawpresto

This comment has been minimized.

Copy link

commented Oct 16, 2015

Thanks for creating this, pmiossec! I'm running into a couple of issues when trying to run this script from the Git repo (not the TFVC git-tfs repo). When I put in the only changeset in the TFVC repo (which would normally have matched with the first commit from git-tfs), after running and switching to a new branch, I got the error: pathspec '[{repo}]{project};C289' did not match any file(s) known to git. fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree. Any ideas? I suspect the error is happening at line 16. I'm using a Windows and it looks like the script did start running. Thanks!

@mikesigs

This comment has been minimized.

Copy link

commented Mar 24, 2016

@sawpresto - Checkout my fork. I got it working on Windows. https://gist.github.com/mikesigs/a1da22915d1376ee889d

One problem was the find command in this script is intended to use the bash version of that command, but the Windows one is used instead. To fix that I reference the find command by its full path. Then on the next line (starting with GIT_COMMITTER_DATE) I had to enclose $gitTfsMetadata in double quotes. I also wrapped the last 6 commands in a conditional, prompting the user to continue or not. Reason being, up to that point everything is reversable by just deleting .git/refs/replace and the new-root branch. In fact, I preferred to run those last 6 commands manually. Also, make a backup before running this!

My script isn't perfect either. I'm a bash script newb. But it worked on Windows, and I hope that can help you/someone else.

But thanks most of all to @pmiossec for the original!

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.