Skip to content

Instantly share code, notes, and snippets.

@frogstarr78
Created April 23, 2010 06:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frogstarr78/376286 to your computer and use it in GitHub Desktop.
Save frogstarr78/376286 to your computer and use it in GitHub Desktop.
Start a new repository based on the commit hash of another repository
#!/bin/bash
# Copyright (c) 2009 Frogstarr78 Software
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# This was written with the desire to create a new git repo based on a partial
# history of commits from another repo. e.g.
#
# A´--B´--C´ topic
# /
# D---E---F---G master
# \
# \
# New Git Repository
#
# In the git repository, with this graph, you'd run this file as:
# git-spawn-repo-from-commit_hash /path/to/new/repo F
#
# the commit_hash argument is optional. You will be prompted for it if you don't provide it.
if [[ $# < 1 ]]
then
echo "Usage: $0 new_git_repository_path [commit_hash]"
exit 1
fi
original_git_repo_path=`pwd`
new_git_repo_path=$1
project_name=`basename $new_git_repo_path`
if [[ $# -eq 2 ]]
then
commit_hash=$2
else
git log | less
# I've set my pager to be cat
echo "Please provide the commit-hash that you want to start on:"
read commit_hash
fi
git format-patch --inline=boundary --full-index -M -B -z $commit_hash
if [[ $? -ne 0 ]]
then
echo "Unable to format-patch based on the provided commit+hash [$commit_hash]."
exit 1
else
cd `dirname $new_git_repo_path`
# I'm living in a ruby world
# and I'm a ruby girl... or guy...
# nm it isn't getting any better! :D
which jeweler
if [[ $? -eq 0 ]]
then
jeweler $project_name
else
which sow
if [[ $? -eq 0 ]]
then
sow $project_name
else
mkdir $new_git_repo_path
fi
fi
cd $new_git_repo_path
mkdir $new_git_repo_path/tmp
mv $original_git_repo_path/*.patch $new_git_repo_path/tmp/
if [[ ! -d '.git' ]]
then
git init
git add .
git commit -m "Initial commit"
fi
done_patching_patches='n'
until [[ $done_patching_patches == 'y' || $done_patching_patches == 'Y' ]]
do
echo "Are you done manually fixing the patch files (y|n) ?"
read done_patching_patches
done
cat tmp/*.patch | git am --whitespace=nowarn
git log
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment