Example #1
0
 /** Creates a new CSP for a fixed set of variables. */
 public CSP(List<Variable> vars)
 {
     variables = new List<Variable>(vars.Count);
     domains = new List<Domain>(vars.Count);
     constraints = new List<Constraint>();
     varIndexHash = new Dictionary<Variable, int>();
     cnet = new Dictionary<Variable, List<Constraint>>();
     Domain emptyDomain = new Domain(new List<Object>(0));
     int index = 0;
     foreach (Variable var in vars)
     {
         variables.Add(var);
         domains.Add(emptyDomain);
         varIndexHash.put(var, index++);
         cnet.put(var, new List<Constraint>());
     }
 }
Example #2
0
        private IDictionary<string, RemoteRefUpdate> PrepareRemoteUpdates()
        {
            IDictionary<string, RemoteRefUpdate> result = new Dictionary<string, RemoteRefUpdate>();
            foreach (RemoteRefUpdate rru in _toPush.Values)
            {
                Ref advertisedRef = _connection.GetRef(rru.RemoteName);
                ObjectId advertisedOld = (advertisedRef == null ? ObjectId.ZeroId : advertisedRef.ObjectId);

                if (rru.NewObjectId.Equals(advertisedOld))
                {
                    if (rru.IsDelete)
                    {
                        // ref does exist neither locally nor remotely
                        rru.Status = RemoteRefUpdate.UpdateStatus.NON_EXISTING;
                    }
                    else
                    {
                        // same object - nothing to do
                        rru.Status = RemoteRefUpdate.UpdateStatus.UP_TO_DATE;
                    }
                    continue;
                }

                // caller has explicitly specified expected old object id, while it
                // has been changed in the mean time - reject
                if (rru.IsExpectingOldObjectId && !rru.ExpectedOldObjectId.Equals(advertisedOld))
                {
                    rru.Status = RemoteRefUpdate.UpdateStatus.REJECTED_REMOTE_CHANGED;
                    continue;
                }

                // Create ref (hasn't existed on remote side) and delete ref
                // are always fast-forward commands, feasible at this level
                if (advertisedOld.Equals(ObjectId.ZeroId) || rru.IsDelete)
                {
                    rru.FastForward = true;
                    result.put(rru.RemoteName, rru);
                    continue;
                }

                // check for fast-forward:
                // - both old and new ref must point to commits, AND
                // - both of them must be known for us, exist in repository, AND
                // - old commit must be ancestor of new commit
                bool fastForward = true;
                try
                {
                    RevCommit oldRev = (_walker.parseAny(advertisedOld) as RevCommit);
                    RevCommit newRev = (_walker.parseAny(rru.NewObjectId) as RevCommit);
                    if (oldRev == null || newRev == null || !_walker.isMergedInto(oldRev, newRev))
                        fastForward = false;
                }
                catch (MissingObjectException)
                {
                    fastForward = false;
                }
                catch (Exception x)
                {
                    throw new TransportException(_transport.Uri, "reading objects from local repository failed: " + x.Message, x);
                }
                rru.FastForward = fastForward;
                if (!fastForward && !rru.ForceUpdate)
                {
                    rru.Status = RemoteRefUpdate.UpdateStatus.REJECTED_NONFASTFORWARD;
                }
                else
                {
                    result.put(rru.RemoteName, rru);
                }
            }
            return result;
        }
