xref: /freebsd-13.1/tools/tools/git/git-svn-init (revision 4a88e7dc)
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_DOCS_REPO=${GIT_DOCS_REPO-git://github.com/freebsd/freebsd-doc.git}
39GIT_SVN_DOCS_ROOT_URI=${GIT_SVN_DOCS_ROOT_URI-svn.freebsd.org/doc}
40GIT_SVN_DOCS_URI=${GIV_SVN_DOCS_URI-repo.freebsd.org/doc}
41
42GIT_PORTS_REPO=${GIT_PORTS_REPO-git://github.com/freebsd/freebsd-ports.git}
43GIT_SVN_PORTS_ROOT_URI=${GIT_SVN_PORTS_ROOT_URI-svn.freebsd.org/ports}
44GIT_SVN_PORTS_URI=${GIT_SVN_PORTS_URI-repo.freebsd.org/ports}
45
46GIT_SRC_REPO=${GIT_SRC_REPO-git://github.com/freebsd/freebsd.git}
47GIT_SVN_SRC_ROOT_URI=${GIT_SVN_SRC_ROOT_URI-svn.freebsd.org/base}
48GIT_SVN_SRC_URI=${GIT_SVN_SRC_URI-repo.freebsd.org/base}
49
50GIT_SVN_DOCS_PUSH_URI=$GIT_SVN_DOCS_URI
51GIT_SVN_PORTS_PUSH_URI=$GIT_SVN_PORTS_URI
52GIT_SVN_SRC_PUSH_URI=$GIT_SVN_SRC_URI
53
54usage()
55{
56	cat <<EOF
57Usage: git-svn-init: [-b base_path] [-n] [-p] [-s]
58
59git-svn-init will instantiate git repositories for src, and ports and connect
60them to the upstream SVN repository. By default it will attempt to do this for
61both ports and src under freebsd in the current working directory.
62-b	Base path for the clone operation (default: freebsd)
63-n	Dry run
64-p	Exclude ports
65-s	Exclude src
66-d	Exclude docs
67
68EOF
69}
70
71clone()
72{
73	echo "Cloning ${3}"
74	${GIT} clone "$repo" -o upstream "$base"/${3}
75}
76
77svn_init()
78{
79	# init git-svn to point to the subversion repo:
80	${GIT} svn init -Thead --rewrite-root=svn+ssh://$1 svn+ssh://$2 .
81
82	# Replace to use upstream instead of the default origin
83	# TODO: Do this from git svn init
84	${GIT} config svn-remote.svn.fetch head:refs/remotes/upstream/trunk
85
86	# Committers need to use proper URL for dcommit
87	${GIT} config svn-remote.svn.pushurl svn+ssh://$3
88
89}
90
91svn_check()
92{
93	cat <<EOF
94[svn-remote "svn"]
95	url = svn+ssh://repo.freebsd.org/base
96	rewriteRoot = svn+ssh://svn.freebsd.org/base
97	pushurl = svn+ssh://repo.freebsd.org/base
98	fetch = head:refs/remotes/upstream/trunk
99EOF
100	[ -z ${DRY_RUN} ] && grep -A4 'svn-remote "svn"' .git/config
101}
102
103svn_connect()
104{
105	# Now make a git branch 'trunk' for git-svn to follow. What we want to
106	# do it set it to point to the final commit in upstream/svn_head.
107	local svn_head_sha=$(git show-ref upstream/svn_head|cut -d" " -f1)
108	${GIT} update-ref refs/remotes/upstream/trunk $svn_head_sha # git-svn really needs this branch
109}
110
111svn_fetch()
112{
113	${GIT} svn fetch
114}
115
116git_pulls()
117{
118	# Get pull requests from the repos:
119	${GIT} config --add remote.upstream.fetch '+refs/pull/*:refs/remotes/upstream/pull/*'
120	${GIT} fetch
121}
122
123git_checkout()
124{
125	# Arrange to have 'master' reference 'trunk'
126	${GIT} checkout trunk
127
128	# Make master reference trunk
129	${GIT} branch --force master trunk
130	${GIT} checkout master
131}
132
133rebase()
134{
135	${GIT} svn rebase
136}
137
138doit()
139{
140	local repo=${1}
141	local base=${2}
142
143	if [ "$3" = "src" ] ; then
144		local svn_root_uri=$GIT_SVN_SRC_ROOT_URI
145		local svn_uri=$GIT_SVN_SRC_URI
146		local svn_push_uri=$GIT_SVN_SRC_PUSH_URI
147	elif [ "$3" = "docs" ] ; then
148		local svn_root_uri=$GIT_SVN_DOCS_ROOT_URI
149		local svn_uri=$GIT_SVN_DOCS_URI
150		local svn_push_uri=$GIT_SVN_DOCS_PUSH_URI
151	elif [ "$3" = "ports" ] ; then
152		local svn_root_uri=$GIT_SVN_PORTS_ROOT_URI
153		local svn_uri=$GIT_SVN_PORTS_URI
154		local svn_push_uri=$GIT_SVN_PORTS_PUSH_URI
155	fi
156
157	clone ${repo} ${base} ${3}
158
159	cd "$base"/${3}
160	svn_init $svn_root_uri $svn_uri $svn_push_uri
161	svn_check $(basename $svn_uri) # get base or ports, not src/ports.
162	svn_connect
163	svn_fetch
164	git_pulls
165	git_checkout
166	rebase
167
168	cd -
169}
170
171ports=1
172source=1
173docs=1
174while getopts "hb:nr:sdp" opt; do
175	case "$opt" in
176		b)
177			base_path="$OPTARG"
178			;;
179		n)
180			DRY_RUN=1
181			;;
182		p)
183			ports=0
184			;;
185		s)
186			source=0
187			;;
188		d)
189			docs=0
190			;;
191		h|*)
192			usage
193			exit 0
194	esac
195done
196
197if [ ! -z "${DRY_RUN}" ] ; then
198	GIT='echo git'
199fi
200
201if [ "$source" -eq 1 ]; then
202	doit ${GIT_SRC_REPO} ${base_path:-freebsd} "src"
203fi
204
205if [ "$ports" -eq 1 ]; then
206	doit ${GIT_PORTS_REPO} ${base_path:-freebsd} "ports"
207fi
208
209if [ "$docs" -eq 1 ]; then
210	doit ${GIT_DOCS_REPO} ${base_path:-freebsd} "docs"
211fi
212