encode() public static method

Convert a string to a byte array in UTF-8 character encoding.
public static encode ( string str ) : byte[]
str string /// The string to convert. May contain any Unicode characters. ///
return byte[]
示例#1
0
 private void ReadTree(string prefix, Tree t)
 {
     TreeEntry[] members = t.Members;
     for (int i = 0; i < members.Length; ++i)
     {
         TreeEntry te = members[i];
         string    name;
         if (prefix.Length > 0)
         {
             name = prefix + "/" + te.Name;
         }
         else
         {
             name = te.Name;
         }
         Tree tr = (te as Tree);
         if (tr != null)
         {
             ReadTree(name, tr);
         }
         else
         {
             var e = new Entry(Repository, te, 0);
             _entries[Constants.encode(name)] = e;
         }
     }
 }
示例#2
0
        ///	<summary>
        /// Read the cache file into memory.
        ///	</summary>
        ///	<exception cref="IOException"> </exception>
        public void Read()
        {
            _changed   = false;
            _statDirty = false;

            _cacheFile.Refresh();

            if (!_cacheFile.Exists)
            {
                _header = null;
                _entries.Clear();
                _lastCacheTime = 0;
                return;
            }

            using (var cache = new FileStream(_cacheFile.FullName, System.IO.FileMode.Open))
                using (var buffer = new EndianBinaryReader(new BigEndianBitConverter(), cache))
                {
                    _header = new Header(buffer);
                    _entries.Clear();

                    for (int i = 0; i < _header.Entries; ++i)
                    {
                        var entry = new Entry(Repository, buffer);
                        _entries[Constants.encode(entry.Name)] = entry;
                    }

                    _lastCacheTime = _cacheFile.lastModified();
                }
        }
示例#3
0
        private static void AppendOneRecord(ObjectId oldId, ObjectId newId, PersonIdent ident, string msg, Repository db, string refName)
        {
            ident = ident == null ? new PersonIdent(db) : new PersonIdent(ident);

            var r = new StringBuilder();

            r.Append(ObjectId.ToString(oldId));
            r.Append(' ');
            r.Append(ObjectId.ToString(newId));
            r.Append(' ');
            r.Append(ident.ToExternalString());
            r.Append('\t');
            r.Append(msg);
            r.Append('\n');

            byte[] rec    = Constants.encode(r.ToString());
            var    logdir = new DirectoryInfo(Path.Combine(db.Directory.FullName, Constants.LOGS));
            var    reflog = new FileInfo(Path.Combine(logdir.FullName, refName));

            if (reflog.Exists || db.Config.getCore().isLogAllRefUpdates())
            {
                DirectoryInfo refdir = reflog.Directory;

                if (!refdir.Exists && !refdir.Mkdirs())
                {
                    throw new IOException("Cannot create directory " + refdir);
                }

                using (var @out = new FileStream(reflog.FullName, System.IO.FileMode.Append, FileAccess.Write))
                {
                    @out.Write(rec, 0, rec.Length);
                }
            }
        }
示例#4
0
        /**
         * Rebuild the {@link Constants#INFO_REFS}.
         * <para />
         * This method rebuilds the contents of the {@link Constants#INFO_REFS} file
         * to match the passed list of references.
         *
         *
         * @
         *             writing is not supported, or attempting to write the file
         *             failed, possibly due to permissions or remote disk full, etc.
         */
        public void writeInfoRefs()
        {
            StringBuilder w = new StringBuilder();

            char[] tmp = new char[Constants.OBJECT_ID_LENGTH * 2];
            foreach (Ref r in refs)
            {
                if (Constants.HEAD.Equals(r.Name))
                {
                    // Historically HEAD has never been published through
                    // the INFO_REFS file. This is a mistake, but its the
                    // way things are.
                    //
                    continue;
                }

                r.ObjectId.CopyTo(tmp, w);
                w.Append('\t');
                w.Append(r.Name);
                w.Append('\n');

                if (r.PeeledObjectId != null)
                {
                    r.PeeledObjectId.CopyTo(tmp, w);
                    w.Append('\t');
                    w.Append(r.Name);
                    w.Append("^{}\n");
                }
            }
            writeFile(Constants.INFO_REFS, Constants.encode(w.ToString()));
        }
