FromString() public static method

Convert an ObjectId from raw binary representation.
public static FromString ( byte bs, int offset ) : ObjectId
bs byte /// The raw byte buffer to read from. At least 20 bytes after /// must be available within this byte array. ///
offset int /// Position to read the first byte of data from. ///
return ObjectId
Esempio n. 1
0
        /// <summary>
        /// The constructor
        /// </summary>
        /// <param name="wrapped">the wrapped database</param>
        public CachedObjectDirectory(ObjectDirectory wrapped) : base(wrapped)
        {
            DirectoryInfo objects = wrapped.getDirectory();

            string[] fanout = objects.GetDirectories().Select(x => x.FullName).ToArray();
            if (fanout == null)
            {
                fanout = new string[0];
            }
            foreach (string d in fanout)
            {
                if (d.Length != 2)
                {
                    continue;
                }
                string[] entries = PathUtil.CombineDirectoryPath(objects, d).GetFiles().Select(x => x.FullName).ToArray();
                if (entries == null)
                {
                    continue;
                }
                foreach (string e in entries)
                {
                    if (e.Length != Constants.OBJECT_ID_STRING_LENGTH - 2)
                    {
                        continue;
                    }
                    try {
                        _unpackedObjects.Add(ObjectId.FromString(d + e));
                    } catch (ArgumentException) {
                        // ignoring the file that does not represent loose object
                    }
                }
            }
        }
Esempio n. 2
0
            public Entry(byte[] raw, int pos)
            {
                oldId = ObjectId.FromString(raw, pos);
                pos  += Constants.OBJECT_ID_LENGTH * 2;
                if (raw[pos++] != ' ')
                {
                    throw new ArgumentException("Raw log message does not parse as log entry");
                }
                newId = ObjectId.FromString(raw, pos);
                pos  += Constants.OBJECT_ID_LENGTH * 2;
                if (raw[pos++] != ' ')
                {
                    throw new ArgumentException("Raw log message does not parse as log entry");
                }
                who = RawParseUtils.parsePersonIdentOnly(raw, pos);
                int p0 = RawParseUtils.next(raw, pos, (byte)'\t');

                if (p0 == -1)
                {
                    throw new ArgumentException("Raw log message does not parse as log entry");
                }

                int p1 = RawParseUtils.nextLF(raw, p0);

                if (p1 == -1)
                {
                    throw new ArgumentException("Raw log message does not parse as log entry");
                }

                comment = RawParseUtils.decode(raw, p0, p1 - 1);
            }
Esempio n. 3
0
        private ObjectId ResolveSimple(string revstr)
        {
            if (ObjectId.IsId(revstr))
            {
                return(ObjectId.FromString(revstr));
            }
            Ref r = _refDb.ReadRef(revstr);

            return(r != null ? r.ObjectId : null);
        }
Esempio n. 4
0
 /**
  * Construct a Tag representing an existing with a known name referencing an known object.
  * This could be either a simple or annotated tag.
  *
  * @param db {@link Repository}
  * @param id target id.
  * @param refName tag name or null
  * @param raw data of an annotated tag.
  */
 public Tag(Repository db, ObjectId id, string refName, byte[] raw)
 {
     Repository = db;
     if (raw != null)
     {
         TagId = id;
         Id    = ObjectId.FromString(raw, 7);
     }
     else
     {
         Id = id;
     }
     if (refName != null && refName.StartsWith("refs/tags/"))
     {
         refName = refName.Substring(10);
     }
     TagName  = refName;
     this.raw = raw;
 }
Esempio n. 5
0
        /// <summary>
        /// Access a Tree by SHA'1 id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Tree or null</returns>
        public Tree MapTree(ObjectId id)
        {
            ObjectLoader or = OpenObject(id);

            if (or == null)
            {
                return(null);
            }

            byte[] raw = or.Bytes;
            switch (((ObjectType)or.Type))
            {
            case ObjectType.Tree:
                return(new Tree(this, id, raw));

            case ObjectType.Commit:
                return(MapTree(ObjectId.FromString(raw, 5)));
            }

            throw new IncorrectObjectTypeException(id, ObjectType.Tree);
        }
