问题
An svn repository I'm mirroring through git-svn has changed URL.
In vanilla svn you'd just do svn switch --relocate old_url_base new_url_base.
How can I do this using git-svn?
Simply changing the svn url in the config file fails.
回答1:
This handles my situation pretty well:
https://git.wiki.kernel.org/index.php/GitSvnSwitch
I cloned using the file:// protocol, and wanted to switch to the http:// protocol.
It is tempting to edit the url setting in the [svn-remote "svn"] section of .git/config, but on its own this does not work. In general you need to follow the following procedure:
- Switch the svn-remote
urlsetting to the new name. - Run
git svn fetch. This needs to fetch at least one new revision from svn! - Change the svn-remote
urlsetting back to the original URL. - Run
git svn rebase -lto do a local rebase (with the changes that came in with the last fetch operation). - Change the svn-remote
urlsetting back to the new URL. - Now,
git svn rebaseshould work again.
Adventurous souls may want to try --rewrite-root.
回答2:
You can see if the following works OK:
If
svn-remote.svn.rewriteRootdoes not exist in config file (.git/config):git config svn-remote.svn.rewriteRoot <currentRepositoryURL>If
svn-remote.svn.rewriteUUIDdoes not exist in config file:git config svn-remote.svn.rewriteUUID <currentRepositoryUUID>The
currentRepositoryUUIDcan be obtained from.git/svn/.metadata.git config svn-remote.svn.url <newRepositoryURL>
回答3:
Unfortunately most of the links in these answers aren't working, so I'm going to duplicate a bit of information from the git wiki for future reference.
This solution worked for me:
Edit the
svn-remoteurl(orfetchpath) in.git/configto point to the new domain/url/pathRun git
git svn fetch. This needs to fetch at least one new revision from svn!If you attempt
git svn rebasenow, you'll get an error message like this:Unable to determine upstream SVN information from working tree historyI think this is because
git svnis confused by the fact that your latest commit prior to the fetch will have agit-svn-idpointing to the old path, which doesn't match the one found in.git/config.As a workaround, change
svn-remoteurl(orfetchpath) back to the original domain/url/pathNow run
git svn rebase -lagain to do a local rebase with the changes that came in with the last fetch operation. This time it will work, apparently becausegit svnwon't be confused by the fact that thegit-svn-idof the new head doesn't match with that found in.git/config.Finally, change
svn-remoteurl(orfetchpath) back to the new domain/url/pathAt this point
git svn rebaseshould work again!
The original information was found here.
回答4:
Git svn relies heavily on the svn URL. Every commit that is imported from svn has a git-svn-id that includes the svn URL.
A valid relocations strategy is to call git-svn clone on the new repository and merge the changes onto that new close. For a more detailed procedure, see this article:
http://www.sanityinc.com/articles/relocating-git-svn-repositories
回答5:
git filter-branch
This script, taken from a blog entry, has worked for me. Supply old and new repo URL as parameter, just like for svn switch --relocate.
The script calls git filter-branch to replace Subversion URLs in the git-svn-id in the commit messages, updates .git/config, and also updates git-svn metadata by recreating it using git svn rebase. While git svn clone might be the more robust solution, the filter-branch approach works much faster for huge repositories (hours vs. days).
#!/bin/sh
# Must be called with two command-line args.
# Example: git-svn-relocate.sh http://old.server https://new.server
if [ $# -ne 2 ]
then
echo "Please invoke this script with two command-line arguments (old and new SVN URLs)."
exit $E_NO_ARGS
fi
# Prepare URLs for regex search and replace.
oldUrl=`echo $1 | awk '{gsub("[\\\.]", "\\\\\\\&");print}'`
newUrl=`echo $2 | awk '{gsub("[\\\&]", "\\\\\\\&");print}'`
filter="sed \"s|^git-svn-id: $oldUrl|git-svn-id: $newUrl|g\""
git filter-branch --msg-filter "$filter" -- --all
sed -i.backup -e "s|$oldUrl|$newUrl|g" .git/config
rm -rf .git/svn
git svn rebase
回答6:
git_fast_filter
Yet faster than git-filter-branch (i.e., minutes instead of hours), but similar in spirit, is to use git_fast_filter. However, this requires a bit more coding, and no neat ready-packed solution exists. In contrast to git-filter-branch, this will create a new repo from an old one. It is assumed that master points to the last SVN commit.
- Clone
git_fast_filterfrom the Gitorious repo. - Create a Python script in the same directory where you cloned
git_fast_filterbased on this Gist, set the executable bit usingchmod +x. Adapt old and new repository paths. (Contents of the script are pasted below, too.) - Initialize a new target repository using
git init, change working directory to this new repo. Execute the following pipe:
(cd path/to/old/repo && git-fast-export --branches --tags --progress=100) | \ path/to/git_fast_filter/commit_filter.py | git-fast-importCopy
.git/config, and perhaps other relevant files in.git/infofrom the old repo to the new repo.- Remove
.git/svn. Make
git-svnaware of the new revision number mappingExecute
git branch refs/remotes/git-svn master- Your git-svn remotes might be called different than
refs/remotes/git-svn, consult.git/config,svn-remotesections
- Your git-svn remotes might be called different than
Execute
git svn info. If this command freezes, something's wrong. It should rebuild the revision number mapping.Remove the fake branch
refs/remotes/git-svn, it will be recreated bygit-svn
- Synchronize by calling
git svn rebase.
Below are the contents of commit_filter.py, replace the values of IN_REPO and OUT_REPO as appropriate:
#!/usr/bin/python
from git_fast_filter import Commit, FastExportFilter
import re
import sys
IN_REPO = "https://svn.code.sf.net/p/matsim/code"
OUT_REPO = "https://svn.code.sf.net/p/matsim/source"
IN_REPO_RE = re.compile("^git-svn-id: %s" % re.escape(IN_REPO), re.M)
OUT_REPO_RE = "git-svn-id: %s" % OUT_REPO
def my_commit_callback(commit):
commit.message = IN_REPO_RE.sub(OUT_REPO_RE, commit.message)
sys.stderr.write(".")
filter = FastExportFilter(commit_callback = my_commit_callback)
filter.run()
回答7:
The above git svn rebase -l solution didn't work for me. I decided to go about it a different way:
- Clone old SVN repo into git repo
oldand new SVN into git reponew - Fetch
oldintonewcd newgit fetch ../oldgit tag old FETCH_HEAD
- Rebase
newon top ofold(should succeed because the trees in the root ofnewand the tip ofoldare identical)git checkout master(Assumes that themasterbranch is pointing at the SVN head. This will be the case with a clean clone; otherwise dcommit before you start.)git rebase --root --onto old
- Rebuild the git-svn metadata of
newto account for the rebasegit update-ref --no-deref refs/remotes/git-svn master(adjust the remote ref depending on how you cloned, e.g. it could berefs/remotes/svn/trunk)rm -r .git/svngit svn info
回答8:
Based off of some of the other responses to this question, I have come up with a Ruby script that handles the git-svn relocating. You can find it at https://gist.github.com/henderea/6e779b66be3580c9a584.
It handles the relocate without checking out another copy, and it even handles the case where there are un-pushed changes in one or more branches (since that breaks the regular logic). It uses stuff from the git filter-branch answer (for the main logic) and the answer about copying branches from one instance of the repo to another (for copying branches with un-pushed changes).
I've been using this to relocate a bunch of git-svn repos that I have for work, and this version of the script (I've been through countless iterations) seems to work for me. It isn't super-fast, but it does seem to handle all of the cases I've encountered and result in a fully-relocated repo.
The script gives you the option to create a copy of the repo before making any changes, so you can use this option to create a backup. Creating a copy is required if you have un-pushed changes in any branches.
The script does not use any gems or other libraries not included in the normal MRI Ruby installation. It does use the readline and fileutils libraries included in MRI.
Hopefully my script will prove useful to someone else. Feel free to make changes to the script.
NOTE: I've only tested this script with git 2.3.0/2.3.1 and Ruby 2.2.0 on OS X 10.10 Yosemite (since that's the environment I use), but I would expect it to work on other environments as well. No guarantees about Windows, though.
来源:https://stackoverflow.com/questions/268736/git-svn-whats-the-equivalent-to-svn-switch-relocate