示例#1
0
 private void CloseConnection(FetchResult result)
 {
     if (conn != null)
     {
         conn.Close();
         result.AddMessages(conn.GetMessages());
         conn = null;
     }
 }
示例#2
0
        /// <exception cref="System.NotSupportedException"></exception>
        /// <exception cref="NGit.Errors.TransportException"></exception>
        private void ReopenConnection()
        {
            if (conn != null)
            {
                return;
            }
            conn = transport.OpenFetch();
            // Since we opened a new connection we cannot be certain
            // that the system we connected to has the same exact set
            // of objects available (think round-robin DNS and mirrors
            // that aren't updated at the same time).
            //
            // We rebuild our askFor list using only the refs that the
            // new connection has offered to us.
            //
            Dictionary <ObjectId, Ref> avail = new Dictionary <ObjectId, Ref>();

            foreach (Ref r in conn.GetRefs())
            {
                avail.Put(r.GetObjectId(), r);
            }
            ICollection <Ref> wants = new AList <Ref>(askFor.Values);

            askFor.Clear();
            foreach (Ref want in wants)
            {
                Ref newRef = avail.Get(want.GetObjectId());
                if (newRef != null)
                {
                    askFor.Put(newRef.GetObjectId(), newRef);
                }
                else
                {
                    RemoveFetchHeadRecord(want.GetObjectId());
                    RemoveTrackingRefUpdate(want.GetObjectId());
                }
            }
        }
示例#3
0
        /// <summary>
        /// Executes the
        /// <code>LsRemote</code>
        /// command with all the options and parameters
        /// collected by the setter methods (e.g.
        /// <see cref="SetHeads(bool)">SetHeads(bool)</see>
        /// ) of this
        /// class. Each instance of this class should only be used for one invocation
        /// of the command. Don't call this method twice on an instance.
        /// </summary>
        /// <returns>a collection of references in the remote repository</returns>
        /// <exception cref="NGit.Api.Errors.InvalidRemoteException">when called with an invalid remote uri
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.TransportException">for errors that occurs during transport
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override ICollection <Ref> Call()
        {
            CheckCallable();
            NGit.Transport.Transport transport = null;
            FetchConnection          fc        = null;

            try
            {
                transport = NGit.Transport.Transport.Open(repo, remote);
                transport.SetOptionUploadPack(uploadPack);
                Configure(transport);
                ICollection <RefSpec> refSpecs = new AList <RefSpec>(1);
                if (tags)
                {
                    refSpecs.AddItem(new RefSpec("refs/tags/*:refs/remotes/origin/tags/*"));
                }
                if (heads)
                {
                    refSpecs.AddItem(new RefSpec("refs/heads/*:refs/remotes/origin/*"));
                }
                ICollection <Ref>         refs;
                IDictionary <string, Ref> refmap = new Dictionary <string, Ref>();
                fc   = transport.OpenFetch();
                refs = fc.GetRefs();
                if (refSpecs.IsEmpty())
                {
                    foreach (Ref r in refs)
                    {
                        refmap.Put(r.GetName(), r);
                    }
                }
                else
                {
                    foreach (Ref r_1 in refs)
                    {
                        foreach (RefSpec rs in refSpecs)
                        {
                            if (rs.MatchSource(r_1))
                            {
                                refmap.Put(r_1.GetName(), r_1);
                                break;
                            }
                        }
                    }
                }
                return(refmap.Values);
            }
            catch (URISyntaxException)
            {
                throw new InvalidRemoteException(MessageFormat.Format(JGitText.Get().invalidRemote
                                                                      , remote));
            }
            catch (NGit.Errors.NotSupportedException e)
            {
                throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfLsRemoteCommand
                                                , e);
            }
            catch (NGit.Errors.TransportException e)
            {
                throw new NGit.Errors.TransportException(e.Message, e);
            }
            finally
            {
                if (fc != null)
                {
                    fc.Close();
                }
                if (transport != null)
                {
                    transport.Close();
                }
            }
        }