Example #3
0
 /**
  * @return a map with all objects referenced by a peeled ref.
  */
 public Dictionary<AnyObjectId, List<Ref>> getAllRefsByPeeledObjectId()
 {
     Dictionary<String, Ref> allRefs = getAllRefs();
     var ret = new Dictionary<AnyObjectId, List<Ref>>(allRefs.Count);
     foreach (Ref @ref in allRefs.Values) {
     Ref ref2 = @ref;
     if (!ref2.Peeled)
         ref2 = Peel(ref2);
     AnyObjectId target = ref2.PeeledObjectId;
     if (target == null)
         target = ref2.ObjectId;
     // We assume most Sets here are singletons
     List<Ref> oset = ret.put(target, new List<Ref>{ref2});
     if (oset != null) {
         // that was not the case (rare)
         if (oset.Count == 1) {
             // Was a read-only singleton, we must copy to a new Set
             oset = new List<Ref>(oset);
         }
         ret.put(target, oset);
         oset.Add(ref2);
     }
     }
     return ret;
 }
        private void verifyPrerequisites()
        {
            if (_prereqs.isEmpty())
                return;

            using (var rw = new RevWalk.RevWalk(_transport.Local))
            {
                RevFlag PREREQ = rw.newFlag("PREREQ");
                RevFlag SEEN = rw.newFlag("SEEN");

                IDictionary<ObjectId, string> missing = new Dictionary<ObjectId, string>();
                var commits = new List<RevObject>();
                foreach (KeyValuePair<ObjectId, string> e in _prereqs)
                {
                    ObjectId p = e.Key;
                    try
                    {
                        RevCommit c = rw.parseCommit(p);
                        if (!c.has(PREREQ))
                        {
                            c.add(PREREQ);
                            commits.Add(c);
                        }
                    }
                    catch (MissingObjectException)
                    {
                        missing.put(p, e.Value);
                    }
                    catch (IOException err)
                    {
                        throw new TransportException(_transport.Uri, "Cannot Read commit " + p.Name, err);
                    }
                }

                if (!missing.isEmpty())
                    throw new MissingBundlePrerequisiteException(_transport.Uri, missing);

                foreach (Ref r in _transport.Local.getAllRefs().Values)
                {
                    try
                    {
                        rw.markStart(rw.parseCommit(r.ObjectId));
                    }
                    catch (IOException)
                    {
                        // If we cannot read the value of the ref skip it.
                    }
                }

                int remaining = commits.Count;
                try
                {
                    RevCommit c;
                    while ((c = rw.next()) != null)
                    {
                        if (c.has(PREREQ))
                        {
                            c.add(SEEN);
                            if (--remaining == 0)
                                break;
                        }
                    }
                }
                catch (IOException err)
                {
                    throw new TransportException(_transport.Uri, "Cannot Read object", err);
                }

                if (remaining > 0)
                {
                    foreach (RevObject o in commits)
                    {
                        if (!o.has(SEEN))
                            missing.put(o, _prereqs.get(o));
                    }
                    throw new MissingBundlePrerequisiteException(_transport.Uri, missing);
                }
            }
        }
        private void readBundleV2()
        {
            byte[] hdrbuf = new byte[1024];
            var avail = new Dictionary<string, Ref>();
            for (; ; )
            {
                string line = readLine(hdrbuf);
                if (line.Length == 0)
                    break;

                if (line[0] == '-')
                {
                    ObjectId id = ObjectId.FromString(line.Slice(1, 41));
                    String shortDesc = null;
                    if (line.Length > 42)
                        shortDesc = line.Substring(42);
                    _prereqs.put(id, shortDesc);
                    continue;
                }

                string name = line.Slice(41, line.Length);
                ObjectId id2 = ObjectId.FromString(line.Slice(0, 40));
                Ref prior = avail.put(name, new Unpeeled(Storage.Network, name, id2));
                if (prior != null)
                {
                    throw duplicateAdvertisement(name);
                }
            }
            available(avail);
        }