Esempio n. 6
0
        private void decode()
        {
            // FIXME: handle I/O errors
            if (raw == null)
            {
                return;
            }

            using (var br = new StreamReader(new MemoryStream(raw)))
            {
                string n = br.ReadLine();
                if (n == null || !n.StartsWith("object "))
                {
                    throw new CorruptObjectException(TagId, "no object");
                }
                Id = ObjectId.FromString(n.Substring(7));
                n  = br.ReadLine();
                if (n == null || !n.StartsWith("type "))
                {
                    throw new CorruptObjectException(TagId, "no type");
                }
                TagType = n.Substring("type ".Length);
                n       = br.ReadLine();

                if (n == null || !n.StartsWith("tag "))
                {
                    throw new CorruptObjectException(TagId, "no tag name");
                }
                TagName = n.Substring("tag ".Length);
                n       = br.ReadLine();

                // We should see a "tagger" header here, but some repos have tags
                // without it.
                if (n == null)
                {
                    throw new CorruptObjectException(TagId, "no tagger header");
                }

                if (n.Length > 0)
                {
                    if (n.StartsWith("tagger "))
                    {
                        Tagger = new PersonIdent(n.Substring("tagger ".Length));
                    }
                    else
                    {
                        throw new CorruptObjectException(TagId, "no tagger/bad header");
                    }
                }

                // Message should start with an empty line, but
                StringBuilder tempMessage = new StringBuilder();
                char[]        readBuf     = new char[2048];
                int           readLen;
                int           readIndex = 0;
                while ((readLen = br.Read(readBuf, readIndex, readBuf.Length)) > 0)
                {
                    //readIndex += readLen;
                    tempMessage.Append(readBuf, 0, readLen);
                }
                message = tempMessage.ToString();
                if (message.StartsWith("\n"))
                {
                    message = message.Substring(1);
                }
            }

            raw = null;
        }
Esempio n. 7
0
        ///	<summary>
        /// Create a commit object with the specified id and data from an existing
        /// commit object in a repository.
        /// </summary>
        /// <param name="db">
        /// The repository to which this commit object belongs.
        /// </param>
        /// <param name="id">Commit id.</param>
        /// <param name="raw">Raw commit object data.</param>
        public Commit(Repository db, ObjectId id, byte[] raw)
        {
            Repository = db;
            CommitId   = id;
            _treeId    = ObjectId.FromString(raw, 5);
            ParentIds  = new ObjectId[1];
            int np     = 0;
            int rawPtr = 46;

            while (true)
            {
                if (raw[rawPtr] != 'p')
                {
                    break;
                }

                if (np == 0)
                {
                    ParentIds[np++] = ObjectId.FromString(raw, rawPtr + 7);
                }
                else if (np == 1)
                {
                    ParentIds = new[] { ParentIds[0], ObjectId.FromString(raw, rawPtr + 7) };
                    np++;
                }
                else
                {
                    if (ParentIds.Length <= np)
                    {
                        ObjectId[] old = ParentIds;
                        ParentIds = new ObjectId[ParentIds.Length + 32];
                        for (int i = 0; i < np; ++i)
                        {
                            ParentIds[i] = old[i];
                        }
                    }
                    ParentIds[np++] = ObjectId.FromString(raw, rawPtr + 7);
                }
                rawPtr += 48;
            }

            if (np != ParentIds.Length)
            {
                ObjectId[] old = ParentIds;
                ParentIds = new ObjectId[np];
                for (int i = 0; i < np; ++i)
                {
                    ParentIds[i] = old[i];
                }
            }
            else
            {
                if (np == 0)
                {
                    ParentIds = EmptyObjectidList;
                }
            }

            _raw = raw;
            Decode();
        }
Esempio n. 8
0
        private void RefreshPackedRefs()
        {
            lock (locker)
            {
                _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);
                }
            }
        }
Esempio n. 9
0
        private Ref ReadRefBasic(String origName, string name, int depth)
        {
            // Prefer loose ref to packed ref as the loose
            // file can be more up-to-date than a packed one.
            //
            Ref @ref;

            _looseRefs.TryGetValue(name, out @ref);
            FileInfo loose = FileForRef(name);

            loose.Refresh();
            DateTime mtime = loose.Exists ? loose.LastWriteTime : DateTime.MinValue;                    // [ammachado] If the file does not exists, LastWriteTimes returns '1600-12-31 22:00:00'

            if (@ref != null)
            {
                DateTime cachedLastModified;
                if (_looseRefsMTime.TryGetValue(name, out cachedLastModified) && cachedLastModified == mtime)
                {
                    return(_packedRefs.ContainsKey(origName) ?
                           new Ref(Ref.Storage.LoosePacked, origName, @ref.ObjectId, @ref.PeeledObjectId, @ref.Peeled)
                                                : @ref);
                }

                _looseRefs.Remove(origName);
                _looseRefsMTime.Remove(origName);
            }

            if (!loose.Exists)
            {
                // File does not exist.
                // Try packed cache.
                //
                _packedRefs.TryGetValue(name, out @ref);
                if (@ref != null && [email protected](origName))
                {
                    @ref = new Ref(Ref.Storage.LoosePacked, origName, name, @ref.ObjectId);
                }
                return(@ref);
            }

            string line = null;

            try
            {
                DateTime cachedLastModified;
                if (_looseRefsMTime.TryGetValue(name, out cachedLastModified) && cachedLastModified == mtime)
                {
                    _looseSymRefs.TryGetValue(name, out line);
                }

                if (string.IsNullOrEmpty(line))
                {
                    line = ReadLine(loose);
                    _looseRefsMTime[name] = mtime;
                    _looseSymRefs[name]   = line;
                }
            }
            catch (FileNotFoundException)
            {
                return(_packedRefs[name]);
            }

            if (string.IsNullOrEmpty(line))
            {
                _looseRefs.Remove(origName);
                _looseRefsMTime.Remove(origName);
                return(new Ref(Ref.Storage.Loose, origName, name, null));
            }

            if (line.StartsWith("ref: "))
            {
                if (depth >= 5)
                {
                    throw new IOException("Exceeded maximum ref depth of " + depth + " at " + name + ".  Circular reference?");
                }

                string   target = line.Substring("ref: ".Length);
                Ref      r      = ReadRefBasic(target, depth + 1);
                DateTime cachedMtime;
                if (_looseRefsMTime.TryGetValue(name, out cachedMtime) && cachedMtime != mtime)
                {
                    SetModified();
                }
                _looseRefsMTime[name] = mtime;

                if (r == null)
                {
                    return(new Ref(Ref.Storage.Loose, origName, target, null));
                }

                if (!origName.Equals(r.Name))
                {
                    r = new Ref(Ref.Storage.LoosePacked, origName, r.Name, r.ObjectId, r.PeeledObjectId, true);
                }

                return(r);
            }

            SetModified();

            ObjectId id;

            try
            {
                id = ObjectId.FromString(line);
            }
            catch (ArgumentException)
            {
                throw new IOException("Not a ref: " + name + ": " + line);
            }

            Ref.Storage storage = _packedRefs.ContainsKey(name) ? Ref.Storage.LoosePacked : Ref.Storage.Loose;
            @ref                  = new Ref(storage, name, id);
            _looseRefs[name]      = @ref;
            _looseRefsMTime[name] = mtime;

            if (!origName.Equals(name))
            {
                @ref = new Ref(Ref.Storage.Loose, origName, name, id);
                _looseRefs[origName] = @ref;
            }

            return(@ref);
        }
