Last active
December 15, 2015 06:29
-
-
Save nullivex/5216559 to your computer and use it in GitHub Desktop.
Dirty script to copy a file with history from one git repo to another and allows stream changes to all the patches using regex. Could definitely be improved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| odir=`pwd` | |
| [ $# -lt 3 ] && echo "Usage: $0 <new repo> {<path>} {<proc>}" | |
| new_repo= | |
| ptr=0 | |
| proc_ptr=0 | |
| path_ptr=0 | |
| for f in $@; do | |
| ptr=$(expr $ptr + 1) | |
| # grab the repo | |
| if [ $ptr -eq 1 ]; then | |
| new_repo=${!ptr} | |
| continue | |
| fi | |
| #separate proc arguments | |
| if [[ "${!ptr}" =~ ^s.+g$ ]]; then | |
| proc_ptr=$(expr $proc_ptr + 1) | |
| proc[$proc_ptr]=${!ptr} | |
| continue | |
| fi | |
| #path arguments | |
| path_ptr=$(expr $path_ptr + 1) | |
| path[$path_ptr]=${!ptr} | |
| continue | |
| done | |
| #setup tmp dir | |
| tmpdir="/tmp/gitcopy/$(basename ${path[1]})" | |
| rm -rf $tmpdir | |
| mkdir -p $tmpdir | |
| # get all the patches | |
| ptr=0 | |
| for p in "${path[@]}"; do | |
| echo "Getting patches from: $p" | |
| # change directory to git repo | |
| cd $odir | |
| cd $(dirname $p) | |
| file=$(basename $p) | |
| # get the initial commit this path appears in | |
| commit=$(git log $file | grep ^commit | tail -1 | awk '{print $2}') | |
| # get the root commit (on first path only) | |
| if [ $ptr -eq 0 ]; then | |
| echo | |
| git format-patch -o $tmpdir --root $commit -- $file | |
| fi | |
| #get additional patches | |
| git format-patch --start-number $(ls -l $tmpdir | wc -l) -o $tmpdir $commit -- $file | |
| ptr=$(expr $ptr + 1) | |
| done | |
| # do stream processing on all files | |
| files=$(find $tmpdir -name "*.patch") | |
| for p in "${proc[@]}"; do | |
| echo "Applying stream process: $p" | |
| echo $files | xargs sed -i "$p" | |
| done | |
| #apply patches to new repo | |
| cd $odir | |
| echo | |
| echo "Done! To apply the patches: cd $odir/$new_repo; git am $tmpdir/*.patch" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is example usage and output