Example #6
0
        private void RefreshPackedRefs()
        {
            _packedRefsFile.Refresh();
            if (!_packedRefsFile.Exists) return;

            DateTime currTime = _packedRefsFile.LastWriteTime;
            long currLen = currTime == DateTime.MinValue ? 0 : _packedRefsFile.Length;
            if (currTime == _packedRefsLastModified && currLen == _packedRefsLength) return;

            if (currTime == DateTime.MinValue)
            {
                _packedRefsLastModified = DateTime.MinValue;
                _packedRefsLength = 0;
                _packedRefs = new Dictionary<string, Ref>();
                return;
            }

            var newPackedRefs = new Dictionary<string, Ref>();
            try
            {
                using (var b = OpenReader(_packedRefsFile))
                {
                    string p;
                    Ref last = null;
                    while ((p = b.ReadLine()) != null)
                    {
                        if (p[0] == '#') continue;

                        if (p[0] == '^')
                        {
                            if (last == null)
                            {
                                throw new IOException("Peeled line before ref.");
                            }

                            ObjectId id = ObjectId.FromString(p.Substring(1));
                            last = new Ref(Ref.Storage.Packed, last.Name, last.Name, last.ObjectId, id, true);
                            newPackedRefs.put(last.Name,  last);
                            continue;
                        }

                        int sp = p.IndexOf(' ');
                        ObjectId id2 = ObjectId.FromString(p.Slice(0, sp));
                        string name = p.Substring(sp + 1);
                        last = new Ref(Ref.Storage.Packed, name, name, id2);
                        newPackedRefs.Add(last.Name, last);
                    }
                }

                _packedRefsLastModified = currTime;
                _packedRefsLength = currLen;
                _packedRefs = newPackedRefs;
                SetModified();
            }
            catch (FileNotFoundException)
            {
                // Ignore it and leave the new map empty.
                //
                _packedRefsLastModified = DateTime.MinValue;
                _packedRefsLength = 0;
                _packedRefs = newPackedRefs;
            }
            catch (IOException e)
            {
                throw new GitException("Cannot read packed refs", e);
            }
        }
        private static void readPackedRefsImpl(Dictionary<string, Ref> avail, StreamReader sr)
        {
            Ref last = null;
            bool peeled = false;
            for (; ; )
            {
                string line = sr.ReadLine();

                if (line == null)
                    break;
                if (line[0] == '#')
                {
                    if (line.StartsWith(RefDirectory.PACKED_REFS_HEADER))
                    {
                        line = line.Substring(RefDirectory.PACKED_REFS_HEADER.Length);
                        peeled = line.Contains(RefDirectory.PACKED_REFS_PEELED);
                    }
                    continue;
                }

                if (line[0] == '^')
                {
                    if (last == null)
                        throw new TransportException("Peeled line before ref");
                    ObjectId pid = ObjectId.FromString(line.Substring(1));
                    last = new PeeledTag(Storage.Packed, last.Name, last.ObjectId, pid);
                    avail.put(last.Name, last);
                    continue;
                }

                int sp = line.IndexOf(' ');
                if (sp < 0)
                    throw new TransportException("Unrecognized ref: " + line);
                ObjectId id = ObjectId.FromString(line.Slice(0, sp));
                string name = line.Substring(sp + 1);
                if (peeled)
                    last = new PeeledNonTag(Storage.Packed, name, id);
                else
                    last = new Unpeeled(Storage.Packed, name, id);

                avail.put(last.Name, last);
            }
        }
 /// <summary>
 /// This <c>read</c> method will read the XML element map from
 /// the provided node and deserialize its children as entry types.
 /// Each entry type must contain a key and value so that the entry
 /// can be inserted in to the map as a pair. If either the key or
 /// value is composite it is read as a root object, which means its
 /// <c>Root</c> annotation must be present and the name of the
 /// object element must match that root element name.
 /// </summary>
 /// <param name="node">
 /// this is the XML element that is to be deserialized
 /// </param>
 /// <param name="map">
 /// this is the map object that is to be populated
 /// </param>
 /// <returns>
 /// this returns the item to attach to the object contact
 /// </returns>
 public Object Read(InputNode node, Dictionary map) {
    InputNode from = node.getParent();
    String name = node.getName();
    while(node != null) {
       Object index = key.Read(node);
       Object item = value.Read(node);
       if(map != null) {
          map.put(index, item);
       }
       node = from.getNext(name);
    }
    return map;
 }
