コード例 #1
0
ファイル: RefUpdateTest.cs プロジェクト: kkl713/GitSharp
        public void testRenameCurrentBranch()
        {
            ObjectId rb = db.Resolve("refs/heads/b");

            db.WriteSymref(Constants.HEAD, "refs/heads/b");
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            Assert.IsTrue(rb.Equals(oldHead), "internal test condition, b == HEAD");
            RefLogWriter.WriteReflog(db, rb, rb, "Just a message", "refs/heads/b");
            Assert.IsTrue(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/b")).Exists, "no log on old branch");
            RefRename renameRef = db.RenameRef("refs/heads/b",
                                               "refs/heads/new/name");

            RefUpdate.RefUpdateResult result = renameRef.Rename();
            Assert.AreEqual(RefUpdate.RefUpdateResult.Renamed, result);
            Assert.AreEqual(rb, db.Resolve("refs/heads/new/name"));
            Assert.IsNull(db.Resolve("refs/heads/b"));
            Assert.AreEqual("Branch: renamed b to new/name", db.ReflogReader(
                                "new/name").getLastEntry().getComment());
            Assert.IsFalse(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/b")).Exists);
            Assert.AreEqual(rb, db.Resolve(Constants.HEAD));
            Assert.AreEqual(2, db.ReflogReader("new/name").getReverseEntries().Count);
            Assert.AreEqual("Branch: renamed b to new/name", db.ReflogReader("new/name").getReverseEntries()[0].getComment());
            Assert.AreEqual("Just a message", db.ReflogReader("new/name").getReverseEntries()[1].getComment());
        }
コード例 #2
0
ファイル: RefUpdateTest.cs プロジェクト: kkl713/GitSharp
        public void testRenameRefNameColission1avoided()
        {
            // setup
            ObjectId rb = db.Resolve("refs/heads/b");

            db.WriteSymref(Constants.HEAD, "refs/heads/a");
            RefUpdate updateRef = db.UpdateRef("refs/heads/a");

            updateRef.NewObjectId = rb;
            updateRef.SetRefLogMessage("Setup", false);
            Assert.AreEqual(RefUpdate.RefUpdateResult.FastForward, updateRef.Update());
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            Assert.IsTrue(rb.Equals(oldHead)); // assumption for this test
            RefLogWriter.WriteReflog(db, rb, rb, "Just a message", "refs/heads/a");
            Assert.IsTrue(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/a")).Exists, "internal check, we have a log");

            // Now this is our test
            RefRename renameRef = db.RenameRef("refs/heads/a", "refs/heads/a/b");

            RefUpdate.RefUpdateResult result = renameRef.Rename();
            Assert.AreEqual(RefUpdate.RefUpdateResult.Renamed, result);
            Assert.IsNull(db.Resolve("refs/heads/a"));
            Assert.AreEqual(rb, db.Resolve("refs/heads/a/b"));
            Assert.AreEqual(3, db.ReflogReader("a/b").getReverseEntries().Count);
            Assert.AreEqual("Branch: renamed a to a/b", db.ReflogReader("a/b")
                            .getReverseEntries()[0].getComment());
            Assert.AreEqual("Just a message", db.ReflogReader("a/b")
                            .getReverseEntries()[1].getComment());
            Assert.AreEqual("Setup", db.ReflogReader("a/b").getReverseEntries()
                            [2].getComment());
            // same thing was logged to HEAD
            Assert.AreEqual("Branch: renamed a to a/b", db.ReflogReader("HEAD")
                            .getReverseEntries()[0].getComment());
        }
コード例 #3
0
        public virtual void TestRenameCurrentBranch()
        {
            ObjectId rb = db.Resolve("refs/heads/b");

            WriteSymref(Constants.HEAD, "refs/heads/b");
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            NUnit.Framework.Assert.AreEqual(oldHead, rb, "internal test condition, b == HEAD"
                                            );
            WriteReflog(db, rb, "Just a message", "refs/heads/b");
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                              (), "log on old branch");
            RefRename renameRef = db.RenameRef("refs/heads/b", "refs/heads/new/name");

            RefUpdate.Result result = renameRef.Rename();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result);
            NUnit.Framework.Assert.AreEqual(rb, db.Resolve("refs/heads/new/name"));
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/b"));
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("new/name").GetLastEntry().GetComment());
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                               ());
            NUnit.Framework.Assert.AreEqual(rb, db.Resolve(Constants.HEAD));
            NUnit.Framework.Assert.AreEqual(2, db.GetReflogReader("new/name").GetReverseEntries
                                                ().Count);
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("new/name").GetReverseEntries()[0].GetComment());
            NUnit.Framework.Assert.AreEqual("Just a message", db.GetReflogReader("new/name").
                                            GetReverseEntries()[1].GetComment());
        }
