Skip to content

Instantly share code, notes, and snippets.

@jamischarles
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamischarles/c50c121322f4fb8b58d1 to your computer and use it in GitHub Desktop.
Save jamischarles/c50c121322f4fb8b58d1 to your computer and use it in GitHub Desktop.
Move Several git files over to a new repo with history. Very optimistic about errors
#!/bin/bash
# $ ./gitmove [destGitRepo] [src_file]
DEST_FOLDER=$1
# this will take all parameters AFTER the first. So you can give a list, or a glob (which is expanded into separate params)
SOURCE_FILES="${@:2}"
# SOURCE_FILE=$2
STARTING_FOLDER=$(pwd)
# files=("$@")
# FILES=($SOURCE_FILE)
# FILES=($1)
# FILES=/path/to/*
for FILE in $SOURCE_FILES
do
echo "\n\n **********************************"
echo "Processing "$FILE" file..."
# back to starting folder
cd $STARTING_FOLDER
echo "Changing to $STARTING_FOLDER"
# get the earliest hash of a source file to copy
HASH=$(git log --format=%H $FILE | tail -1)
# create patch files for that hash range for the given file ($1)
# use the parent hash, so it includes the commit that generates the file
git format-patch -o $DEST_FOLDER/tmp_patch $HASH^..HEAD $FILE
# Optional - Apply the patch if you're ready...
# apply the patches in the destination repo
cd $DEST_FOLDER && git am tmp_patch/*.patch
# FIXME: Have to delete or name and run patches by filename, or else patches will fail when the same ones are re-applied on the
# next cycle
# Optional - Auto-remove the patch folder
echo "Deleting patch folder"
rm -rf tmp_patch
done
# echo $files
# pushd $SOURCE_FILE
# echo $SOURCE_FILE
# for file in $files; do
# echo 'inside'
# echo $file
# done
# popd $SOURCE_FILE
# echo $HASH
# OPTIONAL - DON'T DO THIS if you want it in a different folder / file name in dest
# apply the patches in the destination repo
# cd $DEST_FOLDER && git am tmp_patch/*.patch
# If there are no errors, proceed
# bash doesn't allow changing folder for parent process
# apply the patches in the dest folder
#git am
# TODO:
# - how can we run multiple files through this?
# What happens when file is moved/renamed in source or target repo (after)?
# In target repo, it wipes history and blame for the file. LAME!!!!
# TODO: Find a solution for that.
# Is history preserved?
# What to do when file already exists in target repo?
@jamischarles
Copy link
Author

TODO: Get this working with larger sets of files (Glob causes param length issues).

Consider making this an NPM CLI. Add lots of fixtures and tests...

Graceful failure as well...

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