/// <exception cref="System.Exception"></exception> private DirCacheEntry MakeEntry(string path, FileMode mode) { DirCacheEntry ent = new DirCacheEntry(path); ent.FileMode = mode; ent.SetObjectId(new ObjectInserter.Formatter().IdFor(Constants.OBJ_BLOB, Constants .Encode(path))); return ent; }
public FakeTreeIterator(AbstractTreeIteratorTest _enclosing, string pathName, FileMode fileMode) : base(AbstractTreeIteratorTest.Prefix(pathName), new Config().Get(WorkingTreeOptions .KEY)) { this._enclosing = _enclosing; this.mode = fileMode.GetBits(); int s = pathName.LastIndexOf('/'); byte[] name = Constants.Encode(Sharpen.Runtime.Substring(pathName, s + 1)); this.EnsurePathCapacity(this.pathOffset + name.Length, this.pathOffset); System.Array.Copy(name, 0, this.path, this.pathOffset, name.Length); this.pathLen = this.pathOffset + name.Length; }
private static bool IsFile(FileMode mode) { return (mode.GetBits() & FileMode.TYPE_MASK) == FileMode.TYPE_FILE; }
protected internal virtual DirCacheEntry CreateEntry(string path, FileMode mode, int stage, string content) { DirCacheEntry entry = new DirCacheEntry(path, stage); entry.FileMode = mode; entry.SetObjectId(new ObjectInserter.Formatter().IdFor(Constants.OBJ_BLOB, Constants .Encode(content))); return entry; }
public virtual void TestSingleSubtree_NoRecursion() { DirCache dc = DirCache.NewInCore(); string[] paths = new string[] { "a.", "a/b", "a/c", "a/d", "a0b" }; DirCacheEntry[] ents = new DirCacheEntry[paths.Length]; for (int i = 0; i < paths.Length; i++) { ents[i] = new DirCacheEntry(paths[i]); ents[i].FileMode = FileMode.REGULAR_FILE; } DirCacheBuilder b = dc.Builder(); for (int i_1 = 0; i_1 < ents.Length; i_1++) { b.Add(ents[i_1]); } b.Finish(); string[] expPaths = new string[] { "a.", "a", "a0b" }; FileMode[] expModes = new FileMode[] { FileMode.REGULAR_FILE, FileMode.TREE, FileMode .REGULAR_FILE }; int[] expPos = new int[] { 0, -1, 4 }; DirCacheIterator i_2 = new DirCacheIterator(dc); TreeWalk tw = new TreeWalk(db); tw.AddTree(i_2); tw.Recursive = false; int pathIdx = 0; while (tw.Next()) { NUnit.Framework.Assert.AreSame(i_2, tw.GetTree<DirCacheIterator>(0)); NUnit.Framework.Assert.AreEqual(expModes[pathIdx].GetBits(), tw.GetRawMode(0)); NUnit.Framework.Assert.AreSame(expModes[pathIdx], tw.GetFileMode(0)); NUnit.Framework.Assert.AreEqual(expPaths[pathIdx], tw.PathString); if (expPos[pathIdx] >= 0) { NUnit.Framework.Assert.AreEqual(expPos[pathIdx], i_2.ptr); NUnit.Framework.Assert.AreSame(ents[expPos[pathIdx]], i_2.GetDirCacheEntry()); } else { NUnit.Framework.Assert.AreSame(FileMode.TREE, tw.GetFileMode(0)); } pathIdx++; } NUnit.Framework.Assert.AreEqual(expPaths.Length, pathIdx); }
internal FileEntry(FilePath f, FS fs) { file = f; if (f.IsDirectory()) { if (new FilePath(f, Constants.DOT_GIT).Exists()) { mode = FileMode.GITLINK; } else { mode = FileMode.TREE; } } else { if (fs.CanExecute(file)) { mode = FileMode.EXECUTABLE_FILE; } else { mode = FileMode.REGULAR_FILE; } } }
internal virtual int PathCompare(byte[] bBuf, int bPos, int bLen, FileMode bMode) { return PathCompare(name, 0, name.Length, mode, bBuf, bPos, bLen, bMode); }
private static int LastPathChar(FileMode mode) { return FileMode.TREE.Equals(mode.GetBits()) ? '/' : '\0'; }
/// <summary> /// Run the diff operation. Until this is called, all lists will be empty /// </summary> /// <returns>true if anything is different between index, tree, and workdir</returns> private void UpdateDirectory(IEnumerable <string> paths, bool recursive) { RevWalk rw = new RevWalk(Repository); ObjectId id = Repository.Resolve(Constants.HEAD); var commit = id != null?rw.ParseCommit(id) : null; TreeWalk treeWalk = new TreeWalk(Repository); treeWalk.Reset(); treeWalk.Recursive = false; if (commit != null) { treeWalk.AddTree(commit.Tree); } else { treeWalk.AddTree(new EmptyTreeIterator()); } DirCache dc = Repository.ReadDirCache(); treeWalk.AddTree(new DirCacheIterator(dc)); FileTreeIterator workTree = new FileTreeIterator(Repository.WorkTree, Repository.FileSystem, WorkingTreeOptions.KEY.Parse(Repository.GetConfig())); treeWalk.AddTree(workTree); List <TreeFilter> filters = new List <TreeFilter> (); filters.Add(new SkipWorkTreeFilter(1)); var pathFilters = paths.Where(p => p != ".").Select(p => PathFilter.Create(p)).ToArray(); if (pathFilters.Length > 1) { filters.Add(OrTreeFilter.Create(pathFilters)); // Use an OR to join all path filters } else if (pathFilters.Length == 1) { filters.Add(pathFilters[0]); } if (filters.Count > 1) { treeWalk.Filter = AndTreeFilter.Create(filters); } else { treeWalk.Filter = filters[0]; } while (treeWalk.Next()) { AbstractTreeIterator treeIterator = treeWalk.GetTree <AbstractTreeIterator>(0); DirCacheIterator dirCacheIterator = treeWalk.GetTree <DirCacheIterator>(1); WorkingTreeIterator workingTreeIterator = treeWalk.GetTree <WorkingTreeIterator>(2); NGit.FileMode fileModeTree = treeWalk.GetFileMode(0); if (treeWalk.IsSubtree) { treeWalk.EnterSubtree(); continue; } int stage = dirCacheIterator != null?dirCacheIterator.GetDirCacheEntry().Stage : 0; if (stage > 1) { continue; } else if (stage == 1) { MergeConflict.Add(dirCacheIterator.EntryPathString); changesExist = true; continue; } if (treeIterator != null) { if (dirCacheIterator != null) { if (!treeIterator.EntryObjectId.Equals(dirCacheIterator.EntryObjectId)) { // in repo, in index, content diff => changed Modified.Add(dirCacheIterator.EntryPathString); changesExist = true; } } else { // in repo, not in index => removed if (!fileModeTree.Equals(NGit.FileMode.TYPE_TREE)) { Removed.Add(treeIterator.EntryPathString); changesExist = true; } } } else { if (dirCacheIterator != null) { // not in repo, in index => added Added.Add(dirCacheIterator.EntryPathString); changesExist = true; } else { // not in repo, not in index => untracked if (workingTreeIterator != null && !workingTreeIterator.IsEntryIgnored()) { Untracked.Add(workingTreeIterator.EntryPathString); changesExist = true; } } } if (dirCacheIterator != null) { if (workingTreeIterator == null) { // in index, not in workdir => missing Missing.Add(dirCacheIterator.EntryPathString); changesExist = true; } else { // Workaround to file time resolution issues long itime = dirCacheIterator.GetDirCacheEntry().LastModified; long ftime = workingTreeIterator.GetEntryLastModified(); if (itime / 1000 != ftime / 1000) { if (!dirCacheIterator.IdEqual(workingTreeIterator)) { // in index, in workdir, content differs => modified Modified.Add(dirCacheIterator.EntryPathString); changesExist = true; } } } } } }
/// <summary>Append any entry to the tree.</summary> /// <remarks>Append any entry to the tree.</remarks> /// <param name="nameBuf"> /// buffer holding the name of the entry. The name should be UTF-8 /// encoded, but file name encoding is not a well defined concept /// in Git. /// </param> /// <param name="namePos"> /// first position within /// <code>nameBuf</code> /// of the name data. /// </param> /// <param name="nameLen"> /// number of bytes from /// <code>nameBuf</code> /// to use as the name. /// </param> /// <param name="mode"> /// mode describing the treatment of /// <code>id</code> /// . /// </param> /// <param name="idBuf">buffer holding the raw ObjectId of the entry.</param> /// <param name="idPos"> /// first position within /// <code>idBuf</code> /// to copy the id from. /// </param> public virtual void Append(byte[] nameBuf, int namePos, int nameLen, FileMode mode , byte[] idBuf, int idPos) { if (FmtBuf(nameBuf, namePos, nameLen, mode)) { System.Array.Copy(idBuf, idPos, buf, ptr, Constants.OBJECT_ID_LENGTH); ptr += Constants.OBJECT_ID_LENGTH; } else { try { FmtOverflowBuffer(nameBuf, namePos, nameLen, mode); overflowBuffer.Write(idBuf, idPos, Constants.OBJECT_ID_LENGTH); } catch (IOException badBuffer) { // This should never occur. throw new RuntimeException(badBuffer); } } }
/// <summary>Append any entry to the tree.</summary> /// <remarks>Append any entry to the tree.</remarks> /// <param name="nameBuf"> /// buffer holding the name of the entry. The name should be UTF-8 /// encoded, but file name encoding is not a well defined concept /// in Git. /// </param> /// <param name="namePos"> /// first position within /// <code>nameBuf</code> /// of the name data. /// </param> /// <param name="nameLen"> /// number of bytes from /// <code>nameBuf</code> /// to use as the name. /// </param> /// <param name="mode"> /// mode describing the treatment of /// <code>id</code> /// . /// </param> /// <param name="id">the ObjectId to store in this entry.</param> public virtual void Append(byte[] nameBuf, int namePos, int nameLen, FileMode mode , AnyObjectId id) { if (FmtBuf(nameBuf, namePos, nameLen, mode)) { id.CopyRawTo(buf, ptr); ptr += Constants.OBJECT_ID_LENGTH; } else { try { FmtOverflowBuffer(nameBuf, namePos, nameLen, mode); id.CopyRawTo(overflowBuffer); } catch (IOException badBuffer) { // This should never occur. throw new RuntimeException(badBuffer); } } }
/// <summary>Append any entry to the tree.</summary> /// <remarks>Append any entry to the tree.</remarks> /// <param name="name"> /// name of the entry. The name should be UTF-8 encoded, but file /// name encoding is not a well defined concept in Git. /// </param> /// <param name="mode"> /// mode describing the treatment of /// <code>id</code> /// . /// </param> /// <param name="id">the ObjectId to store in this entry.</param> public virtual void Append(byte[] name, FileMode mode, AnyObjectId id) { Append(name, 0, name.Length, mode, id); }
/// <summary>Append any entry to the tree.</summary> /// <remarks>Append any entry to the tree.</remarks> /// <param name="name">name of the entry.</param> /// <param name="mode"> /// mode describing the treatment of /// <code>id</code> /// . /// </param> /// <param name="id">the ObjectId to store in this entry.</param> public virtual void Append(string name, FileMode mode, AnyObjectId id) { Append(Constants.Encode(name), mode, id); }
private static void AssertAdd(string newName, ObjectId newId, FileMode newMode, DiffEntry add) { NUnit.Framework.Assert.AreEqual(DiffEntry.DEV_NULL, add.oldPath); NUnit.Framework.Assert.AreEqual(DiffEntry.A_ZERO, add.oldId); NUnit.Framework.Assert.AreEqual(FileMode.MISSING, add.oldMode); NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.ADD, add.changeType); NUnit.Framework.Assert.AreEqual(newName, add.newPath); NUnit.Framework.Assert.AreEqual(AbbreviatedObjectId.FromObjectId(newId), add.newId ); NUnit.Framework.Assert.AreEqual(newMode, add.newMode); }
/// <summary>Compares whether two pairs of ObjectId and FileMode are equal.</summary> /// <remarks>Compares whether two pairs of ObjectId and FileMode are equal.</remarks> /// <param name="id1"></param> /// <param name="mode1"></param> /// <param name="id2"></param> /// <param name="mode2"></param> /// <returns> /// <code>true</code> if FileModes and ObjectIds are equal. /// <code>false</code> otherwise /// </returns> private bool EqualIdAndMode(ObjectId id1, FileMode mode1, ObjectId id2, FileMode mode2) { if (!mode1.Equals(mode2)) { return false; } return id1 != null ? id1.Equals(id2) : id2 == null; }
private bool FmtBuf(byte[] nameBuf, int namePos, int nameLen, FileMode mode) { if (buf == null || buf.Length < ptr + EntrySize(mode, nameLen)) { return false; } mode.CopyTo(buf, ptr); ptr += mode.CopyToLength(); buf[ptr++] = (byte)(' '); System.Array.Copy(nameBuf, namePos, buf, ptr, nameLen); ptr += nameLen; buf[ptr++] = 0; return true; }
private void Update(string path, ObjectId mId, FileMode mode) { if (!FileMode.TREE.Equals(mode)) { updated.Put(path, mId); DirCacheEntry entry = new DirCacheEntry(path, DirCacheEntry.STAGE_0); entry.SetObjectId(mId); entry.FileMode = mode; builder.Add(entry); } }
/// <exception cref="System.IO.IOException"></exception> private void FmtOverflowBuffer(byte[] nameBuf, int namePos, int nameLen, FileMode mode) { if (buf != null) { overflowBuffer = new TemporaryBuffer.Heap(int.MaxValue); overflowBuffer.Write(buf, 0, ptr); buf = null; } mode.CopyTo(overflowBuffer); overflowBuffer.Write(unchecked((byte)' ')); overflowBuffer.Write(nameBuf, namePos, nameLen); overflowBuffer.Write(unchecked((byte)0)); }
/// <exception cref="System.IO.IOException"></exception> private void AssertEntry(FileMode type, bool entryIgnored, string pathName) { NUnit.Framework.Assert.IsTrue(walk.Next(), "walk has entry"); NUnit.Framework.Assert.AreEqual(pathName, walk.PathString); NUnit.Framework.Assert.AreEqual(type, walk.GetFileMode(0)); WorkingTreeIterator itr = walk.GetTree<WorkingTreeIterator>(0); NUnit.Framework.Assert.IsNotNull(itr, "has tree"); NUnit.Framework.Assert.AreEqual(entryIgnored, itr.IsEntryIgnored(), "is ignored"); if (D.Equals(type)) { walk.EnterSubtree(); } }
/// <summary>Compute the size of a tree entry record.</summary> /// <remarks> /// Compute the size of a tree entry record. /// This method can be used to estimate the correct size of a tree prior to /// allocating a formatter. Getting the size correct at allocation time /// ensures the internal buffer is sized correctly, reducing copying. /// </remarks> /// <param name="mode">the mode the entry will have.</param> /// <param name="nameLen">the length of the name, in bytes.</param> /// <returns>the length of the record.</returns> public static int EntrySize(FileMode mode, int nameLen) { return mode.CopyToLength() + nameLen + Constants.OBJECT_ID_LENGTH + 2; }
internal NonNoteEntry(byte[] name, FileMode mode, AnyObjectId id) : base(id) { this.name = name; this.mode = mode; }
/// <exception cref="System.Exception"></exception> private static void AssertModes(string path, FileMode mode0, FileMode mode1, TreeWalk tw) { NUnit.Framework.Assert.IsTrue(tw.Next(), "has " + path); NUnit.Framework.Assert.AreEqual(path, tw.PathString); NUnit.Framework.Assert.AreEqual(mode0, tw.GetFileMode(0)); NUnit.Framework.Assert.AreEqual(mode1, tw.GetFileMode(1)); }
// private static int PathCompare(byte[] aBuf, int aPos, int aEnd, FileMode aMode, byte [] bBuf, int bPos, int bEnd, FileMode bMode) { while (aPos < aEnd && bPos < bEnd) { int cmp = (aBuf[aPos++] & unchecked((int)(0xff))) - (bBuf[bPos++] & unchecked((int )(0xff))); if (cmp != 0) { return cmp; } } if (aPos < aEnd) { return (aBuf[aPos] & unchecked((int)(0xff))) - LastPathChar(bMode); } if (bPos < bEnd) { return LastPathChar(aMode) - (bBuf[bPos] & unchecked((int)(0xff))); } return 0; }
/// <param name="execute">set/reset the executable flag</param> public virtual void SetExecutable(bool execute) { mode = execute ? FileMode.EXECUTABLE_FILE : FileMode.REGULAR_FILE; }
public virtual void TestNoSubtree_WithTreeWalk() { DirCache dc = DirCache.NewInCore(); string[] paths = new string[] { "a.", "a0b" }; FileMode[] modes = new FileMode[] { FileMode.EXECUTABLE_FILE, FileMode.GITLINK }; DirCacheEntry[] ents = new DirCacheEntry[paths.Length]; for (int i = 0; i < paths.Length; i++) { ents[i] = new DirCacheEntry(paths[i]); ents[i].FileMode = modes[i]; } DirCacheBuilder b = dc.Builder(); for (int i_1 = 0; i_1 < ents.Length; i_1++) { b.Add(ents[i_1]); } b.Finish(); DirCacheIterator i_2 = new DirCacheIterator(dc); TreeWalk tw = new TreeWalk(db); tw.AddTree(i_2); int pathIdx = 0; while (tw.Next()) { NUnit.Framework.Assert.AreSame(i_2, tw.GetTree<DirCacheIterator>(0)); NUnit.Framework.Assert.AreEqual(pathIdx, i_2.ptr); NUnit.Framework.Assert.AreSame(ents[pathIdx], i_2.GetDirCacheEntry()); NUnit.Framework.Assert.AreEqual(paths[pathIdx], tw.PathString); NUnit.Framework.Assert.AreEqual(modes[pathIdx].GetBits(), tw.GetRawMode(0)); NUnit.Framework.Assert.AreSame(modes[pathIdx], tw.GetFileMode(0)); pathIdx++; } NUnit.Framework.Assert.AreEqual(paths.Length, pathIdx); }
/// <exception cref="System.Exception"></exception> private DirCacheEntry MakeEntry(string path, FileMode mode) { return MakeEntry(path, mode, path); }
/// <exception cref="System.Exception"></exception> private static byte[] Entry(FileMode mode, string name, ObjectId id) { ByteArrayOutputStream @out = new ByteArrayOutputStream(); mode.CopyTo(@out); @out.Write(' '); @out.Write(Constants.Encode(name)); @out.Write(0); id.CopyRawTo(@out); return @out.ToByteArray(); }
protected internal virtual DirCacheEntry CreateEntry(string path, FileMode mode, string content) { return CreateEntry(path, mode, DirCacheEntry.STAGE_0, content); }