コード例 #4
0
ファイル: RefUpdateTest.cs プロジェクト: kkl713/GitSharp
        public void tryRenameWhenLocked(string toLock, string fromName,
                                        string toName, string headPointsTo)
        {
            // setup
            db.WriteSymref(Constants.HEAD, headPointsTo);
            ObjectId oldfromId = db.Resolve(fromName);
            ObjectId oldHeadId = db.Resolve(Constants.HEAD);

            RefLogWriter.WriteReflog(db, oldfromId, oldfromId, "Just a message",
                                     fromName);
            IList <ReflogReader.Entry> oldFromLog = db
                                                    .ReflogReader(fromName).getReverseEntries();
            IList <ReflogReader.Entry> oldHeadLog = oldHeadId != null?db
                                                    .ReflogReader(Constants.HEAD).getReverseEntries() : null;

            Assert.IsTrue(new FileInfo(Path.Combine(db.Directory.FullName, "logs/" + fromName)).Exists, "internal check, we have a log");

            // "someone" has branch X locked
            var lockFile = new LockFile(new FileInfo(Path.Combine(db.Directory.FullName, toLock)));

            try
            {
                Assert.IsTrue(lockFile.Lock());

                // Now this is our test
                RefRename renameRef = db.RenameRef(fromName, toName);
                RefUpdate.RefUpdateResult result = renameRef.Rename();
                Assert.AreEqual(RefUpdate.RefUpdateResult.LockFailure, result);

                // Check that the involved refs are the same despite the failure
                assertExists(false, toName);
                if (!toLock.Equals(toName))
                {
                    assertExists(false, toName + ".lock");
                }
                assertExists(true, toLock + ".lock");
                if (!toLock.Equals(fromName))
                {
                    assertExists(false, "logs/" + fromName + ".lock");
                }
                assertExists(false, "logs/" + toName + ".lock");
                Assert.AreEqual(oldHeadId, db.Resolve(Constants.HEAD));
                Assert.AreEqual(oldfromId, db.Resolve(fromName));
                Assert.IsNull(db.Resolve(toName));
                Assert.AreEqual(oldFromLog.ToString(), db.ReflogReader(fromName)
                                .getReverseEntries().ToString());
                if (oldHeadId != null)
                {
                    Assert.AreEqual(oldHeadLog, db.ReflogReader(Constants.HEAD)
                                    .getReverseEntries());
                }
            }
            finally
            {
                lockFile.Unlock();
            }
        }