示例#4
0
        /// <exception cref="System.NotSupportedException"></exception>
        /// <exception cref="NGit.Errors.TransportException"></exception>
        private void ExecuteImp(ProgressMonitor monitor, FetchResult result)
        {
            conn = transport.OpenFetch();
            try
            {
                result.SetAdvertisedRefs(transport.GetURI(), conn.GetRefsMap());
                ICollection <Ref> matched = new HashSet <Ref>();
                foreach (RefSpec spec in toFetch)
                {
                    if (spec.GetSource() == null)
                    {
                        throw new TransportException(MessageFormat.Format(JGitText.Get().sourceRefNotSpecifiedForRefspec
                                                                          , spec));
                    }
                    if (spec.IsWildcard())
                    {
                        ExpandWildcard(spec, matched);
                    }
                    else
                    {
                        ExpandSingle(spec, matched);
                    }
                }
                ICollection <Ref> additionalTags = Sharpen.Collections.EmptyList <Ref>();
                TagOpt            tagopt         = transport.GetTagOpt();
                if (tagopt == TagOpt.AUTO_FOLLOW)
                {
                    additionalTags = ExpandAutoFollowTags();
                }
                else
                {
                    if (tagopt == TagOpt.FETCH_TAGS)
                    {
                        ExpandFetchTags();
                    }
                }
                bool includedTags;
                if (!askFor.IsEmpty() && !AskForIsComplete())
                {
                    FetchObjects(monitor);
                    includedTags = conn.DidFetchIncludeTags();
                    // Connection was used for object transfer. If we
                    // do another fetch we must open a new connection.
                    //
                    CloseConnection(result);
                }
                else
                {
                    includedTags = false;
                }
                if (tagopt == TagOpt.AUTO_FOLLOW && !additionalTags.IsEmpty())
                {
                    // There are more tags that we want to follow, but
                    // not all were asked for on the initial request.
                    //
                    Sharpen.Collections.AddAll(have, askFor.Keys);
                    askFor.Clear();
                    foreach (Ref r in additionalTags)
                    {
                        ObjectId id = r.GetPeeledObjectId();
                        if (id == null || transport.local.HasObject(id))
                        {
                            WantTag(r);
                        }
                    }
                    if (!askFor.IsEmpty() && (!includedTags || !AskForIsComplete()))
                    {
                        ReopenConnection();
                        if (!askFor.IsEmpty())
                        {
                            FetchObjects(monitor);
                        }
                    }
                }
            }
            finally
            {
                CloseConnection(result);
            }
            RevWalk walk = new RevWalk(transport.local);

            try
            {
                if (transport.IsRemoveDeletedRefs())
                {
                    DeleteStaleTrackingRefs(result, walk);
                }
                foreach (TrackingRefUpdate u in localUpdates)
                {
                    try
                    {
                        u.Update(walk);
                        result.Add(u);
                    }
                    catch (IOException err)
                    {
                        throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingTrackingRef
                                                                          , u.GetLocalName(), err.Message), err);
                    }
                }
            }
            finally
            {
                walk.Release();
            }
            if (!fetchHeadUpdates.IsEmpty())
            {
                try
                {
                    UpdateFETCH_HEAD(result);
                }
                catch (IOException err)
                {
                    throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingFETCH_HEAD
                                                                      , err.Message), err);
                }
            }
        }
示例#5
0
 /// <exception cref="System.NotSupportedException"></exception>
 /// <exception cref="NGit.Errors.TransportException"></exception>
 private void ReopenConnection()
 {
     if (conn != null)
     {
         return;
     }
     conn = transport.OpenFetch();
     // Since we opened a new connection we cannot be certain
     // that the system we connected to has the same exact set
     // of objects available (think round-robin DNS and mirrors
     // that aren't updated at the same time).
     //
     // We rebuild our askFor list using only the refs that the
     // new connection has offered to us.
     //
     Dictionary<ObjectId, Ref> avail = new Dictionary<ObjectId, Ref>();
     foreach (Ref r in conn.GetRefs())
     {
         avail.Put(r.GetObjectId(), r);
     }
     ICollection<Ref> wants = new AList<Ref>(askFor.Values);
     askFor.Clear();
     foreach (Ref want in wants)
     {
         Ref newRef = avail.Get(want.GetObjectId());
         if (newRef != null)
         {
             askFor.Put(newRef.GetObjectId(), newRef);
         }
         else
         {
             RemoveFetchHeadRecord(want.GetObjectId());
             RemoveTrackingRefUpdate(want.GetObjectId());
         }
     }
 }
