public override void Load(GitObjectReader reader) { byte[] shaContents = reader.ReadBytes(20); BaseSHA = Sha.Decode(shaContents); Delta = Zlib.Decompress(reader.ReadToEnd()); }
internal Index(Repository repo) { Repo = repo; using (GitObjectReader stream = new GitObjectReader(File.OpenRead(Path.Combine(Repo.GitDir.FullName, "index")))) { string header = stream.ReadBytes(4).GetString(); if (header != HEADER) { throw new ParseException("Could not parse Index file. Expected HEADER: '{0}', got: '{1}'".FormatWith(HEADER, header)); } Version = stream.ReadBytes(4).Sum(b => (int)b); if (!VERSIONS.Contains(Version)) { throw new ParseException("Unknown version number {0}. Needs to be one of: {1}".FormatWith(Version, String.Join(",", VERSIONS.Select(i => i.ToString()).ToArray()))); } NumberOfEntries = stream.ReadBytes(4).Sum(b => (int)b); Entries = new IndexEntryCollection(NumberOfEntries); for (int i = 0; i < NumberOfEntries; i++) { Entries.Add(new IndexEntry(stream)); } string indexSHA = stream.ReadToEnd().ToSHAString(); } }
/// <summary> /// Returns a MemoryStream with the 'inflated' contents of the object in the GitObjectReader starting at the current stream position and with a decompressed size of <param name="destLength">destLength</param> /// </summary> /// <param name="input">An open GitObjectReader object pointing to a file system object in the Git Repository</param> /// <param name="destLength">Inflate the contents in the stream until the decompressed array reaches this size</param> /// <returns>Inflated contents in a MemoryStream</returns> public static MemoryStream Decompress(GitObjectReader input, long destLength) { //MemoryStream output = new MemoryStream(); //Inflater inflater = new Inflater(); //byte[] buffer = new byte[destLength]; //while (output.Length < destLength) //{ // if (inflater.IsNeedingInput) // inflater.SetInput(input.ReadBytes(bufferLength)); // int outLength = inflater.Inflate(buffer); // if (outLength > 0) // output.Write(buffer, 0, outLength); // else // { // if (inflater.IsFinished) // break; // } //} //// rewind stream to end of content (buffer overhead) //input.Position -= inflater.RemainingInput; //return output; throw new NotImplementedException(); }
internal IndexEntry(GitObjectReader source) { long startPosition = source.Position; Created = GetFileTime(source); Modified = GetFileTime(source); // TODO: really parse all the var stuff var dev = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); var ino = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); var mode = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); var uid = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); var gid = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); Size = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); SHA = source.ReadBytes(20).ToSHAString(); var flags = source.ReadBytes(2); var assumeValid = flags[0].GetBits(0, 1); var updateNeeded = flags[0].GetBits(1, 1); Stage = (IndexStage)flags[0].GetBits(2, 2); Path = source.ReadToNull(); // Read bytes until the length of this entry can be divided by 8 and the name (Path) is still char(0) terminated // http://kerneltrap.org/index.php?q=mailarchive/git/2008/2/11/810634 long endPosition = source.Position; long length = (endPosition - startPosition) ; if( length %8 != 0) source.ReadBytes(8 - ((int)length % 8)); }
public IStorableObject ToGitObject(Repository repo, string sha) { using (GitObjectReader objectReader = new GitObjectReader(Content)) { IStorableObject obj; switch (Type) { case ObjectType.Commit: obj = new Commit(repo, sha); break; case ObjectType.Tree: obj = new Tree(repo, sha); break; case ObjectType.Blob: obj = new Blob(repo, sha); break; case ObjectType.Tag: obj = new Tag(repo, sha); break; default: throw new NotImplementedException(); } obj.Deserialize(objectReader); return obj; } }
internal IndexEntry(GitObjectReader source) { // TODO: really parse all the var stuff Created = new IndexTime(source); Modified = new IndexTime(source); //var time = source.ReadBytes(16); var dev = source.ReadBytes(4); var ino = source.ReadBytes(4); var mode = source.ReadBytes(4); var uid = source.ReadBytes(4); var gid = source.ReadBytes(4); Size = source.ReadBytes(4).ToLong(); SHA = source.ReadBytes(20).ToSHAString(); var flags = source.ReadBytes(2); var assumeValid = flags[0].GetBits(0, 1); var updateNeeded = flags[0].GetBits(1, 1); Stage = (IndexStage)flags[0].GetBits(2, 2); Path = source.ReadToNull().GetString(); string rest = source.ReadToNextNonNull().GetString(); }
/// <summary> /// Loads the tree from the GitObjectReader. The child objects themselves will be lazy loaded /// </summary> /// <param name="input">A reader with inflated tree contents</param> public override void Deserialize(GitObjectReader input) { //string sha = Sha.Compute(input); //if (SHA != sha) // throw new ShaMismatchException(SHA, sha); _childrenRaw = input.ReadToEnd(); }
internal static T LoadObjectFromContent <T>(Repository repo, GitObjectReader input, string sha, long length) where T : IStorableObject { ConstructorInfo constr = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Repository), typeof(String) }, null); IStorableObject result = (IStorableObject)constr.Invoke(new object[] { repo, sha }); // Let the respective object type load itself from the object content result.Deserialize(input); return((T)result); }
internal static string Compute(GitObjectReader input) { long oldPosition = input.BaseStream.Position; input.Rewind(); string hash = Compute(input.ReadToEnd()); input.BaseStream.Position = oldPosition; return(hash); }
public override void Load(GitObjectReader reader) { byte buffer = reader.ReadByte(); long baseOffset = buffer & 0x7f; // Read byte while 8th bit is 1. while ((buffer & 0x80) != 0) { buffer = reader.ReadByte(); baseOffset += 1; baseOffset <<= 7; baseOffset |= ((long)buffer & 0x7f); } Delta = Zlib.Decompress(reader.ReadToEnd()); BackwardsBaseOffset = (int)baseOffset; }
/// <summary> /// Loads the tag from the GitObjectReader /// </summary> /// <param name="input">A reader with inflated tag contents</param> public void Deserialize(GitObjectReader input) { string sha; if (Utility.IsValidSHA(input.GetString(20), out sha)) { // Tag contains a regular SHA so we can assume it's an IStorableObject Object = Repo.Storage.GetObject(sha); return; } else { input.Rewind(); // Skip header input.ReadToNull(); // Skip object keyword input.ReadWord(); TaggedObjectSHA = input.ReadLine().GetString().Trim(); if (!Utility.IsValidSHA(TaggedObjectSHA)) { throw new ParseException("Invalid sha from tag content"); } // Load object; a ParseException will be thrown for unknown types Object = Repo.Storage.GetObject(TaggedObjectSHA); // Skip type and tag input.ReadLine(); input.ReadLine(); // Tagger input.ReadWord(); string taggerLine = input.ReadLine().GetString(); TagDate = Utility.StripDate(taggerLine, out taggerLine); Tagger = Contributer.Parse(taggerLine); //Skip extra '\n' and read message input.ReadBytes(1); Message = input.ReadToEnd().GetString().TrimEnd(); } }
public IStorableObject ToGitObject(Repository repo, string sha) { using (GitObjectReader objectReader = new GitObjectReader(Content)) { switch (Type) { case ObjectType.Commit: return(ObjectStorage.LoadObjectFromContent <Commit>(repo, objectReader, sha, Size)); case ObjectType.Tree: return(ObjectStorage.LoadObjectFromContent <Tree>(repo, objectReader, sha, Size)); case ObjectType.Blob: return(ObjectStorage.LoadObjectFromContent <Blob>(repo, objectReader, sha, Size)); case ObjectType.Tag: return(ObjectStorage.LoadObjectFromContent <Tag>(repo, objectReader, sha, Size)); default: throw new NotImplementedException(); } } }
/// <summary> /// Gets called be the Children getter for lazy loading /// </summary> private void LoadChildren() { _children = new TreeNodeCollection(); try { using (GitObjectReader stream = new GitObjectReader(_childrenRaw)) { while (!stream.IsEndOfStream) { string path, sha; // TODO: Make this a little bit less sucky string m = stream.ReadWord().GetString(); FileMode mode = FileMode.FromBits(int.Parse(m)); path = stream.ReadToNull().GetString(); sha = Sha.Decode(stream.ReadBytes(20)); // TODO: Add support for submodules if (mode.ObjectType != ObjectType.Tree && mode.ObjectType != ObjectType.Blob) continue; TreeNode child = Repo.Storage.GetObject<TreeNode>(sha); child.Path = path; child.Mode = mode; child.Parent = this; _children.Add(child); } } } catch (Exception) { // Reset _children field, otherwise the object would be in an invalid state _children = null; throw; } }
internal Deltified(long size, ObjectType type, GitObjectReader reader) : base(size, type) { Load(reader); }
/// <summary> /// Loads the tag from the GitObjectReader /// </summary> /// <param name="input">A reader with inflated tag contents</param> public void Deserialize(GitObjectReader input) { string sha; if (Utility.IsValidSHA(input.GetString(20), out sha)) { // Tag contains a regular SHA so we can assume it's an IStorableObject Object = Repo.Storage.GetObject(sha); return; } else { input.Rewind(); // Skip object keyword input.ReadWord(); TaggedObjectSHA = input.ReadLine().Trim(); if (!Utility.IsValidSHA(TaggedObjectSHA)) throw new ParseException("Invalid sha from tag content"); // Load object; a ParseException will be thrown for unknown types Object = Repo.Storage.GetObject(TaggedObjectSHA); // Skip type and tag input.ReadLine(); input.ReadLine(); // Tagger input.ReadWord(); string taggerLine = input.ReadLine(); TagDate = Utility.StripDate(taggerLine, out taggerLine); Tagger = Contributer.Parse(taggerLine); //Skip extra '\n' and read message input.ReadBytes(1); Message = input.ReadToEnd().GetString().TrimEnd(); } }
private DateTime GetFileTime(GitObjectReader source) { int seconds = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); int nanoseconds = System.Net.IPAddress.HostToNetworkOrder(source.ReadBytes(4).ToInt()); return Utility.UnixEPOCH.AddSeconds(seconds).AddMilliseconds(nanoseconds / 1000); }
public abstract void Deserialize(GitObjectReader input);
/// <summary> /// Loads the commit from the GitObjectReader /// </summary> /// <param name="input">A reader with inflated commit contents</param> public override void Deserialize(GitObjectReader input) { // Get the contents from the stream to avoid ReadByte() every time string contents = input.ReadToEnd().GetString(); int length = contents.Length; int index = 0; _treeSha = contents.Substring(index + 5, 40); _parentShas = new List<string>(); index += 46; // "tree " + sha + \n while (index + 48 < contents.Length && contents.Substring(index, 7).Equals("parent ")) { // got parent _parentShas.Add(contents.Substring(index + 7, 40)); index += 48; } // Check if we can get an author from the next characters if (index + 7 < length && contents.Substring(index, 7) == "author ") { index += 7; int authorLength = 0; while (contents[index] != '\n') { authorLength++; index++; } String authorLine = contents.Substring(index - authorLength, authorLength); AuthoredDate = Utility.StripDate(authorLine, out authorLine); Author = Contributer.Parse(authorLine); // Skip the \n index++; } // Read committer from the stream if (index + 9 < length && contents.Substring(index, 10) == "committer ") { index += 10; int committerLength = 0; while (contents[index] != '\n') { committerLength++; index++; } String committerLine = contents.Substring(index - committerLength, committerLength); CommittedDate = Utility.StripDate(committerLine, out committerLine); Committer = Contributer.Parse(committerLine); // Skip the \n index++; } index++; // extra \n if (index < contents.Length) Message = contents.Substring(index); else Message = String.Empty; }
public override void Deserialize(GitObjectReader stream) { Content = stream.ReadToEnd(); }
/// <summary> /// Gets called be the Children getter for lazy loading /// </summary> private void LoadChildren() { _children = new TreeNodeCollection(); try { using (GitObjectReader stream = new GitObjectReader(_childrenRaw)) { while (!stream.IsEndOfStream) { string mode, path, sha; mode = stream.ReadWord().GetString(); path = stream.ReadToNull().GetString(); sha = Sha.Decode(stream.ReadBytes(20)); TreeNode child = Repo.Storage.GetObject<TreeNode>(sha); child.Path = path; child.Mode = FileMode.FromBits(int.Parse(mode)); child.Parent = this; _children.Add(child); } } } catch (Exception) { // Reset _children field, otherwise the object would be in an invalid state _children = null; throw; } }
internal REFDelta(long size, ObjectType type, GitObjectReader reader) : base(size, type, reader) { }
internal IndexTime(GitObjectReader input) { Seconds = input.ReadBytes(4).ToInt(); NanoSeconds = input.ReadBytes(4).ToInt(); }
/// <summary> /// Loads the commit from the GitObjectReader /// </summary> /// <param name="input">A reader with inflated commit contents</param> public override void Deserialize(GitObjectReader input) { //Skip 'tree' at beginning of line and read tree sha input.ReadWord(); _treeSha = input.ReadLine().GetString(); // Check for 'parent' at beginning of line _parentShas = new List<string>(); string parentOrAuthor = input.ReadWord().GetString(); // TODO: Make recursive while (parentOrAuthor == "parent") { _parentShas.Add(input.GetString(40)); input.Position++; // Skip 'author' parentOrAuthor = input.ReadWord().GetString(); } // Author string authorLine = input.ReadLine().GetString(); AuthoredDate = Utility.StripDate(authorLine, out authorLine); Author = Contributer.Parse(authorLine); // Committer input.ReadWord(); string committerLine = input.ReadLine().GetString(); CommittedDate = Utility.StripDate(committerLine, out committerLine); Committer = Contributer.Parse(committerLine); //Skip extra '\n' input.Position++; Message = input.ReadToEnd().GetString().TrimEnd(); }
/// <summary> /// Find object in database and return it as an IStorableObject. Use the generic overload if you know the type up front. Throws ObjectNotFoundException if object was not found in database. /// </summary> /// <param name="sha">SHA object identifier. Throws ArgumentException if it is null or not a valid sha</param> /// <returns>GitObject in database</returns> public IStorableObject GetObject(string sha) { if (!Utility.SHAExpression.IsMatch(sha)) { throw new ArgumentException("Need a valid sha", "sha"); } string looseObjectPath = Path.Combine(ObjectsDir, Path.Combine(sha.Substring(0, 2), sha.Substring(2))); // First check if object is stored in loose format if (File.Exists(looseObjectPath)) { // Object is stored loose. Inflate and load it from content using (GitObjectReader reader = new GitObjectReader(Zlib.Decompress(looseObjectPath))) { long size; ObjectType type; size = reader.ReadObjectHeader(out type); switch (type) { case ObjectType.Commit: return(ObjectStorage.LoadObjectFromContent <Commit>(Repo, reader, sha, size)); case ObjectType.Tree: return(ObjectStorage.LoadObjectFromContent <Tree>(Repo, reader, sha, size)); case ObjectType.Blob: return(ObjectStorage.LoadObjectFromContent <Blob>(Repo, reader, sha, size)); case ObjectType.Tag: return(ObjectStorage.LoadObjectFromContent <Tag>(Repo, reader, sha, size)); default: throw new NotImplementedException(); } } } else { IStorableObject result = null; foreach (Pack pack in Packs) { try { result = pack.GetObject(sha); } catch (ObjectNotFoundException) { } } if (result != null) { return(result); } } // Object was not found throw new ObjectNotFoundException(sha); }
public abstract void Load(GitObjectReader reader);