コード例 #5
0
ファイル: RefUpdateTest.cs プロジェクト: shoff/ngit
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void TryRenameWhenLocked(string toLock, string fromName, string toName
                                                , string headPointsTo)
        {
            // setup
            WriteSymref(Constants.HEAD, headPointsTo);
            ObjectId oldfromId = db.Resolve(fromName);
            ObjectId oldHeadId = db.Resolve(Constants.HEAD);

            WriteReflog(db, oldfromId, oldfromId, "Just a message", fromName);
            IList <ReflogReader.Entry> oldFromLog = db.GetReflogReader(fromName).GetReverseEntries
                                                        ();
            IList <ReflogReader.Entry> oldHeadLog = oldHeadId != null?db.GetReflogReader(Constants
                                                                                         .HEAD).GetReverseEntries() : null;

            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/" + fromName).Exists
                                              (), "internal check, we have a log");
            // "someone" has branch X locked
            LockFile lockFile = new LockFile(new FilePath(db.Directory, toLock), db.FileSystem
                                             );

            try
            {
                NUnit.Framework.Assert.IsTrue(lockFile.Lock());
                // Now this is our test
                RefRename        renameRef = db.RenameRef(fromName, toName);
                RefUpdate.Result result    = renameRef.Rename();
                NUnit.Framework.Assert.AreEqual(RefUpdate.Result.LOCK_FAILURE, result);
                // Check that the involved refs are the same despite the failure
                AssertExists(false, toName);
                if (!toLock.Equals(toName))
                {
                    AssertExists(false, toName + ".lock");
                }
                AssertExists(true, toLock + ".lock");
                if (!toLock.Equals(fromName))
                {
                    AssertExists(false, "logs/" + fromName + ".lock");
                }
                AssertExists(false, "logs/" + toName + ".lock");
                NUnit.Framework.Assert.AreEqual(oldHeadId, db.Resolve(Constants.HEAD));
                NUnit.Framework.Assert.AreEqual(oldfromId, db.Resolve(fromName));
                NUnit.Framework.Assert.IsNull(db.Resolve(toName));
                NUnit.Framework.Assert.AreEqual(oldFromLog.ToString(), db.GetReflogReader(fromName
                                                                                          ).GetReverseEntries().ToString());
                if (oldHeadId != null)
                {
                    NUnit.Framework.Assert.AreEqual(oldHeadLog.ToString(), db.GetReflogReader(Constants
                                                                                              .HEAD).GetReverseEntries().ToString());
                }
            }
            finally
            {
                lockFile.Unlock();
            }
        }
コード例 #6
0
        public virtual void TestRenameBranchAlsoInPack()
        {
            ObjectId rb  = db.Resolve("refs/heads/b");
            ObjectId rb2 = db.Resolve("refs/heads/b~1");

            NUnit.Framework.Assert.AreEqual(RefStorage.PACKED, db.GetRef("refs/heads/b").GetStorage
                                                ());
            RefUpdate updateRef = db.UpdateRef("refs/heads/b");

            updateRef.SetNewObjectId(rb2);
            updateRef.SetForceUpdate(true);
            RefUpdate.Result update = updateRef.Update();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update, "internal check new ref is loose"
                                            );
            NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, db.GetRef("refs/heads/b").GetStorage
                                                ());
            WriteReflog(db, rb, "Just a message", "refs/heads/b");
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                              (), "log on old branch");
            RefRename renameRef = db.RenameRef("refs/heads/b", "refs/heads/new/name");

            RefUpdate.Result result = renameRef.Rename();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result);
            NUnit.Framework.Assert.AreEqual(rb2, db.Resolve("refs/heads/new/name"));
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/b"));
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("new/name").GetLastEntry().GetComment());
            NUnit.Framework.Assert.AreEqual(3, db.GetReflogReader("refs/heads/new/name").GetReverseEntries
                                                ().Count);
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("refs/heads/new/name").GetReverseEntries()[0].GetComment());
            NUnit.Framework.Assert.AreEqual(0, db.GetReflogReader("HEAD").GetReverseEntries()
                                            .Count);
            // make sure b's log file is gone too.
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                               ());
            // Create new Repository instance, to reread caches and make sure our
            // assumptions are persistent.
            Repository ndb = new FileRepository(db.Directory);

            NUnit.Framework.Assert.AreEqual(rb2, ndb.Resolve("refs/heads/new/name"));
            NUnit.Framework.Assert.IsNull(ndb.Resolve("refs/heads/b"));
        }
コード例 #7
0
ファイル: RefUpdateTest.cs プロジェクト: kkl713/GitSharp
        public void testRenameBranchNoPreviousLog()
        {
            Assert.IsFalse(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/b")).Exists, "precondition, no log on old branchg");
            ObjectId rb      = db.Resolve("refs/heads/b");
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            Assert.IsFalse(rb.Equals(oldHead)); // assumption for this test
            RefRename renameRef = db.RenameRef("refs/heads/b",
                                               "refs/heads/new/name");

            RefUpdate.RefUpdateResult result = renameRef.Rename();
            Assert.AreEqual(RefUpdate.RefUpdateResult.Renamed, result);
            Assert.AreEqual(rb, db.Resolve("refs/heads/new/name"));
            Assert.IsNull(db.Resolve("refs/heads/b"));
            Assert.AreEqual(1, db.ReflogReader("new/name").getReverseEntries().Count);
            Assert.AreEqual("Branch: renamed b to new/name", db.ReflogReader("new/name")
                            .getLastEntry().getComment());
            Assert.IsFalse(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/b")).Exists);
            Assert.AreEqual(oldHead, db.Resolve(Constants.HEAD)); // unchanged
        }
コード例 #8
0
ファイル: RefUpdateTest.cs プロジェクト: kkl713/GitSharp
        public void testRenameBranchAlsoInPack()
        {
            ObjectId rb  = db.Resolve("refs/heads/b");
            ObjectId rb2 = db.Resolve("refs/heads/b~1");

            Assert.AreEqual(Core.Ref.Storage.Packed, db.getRef("refs/heads/b").StorageFormat);
            RefUpdate updateRef = db.UpdateRef("refs/heads/b");

            updateRef.NewObjectId   = rb2;
            updateRef.IsForceUpdate = true;
            RefUpdate.RefUpdateResult update = updateRef.Update();
            Assert.AreEqual(RefUpdate.RefUpdateResult.Forced, update, "internal check new ref is loose");
            Assert.AreEqual(Core.Ref.Storage.LoosePacked, db.getRef("refs/heads/b")
                            .StorageFormat);
            RefLogWriter.WriteReflog(db, rb, rb, "Just a message", "refs/heads/b");
            Assert.IsTrue(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/b")).Exists, "no log on old branch");
            RefRename renameRef = db.RenameRef("refs/heads/b",
                                               "refs/heads/new/name");

            RefUpdate.RefUpdateResult result = renameRef.Rename();
            Assert.AreEqual(RefUpdate.RefUpdateResult.Renamed, result);
            Assert.AreEqual(rb2, db.Resolve("refs/heads/new/name"));
            Assert.IsNull(db.Resolve("refs/heads/b"));
            Assert.AreEqual("Branch: renamed b to new/name", db.ReflogReader(
                                "new/name").getLastEntry().getComment());
            Assert.AreEqual(3, db.ReflogReader("refs/heads/new/name").getReverseEntries().Count);
            Assert.AreEqual("Branch: renamed b to new/name", db.ReflogReader("refs/heads/new/name").getReverseEntries()[0].getComment());
            Assert.AreEqual(0, db.ReflogReader("HEAD").getReverseEntries().Count);
            // make sure b's log file is gone too.
            Assert.IsFalse(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/b")).Exists);

            // Create new Repository instance, to reread caches and make sure our
            // assumptions are persistent.
            using (Core.Repository ndb = new Core.Repository(db.Directory))
            {
                Assert.AreEqual(rb2, ndb.Resolve("refs/heads/new/name"));
                Assert.IsNull(ndb.Resolve("refs/heads/b"));
            }
        }
コード例 #9
0
        public virtual void TestRenameRefNameColission1avoided()
        {
            // setup
            ObjectId rb = db.Resolve("refs/heads/b");

            WriteSymref(Constants.HEAD, "refs/heads/a");
            RefUpdate updateRef = db.UpdateRef("refs/heads/a");

            updateRef.SetNewObjectId(rb);
            updateRef.SetRefLogMessage("Setup", false);
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FAST_FORWARD, updateRef.Update()
                                            );
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            NUnit.Framework.Assert.AreEqual(oldHead, rb);
            // assumption for this test
            WriteReflog(db, rb, "Just a message", "refs/heads/a");
            NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/a").Exists
                                              (), "internal check, we have a log");
            // Now this is our test
            RefRename renameRef = db.RenameRef("refs/heads/a", "refs/heads/a/b");

            RefUpdate.Result result = renameRef.Rename();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result);
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/a"));
            NUnit.Framework.Assert.AreEqual(rb, db.Resolve("refs/heads/a/b"));
            NUnit.Framework.Assert.AreEqual(3, db.GetReflogReader("a/b").GetReverseEntries().
                                            Count);
            NUnit.Framework.Assert.AreEqual("Branch: renamed a to a/b", db.GetReflogReader("a/b"
                                                                                           ).GetReverseEntries()[0].GetComment());
            NUnit.Framework.Assert.AreEqual("Just a message", db.GetReflogReader("a/b").GetReverseEntries
                                                ()[1].GetComment());
            NUnit.Framework.Assert.AreEqual("Setup", db.GetReflogReader("a/b").GetReverseEntries
                                                ()[2].GetComment());
            // same thing was logged to HEAD
            NUnit.Framework.Assert.AreEqual("Branch: renamed a to a/b", db.GetReflogReader("HEAD"
                                                                                           ).GetReverseEntries()[0].GetComment());
        }
コード例 #10
0
        public virtual void TestRenameBranchNoPreviousLog()
        {
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                               (), "precondition, no log on old branchg");
            ObjectId rb      = db.Resolve("refs/heads/b");
            ObjectId oldHead = db.Resolve(Constants.HEAD);

            NUnit.Framework.Assert.IsFalse(rb.Equals(oldHead));
            // assumption for this test
            RefRename renameRef = db.RenameRef("refs/heads/b", "refs/heads/new/name");

            RefUpdate.Result result = renameRef.Rename();
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result);
            NUnit.Framework.Assert.AreEqual(rb, db.Resolve("refs/heads/new/name"));
            NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/b"));
            NUnit.Framework.Assert.AreEqual(1, db.GetReflogReader("new/name").GetReverseEntries
                                                ().Count);
            NUnit.Framework.Assert.AreEqual("Branch: renamed b to new/name", db.GetReflogReader
                                                ("new/name").GetLastEntry().GetComment());
            NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/b").Exists
                                               ());
            NUnit.Framework.Assert.AreEqual(oldHead, db.Resolve(Constants.HEAD));
        }
