Skip to content

Instantly share code, notes, and snippets.

@mikesigs
Forked from pmiossec/replace_root.sh
Created March 24, 2016 20:30
Show Gist options
  • Select an option

  • Save mikesigs/a1da22915d1376ee889d to your computer and use it in GitHub Desktop.

Select an option

Save mikesigs/a1da22915d1376ee889d to your computer and use it in GitHub Desktop.

Revisions

  1. mikesigs revised this gist Mar 24, 2016. 1 changed file with 25 additions and 10 deletions.
    35 changes: 25 additions & 10 deletions replace_root.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,14 @@
    #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
    @@ -12,25 +20,32 @@ 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
    find . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null
    "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
    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
    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
    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
    git tfs bootstrap
    else
    echo "Aborting! Feel free to run it again after you've verified your changes."
    fi
  2. @pmiossec pmiossec created this gist May 31, 2015.
    36 changes: 36 additions & 0 deletions replace_root.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #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