Changeset View
Changeset View
Standalone View
Standalone View
head/tools/tools/git/importgit
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | error() | ||||
echo "$@" >&2 | echo "$@" >&2 | ||||
if [ -n "$print_usage" ] | if [ -n "$print_usage" ] | ||||
then | then | ||||
usage | usage | ||||
fi | fi | ||||
exit 1 | exit 1 | ||||
} | } | ||||
unset git range commit dry_run | unset git_repo range commit dry_run | ||||
while getopts ":c:g:nr:" o | while getopts ":c:g:nr:" o | ||||
do | do | ||||
case "$o" in | case "$o" in | ||||
c) | c) | ||||
range="${OPTARG}~..${OPTARG}" | range="${OPTARG}~..${OPTARG}" | ||||
;; | ;; | ||||
g) | g) | ||||
Show All 28 Lines | then | ||||
error -u "$range is not a range of commits. Did you mean '-c $range'?" | error -u "$range is not a range of commits. Did you mean '-c $range'?" | ||||
fi | fi | ||||
if [ -z "$git_repo" ] | if [ -z "$git_repo" ] | ||||
then | then | ||||
error -u "-g <repo> argument is mandatory" | error -u "-g <repo> argument is mandatory" | ||||
fi | fi | ||||
git="$git_repo/.git" | if ! type git > /dev/null 2> /dev/null | ||||
if [ ! -d "$git" ] | |||||
then | then | ||||
error "$git_repo does not seem to be a git repo" | error "Install devel/git first" | ||||
fi | fi | ||||
if ! type git > /dev/null 2> /dev/null | GIT="git -C $git_repo" | ||||
if ! $GIT rev-parse --git-dir 2> /dev/null > /dev/null | |||||
then | then | ||||
error "Install devel/git first" | error "$git_repo does not seem to be a git repo" | ||||
fi | fi | ||||
if ! type svn > /dev/null 2> /dev/null | if ! type svn > /dev/null 2> /dev/null | ||||
then | then | ||||
error "Install devel/subversion first" | error "Install devel/subversion first" | ||||
fi | fi | ||||
if [ -n "$(svn status)" ] | if [ -n "$(svn status)" ] | ||||
then | then | ||||
error "Working tree is not clean" | error "Working tree is not clean" | ||||
fi | fi | ||||
if ! svn --non-interactive ls > /dev/null | if ! svn --non-interactive ls > /dev/null | ||||
then | then | ||||
error "Could not communicate with svn server. Is your ssh key loaded?" | error "Could not communicate with svn server. Is your ssh key loaded?" | ||||
fi | fi | ||||
git --git-dir=$git log --format=%H $range | tail -r | while read -r commit | $GIT log --format=%H $range | tail -r | while read -r commit | ||||
do | do | ||||
echo "Applying `git --git-dir=$git show -s --oneline $commit`" | echo "Applying `$GIT show -s --oneline $commit`" | ||||
if [ -n "$(git --git-dir=$git show --diff-filter=CDRTUXB $commit)" ] | if [ -n "$($GIT show --diff-filter=CDRTUXB $commit)" ] | ||||
then | then | ||||
error "Commit performed unsupported change (e.g. delete/rename)" | error "Commit performed unsupported change (e.g. delete/rename)" | ||||
fi | fi | ||||
if [ "$(git --git-dir=$git show -s --format=%P $commit | wc -w)" -ne 1 ] | if [ "$($GIT show -s --format=%P $commit | wc -w)" -ne 1 ] | ||||
then | then | ||||
error "Cannot import merge commits" | error "Cannot import merge commits" | ||||
fi | fi | ||||
git --git-dir=$git diff --diff-filter=A --name-only \ | $GIT diff --diff-filter=A --name-only \ | ||||
${commit}~..$commit | while read -r newfile | ${commit}~..$commit | while read -r newfile | ||||
do | do | ||||
if [ -f "$newfile" ] | if [ -f "$newfile" ] | ||||
then | then | ||||
error "New file $newfile already exists in tree" | error "New file $newfile already exists in tree" | ||||
fi | fi | ||||
done | done | ||||
# The previous while loop ran in a subshell, so we have to check if it | # The previous while loop ran in a subshell, so we have to check if it | ||||
# exited with an error and bail out if so. | # exited with an error and bail out if so. | ||||
ret=$? | ret=$? | ||||
if [ "$ret" -ne 0 ] | if [ "$ret" -ne 0 ] | ||||
then | then | ||||
exit $ret | exit $ret | ||||
fi | fi | ||||
if [ -n "$dry_run" ] | if [ -n "$dry_run" ] | ||||
then | then | ||||
continue | continue | ||||
fi | fi | ||||
git --git-dir=$git show $commit | patch -p 1 -s || \ | $GIT show $commit | patch -p 1 -s || \ | ||||
error "Failed to apply patch" | error "Failed to apply patch" | ||||
git --git-dir=$git diff --diff-filter=A --name-only \ | $GIT diff --diff-filter=A --name-only \ | ||||
${commit}~..$commit | while read -r newfile | ${commit}~..$commit | while read -r newfile | ||||
do | do | ||||
svn add --parents --depth=infinity $newfile || \ | svn add --parents --depth=infinity $newfile || \ | ||||
error "Failed to add new file" | error "Failed to add new file" | ||||
done | done | ||||
# The previous while loop ran in a subshell, so we have to check if it | # The previous while loop ran in a subshell, so we have to check if it | ||||
# exited with an error and bail out if so. | # exited with an error and bail out if so. | ||||
ret=$? | ret=$? | ||||
if [ "$ret" -ne 0 ] | if [ "$ret" -ne 0 ] | ||||
then | then | ||||
exit $ret | exit $ret | ||||
fi | fi | ||||
git --git-dir=$git show -s --format='%B' $commit | svn commit -F - || \ | $GIT show -s --format='%B' $commit | svn commit -F - || \ | ||||
error "Failed to commit" | error "Failed to commit" | ||||
done | done | ||||