Esempio n. 1
0
 /// <summary>Create a repository using the local file system.</summary>
 /// <remarks>Create a repository using the local file system.</remarks>
 /// <param name="options">description of the repository's important paths.</param>
 /// <exception cref="System.IO.IOException">
 /// the user configuration file or repository configuration file
 /// cannot be accessed.
 /// </exception>
 protected internal FileRepository(BaseRepositoryBuilder options) : base(options)
 {
     systemConfig = SystemReader.GetInstance().OpenSystemConfig(null, FileSystem);
     userConfig   = SystemReader.GetInstance().OpenUserConfig(systemConfig, FileSystem);
     repoConfig   = new FileBasedConfig(userConfig, FileSystem.Resolve(Directory, Constants
                                                                       .CONFIG), FileSystem);
     LoadSystemConfig();
     LoadUserConfig();
     LoadRepoConfig();
     repoConfig.AddChangeListener(new _ConfigChangedListener_171(this));
     refs           = new RefDirectory(this);
     objectDatabase = new ObjectDirectory(repoConfig, options.GetObjectDirectory(), options
                                          .GetAlternateObjectDirectories(), FileSystem);
     //
     //
     //
     if (objectDatabase.Exists())
     {
         long repositoryFormatVersion = ((FileBasedConfig)GetConfig()).GetLong(ConfigConstants
                                                                               .CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
         if (repositoryFormatVersion > 0)
         {
             throw new IOException(MessageFormat.Format(JGitText.Get().unknownRepositoryFormat2
                                                        , Sharpen.Extensions.ValueOf(repositoryFormatVersion)));
         }
     }
     if (!IsBare)
     {
         snapshot = FileSnapshot.Save(GetIndexFile());
     }
 }
        /// <exception cref="System.IO.IOException"></exception>
        private RefDirectory.PackedRefList ReadPackedRefs()
        {
            FileSnapshot   snapshot = FileSnapshot.Save(packedRefsFile);
            BufferedReader br;
            MessageDigest  digest = Constants.NewMessageDigest();

            try
            {
                br = new BufferedReader(new InputStreamReader(new DigestInputStream(new FileInputStream
                                                                                        (packedRefsFile), digest), Constants.CHARSET));
            }
            catch (FileNotFoundException)
            {
                // Ignore it and leave the new list empty.
                return(RefDirectory.PackedRefList.NO_PACKED_REFS);
            }
            try
            {
                return(new RefDirectory.PackedRefList(ParsePackedRefs(br), snapshot, ObjectId.FromRaw
                                                          (digest.Digest())));
            }
            finally
            {
                br.Close();
            }
        }
Esempio n. 3
0
 private void SaveStatInformation()
 {
     if (needSnapshot)
     {
         commitSnapshot = FileSnapshot.Save(lck);
     }
 }
Esempio n. 4
0
        private ObjectDirectory.PackList ScanPacksImpl(ObjectDirectory.PackList old)
        {
            IDictionary <string, PackFile> forReuse = ReuseMap(old);
            FileSnapshot         snapshot           = FileSnapshot.Save(packDirectory);
            ICollection <string> names = ListPackDirectory();
            IList <PackFile>     list  = new AList <PackFile>(names.Count >> 2);
            bool foundNew = false;

            foreach (string indexName in names)
            {
                // Must match "pack-[0-9a-f]{40}.idx" to be an index.
                //
                if (indexName.Length != 49 || !indexName.EndsWith(".idx"))
                {
                    continue;
                }
                string @base    = Sharpen.Runtime.Substring(indexName, 0, indexName.Length - 4);
                string packName = @base + ".pack";
                if (!names.Contains(packName))
                {
                    // Sometimes C Git's HTTP fetch transport leaves a
                    // .idx file behind and does not download the .pack.
                    // We have to skip over such useless indexes.
                    //
                    continue;
                }
                PackFile oldPack = Sharpen.Collections.Remove(forReuse, packName);
                if (oldPack != null)
                {
                    list.AddItem(oldPack);
                    continue;
                }
                FilePath packFile = new FilePath(packDirectory, packName);
                FilePath idxFile  = new FilePath(packDirectory, indexName);
                list.AddItem(new PackFile(idxFile, packFile));
                foundNew = true;
            }
            // If we did not discover any new files, the modification time was not
            // changed, and we did not remove any files, then the set of files is
            // the same as the set we were given. Instead of building a new object
            // return the same collection.
            //
            if (!foundNew && forReuse.IsEmpty() && snapshot.Equals(old.snapshot))
            {
                old.snapshot.SetClean(snapshot);
                return(old);
            }
            foreach (PackFile p in forReuse.Values)
            {
                p.Close();
            }
            if (list.IsEmpty())
            {
                return(new ObjectDirectory.PackList(snapshot, NO_PACKS.packs));
            }
            PackFile[] r = Sharpen.Collections.ToArray(list, new PackFile[list.Count]);
            Arrays.Sort(r, PackFile.SORT);
            return(new ObjectDirectory.PackList(snapshot, r));
        }
Esempio n. 5
0
        /// <exception cref="System.IO.IOException"></exception>
        private ObjectDirectory.CachedPackList ScanCachedPacks(ObjectDirectory.CachedPackList
                                                               old)
        {
            FileSnapshot s = FileSnapshot.Save(cachedPacksFile);

            byte[] buf;
            try
            {
                buf = IOUtil.ReadFully(cachedPacksFile);
            }
            catch (FileNotFoundException)
            {
                buf = new byte[0];
            }
            if (old != null && old.snapshot.Equals(s) && Arrays.Equals(old.raw, buf))
            {
                old.snapshot.SetClean(s);
                return(old);
            }
            AList <LocalCachedPack> list = new AList <LocalCachedPack>(4);
            ICollection <ObjectId>  tips = new HashSet <ObjectId>();
            int ptr = 0;

            while (ptr < buf.Length)
            {
                if (buf[ptr] == '#' || buf[ptr] == '\n')
                {
                    ptr = RawParseUtils.NextLF(buf, ptr);
                    continue;
                }
                if (buf[ptr] == '+')
                {
                    tips.AddItem(ObjectId.FromString(buf, ptr + 2));
                    ptr = RawParseUtils.NextLF(buf, ptr + 2);
                    continue;
                }
                IList <string> names = new AList <string>(4);
                while (ptr < buf.Length && buf[ptr] == 'P')
                {
                    int end = RawParseUtils.NextLF(buf, ptr);
                    if (buf[end - 1] == '\n')
                    {
                        end--;
                    }
                    names.AddItem(RawParseUtils.Decode(buf, ptr + 2, end));
                    ptr = RawParseUtils.NextLF(buf, end);
                }
                if (!tips.IsEmpty() && !names.IsEmpty())
                {
                    list.AddItem(new LocalCachedPack(this, tips, names));
                    tips = new HashSet <ObjectId>();
                }
            }
            list.TrimToSize();
            return(new ObjectDirectory.CachedPackList(s, Sharpen.Collections.UnmodifiableList
                                                          (list), buf));
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
0
        public virtual void TestNewFileNoWait()
        {
            FilePath f1 = CreateFile("newfile");

            WaitNextSec(f1);
            FileSnapshot save = FileSnapshot.Save(f1);

            Sharpen.Thread.Sleep(1500);
            NUnit.Framework.Assert.IsTrue(save.IsModified(f1));
        }
Esempio n. 8
0
        public virtual void TestOldFile()
        {
            FilePath f1 = CreateFile("oldfile");

            WaitNextSec(f1);
            FileSnapshot save = FileSnapshot.Save(f1);

            Sharpen.Thread.Sleep(3500);
            NUnit.Framework.Assert.IsFalse(save.IsModified(f1));
        }
Esempio n. 9
0
        public virtual void TestActuallyIsModifiedTrivial()
        {
            FilePath f1 = CreateFile("simple");

            WaitNextSec(f1);
            FileSnapshot save = FileSnapshot.Save(f1);

            Append(f1, unchecked ((byte)'x'));
            WaitNextSec(f1);
            NUnit.Framework.Assert.IsTrue(save.IsModified(f1));
        }
Esempio n. 10
0
        /// <summary>Wait until the lock file information differs from the old file.</summary>
        /// <remarks>
        /// Wait until the lock file information differs from the old file.
        /// <p>
        /// This method tests the last modification date. If both are the same, this
        /// method sleeps until it can force the new lock file's modification date to
        /// be later than the target file.
        /// </remarks>
        /// <exception cref="System.Exception">
        /// the thread was interrupted before the last modified date of
        /// the lock file was different from the last modified date of
        /// the target file.
        /// </exception>
        public virtual void WaitForStatChange()
        {
            FileSnapshot o = FileSnapshot.Save(@ref);
            FileSnapshot n = FileSnapshot.Save(lck);

            while (o.Equals(n))
            {
                Sharpen.Thread.Sleep(25);
                lck.SetLastModified(Runtime.CurrentTimeMillis());
                n = FileSnapshot.Save(lck);
            }
        }
        /// <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);
            }
        }
Esempio n. 12
0
        /// <summary>Detect index changes.</summary>
        /// <remarks>Detect index changes.</remarks>
        private void DetectIndexChanges()
        {
            if (IsBare)
            {
                return;
            }
            FilePath indexFile = GetIndexFile();

            if (snapshot == null)
            {
                snapshot = FileSnapshot.Save(indexFile);
            }
            else
            {
                if (snapshot.IsModified(indexFile))
                {
                    NotifyIndexChanged();
                }
            }
        }
Esempio n. 13
0
 public override void NotifyIndexChanged()
 {
     snapshot = FileSnapshot.Save(GetIndexFile());
     FireEvent(new IndexChangedEvent());
 }
        /// <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));
        }