示例#6
0
 /// <exception cref="System.NotSupportedException"></exception>
 /// <exception cref="NGit.Errors.TransportException"></exception>
 private void ExecuteImp(ProgressMonitor monitor, FetchResult result)
 {
     conn = transport.OpenFetch();
     try
     {
         result.SetAdvertisedRefs(transport.GetURI(), conn.GetRefsMap());
         ICollection<Ref> matched = new HashSet<Ref>();
         foreach (RefSpec spec in toFetch)
         {
             if (spec.GetSource() == null)
             {
                 throw new TransportException(MessageFormat.Format(JGitText.Get().sourceRefNotSpecifiedForRefspec
                     , spec));
             }
             if (spec.IsWildcard())
             {
                 ExpandWildcard(spec, matched);
             }
             else
             {
                 ExpandSingle(spec, matched);
             }
         }
         ICollection<Ref> additionalTags = Sharpen.Collections.EmptyList<Ref>();
         TagOpt tagopt = transport.GetTagOpt();
         if (tagopt == TagOpt.AUTO_FOLLOW)
         {
             additionalTags = ExpandAutoFollowTags();
         }
         else
         {
             if (tagopt == TagOpt.FETCH_TAGS)
             {
                 ExpandFetchTags();
             }
         }
         bool includedTags;
         if (!askFor.IsEmpty() && !AskForIsComplete())
         {
             FetchObjects(monitor);
             includedTags = conn.DidFetchIncludeTags();
             // Connection was used for object transfer. If we
             // do another fetch we must open a new connection.
             //
             CloseConnection(result);
         }
         else
         {
             includedTags = false;
         }
         if (tagopt == TagOpt.AUTO_FOLLOW && !additionalTags.IsEmpty())
         {
             // There are more tags that we want to follow, but
             // not all were asked for on the initial request.
             //
             Sharpen.Collections.AddAll(have, askFor.Keys);
             askFor.Clear();
             foreach (Ref r in additionalTags)
             {
                 ObjectId id = r.GetPeeledObjectId();
                 if (id == null)
                 {
                     id = r.GetObjectId();
                 }
                 if (transport.local.HasObject(id))
                 {
                     WantTag(r);
                 }
             }
             if (!askFor.IsEmpty() && (!includedTags || !AskForIsComplete()))
             {
                 ReopenConnection();
                 if (!askFor.IsEmpty())
                 {
                     FetchObjects(monitor);
                 }
             }
         }
     }
     finally
     {
         CloseConnection(result);
     }
     RevWalk walk = new RevWalk(transport.local);
     try
     {
         if (monitor is BatchingProgressMonitor)
         {
             ((BatchingProgressMonitor)monitor).SetDelayStart(250, TimeUnit.MILLISECONDS);
         }
         monitor.BeginTask(JGitText.Get().updatingReferences, localUpdates.Count);
         if (transport.IsRemoveDeletedRefs())
         {
             DeleteStaleTrackingRefs(result, walk);
         }
         foreach (TrackingRefUpdate u in localUpdates)
         {
             try
             {
                 monitor.Update(1);
                 u.Update(walk);
                 result.Add(u);
             }
             catch (IOException err)
             {
                 throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingTrackingRef
                     , u.GetLocalName(), err.Message), err);
             }
         }
         monitor.EndTask();
     }
     finally
     {
         walk.Release();
     }
     if (!fetchHeadUpdates.IsEmpty())
     {
         try
         {
             UpdateFETCH_HEAD(result);
         }
         catch (IOException err)
         {
             throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingFETCH_HEAD
                 , err.Message), err);
         }
     }
 }
示例#7
0
 private void CloseConnection(FetchResult result)
 {
     if (conn != null)
     {
         conn.Close();
         result.AddMessages(conn.GetMessages());
         conn = null;
     }
 }
