git-svn: what's the equivalent to `svn switch --relocate`?

后端 未结 8 529
深忆病人
深忆病人 2020-11-29 17:40

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.

相关标签:
8条回答
  • 2020-11-29 18:14

    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.

    0 讨论(0)
  • 2020-11-29 18:17

    You can see if the following works OK:

    1. If svn-remote.svn.rewriteRoot does not exist in config file (.git/config):

      git config svn-remote.svn.rewriteRoot <currentRepositoryURL>
      
    2. If svn-remote.svn.rewriteUUID does not exist in config file:

      git config svn-remote.svn.rewriteUUID <currentRepositoryUUID>
      

      The currentRepositoryUUID can be obtained from .git/svn/.metadata.

    3. git config svn-remote.svn.url <newRepositoryURL>

    0 讨论(0)
  • 2020-11-29 18:26

    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
    
    0 讨论(0)
  • 2020-11-29 18:29

    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

    0 讨论(0)
  • 2020-11-29 18:29

    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.

    1. Clone git_fast_filter from the Gitorious repo.
    2. Create a Python script in the same directory where you cloned git_fast_filter based on this Gist, set the executable bit using chmod +x. Adapt old and new repository paths. (Contents of the script are pasted below, too.)
    3. Initialize a new target repository using git init, change working directory to this new repo.
    4. 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-import
      
    5. Copy .git/config, and perhaps other relevant files in .git/info from the old repo to the new repo.

    6. Remove .git/svn.
    7. Make git-svn aware of the new revision number mapping

      1. Execute git branch refs/remotes/git-svn master

        • Your git-svn remotes might be called different than refs/remotes/git-svn, consult .git/config, svn-remote sections
      2. Execute git svn info. If this command freezes, something's wrong. It should rebuild the revision number mapping.

      3. Remove the fake branch refs/remotes/git-svn, it will be recreated by git-svn

    8. 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()
    
    0 讨论(0)
  • 2020-11-29 18:31

    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-remote url (or fetch path) in .git/config to point to the new domain/url/path

    • Run git git svn fetch. This needs to fetch at least one new revision from svn!

    • If you attempt git svn rebase now, you'll get an error message like this:

      Unable to determine upstream SVN information from working tree history
      

      I think this is because git svn is confused by the fact that your latest commit prior to the fetch will have a git-svn-id pointing to the old path, which doesn't match the one found in .git/config.

    • As a workaround, change svn-remote url (or fetch path) back to the original domain/url/path

    • Now run git svn rebase -l again to do a local rebase with the changes that came in with the last fetch operation. This time it will work, apparently because git svn won't be confused by the fact that the git-svn-id of the new head doesn't match with that found in .git/config.

    • Finally, change svn-remote url (or fetch path) back to the new domain/url/path

    • At this point git svn rebase should work again!

    The original information was found here.

    0 讨论(0)
提交回复
热议问题