Exemplo n.º 1
0
		/// <summary>
		/// Compute the tracking status for the <code>branchName</code> in
		/// <code>repository</code>.
		/// </summary>
		/// <remarks>
		/// Compute the tracking status for the <code>branchName</code> in
		/// <code>repository</code>.
		/// </remarks>
		/// <param name="repository">the git repository to compute the status from</param>
		/// <param name="branchName">the local branch</param>
		/// <returns>the tracking status, or null if it is not known</returns>
		/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
		public static NGit.BranchTrackingStatus Of(Repository repository, string branchName
			)
		{
			BranchConfig branchConfig = new BranchConfig(repository.GetConfig(), branchName);
			string trackingBranch = branchConfig.GetTrackingBranch();
			if (trackingBranch == null)
			{
				return null;
			}
			Ref tracking = repository.GetRef(trackingBranch);
			if (tracking == null)
			{
				return null;
			}
			Ref local = repository.GetRef(branchName);
			if (local == null)
			{
				return null;
			}
			RevWalk walk = new RevWalk(repository);
			RevCommit localCommit = walk.ParseCommit(local.GetObjectId());
			RevCommit trackingCommit = walk.ParseCommit(tracking.GetObjectId());
			walk.SetRevFilter(RevFilter.MERGE_BASE);
			walk.MarkStart(localCommit);
			walk.MarkStart(trackingCommit);
			RevCommit mergeBase = walk.Next();
			walk.Reset();
			walk.SetRevFilter(RevFilter.ALL);
			int aheadCount = RevWalkUtils.Count(walk, localCommit, mergeBase);
			int behindCount = RevWalkUtils.Count(walk, trackingCommit, mergeBase);
			return new NGit.BranchTrackingStatus(trackingBranch, aheadCount, behindCount);
		}
Exemplo n.º 2
0
        public virtual void GetTrackingBranchShouldReturnMergeBranchForLocalBranch()
        {
            Config c = Parse(string.Empty + "[remote \"origin\"]\n" + "  fetch = +refs/heads/*:refs/remotes/origin/*\n"
                             + "[branch \"master\"]\n" + "  remote = .\n" + "  merge = refs/heads/master\n");
            //
            BranchConfig branchConfig = new BranchConfig(c, "master");

            NUnit.Framework.Assert.AreEqual("refs/heads/master", branchConfig.GetTrackingBranch
                                                ());
        }
Exemplo n.º 3
0
        public virtual void GetTrackingBranchShouldReturnNullWithoutMergeBranchForLocalBranch
            ()
        {
            Config c = Parse(string.Empty + "[remote \"origin\"]\n" + "  fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
                             + "[branch \"master\"]\n" + "  remote = .\n");
            //
            //
            BranchConfig branchConfig = new BranchConfig(c, "master");

            NUnit.Framework.Assert.IsNull(branchConfig.GetTrackingBranch());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compute the tracking status for the <code>branchName</code> in
        /// <code>repository</code>.
        /// </summary>
        /// <remarks>
        /// Compute the tracking status for the <code>branchName</code> in
        /// <code>repository</code>.
        /// </remarks>
        /// <param name="repository">the git repository to compute the status from</param>
        /// <param name="branchName">the local branch</param>
        /// <returns>the tracking status, or null if it is not known</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public static NGit.BranchTrackingStatus Of(Repository repository, string branchName
                                                   )
        {
            BranchConfig branchConfig   = new BranchConfig(repository.GetConfig(), branchName);
            string       trackingBranch = branchConfig.GetTrackingBranch();

            if (trackingBranch == null)
            {
                return(null);
            }
            Ref tracking = repository.GetRef(trackingBranch);

            if (tracking == null)
            {
                return(null);
            }
            Ref local = repository.GetRef(branchName);

            if (local == null)
            {
                return(null);
            }
            RevWalk   walk           = new RevWalk(repository);
            RevCommit localCommit    = walk.ParseCommit(local.GetObjectId());
            RevCommit trackingCommit = walk.ParseCommit(tracking.GetObjectId());

            walk.SetRevFilter(RevFilter.MERGE_BASE);
            walk.MarkStart(localCommit);
            walk.MarkStart(trackingCommit);
            RevCommit mergeBase = walk.Next();

            walk.Reset();
            walk.SetRevFilter(RevFilter.ALL);
            int aheadCount  = RevWalkUtils.Count(walk, localCommit, mergeBase);
            int behindCount = RevWalkUtils.Count(walk, trackingCommit, mergeBase);

            return(new NGit.BranchTrackingStatus(trackingBranch, aheadCount, behindCount));
        }
Exemplo n.º 5
0
		public virtual void GetTrackingBranchShouldReturnMergeBranchForLocalBranch()
		{
			Config c = Parse(string.Empty + "[remote \"origin\"]\n" + "  fetch = +refs/heads/*:refs/remotes/origin/*\n"
				 + "[branch \"master\"]\n" + "  remote = .\n" + "  merge = refs/heads/master\n");
			//
			BranchConfig branchConfig = new BranchConfig(c, "master");
			NUnit.Framework.Assert.AreEqual("refs/heads/master", branchConfig.GetTrackingBranch
				());
		}
Exemplo n.º 6
0
		public virtual void GetTrackingBranchShouldReturnNullWithoutMergeBranchForLocalBranch
			()
		{
			Config c = Parse(string.Empty + "[remote \"origin\"]\n" + "  fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
				 + "[branch \"master\"]\n" + "  remote = .\n");
			//
			//
			BranchConfig branchConfig = new BranchConfig(c, "master");
			NUnit.Framework.Assert.IsNull(branchConfig.GetTrackingBranch());
		}