~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ngit/NGit/NGit.Transport/FetchProcess.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
 
87
87
                private FetchConnection conn;
88
88
 
 
89
                private IDictionary<string, Ref> localRefs;
 
90
 
89
91
                internal FetchProcess(NGit.Transport.Transport t, ICollection<RefSpec> f)
90
92
                {
91
93
                        transport = t;
100
102
                        localUpdates.Clear();
101
103
                        fetchHeadUpdates.Clear();
102
104
                        packLocks.Clear();
 
105
                        localRefs = null;
103
106
                        try
104
107
                        {
105
108
                                ExecuteImp(monitor, result);
205
208
                        {
206
209
                                CloseConnection(result);
207
210
                        }
 
211
                        BatchRefUpdate batch = transport.local.RefDatabase.NewBatchUpdate().SetAllowNonFastForwards
 
212
                                (true).SetRefLogMessage("fetch", true);
208
213
                        RevWalk walk = new RevWalk(transport.local);
209
214
                        try
210
215
                        {
212
217
                                {
213
218
                                        ((BatchingProgressMonitor)monitor).SetDelayStart(250, TimeUnit.MILLISECONDS);
214
219
                                }
215
 
                                monitor.BeginTask(JGitText.Get().updatingReferences, localUpdates.Count);
216
220
                                if (transport.IsRemoveDeletedRefs())
217
221
                                {
218
 
                                        DeleteStaleTrackingRefs(result, walk);
 
222
                                        DeleteStaleTrackingRefs(result, batch);
219
223
                                }
220
224
                                foreach (TrackingRefUpdate u in localUpdates)
221
225
                                {
222
 
                                        try
223
 
                                        {
224
 
                                                monitor.Update(1);
225
 
                                                u.Update(walk);
226
 
                                                result.Add(u);
227
 
                                        }
228
 
                                        catch (IOException err)
229
 
                                        {
230
 
                                                throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingTrackingRef
231
 
                                                        , u.GetLocalName(), err.Message), err);
232
 
                                        }
233
 
                                }
234
 
                                monitor.EndTask();
 
226
                                        result.Add(u);
 
227
                                        batch.AddCommand(u.AsReceiveCommand());
 
228
                                }
 
229
                                foreach (ReceiveCommand cmd in batch.GetCommands())
 
230
                                {
 
231
                                        cmd.UpdateType(walk);
 
232
                                        if (cmd.GetType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD && cmd is TrackingRefUpdate.Command
 
233
                                                 && !((TrackingRefUpdate.Command)cmd).CanForceUpdate())
 
234
                                        {
 
235
                                                cmd.SetResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD);
 
236
                                        }
 
237
                                }
 
238
                                if (transport.IsDryRun())
 
239
                                {
 
240
                                        foreach (ReceiveCommand cmd_1 in batch.GetCommands())
 
241
                                        {
 
242
                                                if (cmd_1.GetResult() == ReceiveCommand.Result.NOT_ATTEMPTED)
 
243
                                                {
 
244
                                                        cmd_1.SetResult(ReceiveCommand.Result.OK);
 
245
                                                }
 
246
                                        }
 
247
                                }
 
248
                                else
 
249
                                {
 
250
                                        batch.Execute(walk, monitor);
 
251
                                }
 
252
                        }
 
253
                        catch (IOException err)
 
254
                        {
 
255
                                throw new TransportException(MessageFormat.Format(JGitText.Get().failureUpdatingTrackingRef
 
256
                                        , GetFirstFailedRefName(batch), err.Message), err);
235
257
                        }
236
258
                        finally
