public virtual void RenameBranchExistingSection()
        {
            string       branch = "b1";
            StoredConfig config = git.GetRepository().GetConfig();

            config.SetBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER, ConfigConstants
                              .CONFIG_KEY_REBASE, true);
            config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER, "a", "a"
                             );
            config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a", "b");
            config.Save();
            NUnit.Framework.Assert.IsNotNull(git.BranchRename().SetNewName(branch).Call());
            config = git.GetRepository().GetConfig();
            CollectionAssert.AreEquivalent(new string[] { "b", "a" }, config.GetStringList(ConfigConstants
                                                                                           .CONFIG_BRANCH_SECTION, branch, "a"));
        }
예제 #2
0
 /// <exception cref="NGit.Api.Errors.RefNotFoundException">
 /// if the old branch can not be found (branch with provided old
 /// name does not exist or old name resolves to a tag)
 /// </exception>
 /// <exception cref="NGit.Api.Errors.InvalidRefNameException">
 /// if the provided new name is <code>null</code> or otherwise
 /// invalid
 /// </exception>
 /// <exception cref="NGit.Api.Errors.RefAlreadyExistsException">if a branch with the new name already exists
 ///     </exception>
 /// <exception cref="NGit.Api.Errors.DetachedHeadException">
 /// if rename is tried without specifying the old name and HEAD
 /// is detached
 /// </exception>
 /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
 public override Ref Call()
 {
     CheckCallable();
     if (newName == null)
     {
         throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid
                                                                , "<null>"));
     }
     try
     {
         string fullOldName;
         string fullNewName;
         if (repo.GetRef(newName) != null)
         {
             throw new RefAlreadyExistsException(MessageFormat.Format(JGitText.Get().refAlreadyExists1
                                                                      , newName));
         }
         if (oldName != null)
         {
             Ref @ref = repo.GetRef(oldName);
             if (@ref == null)
             {
                 throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved
                                                                     , oldName));
             }
             if (@ref.GetName().StartsWith(Constants.R_TAGS))
             {
                 throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().renameBranchFailedBecauseTag
                                                                     , oldName));
             }
             fullOldName = @ref.GetName();
         }
         else
         {
             fullOldName = repo.GetFullBranch();
             if (ObjectId.IsId(fullOldName))
             {
                 throw new DetachedHeadException();
             }
         }
         if (fullOldName.StartsWith(Constants.R_REMOTES))
         {
             fullNewName = Constants.R_REMOTES + newName;
         }
         else
         {
             fullNewName = Constants.R_HEADS + newName;
         }
         if (!Repository.IsValidRefName(fullNewName))
         {
             throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid
                                                                    , fullNewName));
         }
         RefRename        rename       = repo.RenameRef(fullOldName, fullNewName);
         RefUpdate.Result renameResult = rename.Rename();
         SetCallable(false);
         if (RefUpdate.Result.RENAMED != renameResult)
         {
             throw new JGitInternalException(MessageFormat.Format(JGitText.Get().renameBranchUnexpectedResult
                                                                  , renameResult.ToString()));
         }
         if (fullNewName.StartsWith(Constants.R_HEADS))
         {
             string shortOldName = Sharpen.Runtime.Substring(fullOldName, Constants.R_HEADS.Length
                                                             );
             StoredConfig repoConfig = repo.GetConfig();
             // Copy all configuration values over to the new branch
             foreach (string name in repoConfig.GetNames(ConfigConstants.CONFIG_BRANCH_SECTION
                                                         , shortOldName))
             {
                 string[] values = repoConfig.GetStringList(ConfigConstants.CONFIG_BRANCH_SECTION,
                                                            shortOldName, name);
                 if (values.Length == 0)
                 {
                     continue;
                 }
                 // Keep any existing values already configured for the
                 // new branch name
                 string[] existing = repoConfig.GetStringList(ConfigConstants.CONFIG_BRANCH_SECTION
                                                              , newName, name);
                 if (existing.Length > 0)
                 {
                     string[] newValues = new string[values.Length + existing.Length];
                     System.Array.Copy(existing, 0, newValues, 0, existing.Length);
                     System.Array.Copy(values, 0, newValues, existing.Length, values.Length);
                     values = newValues;
                 }
                 repoConfig.SetStringList(ConfigConstants.CONFIG_BRANCH_SECTION, newName, name, Arrays
                                          .AsList(values));
             }
             repoConfig.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName);
             repoConfig.Save();
         }
         Ref resultRef = repo.GetRef(newName);
         if (resultRef == null)
         {
             throw new JGitInternalException(JGitText.Get().renameBranchFailedUnknownReason);
         }
         return(resultRef);
     }
     catch (IOException ioe)
     {
         throw new JGitInternalException(ioe.Message, ioe);
     }
 }