3 # Copyright (c) 2015 Ryan Stone. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 echo "Usage: importgit <-c commit | -r c1..c2> -g /path/to/git/repo [-n]" >&2
46 if [ -n "$print_usage" ]
53 unset git_repo range commit dry_run
55 while getopts ":c:g:nr:" o
59 range="${OPTARG}~..${OPTARG}"
71 error -u "Unrecognized argument '-$OPTARG'"
80 error -u "Unrecognized argument $1"
85 error -u "-c or -r argument is mandatory"
88 if ! echo "$range" | egrep -qs '^[^.]+\.\.[^.]*$'
90 error -u "$range is not a range of commits. Did you mean '-c $range'?"
95 error -u "-g <repo> argument is mandatory"
98 if ! type git > /dev/null 2> /dev/null
100 error "Install devel/git first"
103 GIT="git -C $git_repo"
105 if ! $GIT rev-parse --git-dir 2> /dev/null > /dev/null
107 error "$git_repo does not seem to be a git repo"
110 if ! type svn > /dev/null 2> /dev/null
112 error "Install devel/subversion first"
115 if [ -n "$(svn status)" ]
117 error "Working tree is not clean"
120 if ! svn --non-interactive ls > /dev/null
122 error "Could not communicate with svn server. Is your ssh key loaded?"
125 $GIT log --format=%H $range | tail -r | while read -r commit
127 echo "Applying `$GIT show -s --oneline $commit`"
129 if [ -n "$($GIT show --diff-filter=CDRTUXB $commit)" ]
131 error "Commit performed unsupported change (e.g. delete/rename)"
134 if [ "$($GIT show -s --format=%P $commit | wc -w)" -ne 1 ]
136 error "Cannot import merge commits"
139 $GIT diff --diff-filter=A --name-only \
140 ${commit}~..$commit | while read -r newfile
144 error "New file $newfile already exists in tree"
148 # The previous while loop ran in a subshell, so we have to check if it
149 # exited with an error and bail out if so.
161 $GIT show $commit | patch -p 1 -s || \
162 error "Failed to apply patch"
164 $GIT diff --diff-filter=A --name-only \
165 ${commit}~..$commit | while read -r newfile
167 svn add --parents --depth=infinity $newfile || \
168 error "Failed to add new file"
171 # The previous while loop ran in a subshell, so we have to check if it
172 # exited with an error and bail out if so.
179 $GIT show -s --format='%B' $commit | svn commit -F - || \
180 error "Failed to commit"