示例#5
0
        ///	<summary>
        /// Construct and write tree out of index.
        ///	</summary>
        ///	<returns> SHA-1 of the constructed tree</returns>
        /// <exception cref="IOException"></exception>
        public ObjectId writeTree()
        {
            CheckWriteOk();
            var writer  = new ObjectWriter(Repository);
            var current = new Tree(Repository);
            var trees   = new Stack <Tree>();

            trees.Push(current);
            var prevName = new string[0];

            foreach (Entry e in _entries.Values)
            {
                if (e.Stage != STAGE_0)
                {
                    continue;
                }

                string[] newName = SplitDirPath(e.Name);
                int      c       = LongestCommonPath(prevName, newName);
                while (c < trees.Count - 1)
                {
                    current.Id = writer.WriteTree(current);
                    trees.Pop();
                    current = trees.Count == 0 ? null : trees.Peek();
                }

                while (trees.Count < newName.Length)
                {
                    if (!current.ExistsTree(newName[trees.Count - 1]))
                    {
                        current = new Tree(current, Constants.encode(newName[trees.Count - 1]));
                        current.Parent.AddEntry(current);
                        trees.Push(current);
                    }
                    else
                    {
                        current = (Tree)current.findTreeMember(newName[trees.Count - 1]);
                        trees.Push(current);
                    }
                }

                var ne = new FileTreeEntry(current, e.ObjectId, Constants.encode(newName[newName.Length - 1]),
                                           (e.Mode & FileMode.ExecutableFile.Bits) == FileMode.ExecutableFile.Bits);
                current.AddEntry(ne);
            }

            while (trees.Count != 0)
            {
                current.Id = writer.WriteTree(current);
                trees.Pop();

                if (trees.Count != 0)
                {
                    current = trees.Peek();
                }
            }

            return(current.TreeId);
        }
示例#6
0
        ///	<summary>
        /// Add tree entry to index
        /// </summary>
        ///	<param name="te"> tree entry </param>
        ///	<returns> new or modified index entry </returns>
        ///	<exception cref="IOException"> </exception>
        public Entry addEntry(TreeEntry te)
        {
            byte[] key = Constants.encode(te.FullName);
            var    e   = new Entry(Repository, te, 0);

            _entries[key] = e;
            return(e);
        }
示例#7
0
 /// <summary>
 /// Writes a symref (e.g. HEAD) to disk
 /// * @param name
 /// </summary>
 /// <param name="name">symref name</param>
 /// <param name="target">pointed to ref</param>
 public void Link(string name, string target)
 {
     lock (locker)
     {
         byte[] content = Constants.encode("ref: " + target + "\n");
         LockAndWriteFile(FileForRef(name), content);
         UncacheSymRef(name);
         Repository.fireRefsMaybeChanged();
     }
 }
示例#8
0
        public bool Remove(string relative_file_path)
        {
            if (string.IsNullOrEmpty(relative_file_path))
            {
                return(false);
            }
            var key = Core.Repository.GitInternalSlash(Constants.encode(relative_file_path));

            return(_entries.Remove(key));
        }
示例#9
0
        private byte[] MakeKey(FileSystemInfo wd, FileSystemInfo f)
        {
            if (!string.IsNullOrEmpty(f.DirectoryName()) &&
                wd.IsDirectory() && wd.Exists &&
                !f.DirectoryName().StartsWith(wd.DirectoryName()))
            {
                throw new Exception("Path is not in working dir");
            }

            string relName = Repository.StripWorkDir(wd, f);

            return(Core.Repository.GitInternalSlash(Constants.encode(relName)));
        }
