xref: /freebsd-12.1/tools/tools/git/git-svn-init (revision 94b92b56)
1#!/bin/sh
2
3# $FreeBSD$
4
5# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6#
7#  Copyright(c) 2018 Intel Corporation.
8#
9#  Redistribution and use in source and binary forms, with or without
10#  modification, are permitted provided that the following conditions
11#  are met:
12#  1. Redistributions of source code must retain the above copyright
13#     notice, this list of conditions and the following disclaimer.
14#  2. Redistributions in binary form must reproduce the above copyright
15#     notice, this list of conditions and the following disclaimer in the
16#     documentation and/or other materials provided with the distribution.
17#
18#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28#  SUCH DAMAGE.
29
30# This is the codified version of what was/is on the wiki page for using git in
31# your workflow. It sets up proper repositories, with the correct remotes.
32
33# Environment variables which can be overridden if desired. Not worth
34# parameterizing them.
35GIT_IN_PATH=$(which git)
36GIT=${GIT-${GIT_IN_PATH}}
37
38GIT_PORTS_REPO=${GIT_PORTS_REPO-git://github.com/freebsd/freebsd-ports.git}
39GIT_SVN_PORTS_ROOT_URI=${GIT_SVN_PORTS_ROOT_URI-svn.freebsd.org/ports}
40GIT_SVN_PORTS_URI=${GIT_SVN_PORTS_URI-repo.freebsd.org/ports}
41
42GIT_SRC_REPO=${GIT_SRC_REPO-git://github.com/freebsd/freebsd.git}
43GIT_SVN_SRC_ROOT_URI=${GIT_SVN_SRC_ROOT_URI-svn.freebsd.org/base}
44GIT_SVN_SRC_URI=${GIT_SVN_SRC_URI-repo.freebsd.org/base}
45
46GIT_SVN_PORTS_PUSH_URI=$GIT_SVN_PORTS_URI
47GIT_SVN_SRC_PUSH_URI=$GIT_SVN_SRC_URI
48
49usage()
50{
51	cat <<EOF
52Usage: git-svn-init: [-b base_path] [-n] [-p] [-s]
53
54git-svn-init will instantiate git repositories for src, and ports and connect
55them to the upstream SVN repository. By default it will attempt to do this for
56both ports and src under freebsd in the current working directory.
57-b	Base path for the clone operation (default: freebsd)
58-n	Dry run
59-p	Exclude ports
60-s	Exclude src
61
62EOF
63}
64
65clone()
66{
67	echo "Cloning ${3}"
68	${GIT} clone "$repo" -o upstream "$base"/${3}
69}
70
71svn_init()
72{
73	# init git-svn to point to the subversion repo:
74	${GIT} svn init -Thead --rewrite-root=svn+ssh://$1 svn+ssh://$2 .
75
76	# Replace to use upstream instead of the default origin
77	# TODO: Do this from git svn init
78	${GIT} config svn-remote.svn.fetch head:refs/remotes/upstream/trunk
79
80	# Committers need to use proper URL for dcommit
81	${GIT} config svn-remote.svn.pushurl svn+ssh://$3
82
83}
84
85svn_check()
86{
87	cat <<EOF
88[svn-remote "svn"]
89	url = svn+ssh://repo.freebsd.org/base
90	rewriteRoot = svn+ssh://svn.freebsd.org/base
91	pushurl = svn+ssh://repo.freebsd.org/base
92	fetch = head:refs/remotes/upstream/trunk
93EOF
94	[ -z ${DRY_RUN} ] && grep -A4 'svn-remote "svn"' .git/config
95}
96
97svn_connect()
98{
99	# Now make a git branch 'trunk' for git-svn to follow. What we want to
100	# do it set it to point to the final commit in upstream/svn_head.
101	local svn_head_sha=$(git show-ref upstream/svn_head|cut -d" " -f1)
102	${GIT} update-ref refs/remotes/upstream/trunk $svn_head_sha # git-svn really needs this branch
103}
104
105svn_fetch()
106{
107	${GIT} svn fetch
108}
109
110git_pulls()
111{
112	# Get pull requests from the repos:
113	${GIT} config --add remote.upstream.fetch '+refs/pull/*:refs/remotes/upstream/pull/*'
114	${GIT} fetch
115}
116
117git_checkout()
118{
119	# Arrange to have 'master' reference 'trunk'
120	${GIT} checkout trunk
121
122	# Make master reference trunk
123	${GIT} branch --force master trunk
124	${GIT} checkout master
125}
126
127rebase()
128{
129	${GIT} svn rebase
130}
131
132doit()
133{
134	local repo=${1}
135	local base=${2}
136
137	if [ "$3" = "src" ] ; then
138		local svn_root_uri=$GIT_SVN_SRC_ROOT_URI
139		local svn_uri=$GIT_SVN_SRC_URI
140		local svn_push_uri=$GIT_SVN_SRC_PUSH_URI
141	else
142		local svn_root_uri=$GIT_SVN_PORTS_ROOT_URI
143		local svn_uri=$GIT_SVN_PORTS_URI
144		local svn_push_uri=$GIT_SVN_PORTS_PUSH_URI
145	fi
146
147	clone ${repo} ${base} ${3}
148
149	cd "$base"/${3}
150	svn_init $svn_root_uri $svn_uri $svn_push_uri
151	svn_check $(basename $svn_uri) # get base or ports, not src/ports.
152	svn_connect
153	svn_fetch
154	git_pulls
155	git_checkout
156	rebase
157
158	cd -
159}
160
161ports=1
162source=1
163while getopts "hb:nr:sp" opt; do
164	case "$opt" in
165		b)
166			base_path="$OPTARG"
167			;;
168		n)
169			DRY_RUN=1
170			;;
171		p)
172			ports=0
173			;;
174		s)
175			source=0
176			;;
177		h|*)
178			usage
179			exit 0
180	esac
181done
182
183if [ ! -z "${DRY_RUN}" ] ; then
184	GIT='echo git'
185fi
186
187if [ "$source" -eq 1 ]; then
188	doit ${GIT_SRC_REPO} ${base_path:-freebsd} "src"
189fi
190
191if [ "$ports" -eq 1 ]; then
192	doit ${GIT_PORTS_REPO} ${base_path:-freebsd} "ports"
193fi
194