Beispiel #1
0
        private void DoGitInit(IList <string> branches, IList <string> tags)
        {
            StringBuilder arguments = new StringBuilder("svn init --prefix=svn/ ");

            if (Options.GetUserName() != null)
            {
                arguments.AppendFormat("--username=\"{0}\" ", Options.GetUserName());
            }

            if (Options.IncludeMetaData == false)
            {
                arguments.Append("--no-metadata ");
            }

            if (Options.NoMinimizeUrl)
            {
                arguments.Append("--no-minimize-url ");
            }

            if (Options.RootIsTrunk)
            {
                // Non-standard repository layout.
                // The repository root is effectively trunk.
                //
                // Note: There's a bug of git svn init so that we cannot assign the svn url to --trunk.
                // We assign "/" to --trunk to tell git svn init we're using root as trunk.
                arguments.Append("--trunk=\"/\" ");
            }
            else
            {
                // Add each component to the command that was passed as an argument.
                if (string.IsNullOrWhiteSpace(Options.SubpathToTrunk) == false)
                {
                    arguments.AppendFormat("--trunk=\"{0}\" ", Options.SubpathToTrunk);
                }

                if (Options.NoTags == false)
                {
                    if (tags.Count == 0)
                    {
                        // Fill default tags here so that they can be filtered later
                        tags.Add("tags");
                    }

                    // Process default or user-supplied tags
                    foreach (var t in tags)
                    {
                        arguments.AppendFormat("--tags=\"{0}\" ", t);
                    }
                }

                if (Options.NoBranches == false)
                {
                    if (branches.Count == 0)
                    {
                        // Fill default branches here so that they can be filtered later
                        branches.Add("branches");
                    }

                    // Process default or user-supplied branches
                    foreach (var b in branches)
                    {
                        arguments.AppendFormat("--branches=\"{0}\" ", b);
                    }
                }
            }

            arguments.Append(_svnUrl);

            this._lockBreaker.BreakLocksIfEnabled();
            if (CommandRunner.RunGitSvnInteractiveCommand(arguments.ToString(), Options.GetPassword()) != 0)
            {
                string exceptionMessage = string.Format(ExceptionHelper.ExceptionMessage.FAIL_TO_EXECUTE_COMMAND, $"git {arguments.ToString()}");
                throw new MigrateException(exceptionMessage);
            }

            // Setup author file after the init.
            if (string.IsNullOrWhiteSpace(Options.Authors) == false)
            {
                string args = string.Format("{0} svn.authorsfile {1}",
                                            GitConfigCommandArguments, Options.Authors);
                CommandRunner.Run("git", args);
            }
        }
Beispiel #2
0
        public void FixBranches()
        {
            Log("Start fixing branches...");
            List <string> svnBranches = new List <string>();

            if (_metaInfo.RemoteBranches != null)
            {
                if (_metaInfo.Tags == null)
                {
                    svnBranches = _metaInfo.RemoteBranches.ToList();
                }
                else
                {
                    svnBranches = _metaInfo.RemoteBranches.Except(_metaInfo.Tags).ToList();
                }

                svnBranches.RemoveAll(b => !Regex.IsMatch(b.Trim(), @"^svn\/"));
            }

            if (Options.IsVerbose)
            {
                Log("To fix branches include:");
                foreach (var b in svnBranches)
                {
                    Log(b);
                }
            }

            if (Options.Rebase)
            {
                Log("Rebasing...");
                CommandInfo cmdInfo = CommandInfoBuilder.BuildGitSvnFetchCommandInfo(Options.GetUserName());

                int exitCode = 0;
                if (string.IsNullOrWhiteSpace(Options.GetUserName()))
                {
                    exitCode = RunCommand(cmdInfo);
                }
                else
                {
                    exitCode = CommandRunner.RunGitSvnInteractiveCommand(cmdInfo.Arguments, Options.GetPassword());
                }

                if (exitCode != 0)
                {
                    throw new MigrateException(string.Format(ExceptionHelper.ExceptionMessage.FAIL_TO_EXECUTE_COMMAND, cmdInfo.ToString()));
                }
            }

            // In case of large branches, we build a hash set to boost the query later.
            HashSet <string> localBranchSet = new HashSet <string>(_metaInfo.LocalBranches);

            if (Options.IsVerbose)
            {
                StringBuilder sb = new StringBuilder("We have following local branches:");
                sb.AppendLine();

                foreach (var b in _metaInfo.LocalBranches)
                {
                    sb.AppendLine(b);
                }

                Log(sb.ToString());
            }

            foreach (var b in svnBranches)
            {
                var  branch = Regex.Replace(b, @"^svn\/", "").Trim();
                bool isTrunkBranchOrIsLocalBranch = branch.Equals("trunk", StringComparison.InvariantCulture) ||
                                                    localBranchSet.Contains(branch);

                Log($"Current branch is {b}");
                Log($"Current branch without prefix: {branch}");
                Log($"IsTrunkBranchOrIsLocalBranch: {isTrunkBranchOrIsLocalBranch}");
                if (Options.Rebase && isTrunkBranchOrIsLocalBranch)
                {
                    string      localBranch = branch == "trunk" ? "master" : branch;
                    CommandInfo forceCheckoutLocalBranchCommandInfo = CommandInfoBuilder.BuildForceCheckoutLocalBranchCommandInfo(localBranch);
                    CommandInfo rebaseRemoteBranchCommandInfo       = CommandInfoBuilder.BuildGitRebaseRemoteSvnBranchCommandInfo(branch);

                    RunCommand(CommandInfoBuilder.BuildForceCheckoutLocalBranchCommandInfo(localBranch));

                    RunCommand(CommandInfoBuilder.BuildGitRebaseRemoteSvnBranchCommandInfo(branch));

                    continue;
                }

                if (isTrunkBranchOrIsLocalBranch)
                {
                    Log($"{branch} is trunk branch or local branch, skip.");
                    continue;
                }

                // Now checkout the remote svn branch.
                RunCommand(CommandInfoBuilder.BuildCheckoutSvnRemoteBranchCommandInfo(branch));
            }

            Log("End fixing branches.");
        }