示例#10
0
        /**
         * Rebuild the {@link Constants#PACKED_REFS} file.
         * <para />
         * This method rebuilds the contents of the {@link Constants#PACKED_REFS}
         * file to match the passed list of references, including only those refs
         * that have a storage type of {@link Ref.Storage#PACKED}.
         *
         * @
         *             writing is not supported, or attempting to write the file
         *             failed, possibly due to permissions or remote disk full, etc.
         */
        public void writePackedRefs()
        {
            bool peeled = false;

            foreach (Ref r in refs)
            {
                if (r.StorageFormat != Ref.Storage.Packed)
                {
                    continue;
                }
                if (r.PeeledObjectId != null)
                {
                    peeled = true;
                }
            }

            StringBuilder w = new StringBuilder();

            if (peeled)
            {
                w.Append("# pack-refs with:");
                if (peeled)
                {
                    w.Append(" peeled");
                }
                w.Append('\n');
            }

            char[] tmp = new char[Constants.OBJECT_ID_LENGTH * 2];
            foreach (Ref r in refs)
            {
                if (r.StorageFormat != Ref.Storage.Packed)
                {
                    continue;
                }

                r.ObjectId.CopyTo(tmp, w);
                w.Append(' ');
                w.Append(r.Name);
                w.Append('\n');

                if (r.PeeledObjectId != null)
                {
                    w.Append('^');
                    r.PeeledObjectId.CopyTo(tmp, w);
                    w.Append('\n');
                }
            }
            writeFile(Constants.PACKED_REFS, Constants.encode(w.ToString()));
        }
示例#11
0
        /// <summary>
        /// Rebuild the <see cref="Constants.PACKED_REFS"/> file.
        /// <para />
        /// This method rebuilds the contents of the <see cref="Constants.PACKED_REFS"/>
        /// file to match the passed list of references, including only those refs
        /// that have a storage type of <see cref="Storage.Packed"/>.
        /// </summary>
        public void writePackedRefs()
        {
            bool peeled = false;

            foreach (Ref r in refs)
            {
                if (r.StorageFormat.IsPacked && r.IsPeeled)
                {
                    peeled = true;
                    break;
                }
            }

            var w = new StringBuilder();

            if (peeled)
            {
                w.Append(RefDirectory.PACKED_REFS_HEADER);
                if (peeled)
                {
                    w.Append(RefDirectory.PACKED_REFS_PEELED);
                }
                w.Append('\n');
            }

            var tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];

            foreach (Ref r in refs)
            {
                if (r.StorageFormat != Storage.Packed)
                {
                    continue;
                }

                r.ObjectId.CopyTo(tmp, w);
                w.Append(' ');
                w.Append(r.Name);
                w.Append('\n');

                if (r.PeeledObjectId != null)
                {
                    w.Append('^');
                    r.PeeledObjectId.CopyTo(tmp, w);
                    w.Append('\n');
                }
            }
            writeFile(Constants.PACKED_REFS, Constants.encode(w.ToString()));
        }
示例#12
0
        public void save()
        {
            byte[] o = Constants.encode(toText());
            using (LockFile lf = new LockFile(getFile()))
            {
                if (!lf.Lock())
                {
                    throw new IOException("Cannot lock " + getFile());
                }

                lf.Write(o);
                if (!lf.Commit())
                {
                    throw new IOException("Cannot commit write to " + getFile());
                }
            }
        }
示例#13
0
        /// <summary>
        /// Create the <code>pack-*.keep</code> file, with the given message.
        /// </summary>
        /// <param name="msg">message to store in the file.</param>
        /// <returns>
        /// true if the keep file was successfully written; false otherwise.
        /// </returns>
        /// <exception cref="IOException">
        /// The keep file could not be written.
        /// </exception>
        public bool Lock(string msg)
        {
            if (msg == null)
            {
                return(false);
            }
            if (!msg.EndsWith("\n"))
            {
                msg += "\n";
            }
            var lf = new LockFile(_keepFile);

            if (!lf.Lock())
            {
                return(false);
            }
            lf.Write(Constants.encode(msg));
            return(lf.Commit());
        }
