示例#1
0
        /// <summary>Load the configuration as a Git text style configuration file.</summary>
        /// <remarks>
        /// Load the configuration as a Git text style configuration file.
        /// <p>
        /// If the file does not exist, this configuration is cleared, and thus
        /// behaves the same as though the file exists, but is empty.
        /// </remarks>
        /// <exception cref="System.IO.IOException">the file could not be read (but does exist).
        ///     </exception>
        /// <exception cref="NGit.Errors.ConfigInvalidException">the file is not a properly formatted configuration file.
        ///     </exception>
        public override void Load()
        {
            FileSnapshot oldSnapshot = snapshot;
            FileSnapshot newSnapshot = FileSnapshot.Save(GetFile());

            try
            {
                byte[]   @in     = IOUtil.ReadFully(GetFile());
                ObjectId newHash = Hash(@in);
                if (hash.Equals(newHash))
                {
                    if (oldSnapshot.Equals(newSnapshot))
                    {
                        oldSnapshot.SetClean(newSnapshot);
                    }
                    else
                    {
                        snapshot = newSnapshot;
                    }
                }
                else
                {
                    string decoded;
                    if (@in.Length >= 3 && @in[0] == unchecked ((byte)unchecked ((int)(0xEF))) && @in[1
                        ] == unchecked ((byte)unchecked ((int)(0xBB))) && @in[2] == unchecked ((byte)unchecked (
                                                                                                   (int)(0xBF))))
                    {
                        decoded = RawParseUtils.Decode(RawParseUtils.UTF8_CHARSET, @in, 3, @in.Length);
                        utf8Bom = true;
                    }
                    else
                    {
                        decoded = RawParseUtils.Decode(@in);
                    }
                    FromText(decoded);
                    snapshot = newSnapshot;
                    hash     = newHash;
                }
            }
            catch (FileNotFoundException)
            {
                Clear();
                snapshot = newSnapshot;
            }
            catch (IOException e)
            {
                IOException e2 = new IOException(MessageFormat.Format(JGitText.Get().cannotReadFile
                                                                      , GetFile()));
                Sharpen.Extensions.InitCause(e2, e);
                throw e2;
            }
            catch (ConfigInvalidException e)
            {
                throw new ConfigInvalidException(MessageFormat.Format(JGitText.Get().cannotReadFile
                                                                      , GetFile()), e);
            }
        }
        /// <summary>Load the configuration as a Git text style configuration file.</summary>
        /// <remarks>
        /// Load the configuration as a Git text style configuration file.
        /// <p>
        /// If the file does not exist, this configuration is cleared, and thus
        /// behaves the same as though the file exists, but is empty.
        /// </remarks>
        /// <exception cref="System.IO.IOException">the file could not be read (but does exist).
        ///     </exception>
        /// <exception cref="NGit.Errors.ConfigInvalidException">the file is not a properly formatted configuration file.
        ///     </exception>
        public override void Load()
        {
            FileSnapshot oldSnapshot = snapshot;
            FileSnapshot newSnapshot = FileSnapshot.Save(GetFile());

            try
            {
                byte[]   @in     = IOUtil.ReadFully(GetFile());
                ObjectId newHash = Hash(@in);
                if (hash.Equals(newHash))
                {
                    if (oldSnapshot.Equals(newSnapshot))
                    {
                        oldSnapshot.SetClean(newSnapshot);
                    }
                    else
                    {
                        snapshot = newSnapshot;
                    }
                }
                else
                {
                    FromText(RawParseUtils.Decode(@in));
                    snapshot = newSnapshot;
                    hash     = newHash;
                }
            }
            catch (FileNotFoundException)
            {
                Clear();
                snapshot = newSnapshot;
            }
            catch (IOException e)
            {
                IOException e2 = new IOException(MessageFormat.Format(JGitText.Get().cannotReadFile
                                                                      , GetFile()));
                Sharpen.Extensions.InitCause(e2, e);
                throw e2;
            }
            catch (ConfigInvalidException e)
            {
                throw new ConfigInvalidException(MessageFormat.Format(JGitText.Get().cannotReadFile
                                                                      , GetFile()), e);
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        private RefDirectory.LooseRef ScanRef(RefDirectory.LooseRef @ref, string name)
        {
            FilePath     path            = FileFor(name);
            FileSnapshot currentSnapshot = null;

            if (@ref != null)
            {
                currentSnapshot = @ref.GetSnapShot();
                if (!currentSnapshot.IsModified(path))
                {
                    return(@ref);
                }
                name = @ref.GetName();
            }
            int limit = 4096;

            byte[]       buf;
            FileSnapshot otherSnapshot = FileSnapshot.Save(path);

            try
            {
                buf = IOUtil.ReadSome(path, limit);
            }
            catch (FileNotFoundException)
            {
                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))
            {
                if (n == limit)
                {
                    return(null);
                }
                // possibly truncated ref
                // 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(MessageFormat.Format(JGitText.Get().notARef, name, content)
                                          );
                }
                string target = RawParseUtils.Decode(buf, 5, n);
                if (@ref != null && @ref.IsSymbolic() && @ref.GetTarget().GetName().Equals(target
                                                                                           ))
                {
                    currentSnapshot.SetClean(otherSnapshot);
                    return(@ref);
                }
                return(NewSymbolicRef(otherSnapshot, 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);
                if (@ref != null && [email protected]() && @ref.GetTarget().GetObjectId().Equals(id
                                                                                                ))
                {
                    currentSnapshot.SetClean(otherSnapshot);
                    return(@ref);
                }
            }
            catch (ArgumentException)
            {
                while (0 < n && char.IsWhiteSpace((char)buf[n - 1]))
                {
                    n--;
                }
                string content = RawParseUtils.Decode(buf, 0, n);
                throw new IOException(MessageFormat.Format(JGitText.Get().notARef, name, content)
                                      );
            }
            return(new RefDirectory.LooseUnpeeled(otherSnapshot, name, id));
        }