237
259
                        {
394
416
                                        {
395
417
                                                ow.MarkStart(ow.ParseAny(want));
396
418
                                        }
397
 
                                        foreach (Ref @ref in transport.local.GetAllRefs().Values)
 
419
                                        foreach (Ref @ref in LocalRefs().Values)
398
420
                                        {
399
421
                                                ow.MarkUninteresting(ow.ParseAny(@ref.GetObjectId()));
400
422
                                        }
447
469
                private ICollection<Ref> ExpandAutoFollowTags()
448
470
                {
449
471
                        ICollection<Ref> additionalTags = new AList<Ref>();
450
 
                        IDictionary<string, Ref> haveRefs = transport.local.GetAllRefs();
 
472
                        IDictionary<string, Ref> haveRefs = LocalRefs();
451
473
                        foreach (Ref r in conn.GetRefs())
452
474
                        {
453
475
                                if (!IsTag(r))
498
520
                /// <exception cref="NGit.Errors.TransportException"></exception>
499
521
                private void ExpandFetchTags()
500
522
                {
501
 
                        IDictionary<string, Ref> haveRefs = transport.local.GetAllRefs();
 
523
                        IDictionary<string, Ref> haveRefs = LocalRefs();
502
524
                        foreach (Ref r in conn.GetRefs())
503
525
                        {
504
526
                                if (!IsTag(r))
525
547
                        ObjectId newId = src.GetObjectId();
526
548
                        if (spec.GetDestination() != null)
527
549
                        {
528
 
                                try
529
 
                                {
530
 
                                        TrackingRefUpdate tru = CreateUpdate(spec, newId);
531
 
                                        if (newId.Equals(tru.GetOldObjectId()))
532
 
                                        {
533
 
                                                return;
534
 
                                        }
535
 
                                        localUpdates.AddItem(tru);
536
 
                                }
537
 
                                catch (IOException err)
538
 
                                {
539
 
                                        // Bad symbolic ref? That is the most likely cause.
540
 
                                        //
541
 
                                        throw new TransportException(MessageFormat.Format(JGitText.Get().cannotResolveLocalTrackingRefForUpdating
542
 
                                                , spec.GetDestination()), err);
543
 
                                }
 
550
                                TrackingRefUpdate tru = CreateUpdate(spec, newId);
 
551
                                if (newId.Equals(tru.GetOldObjectId()))
 
552
                                {
 
553
                                        return;
 
554
                                }
 
555
                                localUpdates.AddItem(tru);
544
556
                        }
545
557
                        askFor.Put(newId, src);
546
558
                        FetchHeadRecord fhr = new FetchHeadRecord();
551
563
                        fetchHeadUpdates.AddItem(fhr);
552
564
                }
553
565
 
554
 
                /// <exception cref="System.IO.IOException"></exception>
 
566
                /// <exception cref="NGit.Errors.TransportException"></exception>
555
567
                private TrackingRefUpdate CreateUpdate(RefSpec spec, ObjectId newId)
556
568
                {
557
 
                        return new TrackingRefUpdate(transport.local, spec, newId, "fetch");
 
569
                        Ref @ref = LocalRefs().Get(spec.GetDestination());
 
570
                        ObjectId oldId = @ref != null && @ref.GetObjectId() != null ? @ref.GetObjectId() : 
 
571
                                ObjectId.ZeroId;
 
572
                        return new TrackingRefUpdate(spec.IsForceUpdate(), spec.GetSource(), spec.GetDestination
 
573
                                (), oldId, newId);
558
574
                }
559
575
 
560
576
                /// <exception cref="NGit.Errors.TransportException"></exception>
561
 
                private void DeleteStaleTrackingRefs(FetchResult result, RevWalk walk)
562
 
                {
563
 
                        Repository db = transport.local;
564
 
                        foreach (Ref @ref in db.GetAllRefs().Values)
 
577
                private IDictionary<string, Ref> LocalRefs()
 
578
                {
 
579
                        if (localRefs == null)
 
580
                        {
 
581
                                try
 
582
                                {
 
583
                                        localRefs = transport.local.RefDatabase.GetRefs(RefDatabase.ALL);
 
584
                                }
 
585
                                catch (IOException err)
 
586
                                {
 
587
                                        throw new TransportException(JGitText.Get().cannotListRefs, err);
 
588
                                }
 
589
                        }
 
590
                        return localRefs;
 
591
                }
 
592
 
 
593
                /// <exception cref="System.IO.IOException"></exception>
 
594
                private void DeleteStaleTrackingRefs(FetchResult result, BatchRefUpdate batch)
 
595
                {
 
596
                        foreach (Ref @ref in LocalRefs().Values)
565
597
                        {
566
598
                                string refname = @ref.GetName();
567
599
                                foreach (RefSpec spec in toFetch)
571
603
                                                RefSpec s = spec.ExpandFromDestination(refname);
572
604
                                                if (result.GetAdvertisedRef(s.GetSource()) == null)
573
605
                                                {
574
 
                                                        DeleteTrackingRef(result, db, walk, s, @ref);
 
606
                                                        DeleteTrackingRef(result, batch, s, @ref);
575
607
                                                }
576
608
                                        }
577
609
                                }
578
610
                        }
579
611
                }
580
612
 
581
 
                /// <exception cref="NGit.Errors.TransportException"></exception>
582
 
                private void DeleteTrackingRef(FetchResult result, Repository db, RevWalk walk, RefSpec
583
 
                         spec, Ref localRef)
 
613
                private void DeleteTrackingRef(FetchResult result, BatchRefUpdate batch, RefSpec 
 
614
                        spec, Ref localRef)
584
615
                {
585
 
                        string name = localRef.GetName();
586
 
                        try
587
 
                        {
588
 
                                TrackingRefUpdate u = new TrackingRefUpdate(db, name, spec.GetSource(), true, ObjectId
589
 
                                        .ZeroId, "deleted");
590
 
                                result.Add(u);
591
 
                                if (transport.IsDryRun())
592
 
                                {
593
 
                                        return;
594
 
                                }
595
 
                                u.Delete(walk);
596
 
                                switch (u.GetResult())
597
 
                                {
598
 
                                        case RefUpdate.Result.NEW:
599
 
                                        case RefUpdate.Result.NO_CHANGE:
600
 
                                        case RefUpdate.Result.FAST_FORWARD:
601
 
                                        case RefUpdate.Result.FORCED:
602
 
                                        {
603
 
                                                break;
604
 
                                        }
605
 
 
606
 
                                        default:
607
 
                                        {
608
 
                                                throw new TransportException(transport.GetURI(), MessageFormat.Format(JGitText.Get
609
 
                                                        ().cannotDeleteStaleTrackingRef2, name, u.GetResult().ToString()));
610
 
                                        }
611
 
                                }
612
 
                        }
613
 
                        catch (IOException e)
614
 
                        {
615
 
                                throw new TransportException(transport.GetURI(), MessageFormat.Format(JGitText.Get
616
 
                                        ().cannotDeleteStaleTrackingRef, name), e);
617
 
                        }
 
616
                        if (localRef.GetObjectId() == null)
 
617
                        {
 
618
                                return;
 
619
                        }
 
620
                        TrackingRefUpdate update = new TrackingRefUpdate(true, spec.GetSource(), localRef
 
621
                                .GetName(), localRef.GetObjectId(), ObjectId.ZeroId);
 
622
                        result.Add(update);
 
623
                        batch.AddCommand(update.AsReceiveCommand());
618
624
                }
619
625
 
620
626
                private static bool IsTag(Ref r)
626
632
                {
627
633
                        return name.StartsWith(Constants.R_TAGS);
628
634
                }
 
635
 
 
636
                private static string GetFirstFailedRefName(BatchRefUpdate batch)
 
637
                {
 
638
                        foreach (ReceiveCommand cmd in batch.GetCommands())
 
639
                        {
 
640
                                if (cmd.GetResult() != ReceiveCommand.Result.OK)
 
641
                                {
 
642
                                        return cmd.GetRefName();
 
643
                                }
 
644
                        }
 
645
                        return string.Empty;
 
646
                }
629
647
        }
630
648
}