示例#14
0
        public void log(RefUpdate update, string msg, bool deref)
        {
            ObjectId oldId = update.getOldObjectId();
            ObjectId newId = update.getNewObjectId();
            Ref      @ref  = update.getRef();

            PersonIdent ident = update.getRefLogIdent();

            if (ident == null)
            {
                ident = new PersonIdent(parent);
            }
            else
            {
                ident = new PersonIdent(ident);
            }

            var r = new StringBuilder();

            r.Append(ObjectId.ToString(oldId));
            r.Append(' ');
            r.Append(ObjectId.ToString(newId));
            r.Append(' ');
            r.Append(ident.ToExternalString());
            r.Append('\t');
            r.Append(msg);
            r.Append('\n');
            byte[] rec = Constants.encode(r.ToString());

            if (deref && @ref.isSymbolic())
            {
                log(@ref.getName(), rec);
                log(@ref.getLeaf().getName(), rec);
            }
            else
            {
                log(@ref.getName(), rec);
            }
        }
示例#15
0
        protected override RefUpdateResult doLink(string target)
        {
            _lock.setNeedStatInformation(true);
            _lock.Write(Constants.encode(RefDirectory.SYMREF + target + '\n'));

            string msg = getRefLogMessage();

            if (msg != null)
            {
                _database.log(this, msg, false);
            }
            if (!_lock.Commit())
            {
                return(RefUpdateResult.LOCK_FAILURE);
            }
            _database.storedSymbolicRef(this, _lock.CommitLastModified, target);

            if (Ref.getStorage() == Storage.New)
            {
                return(RefUpdateResult.NEW);
            }
            return(RefUpdateResult.FORCED);
        }
示例#16
0
 internal Entry(Repository repository, TreeEntry f, int stage)
     : this(repository)
 {
     Ctime = -1;                 // hmm
     Mtime = -1;
     _dev  = -1;
     _ino  = -1;
     Mode  = f.Mode.Bits;
     _uid  = -1;
     _gid  = -1;
     try
     {
         _size = (int)Repository.OpenBlob(f.Id).Size;
     }
     catch (IOException e)
     {
         e.printStackTrace();
         _size = -1;
     }
     ObjectId = f.Id;
     _name    = Constants.encode(f.FullName);
     _flags   = (short)((stage << 12) | _name.Length);               // TODO: fix _flags
 }
示例#17
0
        ///	<summary>
        /// Adds a new or existing file with the specified name to this tree.
        ///	Trees are added if necessary as the name may contain '/':s.
        ///	</summary>
        ///	<param name="name"> Name </param>
        ///	<returns>A <seealso cref="FileTreeEntry"/> for the added file.</returns>
        ///	<exception cref="IOException"></exception>

        public FileTreeEntry AddFile(string name)
        {
            return(AddFile(Repository.GitInternalSlash(Constants.encode(name)), 0));
        }
示例#18
0
 /// <summary>
 /// Look up an entry with the specified path.
 /// </summary>
 /// <param name="path"></param>
 /// <returns>Index entry for the path or null if not in index.</returns>
 public Entry GetEntry(string path)
 {
     byte[] val = Repository.GitInternalSlash(Constants.encode(path));
     return(_entries.Where(e => e.Key.SequenceEqual(val)).FirstOrDefault().Value);
 }
示例#19
0
 ///	<summary>
 /// Adds a new or existing Tree with the specified name to this tree.
 ///	Trees are added if necessary as the name may contain '/':s.
 ///	</summary>
 ///	<param name="name"></param>
 ///	<returns>A <seealso cref="FileTreeEntry"/> for the added tree.</returns>
 ///	<exception cref="IOException"> </exception>
 public Tree AddTree(string name)
 {
     return(AddTree(Repository.GitInternalSlash(Constants.encode(name)), 0));
 }
示例#20
0
 private TreeEntry FindMember(string s, byte slast)
 {
     return(FindMember(Repository.GitInternalSlash(Constants.encode(s)), slast, 0));
 }
示例#21
0
 public void Rename(string n)
 {
     Rename(Constants.encode(n));
 }