Skip to content

Instantly share code, notes, and snippets.

@nullivex
Last active December 15, 2015 06:29
Show Gist options
  • Select an option

  • Save nullivex/5216559 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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"
@nullivex
Copy link
Copy Markdown
Author

Here is example usage and output

./gitcopy.sh lib-db ostp/lib/db.php vidcache/lib/db.php "s@lib/db\.php@db.php@g"
Getting patches from: ostp/lib/db.php

/tmp/gitcopy/db.php/0001-initial-commit.patch
/tmp/gitcopy/db.php/0002-restructured-entire-order-inventory-system.patch
/tmp/gitcopy/db.php/0003-changed-items-to-catalog.patch
/tmp/gitcopy/db.php/0004-Major-update-spazm.patch
/tmp/gitcopy/db.php/0005-new-item-personality-system.patch
/tmp/gitcopy/db.php/0006-lots-of-changes.patch
/tmp/gitcopy/db.php/0007-added-new-brands-manager.patch
/tmp/gitcopy/db.php/0008-new-account-management-structure-with-db-structure.patch
/tmp/gitcopy/db.php/0009-all-item-basics-now-working-in-order.patch
/tmp/gitcopy/db.php/0010-dos2unix-entire-.php-tree-Item-Db-upgrades.patch
Getting patches from: vidcache/lib/db.php
/tmp/gitcopy/db.php/0011-various-fixes-and-part-of-79-and-83.patch
/tmp/gitcopy/db.php/0012-fix-Db-bug-when-using-array-of-selectors-on-update.patch
/tmp/gitcopy/db.php/0013-upgraded-to-work-with-php-E_STRICT-on-and-upgraded-t.patch
/tmp/gitcopy/db.php/0014-fixes-to-array-by-reference-stuff-fixed-install-scri.patch
Applying stream process: s@lib/db\.php@db.php@g

Done! To apply the patches: cd /home/btong/lib-db; git am /tmp/gitcopy/db.php/*.patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment