public virtual void TestPullMerge()
        {
            PullResult res = target.Pull().Call();

            // nothing to update since we don't have different data yet
            NUnit.Framework.Assert.IsTrue(res.GetFetchResult().GetTrackingRefUpdates().IsEmpty
                                              ());
            NUnit.Framework.Assert.IsTrue(res.GetMergeResult().GetMergeStatus().Equals(MergeStatus
                                                                                       .ALREADY_UP_TO_DATE));
            WriteToFile(sourceFile, "Source change");
            source.Add().AddFilepattern("SomeFile.txt");
            RevCommit sourceCommit = source.Commit().SetMessage("Source change in remote").Call
                                         ();
            FilePath targetFile2 = new FilePath(dbTarget.WorkTree, "OtherFile.txt");

            WriteToFile(targetFile2, "Unconflicting change");
            target.Add().AddFilepattern("OtherFile.txt").Call();
            RevCommit targetCommit = target.Commit().SetMessage("Unconflicting change in local"
                                                                ).Call();

            res = target.Pull().Call();
            MergeCommandResult mergeResult = res.GetMergeResult();

            ObjectId[] mergedCommits = mergeResult.GetMergedCommits();
            NUnit.Framework.Assert.AreEqual(targetCommit.Id, mergedCommits[0]);
            NUnit.Framework.Assert.AreEqual(sourceCommit.Id, mergedCommits[1]);
            RevCommit mergeCommit = new RevWalk(dbTarget).ParseCommit(mergeResult.GetNewHead(
                                                                          ));
            string message = "Merge branch 'master' of " + db.WorkTree;

            NUnit.Framework.Assert.AreEqual(message, mergeCommit.GetShortMessage());
        }
Пример #2
0
		public virtual void TestPullMerge()
		{
			PullResult res = target.Pull().Call();
			// nothing to update since we don't have different data yet
			NUnit.Framework.Assert.IsTrue(res.GetFetchResult().GetTrackingRefUpdates().IsEmpty
				());
			NUnit.Framework.Assert.IsTrue(res.GetMergeResult().GetMergeStatus().Equals(MergeStatus
				.ALREADY_UP_TO_DATE));
			WriteToFile(sourceFile, "Source change");
			source.Add().AddFilepattern("SomeFile.txt");
			RevCommit sourceCommit = source.Commit().SetMessage("Source change in remote").Call
				();
			FilePath targetFile2 = new FilePath(dbTarget.WorkTree, "OtherFile.txt");
			WriteToFile(targetFile2, "Unconflicting change");
			target.Add().AddFilepattern("OtherFile.txt").Call();
			RevCommit targetCommit = target.Commit().SetMessage("Unconflicting change in local"
				).Call();
			res = target.Pull().Call();
			MergeCommandResult mergeResult = res.GetMergeResult();
			ObjectId[] mergedCommits = mergeResult.GetMergedCommits();
			NUnit.Framework.Assert.AreEqual(targetCommit.Id, mergedCommits[0]);
			NUnit.Framework.Assert.AreEqual(sourceCommit.Id, mergedCommits[1]);
			RevCommit mergeCommit = new RevWalk(dbTarget).ParseCommit(mergeResult.GetNewHead(
				));
			string message = "Merge branch 'master' of " + db.WorkTree;
			NUnit.Framework.Assert.AreEqual(message, mergeCommit.GetShortMessage());
		}
Пример #3
0
        /// <exception cref="NGit.Api.Errors.RefAlreadyExistsException">
        /// when trying to create (without force) a branch with a name
        /// that already exists
        /// </exception>
        /// <exception cref="NGit.Api.Errors.RefNotFoundException">if the start point can not be found
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.InvalidRefNameException">
        /// if the provided name is <code>null</code> or otherwise
        /// invalid
        /// </exception>
        /// <returns>the newly created branch</returns>
        /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception>
        public override Ref Call()
        {
            CheckCallable();
            ProcessOptions();
            try
            {
                Ref  refToCheck = repo.GetRef(name);
                bool exists     = refToCheck != null && refToCheck.GetName().StartsWith(Constants.R_HEADS
                                                                                        );
                if (!force && exists)
                {
                    throw new RefAlreadyExistsException(MessageFormat.Format(JGitText.Get().refAlreadExists
                                                                             , name));
                }
                ObjectId startAt            = GetStartPoint();
                string   startPointFullName = null;
                if (startPoint != null)
                {
                    Ref baseRef = repo.GetRef(startPoint);
                    if (baseRef != null)
                    {
                        startPointFullName = baseRef.GetName();
                    }
                }
                // determine whether we are based on a commit,
                // a branch, or a tag and compose the reflog message
                string refLogMessage;
                string baseBranch = string.Empty;
                if (startPointFullName == null)
                {
                    string baseCommit;
                    if (startCommit != null)
                    {
                        baseCommit = startCommit.GetShortMessage();
                    }
                    else
                    {
                        RevCommit commit = new RevWalk(repo).ParseCommit(repo.Resolve(startPoint));
                        baseCommit = commit.GetShortMessage();
                    }
                    if (exists)
                    {
                        refLogMessage = "branch: Reset start-point to commit " + baseCommit;
                    }
                    else
                    {
                        refLogMessage = "branch: Created from commit " + baseCommit;
                    }
                }
                else
                {
                    if (startPointFullName.StartsWith(Constants.R_HEADS) || startPointFullName.StartsWith
                            (Constants.R_REMOTES))
                    {
                        baseBranch = startPointFullName;
                        if (exists)
                        {
                            refLogMessage = "branch: Reset start-point to branch " + startPointFullName;
                        }
                        else
                        {
                            // TODO
                            refLogMessage = "branch: Created from branch " + baseBranch;
                        }
                    }
                    else
                    {
                        if (exists)
                        {
                            refLogMessage = "branch: Reset start-point to tag " + startPointFullName;
                        }
                        else
                        {
                            refLogMessage = "branch: Created from tag " + startPointFullName;
                        }
                    }
                }
                RefUpdate updateRef = repo.UpdateRef(Constants.R_HEADS + name);
                updateRef.SetNewObjectId(startAt);
                updateRef.SetRefLogMessage(refLogMessage, false);
                RefUpdate.Result updateResult;
                if (exists && force)
                {
                    updateResult = updateRef.ForceUpdate();
                }
                else
                {
                    updateResult = updateRef.Update();
                }
                SetCallable(false);
                bool ok = false;
                switch (updateResult)
                {
                case RefUpdate.Result.NEW:
                {
                    ok = !exists;
                    break;
                }

                case RefUpdate.Result.NO_CHANGE:
                case RefUpdate.Result.FAST_FORWARD:
                case RefUpdate.Result.FORCED:
                {
                    ok = exists;
                    break;
                }

                default:
                {
                    break;
                    break;
                }
                }
                if (!ok)
                {
                    throw new JGitInternalException(MessageFormat.Format(JGitText.Get().createBranchUnexpectedResult
                                                                         , updateResult.ToString()));
                }
                Ref result = repo.GetRef(name);
                if (result == null)
                {
                    throw new JGitInternalException(JGitText.Get().createBranchFailedUnknownReason);
                }
                if (baseBranch.Length == 0)
                {
                    return(result);
                }
                // if we are based on another branch, see
                // if we need to configure upstream configuration: first check
                // whether the setting was done explicitly
                bool doConfigure;
                if (upstreamMode == CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM || upstreamMode
                    == CreateBranchCommand.SetupUpstreamMode.TRACK)
                {
                    // explicitly set to configure
                    doConfigure = true;
                }
                else
                {
                    if (upstreamMode == CreateBranchCommand.SetupUpstreamMode.NOTRACK)
                    {
                        // explicitly set to not configure
                        doConfigure = false;
                    }
                    else
                    {
                        // if there was no explicit setting, check the configuration
                        string autosetupflag = repo.GetConfig().GetString(ConfigConstants.CONFIG_BRANCH_SECTION
                                                                          , null, ConfigConstants.CONFIG_KEY_AUTOSETUPMERGE);
                        if ("false".Equals(autosetupflag))
                        {
                            doConfigure = false;
                        }
                        else
                        {
                            if ("always".Equals(autosetupflag))
                            {
                                doConfigure = true;
                            }
                            else
                            {
                                // in this case, the default is to configure
                                // only in case the base branch was a remote branch
                                doConfigure = baseBranch.StartsWith(Constants.R_REMOTES);
                            }
                        }
                    }
                }
                if (doConfigure)
                {
                    StoredConfig config   = repo.GetConfig();
                    string[]     tokens   = baseBranch.Split("/", 4);
                    bool         isRemote = tokens[1].Equals("remotes");
                    if (isRemote)
                    {
                        // refs/remotes/<remote name>/<branch>
                        string remoteName = tokens[2];
                        string branchName = tokens[3];
                        config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_REMOTE
                                         , remoteName);
                        config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_MERGE
                                         , Constants.R_HEADS + branchName);
                    }
                    else
                    {
                        // set "." as remote
                        config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_REMOTE
                                         , ".");
                        config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_MERGE
                                         , baseBranch);
                    }
                    config.Save();
                }
                return(result);
            }
            catch (IOException ioe)
            {
                throw new JGitInternalException(ioe.Message, ioe);
            }
        }