Esempio n. 10
0
        private LooseRef scanRef(LooseRef @ref, string name)
        {
            FileInfo path     = fileFor(name);
            long     modified = 0;

            modified = path.lastModified();

            if (@ref != null)
            {
                if (@ref.getLastModified() == modified)
                {
                    return(@ref);
                }
                name = @ref.getName();
            }
            else if (modified == 0)
            {
                return(null);
            }

            byte[] buf;
            try
            {
                buf = IO.ReadFully(path, 4096);
            }
            catch (FileNotFoundException)
            {
                return(null); // doesn't exist; not a reference.
            }
            catch (DirectoryNotFoundException)
            {
                return(null); // doesn't exist; not a reference.
            }

            int n = buf.Length;

            if (n == 0)
            {
                return(null); // empty file; not a reference.
            }
            if (isSymRef(buf, n))
            {
                // trim trailing whitespace
                while (0 < n && Char.IsWhiteSpace((char)buf[n - 1]))
                {
                    n--;
                }
                if (n < 6)
                {
                    string content = RawParseUtils.decode(buf, 0, n);
                    throw new IOException("Not a ref: " + name + ": " + content);
                }
                string target = RawParseUtils.decode(buf, 5, n);
                return(newSymbolicRef(modified, name, target));
            }

            if (n < Constants.OBJECT_ID_STRING_LENGTH)
            {
                return(null); // impossibly short object identifier; not a reference.
            }
            ObjectId id;

            try
            {
                id = ObjectId.FromString(buf, 0);
            }
            catch (ArgumentException)
            {
                while (0 < n && Char.IsWhiteSpace((char)buf[n - 1]))
                {
                    n--;
                }
                string content = RawParseUtils.decode(buf, 0, n);
                throw new IOException("Not a ref: " + name + ": " + content);
            }
            return(new LooseUnpeeled(modified, name, id));
        }
Esempio n. 11
0
        private static RefList <Ref> parsePackedRefs(TextReader br)
        {
            var all = new RefList <Ref> .Builder <Ref>();

            Ref  last     = null;
            bool peeled   = false;
            bool needSort = false;

            string p;

            while ((p = br.ReadLine()) != null)
            {
                if (p[0] == '#')
                {
                    if (p.StartsWith(PACKED_REFS_HEADER))
                    {
                        p      = p.Substring(PACKED_REFS_HEADER.Length);
                        peeled = p.Contains(PACKED_REFS_PEELED);
                    }
                    continue;
                }

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

                    ObjectId id = ObjectId.FromString(p.Substring(1));
                    last = new PeeledTag(Storage.Packed, last.getName(), last
                                         .getObjectId(), id);
                    all.set(all.size() - 1, last);
                    continue;
                }

                int         sp   = p.IndexOf(' ');
                ObjectId    id2  = ObjectId.FromString(p.Slice(0, sp));
                string      name = copy(p, sp + 1, p.Length);
                ObjectIdRef cur;
                if (peeled)
                {
                    cur = new PeeledNonTag(Storage.Packed, name, id2);
                }
                else
                {
                    cur = new Unpeeled(Storage.Packed, name, id2);
                }
                if (last != null && RefComparator.compareTo(last, cur) > 0)
                {
                    needSort = true;
                }
                all.add(cur);
                last = cur;
            }

            if (needSort)
            {
                all.sort();
            }
            return(all.toRefList());
        }