How do I access svn branches using git-svn with a

2019-06-27 15:55发布

The standard repo layout in svn is as follows.

/trunk
/branches
    featureX
    featureY
/tags
    1.0
    2.0

The repository I'm working with is a much flatter structure.

trunk
featureX
featureY

Essentially, trunk is at the same level as the other branches. I can't use the -s or -b option with git svn init because of this.

How would I pull in trunk as the git branch master and pull in featureX as a git branch of the same name? I don't care about any other branches or tags.

I've seen similar questions and people have suggested restructuring the svn repository. This is off the table as far as this question is concerned.

2条回答
看我几分像从前
2楼-- · 2019-06-27 16:39

I've figured out a way to pull in multiple branches from an arbitrary svn repository structure.

The -b option for git svn init will only work if all branches are grouped together within a subdirectory in the repository, such as in the standard layout. If all branches, including the trunk, are side by side in the same folder, this won't really work. You can pull in selected branches from the svn repository by essentially creating multiple "trunks" int your git repository.

Assume the flat structure from the question with the three branches trunk, featureX, and featureY.

  1. Instantiate your git repository.

    mkdir myproject
    cd myproject
    git svn init url:to/svn/repo -T trunk
    

    This will create a git repository with svn metadata in the .git/config file.

  2. Open the config file and examine the svn metadata

    vim .git/config
    

    Your config file will look something like this.

    [core]
      repositoryformatversion = 0
      filemode = true
      bare = false
      logallrefupdates = true
      autocrlf = false
    [svn-remote "svn"]
      url = url:to/svn/repo
      fetch = trunk:refs/remotes/trunk
    

    The svn-remote header defines a reference called "svn" pointing to your svn repository. The fetch parameter tells git-svn where to pull new revisions from within the svn repository. Now we need to tell git-svn about the other branch we're interested in.

  3. Duplicate the svn-remote section

    Copy the entire svn-remote section of the config file and paste it below the existing config text.

    [core]
      repositoryformatversion = 0
      filemode = true
      bare = false
      logallrefupdates = true
      autocrlf = false
    [svn-remote "svn"]
      url = url:to/svn/repo
      fetch = trunk:refs/remotes/trunk
    [svn-remote "svn"]
      url = url:to/svn/repo
      fetch = trunk:refs/remotes/trunk
    
  4. Modify the new svn-remote section

    Change the name of the svn-remote section header and the name of the branch that it points to.

    [core]
      repositoryformatversion = 0
      filemode = true
      bare = false
      logallrefupdates = true
      autocrlf = false
    [svn-remote "svn"]
      url = url:to/svn/repo
      fetch = trunk:refs/remotes/trunk
    [svn-remote "svn-featureX"]
      url = url:to/svn/repo
      fetch = featureX:refs/remotes/featureX
    

    Now git-svn will track both svn branches. You can use the names "svn" and "svn-featureX" with any git-svn command that takes an [svn-remote] parameter. You can use the names "trunk" and "featureX" with any git command that takes a remote branch name as a parameter.

This solution won't scale well, and is a bit of a hack to work with a malformed svn repository. So long as you only need to track a handful of svn branches, this will work just fine. If the number of svn branches you need to work with becomes too large, take a serious look at restructuring your svn repository to the standard layout.

查看更多
迷人小祖宗
3楼-- · 2019-06-27 16:58

As this article illustrates, you can write rules for running an importer tool like svn2git
(you have less complete version of svn2git here)

See those examples for rules that you could rewrite in order to match those flat SVN directories/branches and declare them as Git branches.

查看更多
登录 后发表回答