/// <exception cref="NGit.Errors.TransportException"></exception> private void Sendpack(IList <RemoteRefUpdate> updates, ProgressMonitor monitor) { string pathPack = null; string pathIdx = null; PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader ()); try { ICollection <ObjectId> need = new HashSet <ObjectId>(); ICollection <ObjectId> have = new HashSet <ObjectId>(); foreach (RemoteRefUpdate r in updates) { need.AddItem(r.GetNewObjectId()); } foreach (Ref r_1 in GetRefs()) { have.AddItem(r_1.GetObjectId()); if (r_1.GetPeeledObjectId() != null) { have.AddItem(r_1.GetPeeledObjectId()); } } writer.PreparePack(monitor, need, have); // We don't have to continue further if the pack will // be an empty pack, as the remote has all objects it // needs to complete this change. // if (writer.GetObjectCount() == 0) { return; } packNames = new LinkedHashMap <string, string>(); foreach (string n in dest.GetPackNames()) { packNames.Put(n, n); } string @base = "pack-" + writer.ComputeName().Name; string packName = @base + ".pack"; pathPack = "pack/" + packName; pathIdx = "pack/" + @base + ".idx"; if (Sharpen.Collections.Remove(packNames, packName) != null) { // The remote already contains this pack. We should // remove the index before overwriting to prevent bad // offsets from appearing to clients. // dest.WriteInfoPacks(packNames.Keys); dest.DeleteFile(pathIdx); } // Write the pack file, then the index, as readers look the // other direction (index, then pack file). // string wt = "Put " + Sharpen.Runtime.Substring(@base, 0, 12); OutputStream os = dest.WriteFile(pathPack, monitor, wt + "..pack"); try { os = new SafeBufferedOutputStream(os); writer.WritePack(monitor, monitor, os); } finally { os.Close(); } os = dest.WriteFile(pathIdx, monitor, wt + "..idx"); try { os = new SafeBufferedOutputStream(os); writer.WriteIndex(os); } finally { os.Close(); } // Record the pack at the start of the pack info list. This // way clients are likely to consult the newest pack first, // and discover the most recent objects there. // AList <string> infoPacks = new AList <string>(); infoPacks.AddItem(packName); Sharpen.Collections.AddAll(infoPacks, packNames.Keys); dest.WriteInfoPacks(infoPacks); } catch (IOException err) { SafeDelete(pathIdx); SafeDelete(pathPack); throw new TransportException(uri, JGitText.Get().cannotStoreObjects, err); } finally { writer.Release(); } }
/// <exception cref="NGit.Errors.TransportException"></exception> private void DownloadObject(ProgressMonitor pm, AnyObjectId id) { if (AlreadyHave(id)) { return; } for (; ;) { // Try a pack file we know about, but don't have yet. Odds are // that if it has this object, it has others related to it so // getting the pack is a good bet. // if (DownloadPackedObject(pm, id)) { return; } // Search for a loose object over all alternates, starting // from the one we last successfully located an object through. // string idStr = id.Name; string subdir = Sharpen.Runtime.Substring(idStr, 0, 2); string file = Sharpen.Runtime.Substring(idStr, 2); string looseName = subdir + "/" + file; for (int i = lastRemoteIdx; i < remotes.Count; i++) { if (DownloadLooseObject(id, looseName, remotes[i])) { lastRemoteIdx = i; return; } } for (int i_1 = 0; i_1 < lastRemoteIdx; i_1++) { if (DownloadLooseObject(id, looseName, remotes[i_1])) { lastRemoteIdx = i_1; return; } } // Try to obtain more pack information and search those. // while (!noPacksYet.IsEmpty()) { WalkRemoteObjectDatabase wrr = noPacksYet.RemoveFirst(); ICollection <string> packNameList; try { pm.BeginTask("Listing packs", ProgressMonitor.UNKNOWN); packNameList = wrr.GetPackNames(); } catch (IOException e) { // Try another repository. // RecordError(id, e); continue; } finally { pm.EndTask(); } if (packNameList == null || packNameList.IsEmpty()) { continue; } foreach (string packName in packNameList) { if (packsConsidered.AddItem(packName)) { unfetchedPacks.AddItem(new WalkFetchConnection.RemotePack(this, wrr, packName)); } } if (DownloadPackedObject(pm, id)) { return; } } // Try to expand the first alternate we haven't expanded yet. // ICollection <WalkRemoteObjectDatabase> al = ExpandOneAlternate(id, pm); if (al != null && !al.IsEmpty()) { foreach (WalkRemoteObjectDatabase alt in al) { remotes.AddItem(alt); noPacksYet.AddItem(alt); noAlternatesYet.AddItem(alt); } continue; } // We could not obtain the object. There may be reasons why. // IList <Exception> failures = fetchErrors.Get((ObjectId)id); TransportException te; te = new TransportException(MessageFormat.Format(JGitText.Get().cannotGet, id.Name )); if (failures != null && !failures.IsEmpty()) { if (failures.Count == 1) { Sharpen.Extensions.InitCause(te, failures[0]); } else { Sharpen.Extensions.InitCause(te, new CompoundException(failures)); } } throw te; } }