示例#8
0
		/// <exception cref="System.NotSupportedException"></exception>
		/// <exception cref="NGit.Errors.TransportException"></exception>
		private void ExecuteImp(ProgressMonitor monitor, FetchResult result)
		{
			conn = transport.OpenFetch();
			try
			{
				result.SetAdvertisedRefs(transport.GetURI(), conn.GetRefsMap());
				ICollection<Ref> matched = new HashSet<Ref>();
				foreach (RefSpec spec in toFetch)
				{
					if (spec.GetSource() == null)
					{
						throw new TransportException(MessageFormat.Format(JGitText.Get().sourceRefNotSpecifiedForRefspec
							, spec));
					}
					if (spec.IsWildcard())
					{
						ExpandWildcard(spec, matched);
					}
					else
					{
						ExpandSingle(spec, matched);
					}
				}
				ICollection<Ref> additionalTags = Sharpen.Collections.EmptyList<Ref>();
				TagOpt tagopt = transport.GetTagOpt();
				if (tagopt == TagOpt.AUTO_FOLLOW)
				{
					additionalTags = ExpandAutoFollowTags();
				}
				else
				{
					if (tagopt == TagOpt.FETCH_TAGS)
					{
						ExpandFetchTags();
					}
				}
				bool includedTags;
				if (!askFor.IsEmpty() && !AskForIsComplete())
				{
					FetchObjects(monitor);
					includedTags = conn.DidFetchIncludeTags();
					// Connection was used for object transfer. If we
					// do another fetch we must open a new connection.
					//
					CloseConnection(result);
				}
				else
				{
					includedTags = false;
				}
				if (tagopt == TagOpt.AUTO_FOLLOW && !additionalTags.IsEmpty())
				{
					// There are more tags that we want to follow, but
					// not all were asked for on the initial request.
					//
					Sharpen.Collections.AddAll(have, askFor.Keys);
					askFor.Clear();
					foreach (Ref r in additionalTags)
					{
						ObjectId id = r.GetPeeledObjectId();
						if (id == null)
						{
							id = r.GetObjectId();
						}
						if (transport.local.HasObject(id))
						{
							WantTag(r);
						}
					}
					if (!askFor.IsEmpty() && (!includedTags || !AskForIsComplete()))
					{
						ReopenConnection();
						if (!askFor.IsEmpty())
						{
							FetchObjects(monitor);
						}
					}
				}
			}
			finally
			{
				CloseConnection(result);
			}
			BatchRefUpdate batch = transport.local.RefDatabase.NewBatchUpdate().SetAllowNonFastForwards
				(true).SetRefLogMessage("fetch", true);
			RevWalk walk = new RevWalk(transport.local);
			try
			{
				if (monitor is BatchingProgressMonitor)
				{
					((BatchingProgressMonitor)monitor).SetDelayStart(250, TimeUnit.MILLISECONDS);
				}
				if (transport.IsRemoveDeletedRefs())
				{
					DeleteStaleTrackingRefs(result, batch);
				}
				foreach (TrackingRefUpdate u in localUpdates)
				{
					result.Add(u);
					batch.AddCommand(u.AsReceiveCommand());
				}
				foreach (ReceiveCommand cmd in batch.GetCommands())
				{
					cmd.UpdateType(walk);
					if (cmd.GetType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD && cmd is TrackingRefUpdate.Command
						 && !((TrackingRefUpdate.Command)cmd).CanForceUpdate())
					{
						cmd.SetResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD);
					}
				}
				if (transport.IsDryRun())
				{
					foreach (ReceiveCommand cmd_1 in batch.GetCommands())
					{
						if (cmd_1.GetResult() == ReceiveCommand.Result.NOT_ATTEMPTED)
						{
							cmd_1.SetResult(ReceiveCommand.Result.OK);
						}
					}
				}
				else
				{
					batch.Execute(walk, monitor);
				}
			}
			catch (IOException err)
			{
				throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingTrackingRef
					, GetFirstFailedRefName(batch), err.Message), err);
			}
			finally
			{
				walk.Release();
			}
			if (!fetchHeadUpdates.IsEmpty())
			{
				try
				{
					UpdateFETCH_HEAD(result);
				}
				catch (IOException err)
				{
					throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingFETCH_HEAD
						, err.Message), err);
				}
			}
		}
