summaryrefslogtreecommitdiff
path: root/exporters/darcs/x2d
blob: ea4bdbda5de34437f745ec564f8ab0c9e81513cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
#
#   x2d - convert git, bzr or hg repos to darcs using fast-export
#
#   Copyright (c) 2008 by Miklos Vajna <vmiklos@frugalware.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

usage()
{
	echo "Usage: x2d -f format repo"
}

die()
{
	echo "$@"
	usage
	exit 1
}

check_up_to_date()
{
	upstreamnum=$(darcs show repo|grep 'Num Patches'|sed 's/.*: //')
	if [ "$upstreamnum" = "$(cd $origin; eval $*)" ]; then
		echo "No remote changes to pull!"
		exit 0
	fi
}

case $1 in
	-h|--help)
		usage
		exit 0
		;;
	-f)
		format="$2"
		shift 2
		;;
esac

[ -n "$format" ] || die "Source format is not given!"

case $format in
	git|bzr|hg)
		;;
	*)
		die "The requested source format is not yet supported!"
		;;
esac

common_opts=""
while [ -n "$2" ]
do
	common_opts="$common_opts $1"
	shift 1
done
origin="$1"
shift 1

[ -d "$origin" ] || die "Source repo does not exist!"

# convert to abspath
cd $origin
origin=$(pwd)

dmark="$origin.darcs/_darcs/fast-import/dfe-marks"
fmark="$origin.darcs/_darcs/fast-import/ffi-marks"

mkdir -p $origin.darcs
cd $origin.darcs

common_opts="$common_opts --logfile $origin.darcs/_darcs/fast-import/log"
pypath="/$(python -c 'from distutils import sysconfig; print sysconfig.get_python_lib()[1:]')/"

if [ ! -f $dmark ]; then
	darcs init
	mkdir -p _darcs/fast-import
	case $format in
		git)
			(cd $origin; git fast-export --export-marks=$fmark HEAD) | \
				darcs-fast-import --export-marks=$dmark $common_opts
			;;
		bzr)
			(cd $origin; bzr fast-export \
				--export-marks=$fmark . ) | darcs-fast-import --export-marks=$dmark $common_opts
			;;
		hg)
			(cd $origin; $pypath/bzrlib/plugins/fastimport/exporters/hg-fast-export.py -r . ) | \
				darcs-fast-import --export-marks=$dmark $common_opts
	esac
else
	case $format in
		git)
			check_up_to_date "git rev-list HEAD |wc -l"
			(cd $origin; git fast-export --export-marks=$fmark --import-marks=$fmark HEAD) | \
				darcs-fast-import --export-marks=$dmark --import-marks=$dmark $common_opts
			;;
		bzr)
			# bzr revno is not good here, because at merges
			# it produces less revision than the number we
			# have in darcs
			check_up_to_date "bzr log --include-merges |grep -c revno:"
			(cd $origin; bzr fast-export \
				--export-marks=$fmark --import-marks=$fmark . ) | \
				darcs-fast-import --export-marks=$dmark --import-marks=$dmark $common_opts
			;;
		hg)
			check_up_to_date 'echo $(($(hg tip --template "{rev}")+1))'
			(cd $origin; $pypath/bzrlib/plugins/fastimport/exporters/hg-fast-export.py -r . ) | \
				darcs-fast-import --export-marks=$dmark --import-marks=$dmark $common_opts
			;;
	esac
fi