Example #9
0
        private Dictionary<string, Host> parse(Stream stream)
        {
            var m = new Dictionary<string, Host>();
            var sr = new StreamReader(stream);
            var current = new List<Host>(4);
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length == 0 || line.StartsWith("#"))
                    continue;

                var regex = new Regex("[ \t]*[= \t]");

                string[] parts = regex.Split(line, 2);
                string keyword = parts[0].Trim();
                string argValue = parts[1].Trim();

                var regex2 = new Regex("[ \t]");
                if (StringUtils.equalsIgnoreCase("Host", keyword))
                {
                    current.Clear();
                    foreach (string pattern in regex2.Split(argValue))
                    {
                        string name = dequote(pattern);
                        Host c = m.get(name);
                        if (c == null)
                        {
                            c = new Host();
                            m.put(name, c);
                        }
                        current.Add(c);
                    }
                    continue;
                }

                if (current.isEmpty())
                {
                    // We received an option outside of a Host block. We
                    // don't know who this should match against, so skip.
                    //
                    continue;
                }

                if (StringUtils.equalsIgnoreCase("HostName", keyword))
                {
                    foreach (Host c in current)
                        if (c.hostName == null)
                            c.hostName = dequote(argValue);
                }
                else if (StringUtils.equalsIgnoreCase("User", keyword))
                {
                    foreach (Host c in current)
                        if (c.user == null)
                            c.user = dequote(argValue);
                }
                else if (StringUtils.equalsIgnoreCase("Port", keyword))
                {
                    try
                    {
                        int port = int.Parse(dequote(argValue));
                        foreach (Host c in current)
                            if (c.port == 0)
                                c.port = port;
                    }
                    catch (FormatException)
                    {
                        // Bad port number. Don't set it.
                    }
                }
                else if (StringUtils.equalsIgnoreCase("IdentityFile", keyword))
                {
                    foreach (Host c in current)
                        if (c.identityFile == null)
                            c.identityFile = toFile(dequote(argValue));
                }
                else if (StringUtils.equalsIgnoreCase("PreferredAuthentications", keyword))
                {
                    foreach (Host c in current)
                        if (c.preferredAuthentications == null)
                            c.preferredAuthentications = nows(dequote(argValue));
                }
                else if (StringUtils.equalsIgnoreCase("BatchMode", keyword))
                {
                    foreach (Host c in current)
                        if (c.batchMode == null)
                            c.batchMode = yesno(dequote(argValue));
                }
                else if (StringUtils.equalsIgnoreCase("StrictHostKeyChecking", keyword))
                {
                    string value = dequote(argValue);
                    foreach (Host c in current)
                        if (c.strictHostKeyChecking == null)
                            c.strictHostKeyChecking = value;
                }
            }

            return m;
        }
Example #10
0
        private void readAdvertisedRefsImpl()
        {
            var avail = new Dictionary<string, Ref>();
            while (true)
            {
                string line;

                try
                {
                    line = pckIn.ReadString();
                }
                catch (EndOfStreamException)
                {
                    if (avail.Count == 0)
                    {
                        throw noRepository();
                    }

                    throw;
                }

                if (line == PacketLineIn.END)
                    break;

                if (avail.Count == 0)
                {
                    int nul = line.IndexOf('\0');
                    if (nul >= 0)
                    {
                        // The first line (if any) may contain "hidden"
                        // capability values after a NUL byte.
                        foreach (string c in line.Substring(nul + 1).Split(' '))
                            remoteCapabilies.Add(c);
                        line = line.Slice(0, nul);
                    }
                }

                string name = line.Slice(41, line.Length);
                if (avail.Count == 0 && name.Equals("capabilities^{}"))
                {
                    // special line from git-receive-pack to show
                    // capabilities when there are no refs to advertise
                    continue;
                }

                ObjectId id = ObjectId.FromString(line.Slice(0, 40));
                if (name.Equals(".have"))
                    additionalHaves.Add(id);
                else if (name.EndsWith("^{}"))
                {
                    name = name.Slice(0, name.Length - 3);
                    Ref prior = avail.get(name);
                    if (prior == null)
                        throw new PackProtocolException(uri, "advertisement of " + name + "^{} came before " + name);

                    if (prior.PeeledObjectId != null)
                        throw duplicateAdvertisement(name + "^{}");

                    avail.put(name, new PeeledTag(Storage.Network, name, prior.ObjectId, id));
                }
                else
                {
                    Ref prior = avail.put(name, new PeeledNonTag(Storage.Network, name, id));
                    if (prior != null)
                        throw duplicateAdvertisement(name);

                }
            }
            available(avail);
        }
Example #11
0
 public void Commit(Dictionary map) {
    map.put(name, value);
 }