示例#9
0
        /// <exception cref="System.NotSupportedException"></exception>
        /// <exception cref="NGit.Errors.TransportException"></exception>
        private void ExecuteImp(ProgressMonitor monitor, FetchResult result)
        {
            conn = transport.OpenFetch();
            try
            {
                result.SetAdvertisedRefs(transport.GetURI(), conn.GetRefsMap());
                ICollection <Ref> matched = new HashSet <Ref>();
                foreach (RefSpec spec in toFetch)
                {
                    if (spec.GetSource() == null)
                    {
                        throw new TransportException(MessageFormat.Format(JGitText.Get().sourceRefNotSpecifiedForRefspec
                                                                          , spec));
                    }
                    if (spec.IsWildcard())
                    {
                        ExpandWildcard(spec, matched);
                    }
                    else
                    {
                        ExpandSingle(spec, matched);
                    }
                }
                ICollection <Ref> additionalTags = Sharpen.Collections.EmptyList <Ref>();
                TagOpt            tagopt         = transport.GetTagOpt();
                if (tagopt == TagOpt.AUTO_FOLLOW)
                {
                    additionalTags = ExpandAutoFollowTags();
                }
                else
                {
                    if (tagopt == TagOpt.FETCH_TAGS)
                    {
                        ExpandFetchTags();
                    }
                }
                bool includedTags;
                if (!askFor.IsEmpty() && !AskForIsComplete())
                {
                    FetchObjects(monitor);
                    includedTags = conn.DidFetchIncludeTags();
                    // Connection was used for object transfer. If we
                    // do another fetch we must open a new connection.
                    //
                    CloseConnection(result);
                }
                else
                {
                    includedTags = false;
                }
                if (tagopt == TagOpt.AUTO_FOLLOW && !additionalTags.IsEmpty())
                {
                    // There are more tags that we want to follow, but
                    // not all were asked for on the initial request.
                    //
                    Sharpen.Collections.AddAll(have, askFor.Keys);
                    askFor.Clear();
                    foreach (Ref r in additionalTags)
                    {
                        ObjectId id = r.GetPeeledObjectId();
                        if (id == null)
                        {
                            id = r.GetObjectId();
                        }
                        if (transport.local.HasObject(id))
                        {
                            WantTag(r);
                        }
                    }
                    if (!askFor.IsEmpty() && (!includedTags || !AskForIsComplete()))
                    {
                        ReopenConnection();
                        if (!askFor.IsEmpty())
                        {
                            FetchObjects(monitor);
                        }
                    }
                }
            }
            finally
            {
                CloseConnection(result);
            }
            BatchRefUpdate batch = transport.local.RefDatabase.NewBatchUpdate().SetAllowNonFastForwards
                                       (true).SetRefLogMessage("fetch", true);
            RevWalk walk = new RevWalk(transport.local);

            try
            {
                if (monitor is BatchingProgressMonitor)
                {
                    ((BatchingProgressMonitor)monitor).SetDelayStart(250, TimeUnit.MILLISECONDS);
                }
                if (transport.IsRemoveDeletedRefs())
                {
                    DeleteStaleTrackingRefs(result, batch);
                }
                foreach (TrackingRefUpdate u in localUpdates)
                {
                    result.Add(u);
                    batch.AddCommand(u.AsReceiveCommand());
                }
                foreach (ReceiveCommand cmd in batch.GetCommands())
                {
                    cmd.UpdateType(walk);
                    if (cmd.GetType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD && cmd is TrackingRefUpdate.Command &&
                        !((TrackingRefUpdate.Command)cmd).CanForceUpdate())
                    {
                        cmd.SetResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD);
                    }
                }
                if (transport.IsDryRun())
                {
                    foreach (ReceiveCommand cmd_1 in batch.GetCommands())
                    {
                        if (cmd_1.GetResult() == ReceiveCommand.Result.NOT_ATTEMPTED)
                        {
                            cmd_1.SetResult(ReceiveCommand.Result.OK);
                        }
                    }
                }
                else
                {
                    batch.Execute(walk, monitor);
                }
            }
            catch (IOException err)
            {
                throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingTrackingRef
                                                                  , GetFirstFailedRefName(batch), err.Message), err);
            }
            finally
            {
                walk.Release();
            }
            if (!fetchHeadUpdates.IsEmpty())
            {
                try
                {
                    UpdateFETCH_HEAD(result);
                }
                catch (IOException err)
                {
                    throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingFETCH_HEAD
                                                                      , err.Message), err);
                }
            }
        }