/// <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, "config" ), FileSystem); // // LoadSystemConfig(); LoadUserConfig(); LoadRepoConfig(); repoConfig.AddChangeListener(new _ConfigChangedListener_168(this)); refs = new RefDirectory(this); objectDatabase = new ObjectDirectory(repoConfig, options.GetObjectDirectory(), options .GetAlternateObjectDirectories(), FileSystem); // // // if (objectDatabase.Exists()) { string repositoryFormatVersion = ((FileBasedConfig)GetConfig()).GetString(ConfigConstants .CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION); if (!"0".Equals(repositoryFormatVersion)) { throw new IOException(MessageFormat.Format(JGitText.Get().unknownRepositoryFormat2 , repositoryFormatVersion)); } } }
/// <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 FileObjectDatabase.AlternateHandle OpenAlternate(FilePath objdir) { FilePath parent = objdir.GetParentFile(); if (RepositoryCache.FileKey.IsGitRepository(parent, fs)) { RepositoryCache.FileKey key = RepositoryCache.FileKey.Exact(parent, fs); FileRepository db = (FileRepository)RepositoryCache.Open(key); return(new FileObjectDatabase.AlternateRepository(db)); } NGit.Storage.File.ObjectDirectory db_1 = new NGit.Storage.File.ObjectDirectory(config , objdir, null, fs); return(new FileObjectDatabase.AlternateHandle(db_1)); }
internal LocalCachedPack(ObjectDirectory odb, ICollection <ObjectId> tips, IList <string > packNames) { this.odb = odb; if (tips.Count == 1) { this.tips = Sharpen.Collections.Singleton(tips.Iterator().Next()); } else { this.tips = Sharpen.Collections.UnmodifiableSet(tips); } this.packNames = Sharpen.Collections.ToArray(packNames, new string[packNames.Count ]); }
internal LocalCachedPack(ObjectDirectory odb, ICollection<ObjectId> tips, IList<string > packNames) { this.odb = odb; if (tips.Count == 1) { this.tips = Sharpen.Collections.Singleton(tips.Iterator().Next()); } else { this.tips = Sharpen.Collections.UnmodifiableSet(tips); } this.packNames = Sharpen.Collections.ToArray(packNames, new string[packNames.Count ]); }
/// <summary>The constructor</summary> /// <param name="wrapped">the wrapped database</param> internal CachedObjectDirectory(ObjectDirectory wrapped) { this.wrapped = wrapped; FilePath objects = wrapped.GetDirectory(); string[] fanout = objects.List(); if (fanout == null) { fanout = new string[0]; } foreach (string d in fanout) { if (d.Length != 2) { continue; } string[] entries = new FilePath(objects, d).List(); if (entries == null) { continue; } foreach (string e in entries) { if (e.Length != Constants.OBJECT_ID_STRING_LENGTH - 2) { continue; } try { ObjectId id = ObjectId.FromString(d + e); unpackedObjects.Add(new CachedObjectDirectory.UnpackedObjectId(id)); } catch (ArgumentException) { } } } }
/// <summary>The constructor</summary> /// <param name="wrapped">the wrapped database</param> internal CachedObjectDirectory(ObjectDirectory wrapped) { this.wrapped = wrapped; FilePath objects = wrapped.GetDirectory(); string[] fanout = objects.List(); if (fanout == null) { fanout = new string[0]; } foreach (string d in fanout) { if (d.Length != 2) { continue; } string[] entries = new FilePath(objects, d).List(); if (entries == null) { continue; } foreach (string e in entries) { if (e.Length != Constants.OBJECT_ID_STRING_LENGTH - 2) { continue; } try { ObjectId id = ObjectId.FromString(d + e); unpackedObjects.Add(new CachedObjectDirectory.UnpackedObjectId(id)); } catch (ArgumentException) { } } } }
/// <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) { userConfig = SystemReader.GetInstance().OpenUserConfig(FileSystem); repoConfig = new FileBasedConfig(userConfig, FileSystem.Resolve(Directory, "config" ), FileSystem); // // LoadUserConfig(); LoadRepoConfig(); ((FileBasedConfig)GetConfig()).AddChangeListener(new _ConfigChangedListener_163(this )); refs = new RefDirectory(this); objectDatabase = new ObjectDirectory(repoConfig, options.GetObjectDirectory(), options .GetAlternateObjectDirectories(), FileSystem); // // // if (objectDatabase.Exists()) { string repositoryFormatVersion = ((FileBasedConfig)GetConfig()).GetString(ConfigConstants .CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION); if (!"0".Equals(repositoryFormatVersion)) { throw new IOException(MessageFormat.Format(JGitText.Get().unknownRepositoryFormat2 , repositoryFormatVersion)); } } }
/// <exception cref="System.IO.IOException"></exception> private FileObjectDatabase.AlternateHandle OpenAlternate(FilePath objdir) { FilePath parent = objdir.GetParentFile(); if (RepositoryCache.FileKey.IsGitRepository(parent, fs)) { RepositoryCache.FileKey key = RepositoryCache.FileKey.Exact(parent, fs); FileRepository db = (FileRepository)RepositoryCache.Open(key); return new FileObjectDatabase.AlternateRepository(db); } NGit.Storage.File.ObjectDirectory db_1 = new NGit.Storage.File.ObjectDirectory(config , objdir, null, fs); return new FileObjectDatabase.AlternateHandle(db_1); }
private static IDictionary<string, PackFile> ReuseMap(ObjectDirectory.PackList old ) { IDictionary<string, PackFile> forReuse = new Dictionary<string, PackFile>(); foreach (PackFile p in old.packs) { if (p.Invalid()) { // The pack instance is corrupted, and cannot be safely used // again. Do not include it in our reuse map. // p.Close(); continue; } PackFile prior = forReuse.Put(p.GetPackFile().GetName(), p); if (prior != null) { // This should never occur. It should be impossible for us // to have two pack files with the same name, as all of them // came out of the same directory. If it does, we promised to // close any PackFiles we did not reuse, so close the second, // readers are likely to be actively using the first. // forReuse.Put(prior.GetPackFile().GetName(), prior); p.Close(); } } return forReuse; }
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); }
private ObjectDirectory.PackList ScanPacks(ObjectDirectory.PackList original) { lock (packList) { ObjectDirectory.PackList o; ObjectDirectory.PackList n; do { o = packList.Get(); if (o != original) { // Another thread did the scan for us, while we // were blocked on the monitor above. // return o; } n = ScanPacksImpl(o); if (n == o) { return n; } } while (!packList.CompareAndSet(o, n)); return n; } }
/// <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); }
/// <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()); } }
/// <summary> /// Like "git prune-packed" this method tries to prune all loose objects /// which can be found in packs. /// </summary> /// <remarks> /// Like "git prune-packed" this method tries to prune all loose objects /// which can be found in packs. If certain objects can't be pruned (e.g. /// because the filesystem delete operation fails) this is silently ignored. /// </remarks> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public virtual void PrunePacked() { ObjectDirectory objdb = ((ObjectDirectory)repo.ObjectDatabase); ICollection <PackFile> packs = objdb.GetPacks(); FilePath objects = repo.ObjectsDirectory; string[] fanout = objects.List(); if (fanout != null && fanout.Length > 0) { pm.BeginTask(JGitText.Get().pruneLoosePackedObjects, fanout.Length); try { foreach (string d in fanout) { pm.Update(1); if (d.Length != 2) { continue; } string[] entries = new FilePath(objects, d).List(); if (entries == null) { continue; } foreach (string e in entries) { if (e.Length != Constants.OBJECT_ID_STRING_LENGTH - 2) { continue; } ObjectId id; try { id = ObjectId.FromString(d + e); } catch (ArgumentException) { // ignoring the file that does not represent loose // object continue; } bool found = false; foreach (PackFile p in packs) { if (p.HasObject(id)) { found = true; break; } } if (found) { FileUtils.Delete(objdb.FileFor(id), FileUtils.RETRY | FileUtils.SKIP_MISSING | FileUtils .IGNORE_ERRORS); } } } } finally { pm.EndTask(); } } }