コード例 #11
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);
     }
 }
コード例 #12
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>
 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().refAlreadExists
                                                                      , 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);
         bool ok = RefUpdate.Result.RENAMED == renameResult;
         if (ok)
         {
             if (fullNewName.StartsWith(Constants.R_HEADS))
             {
                 // move the upstream configuration over to the new branch
                 string shortOldName = Sharpen.Runtime.Substring(fullOldName, Constants.R_HEADS.Length
                                                                 );
                 StoredConfig repoConfig = repo.GetConfig();
                 string       oldRemote  = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName
                                                                , ConfigConstants.CONFIG_KEY_REMOTE);
                 if (oldRemote != null)
                 {
                     repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants
                                          .CONFIG_KEY_REMOTE, oldRemote);
                 }
                 string oldMerge = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName
                                                        , ConfigConstants.CONFIG_KEY_MERGE);
                 if (oldMerge != null)
                 {
                     repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants
                                          .CONFIG_KEY_MERGE, oldMerge);
                 }
                 repoConfig.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName);
                 repoConfig.Save();
             }
         }
         else
         {
             throw new JGitInternalException(MessageFormat.Format(JGitText.Get().renameBranchUnexpectedResult
                                                                  , renameResult.ToString()));
         }
         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);
     }
 }