public virtual void TestBreakModify_DontBreakAboveScore() { ObjectId aId = Blob("blah\nblah\nfoo"); ObjectId bId = Blob("blah\nblah\nbar"); DiffEntry m = DiffEntry.Modify(PATH_A); m.oldId = AbbreviatedObjectId.FromObjectId(aId); m.newId = AbbreviatedObjectId.FromObjectId(bId); DiffEntry a = DiffEntry.Add(PATH_B, aId); rd.Add(a); rd.Add(m); rd.SetBreakScore(20); // Should not break the modify IList <DiffEntry> entries = rd.Compute(); NUnit.Framework.Assert.AreEqual(2, entries.Count); NUnit.Framework.Assert.AreSame(m, entries[0]); NUnit.Framework.Assert.AreSame(a, entries[1]); }
public virtual void TestBreakModify_BreakBelowScore() { ObjectId aId = Blob("foo"); ObjectId bId = Blob("bar"); DiffEntry m = DiffEntry.Modify(PATH_A); m.oldId = AbbreviatedObjectId.FromObjectId(aId); m.newId = AbbreviatedObjectId.FromObjectId(bId); DiffEntry a = DiffEntry.Add(PATH_B, aId); rd.Add(a); rd.Add(m); rd.SetBreakScore(20); // Should break the modify IList <DiffEntry> entries = rd.Compute(); NUnit.Framework.Assert.AreEqual(2, entries.Count); AssertAdd(PATH_A, bId, FileMode.REGULAR_FILE, entries[0]); AssertRename(DiffEntry.BreakModify(m)[0], a, 100, entries[1]); }
public void testPrefixCompare_Full() { const string s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a"; AbbreviatedObjectId a = AbbreviatedObjectId.FromString(s1); ObjectId i1 = ObjectId.FromString(s1); Assert.AreEqual(0, a.prefixCompare(i1)); Assert.IsTrue(i1.startsWith(a)); const string s2 = "7b6e8067ec96acef9a4184b43210d583b6d2f99b"; ObjectId i2 = ObjectId.FromString(s2); Assert.IsTrue(a.prefixCompare(i2) < 0); Assert.IsFalse(i2.startsWith(a)); const string s3 = "7b6e8067ec96acef9a4184b43210d583b6d2f999"; ObjectId i3 = ObjectId.FromString(s3); Assert.IsTrue(a.prefixCompare(i3) > 0); Assert.IsFalse(i3.startsWith(a)); }
public virtual void TestAbbreviateOnEmptyRepository() { ObjectId id = Id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba"); NUnit.Framework.Assert.AreEqual(id.Abbreviate(2), reader.Abbreviate(id, 2)); NUnit.Framework.Assert.AreEqual(id.Abbreviate(7), reader.Abbreviate(id, 7)); NUnit.Framework.Assert.AreEqual(id.Abbreviate(8), reader.Abbreviate(id, 8)); NUnit.Framework.Assert.AreEqual(id.Abbreviate(10), reader.Abbreviate(id, 10)); NUnit.Framework.Assert.AreEqual(id.Abbreviate(16), reader.Abbreviate(id, 16)); NUnit.Framework.Assert.AreEqual(AbbreviatedObjectId.FromObjectId(id), reader.Abbreviate (id, Constants.OBJECT_ID_STRING_LENGTH)); // ICollection <ObjectId> matches; matches = reader.Resolve(reader.Abbreviate(id, 8)); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(0, matches.Count); matches = reader.Resolve(AbbreviatedObjectId.FromObjectId(id)); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(1, matches.Count); NUnit.Framework.Assert.AreEqual(id, matches.Iterator().Next()); }
/// <exception cref="System.IO.IOException"></exception> private NonNoteEntry MergeNonNotes(NonNoteEntry baseList, NonNoteEntry oursList, NonNoteEntry theirsList) { if (baseList == null && oursList == null && theirsList == null) { return(null); } ObjectId baseId = Write(baseList); ObjectId oursId = Write(oursList); ObjectId theirsId = Write(theirsList); inserter.Flush(); ObjectId resultTreeId; if (nonNotesMergeStrategy is ThreeWayMergeStrategy) { ThreeWayMerger m = ((ThreeWayMerger)((ThreeWayMergeStrategy)nonNotesMergeStrategy ).NewMerger(db, true)); m.SetBase(baseId); if (!m.Merge(oursId, theirsId)) { throw new NotesMergeConflictException(baseList, oursList, theirsList); } resultTreeId = m.GetResultTreeId(); } else { Merger m = nonNotesMergeStrategy.NewMerger(db, true); if (!m.Merge(new AnyObjectId[] { oursId, theirsId })) { throw new NotesMergeConflictException(baseList, oursList, theirsList); } resultTreeId = m.GetResultTreeId(); } AbbreviatedObjectId none = AbbreviatedObjectId.FromString(string.Empty); return(NoteParser.Parse(none, resultTreeId, reader).nonNotes); }
internal override void ParseIndexLine(int ptr, int eol) { // "index $asha1,$bsha1..$csha1" // IList <AbbreviatedObjectId> ids = new AList <AbbreviatedObjectId>(); while (ptr < eol) { int comma = RawParseUtils.NextLF(buf, ptr, ','); if (eol <= comma) { break; } ids.AddItem(AbbreviatedObjectId.FromString(buf, ptr, comma - 1)); ptr = comma; } oldIds = new AbbreviatedObjectId[ids.Count + 1]; Sharpen.Collections.ToArray(ids, oldIds); int dot2 = RawParseUtils.NextLF(buf, ptr, '.'); oldIds[ids.Count] = AbbreviatedObjectId.FromString(buf, ptr, dot2 - 1); newId = AbbreviatedObjectId.FromString(buf, dot2 + 1, eol - 1); oldModes = new FileMode[oldIds.Length]; }
public void parseIndexLine(int ptr, int end) { // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1 // can be unique abbreviations // int dot2 = RawParseUtils.nextLF(buf, ptr, (byte)'.'); int mode = RawParseUtils.nextLF(buf, dot2, (byte)' '); oldId = AbbreviatedObjectId.FromString(buf, ptr, dot2 - 1); newId = AbbreviatedObjectId.FromString(buf, dot2 + 1, mode - 1); if (mode < end) newMode = oldMode = parseFileMode(mode, end); }
protected virtual void ParseIndexLine(int ptr, int end) { // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1 // can be unique abbreviations // int dot2 = RawParseUtils.nextLF(Buffer, ptr, (byte)'.'); int mode = RawParseUtils.nextLF(Buffer, dot2, (byte)' '); oldId = AbbreviatedObjectId.FromString(Buffer, ptr, dot2 - 1); NewId = AbbreviatedObjectId.FromString(Buffer, dot2 + 1, mode - 1); if (mode < end) { _newMode = _oldMode = ParseFileMode(mode, end); } }
/// <exception cref="System.IO.IOException"></exception> private IList <RebaseCommand.Step> LoadSteps() { byte[] buf = IOUtil.ReadFully(new FilePath(rebaseDir, GIT_REBASE_TODO)); int ptr = 0; int tokenBegin = 0; AList <RebaseCommand.Step> r = new AList <RebaseCommand.Step>(); while (ptr < buf.Length) { tokenBegin = ptr; ptr = RawParseUtils.NextLF(buf, ptr); int nextSpace = 0; int tokenCount = 0; RebaseCommand.Step current = null; while (tokenCount < 3 && nextSpace < ptr) { switch (tokenCount) { case 0: { nextSpace = RawParseUtils.Next(buf, tokenBegin, ' '); string actionToken = Sharpen.Runtime.GetStringForBytes(buf, tokenBegin, nextSpace - tokenBegin - 1); tokenBegin = nextSpace; if (actionToken[0] == '#') { tokenCount = 3; break; } RebaseCommand.Action action = RebaseCommand.Action.Parse(actionToken); if (action != null) { current = new RebaseCommand.Step(RebaseCommand.Action.Parse(actionToken)); } break; } case 1: { if (current == null) { break; } nextSpace = RawParseUtils.Next(buf, tokenBegin, ' '); string commitToken = Sharpen.Runtime.GetStringForBytes(buf, tokenBegin, nextSpace - tokenBegin - 1); tokenBegin = nextSpace; current.commit = AbbreviatedObjectId.FromString(commitToken); break; } case 2: { if (current == null) { break; } nextSpace = ptr; int length = ptr - tokenBegin; current.shortMessage = new byte[length]; System.Array.Copy(buf, tokenBegin, current.shortMessage, 0, length); r.AddItem(current); break; } } tokenCount++; } } return(r); }
public virtual void TestAbbreviateIsActuallyUnique() { // This test is far more difficult. We have to manually craft // an input that contains collisions at a particular prefix, // but this is computationally difficult. Instead we force an // index file to have what we want. // ObjectId id = Id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba"); byte[] idBuf = ToByteArray(id); IList <PackedObjectInfo> objects = new AList <PackedObjectInfo>(); for (int i = 0; i < 256; i++) { idBuf[9] = unchecked ((byte)i); objects.AddItem(new PackedObjectInfo(ObjectId.FromRaw(idBuf))); } string packName = "pack-" + id.Name; FilePath packDir = new FilePath(((ObjectDirectory)db.ObjectDatabase).GetDirectory (), "pack"); FilePath idxFile = new FilePath(packDir, packName + ".idx"); FilePath packFile = new FilePath(packDir, packName + ".pack"); FileUtils.Mkdir(packDir, true); OutputStream dst = new BufferedOutputStream(new FileOutputStream(idxFile)); try { PackIndexWriter writer = new PackIndexWriterV2(dst); writer.Write(objects, new byte[Constants.OBJECT_ID_LENGTH]); } finally { dst.Close(); } new FileOutputStream(packFile).Close(); NUnit.Framework.Assert.AreEqual(id.Abbreviate(20), reader.Abbreviate(id, 2)); AbbreviatedObjectId abbrev8 = id.Abbreviate(8); ICollection <ObjectId> matches = reader.Resolve(abbrev8); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count); foreach (PackedObjectInfo info in objects) { NUnit.Framework.Assert.IsTrue(matches.Contains(info), "contains " + info.Name); } try { db.Resolve(abbrev8.Name); NUnit.Framework.Assert.Fail("did not throw AmbiguousObjectException"); } catch (AmbiguousObjectException err) { NUnit.Framework.Assert.AreEqual(abbrev8, err.GetAbbreviatedObjectId()); matches = err.GetCandidates(); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count); foreach (PackedObjectInfo info_1 in objects) { NUnit.Framework.Assert.IsTrue(matches.Contains(info_1), "contains " + info_1.Name ); } } NUnit.Framework.Assert.AreEqual(id, db.Resolve(id.Abbreviate(20).Name)); }
/// <exception cref="System.IO.IOException"></exception> internal override void Resolve(ICollection <ObjectId> matches, AbbreviatedObjectId id) { // Go through the packs once. If we didn't find any resolutions // scan for new packs and check once more. // int oldSize = matches.Count; ObjectDirectory.PackList pList = packList.Get(); for (; ;) { foreach (PackFile p in pList.packs) { try { p.Resolve(matches, id, RESOLVE_ABBREV_LIMIT); } catch (IOException) { // Assume the pack is corrupted. // RemovePack(p); } if (matches.Count > RESOLVE_ABBREV_LIMIT) { return; } } if (matches.Count == oldSize) { ObjectDirectory.PackList nList = ScanPacks(pList); if (nList == pList || nList.packs.Length == 0) { break; } pList = nList; continue; } break; } string fanOut = Sharpen.Runtime.Substring(id.Name, 0, 2); string[] entries = new FilePath(GetDirectory(), fanOut).List(); if (entries != null) { foreach (string e in entries) { if (e.Length != Constants.OBJECT_ID_STRING_LENGTH - 2) { continue; } try { ObjectId entId = ObjectId.FromString(fanOut + e); if (id.PrefixCompare(entId) == 0) { matches.AddItem(entId); } } catch (ArgumentException) { continue; } if (matches.Count > RESOLVE_ABBREV_LIMIT) { return; } } } foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates()) { alt.db.Resolve(matches, id); if (matches.Count > RESOLVE_ABBREV_LIMIT) { return; } } }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> private void Load(ObjectId rootTree) { AbbreviatedObjectId none = AbbreviatedObjectId.FromString(string.Empty); root = NoteParser.Parse(none, rootTree, reader); }
public OldImage(FileHeader fh, AbbreviatedObjectId id) { _fh = fh; _id = id; }
/// <summary> /// Parse a tree object into a /// <see cref="NoteBucket">NoteBucket</see> /// instance. /// The type of note tree is automatically detected by examining the items /// within the tree, and allocating the proper storage type based on the /// first note-like entry encountered. Since the method parses by guessing /// the type on the first element, malformed note trees can be read as the /// wrong type of tree. /// This method is not recursive, it parses the one tree given to it and /// returns the bucket. If there are subtrees for note storage, they are /// setup as lazy pointers that will be resolved at a later time. /// </summary> /// <param name="prefix"> /// common hex digits that all notes within this tree share. The /// root tree has /// <code>prefix.length() == 0</code> /// , the first-level /// subtrees should be /// <code>prefix.length()==2</code> /// , etc. /// </param> /// <param name="treeId">the tree to read from the repository.</param> /// <param name="reader">reader to access the tree object.</param> /// <returns>bucket to holding the notes of the specified tree.</returns> /// <exception cref="System.IO.IOException"> /// <code>treeId</code> /// cannot be accessed. /// </exception> internal static InMemoryNoteBucket Parse(AbbreviatedObjectId prefix, ObjectId treeId , ObjectReader reader) { return(new NGit.Notes.NoteParser(prefix, reader, treeId).Parse()); }
/// <exception cref="System.IO.IOException"></exception> internal abstract void Resolve(ICollection <ObjectId> matches, AbbreviatedObjectId id, int matchLimit);
/// <exception cref="System.IO.IOException"></exception> private byte[] Open(DiffEntry.Side side, DiffEntry entry) { if (entry.GetMode(side) == FileMode.MISSING) { return(EMPTY); } if (entry.GetMode(side).GetObjectType() != Constants.OBJ_BLOB) { return(EMPTY); } if (IsBinary()) { return(BINARY); } AbbreviatedObjectId id = entry.GetId(side); if (!id.IsComplete) { ICollection <ObjectId> ids = reader.Resolve(id); if (ids.Count == 1) { id = AbbreviatedObjectId.FromObjectId(ids.Iterator().Next()); switch (side) { case DiffEntry.Side.OLD: { entry.oldId = id; break; } case DiffEntry.Side.NEW: { entry.newId = id; break; } } } else { if (ids.Count == 0) { throw new MissingObjectException(id, Constants.OBJ_BLOB); } else { throw new AmbiguousObjectException(id, ids); } } } try { ObjectLoader ldr = source.Open(side, entry); return(ldr.GetBytes(binaryFileThreshold)); } catch (LargeObjectException.ExceedsLimit) { return(BINARY); } catch (LargeObjectException.ExceedsByteArrayLimit) { return(BINARY); } catch (LargeObjectException.OutOfMemory) { return(BINARY); } catch (LargeObjectException tooBig) { tooBig.SetObjectId(id.ToObjectId()); throw; } }
/// <exception cref="System.IO.IOException"></exception> internal virtual void Resolve(ICollection <ObjectId> matches, AbbreviatedObjectId id, int matchLimit) { Idx().Resolve(matches, id, matchLimit); }
/// <summary>Construct a MissingObjectException for the specified object id.</summary> /// <remarks> /// Construct a MissingObjectException for the specified object id. Expected /// type is reported to simplify tracking down the problem. /// </remarks> /// <param name="id">SHA-1</param> /// <param name="type">object type</param> public MissingObjectException(AbbreviatedObjectId id, int type) : base(MessageFormat .Format(JGitText.Get().missingObject, Constants.TypeString(type), id.Name)) { missing = null; }
/// <summary> /// Convert the TreeWalk into DiffEntry headers, depending on /// <code>includeTrees</code> /// it will add tree objects into result or not. /// </summary> /// <param name="walk"> /// the TreeWalk to walk through. Must have exactly two trees and /// when /// <code>includeTrees</code> /// parameter is /// <code>true</code> /// it can't /// be recursive. /// </param> /// <param name="includeTrees">include tree object's.</param> /// <returns>headers describing the changed files.</returns> /// <exception cref="System.IO.IOException">the repository cannot be accessed.</exception> /// <exception cref="System.ArgumentException"> /// when /// <code>includeTrees</code> /// is true and given TreeWalk is /// recursive. Or when given TreeWalk doesn't have exactly two /// trees /// </exception> public static IList <NGit.Diff.DiffEntry> Scan(TreeWalk walk, bool includeTrees) { if (walk.TreeCount != 2) { throw new ArgumentException(JGitText.Get().treeWalkMustHaveExactlyTwoTrees); } if (includeTrees && walk.Recursive) { throw new ArgumentException(JGitText.Get().cannotBeRecursiveWhenTreesAreIncluded); } IList <NGit.Diff.DiffEntry> r = new AList <NGit.Diff.DiffEntry>(); MutableObjectId idBuf = new MutableObjectId(); while (walk.Next()) { NGit.Diff.DiffEntry entry = new NGit.Diff.DiffEntry(); walk.GetObjectId(idBuf, 0); entry.oldId = AbbreviatedObjectId.FromObjectId(idBuf); walk.GetObjectId(idBuf, 1); entry.newId = AbbreviatedObjectId.FromObjectId(idBuf); entry.oldMode = walk.GetFileMode(0); entry.newMode = walk.GetFileMode(1); entry.newPath = entry.oldPath = walk.PathString; if (entry.oldMode == FileMode.MISSING) { entry.oldPath = NGit.Diff.DiffEntry.DEV_NULL; entry.changeType = DiffEntry.ChangeType.ADD; r.AddItem(entry); } else { if (entry.newMode == FileMode.MISSING) { entry.newPath = NGit.Diff.DiffEntry.DEV_NULL; entry.changeType = DiffEntry.ChangeType.DELETE; r.AddItem(entry); } else { if (!entry.oldId.Equals(entry.newId)) { entry.changeType = DiffEntry.ChangeType.MODIFY; if (RenameDetector.SameType(entry.oldMode, entry.newMode)) { r.AddItem(entry); } else { Sharpen.Collections.AddAll(r, BreakModify(entry)); } } else { if (entry.oldMode != entry.newMode) { entry.changeType = DiffEntry.ChangeType.MODIFY; r.AddItem(entry); } } } } if (includeTrees && walk.IsSubtree) { walk.EnterSubtree(); } } return(r); }