public virtual void TestModify()
        {
            Git    git  = new Git(db);
            string path = "file";

            WriteTrashFile(path, "content");
            git.Add().AddFilepattern(path).Call();
            git.Commit().SetMessage("commit").Call();
            DirCache      dc            = db.ReadDirCache();
            DirCacheEntry entry         = dc.GetEntry(path);
            long          masterLastMod = entry.LastModified;

            git.Checkout().SetCreateBranch(true).SetName("side").Call();
            Sharpen.Thread.Sleep(10);
            string path2 = "file2";

            WriteTrashFile(path2, "side content");
            git.Add().AddFilepattern(path2).Call();
            git.Commit().SetMessage("commit").Call();
            dc    = db.ReadDirCache();
            entry = dc.GetEntry(path);
            long sideLastMode = entry.LastModified;

            Sharpen.Thread.Sleep(2000);
            WriteTrashFile(path, "uncommitted content");
            git.Checkout().SetName("master").Call();
            dc    = db.ReadDirCache();
            entry = dc.GetEntry(path);
            NUnit.Framework.Assert.IsTrue(masterLastMod == sideLastMode, "shall have equal mod time!"
                                          );
            NUnit.Framework.Assert.IsTrue(entry.LastModified == masterLastMod, "shall not equal master timestamp!"
                                          );
        }
Exemplo n.º 2
0
        public virtual void TestUpdateSmudgedEntries()
        {
            git.BranchCreate().SetName("test2").Call();
            RefUpdate rup = db.UpdateRef(Constants.HEAD);

            rup.Link("refs/heads/test2");
            FilePath file  = new FilePath(db.WorkTree, "Test.txt");
            long     size  = file.Length();
            long     mTime = file.LastModified() - 5000L;

            NUnit.Framework.Assert.IsTrue(file.SetLastModified(mTime));
            DirCache      cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem);
            DirCacheEntry entry = cache.GetEntry("Test.txt");

            NUnit.Framework.Assert.IsNotNull(entry);
            entry.SetLength(0);
            entry.LastModified = 0;
            cache.Write();
            NUnit.Framework.Assert.IsTrue(cache.Commit());
            cache = DirCache.Read(db.GetIndexFile(), db.FileSystem);
            entry = cache.GetEntry("Test.txt");
            NUnit.Framework.Assert.IsNotNull(entry);
            NUnit.Framework.Assert.AreEqual(0, entry.Length);
            NUnit.Framework.Assert.AreEqual(0, entry.LastModified);
            db.GetIndexFile().SetLastModified(db.GetIndexFile().LastModified() - 5000);
            NUnit.Framework.Assert.IsNotNull(git.Checkout().SetName("test").Call());
            cache = DirCache.Read(db.GetIndexFile(), db.FileSystem);
            entry = cache.GetEntry("Test.txt");
            NUnit.Framework.Assert.IsNotNull(entry);
            NUnit.Framework.Assert.AreEqual(size, entry.Length);
            NUnit.Framework.Assert.AreEqual(mTime, entry.LastModified);
        }
        public virtual void TestLastModifiedTimes()
        {
            Git    git  = new Git(db);
            string path = "file";

            WriteTrashFile(path, "content");
            string path2 = "file2";

            WriteTrashFile(path2, "content2");
            git.Add().AddFilepattern(path).Call();
            git.Add().AddFilepattern(path2).Call();
            git.Commit().SetMessage("commit").Call();
            DirCache      dc     = db.ReadDirCache();
            DirCacheEntry entry  = dc.GetEntry(path);
            DirCacheEntry entry2 = dc.GetEntry(path);

            NUnit.Framework.Assert.IsTrue(entry.LastModified != 0, "last modified shall not be zero!"
                                          );
            NUnit.Framework.Assert.IsTrue(entry2.LastModified != 0, "last modified shall not be zero!"
                                          );
            WriteTrashFile(path, "new content");
            git.Add().AddFilepattern(path).Call();
            git.Commit().SetMessage("commit2").Call();
            dc     = db.ReadDirCache();
            entry  = dc.GetEntry(path);
            entry2 = dc.GetEntry(path);
            NUnit.Framework.Assert.IsTrue(entry.LastModified != 0, "last modified shall not be zero!"
                                          );
            NUnit.Framework.Assert.IsTrue(entry2.LastModified != 0, "last modified shall not be zero!"
                                          );
        }
Exemplo n.º 4
0
        /// <summary>Represent the state of the index in one String.</summary>
        /// <remarks>
        /// Represent the state of the index in one String. This representation is
        /// useful when writing tests which do assertions on the state of the index.
        /// By default information about path, mode, stage (if different from 0) is
        /// included. A bitmask controls which additional info about
        /// modificationTimes, smudge state and length is included.
        /// <p>
        /// The format of the returned string is described with this BNF:
        /// <pre>
        /// result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
        /// mode = ", mode:" number .
        /// stage = ", stage:" number .
        /// time = ", time:t" timestamp-index .
        /// smudge = "" | ", smudged" .
        /// length = ", length:" number .
        /// sha1 = ", sha1:" hex-sha1 .
        /// content = ", content:" blob-data .
        /// </pre>
        /// 'stage' is only presented when the stage is different from 0. All
        /// reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
        /// smallest reported time-stamp will be called "t0". This allows to write
        /// assertions against the string although the concrete value of the time
        /// stamps is unknown.
        /// </remarks>
        /// <param name="repo">the repository the index state should be determined for</param>
        /// <param name="includedOptions">
        /// a bitmask constructed out of the constants
        /// <see cref="MOD_TIME">MOD_TIME</see>
        /// ,
        /// <see cref="SMUDGE">SMUDGE</see>
        /// ,
        /// <see cref="LENGTH">LENGTH</see>
        /// ,
        /// <see cref="CONTENT_ID">CONTENT_ID</see>
        /// and
        /// <see cref="CONTENT">CONTENT</see>
        /// controlling which info is present in the
        /// resulting string.
        /// </param>
        /// <returns>a string encoding the index state</returns>
        /// <exception cref="System.InvalidOperationException">System.InvalidOperationException
        ///     </exception>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public virtual string IndexState(Repository repo, int includedOptions)
        {
            DirCache       dc         = repo.ReadDirCache();
            StringBuilder  sb         = new StringBuilder();
            TreeSet <long> timeStamps = null;

            // iterate once over the dircache just to collect all time stamps
            if (0 != (includedOptions & MOD_TIME))
            {
                timeStamps = new TreeSet <long>();
                for (int i = 0; i < dc.GetEntryCount(); ++i)
                {
                    timeStamps.AddItem(Sharpen.Extensions.ValueOf(dc.GetEntry(i).LastModified));
                }
            }
            // iterate again, now produce the result string
            for (int i_1 = 0; i_1 < dc.GetEntryCount(); ++i_1)
            {
                DirCacheEntry entry = dc.GetEntry(i_1);
                sb.Append("[" + entry.PathString + ", mode:" + entry.FileMode);
                int stage = entry.Stage;
                if (stage != 0)
                {
                    sb.Append(", stage:" + stage);
                }
                if (0 != (includedOptions & MOD_TIME))
                {
                    sb.Append(", time:t" + timeStamps.HeadSet(Sharpen.Extensions.ValueOf(entry.LastModified
                                                                                         )).Count);
                }
                if (0 != (includedOptions & SMUDGE))
                {
                    if (entry.IsSmudged)
                    {
                        sb.Append(", smudged");
                    }
                }
                if (0 != (includedOptions & LENGTH))
                {
                    sb.Append(", length:" + Sharpen.Extensions.ToString(entry.Length));
                }
                if (0 != (includedOptions & CONTENT_ID))
                {
                    sb.Append(", sha1:" + ObjectId.ToString(entry.GetObjectId()));
                }
                if (0 != (includedOptions & CONTENT))
                {
                    sb.Append(", content:" + Sharpen.Runtime.GetStringForBytes(db.Open(entry.GetObjectId
                                                                                           (), Constants.OBJ_BLOB).GetCachedBytes(), "UTF-8"));
                }
                if (0 != (includedOptions & ASSUME_UNCHANGED))
                {
                    sb.Append(", assume-unchanged:" + entry.IsAssumeValid.ToString().ToLower());
                }
                sb.Append("]");
            }
            return(sb.ToString());
        }
Exemplo n.º 5
0
 /// <returns>list of files with the flag assume-unchanged</returns>
 public virtual ICollection <string> GetAssumeUnchanged()
 {
     if (assumeUnchanged == null)
     {
         HashSet <string> unchanged = new HashSet <string>();
         for (int i = 0; i < dirCache.GetEntryCount(); i++)
         {
             if (dirCache.GetEntry(i).IsAssumeValid)
             {
                 unchanged.AddItem(dirCache.GetEntry(i).PathString);
             }
         }
         assumeUnchanged = unchanged;
     }
     return(assumeUnchanged);
 }
Exemplo n.º 6
0
        public virtual void TestCherryPickOverExecutableChangeOnNonExectuableFileSystem()
        {
            Git      git  = new Git(db);
            FilePath file = WriteTrashFile("test.txt", "a");

            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("test.txt").Call());
            NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("commit1").Call());
            NUnit.Framework.Assert.IsNotNull(git.Checkout().SetCreateBranch(true).SetName("a"
                                                                                          ).Call());
            WriteTrashFile("test.txt", "b");
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("test.txt").Call());
            RevCommit commit2 = git.Commit().SetMessage("commit2").Call();

            NUnit.Framework.Assert.IsNotNull(commit2);
            NUnit.Framework.Assert.IsNotNull(git.Checkout().SetName(Constants.MASTER).Call());
            DirCache cache = db.LockDirCache();

            cache.GetEntry("test.txt").FileMode = FileMode.EXECUTABLE_FILE;
            cache.Write();
            NUnit.Framework.Assert.IsTrue(cache.Commit());
            cache.Unlock();
            NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("commit3").Call());
            git.GetRepository().GetConfig().SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null
                                                       , ConfigConstants.CONFIG_KEY_FILEMODE, false);
            CherryPickResult result = git.CherryPick().Include(commit2).Call();

            NUnit.Framework.Assert.IsNotNull(result);
            NUnit.Framework.Assert.AreEqual(CherryPickResult.CherryPickStatus.OK, result.GetStatus
                                                ());
        }
Exemplo n.º 7
0
        /// <summary>Reverts the worktree after an unsuccessful merge.</summary>
        /// <remarks>
        /// Reverts the worktree after an unsuccessful merge. We know that for all
        /// modified files the old content was in the old index and the index
        /// contained only stage 0. In case if inCore operation just clear
        /// the history of modified files.
        /// </remarks>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        /// <exception cref="NGit.Errors.CorruptObjectException">NGit.Errors.CorruptObjectException
        ///     </exception>
        /// <exception cref="NGit.Errors.NoWorkTreeException">NGit.Errors.NoWorkTreeException
        ///     </exception>
        private void CleanUp()
        {
            if (inCore)
            {
                modifiedFiles.Clear();
                return;
            }
            DirCache          dc       = db.ReadDirCache();
            ObjectReader      or       = db.ObjectDatabase.NewReader();
            Iterator <string> mpathsIt = modifiedFiles.Iterator();

            while (mpathsIt.HasNext())
            {
                string           mpath = mpathsIt.Next();
                DirCacheEntry    entry = dc.GetEntry(mpath);
                FileOutputStream fos   = new FileOutputStream(new FilePath(db.WorkTree, mpath));
                try
                {
                    or.Open(entry.GetObjectId()).CopyTo(fos);
                }
                finally
                {
                    fos.Close();
                }
                mpathsIt.Remove();
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Generate a list of lines with information about when the lines were
        /// introduced into the file path.
        /// </summary>
        /// <remarks>
        /// Generate a list of lines with information about when the lines were
        /// introduced into the file path.
        /// </remarks>
        /// <returns>list of lines</returns>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override BlameResult Call()
        {
            CheckCallable();
            BlameGenerator gen = new BlameGenerator(repo, path);

            try
            {
                if (diffAlgorithm != null)
                {
                    gen.SetDiffAlgorithm(diffAlgorithm);
                }
                if (textComparator != null)
                {
                    gen.SetTextComparator(textComparator);
                }
                if (followFileRenames != null)
                {
                    gen.SetFollowFileRenames(followFileRenames);
                }
                if (reverseEndCommits != null)
                {
                    gen.Reverse(startCommit, reverseEndCommits);
                }
                else
                {
                    if (startCommit != null)
                    {
                        gen.Push(null, startCommit);
                    }
                    else
                    {
                        gen.Push(null, repo.Resolve(Constants.HEAD));
                        if (!repo.IsBare)
                        {
                            DirCache dc    = repo.ReadDirCache();
                            int      entry = dc.FindEntry(path);
                            if (0 <= entry)
                            {
                                gen.Push(null, dc.GetEntry(entry).GetObjectId());
                            }
                            FilePath inTree = new FilePath(repo.WorkTree, path);
                            if (inTree.IsFile())
                            {
                                gen.Push(null, new RawText(inTree));
                            }
                        }
                    }
                }
                return(gen.ComputeBlameResult());
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            finally
            {
                gen.Release();
            }
        }
Exemplo n.º 9
0
        /// <exception cref="NGit.Errors.CorruptObjectException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void AssertIndex(Dictionary <string, string> i)
        {
            string   expectedValue;
            string   path;
            DirCache read = DirCache.Read(db.GetIndexFile(), db.FileSystem);

            NUnit.Framework.Assert.AreEqual(i.Count, read.GetEntryCount(), "Index has not the right size."
                                            );
            for (int j = 0; j < read.GetEntryCount(); j++)
            {
                path          = read.GetEntry(j).PathString;
                expectedValue = i.Get(path);
                NUnit.Framework.Assert.IsNotNull(expectedValue, "found unexpected entry for path "
                                                 + path + " in index");
                NUnit.Framework.Assert.IsTrue(Arrays.Equals(db.Open(read.GetEntry(j).GetObjectId(
                                                                        )).GetCachedBytes(), Sharpen.Runtime.GetBytesForString(i.Get(path))), "unexpected content for path "
                                              + path + " in index. Expected: <" + expectedValue + ">");
            }
        }
Exemplo n.º 10
0
        public GitFileStatus GetFileStatusNoCache(string fileName)
        {
            if (Directory.Exists(fileName))
            {
                return(GitFileStatus.Ignored);
            }

            var      fileNameRel = GetRelativeFileNameForGit(fileName);
            TreeWalk treeWalk    = new TreeWalk(this.repository)
            {
                Recursive = true
            };
            RevTree revTree = head == null ? null : new RevWalk(repository).ParseTree(head);

            if (revTree != null)
            {
                treeWalk.AddTree(revTree);
            }
            else
            {
                treeWalk.AddTree(new EmptyTreeIterator());
            }
            treeWalk.AddTree(new DirCacheIterator(dirCache));
            treeWalk.AddTree(new FileTreeIterator(this.repository));

            var filters = new TreeFilter[] {
                PathFilter.Create(fileNameRel),
                new SkipWorkTreeFilter(INDEX),
                new IndexDiffFilter(INDEX, WORKDIR)
            };

            treeWalk.Filter = AndTreeFilter.Create(filters);

            var status = GitFileStatus.NotControlled;

            if (treeWalk.Next())
            {
                status = GetFileStatus(treeWalk);
            }

            if (status == GitFileStatus.NotControlled)
            {
                var dirCacheEntry2 = dirCache.GetEntry(fileNameRel);
                if (dirCacheEntry2 != null)
                {
                    var treeEntry2 = TreeWalk.ForPath(repository, fileNameRel, revTree);
                    if (treeEntry2 != null && treeEntry2.GetObjectId(0).Equals(dirCacheEntry2.GetObjectId()))
                    {
                        return(GitFileStatus.Tracked);
                    }
                }
            }
            return(GitFileStatus.NotControlled);
        }
Exemplo n.º 11
0
        public virtual void TestMixedResetRetainsSizeAndModifiedTime()
        {
            git = new Git(db);
            WriteTrashFile("a.txt", "a").SetLastModified(Runtime.CurrentTimeMillis() - 60 * 1000
                                                         );
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("a.txt").Call());
            NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("a commit").Call());
            WriteTrashFile("b.txt", "b").SetLastModified(Runtime.CurrentTimeMillis() - 60 * 1000
                                                         );
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("b.txt").Call());
            RevCommit commit2 = git.Commit().SetMessage("b commit").Call();

            NUnit.Framework.Assert.IsNotNull(commit2);
            DirCache      cache  = db.ReadDirCache();
            DirCacheEntry aEntry = cache.GetEntry("a.txt");

            NUnit.Framework.Assert.IsNotNull(aEntry);
            NUnit.Framework.Assert.IsTrue(aEntry.Length > 0);
            NUnit.Framework.Assert.IsTrue(aEntry.LastModified > 0);
            DirCacheEntry bEntry = cache.GetEntry("b.txt");

            NUnit.Framework.Assert.IsNotNull(bEntry);
            NUnit.Framework.Assert.IsTrue(bEntry.Length > 0);
            NUnit.Framework.Assert.IsTrue(bEntry.LastModified > 0);
            git.Reset().SetMode(ResetCommand.ResetType.MIXED).SetRef(commit2.GetName()).Call(
                );
            cache = db.ReadDirCache();
            DirCacheEntry mixedAEntry = cache.GetEntry("a.txt");

            NUnit.Framework.Assert.IsNotNull(mixedAEntry);
            NUnit.Framework.Assert.AreEqual(aEntry.LastModified, mixedAEntry.LastModified);
            NUnit.Framework.Assert.AreEqual(aEntry.LastModified, mixedAEntry.LastModified);
            DirCacheEntry mixedBEntry = cache.GetEntry("b.txt");

            NUnit.Framework.Assert.IsNotNull(mixedBEntry);
            NUnit.Framework.Assert.AreEqual(bEntry.LastModified, mixedBEntry.LastModified);
            NUnit.Framework.Assert.AreEqual(bEntry.LastModified, mixedBEntry.LastModified);
        }
Exemplo n.º 12
0
        /// <exception cref="System.IO.IOException"></exception>
        private void AssumeUnchanged(string path)
        {
            DirCache      dirc = db.LockDirCache();
            DirCacheEntry ent  = dirc.GetEntry(path);

            if (ent != null)
            {
                ent.IsAssumeValid = true;
            }
            dirc.Write();
            if (!dirc.Commit())
            {
                throw new IOException("could not commit");
            }
        }
Exemplo n.º 13
0
        public virtual void CommitIgnoresSmudgedEntryWithDifferentId()
        {
            Git      git   = new Git(db);
            FilePath file1 = WriteTrashFile("file1.txt", "content1");

            NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() - 5000));
            FilePath file2 = WriteTrashFile("file2.txt", "content2");

            NUnit.Framework.Assert.IsTrue(file2.SetLastModified(file2.LastModified() - 5000));
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("file1.txt").AddFilepattern
                                                 ("file2.txt").Call());
            RevCommit commit = git.Commit().SetMessage("add files").Call();

            NUnit.Framework.Assert.IsNotNull(commit);
            DirCache cache     = DirCache.Read(db.GetIndexFile(), db.FileSystem);
            int      file1Size = cache.GetEntry("file1.txt").Length;
            int      file2Size = cache.GetEntry("file2.txt").Length;

            NUnit.Framework.Assert.IsTrue(file1Size > 0);
            NUnit.Framework.Assert.IsTrue(file2Size > 0);
            WriteTrashFile("file2.txt", "content3");
            NUnit.Framework.Assert.IsNotNull(git.Add().AddFilepattern("file2.txt").Call());
            WriteTrashFile("file2.txt", "content4");
            // Smudge entries
            cache = DirCache.Lock(db.GetIndexFile(), db.FileSystem);
            cache.GetEntry("file1.txt").SetLength(0);
            cache.GetEntry("file2.txt").SetLength(0);
            cache.Write();
            NUnit.Framework.Assert.IsTrue(cache.Commit());
            // Verify entries smudged
            cache = db.ReadDirCache();
            NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file1.txt").Length);
            NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file2.txt").Length);
            long indexTime = db.GetIndexFile().LastModified();

            db.GetIndexFile().SetLastModified(indexTime - 5000);
            Write(file1, "content5");
            NUnit.Framework.Assert.IsTrue(file1.SetLastModified(file1.LastModified() + 1000));
            NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("edit file").SetOnly("file1.txt"
                                                                                          ).Call());
            cache = db.ReadDirCache();
            NUnit.Framework.Assert.AreEqual(file1Size, cache.GetEntry("file1.txt").Length);
            NUnit.Framework.Assert.AreEqual(0, cache.GetEntry("file2.txt").Length);
        }
		public virtual void TestReadIndex_LsFiles()
		{
			IDictionary<string, DirCacheCGitCompatabilityTest.CGitIndexRecord> ls = ReadLsFiles
				();
			DirCache dc = new DirCache(index, FS.DETECTED);
			NUnit.Framework.Assert.AreEqual(0, dc.GetEntryCount());
			dc.Read();
			NUnit.Framework.Assert.AreEqual(ls.Count, dc.GetEntryCount());
			{
				Iterator<DirCacheCGitCompatabilityTest.CGitIndexRecord> rItr = ls.Values.Iterator
					();
				for (int i = 0; rItr.HasNext(); i++)
				{
					AssertEqual(rItr.Next(), dc.GetEntry(i));
				}
			}
		}
Exemplo n.º 15
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ApplyChanges(TreeWalk treeWalk, DirCache cache, DirCacheEditor editor
                                  )
        {
            FilePath workingTree = repo.WorkTree;

            while (treeWalk.Next())
            {
                string   path = treeWalk.PathString;
                FilePath file = new FilePath(workingTree, path);
                // State of the stashed HEAD, index, and working directory
                AbstractTreeIterator stashHeadIter    = treeWalk.GetTree <AbstractTreeIterator>(0);
                AbstractTreeIterator stashIndexIter   = treeWalk.GetTree <AbstractTreeIterator>(1);
                AbstractTreeIterator stashWorkingIter = treeWalk.GetTree <AbstractTreeIterator>(2);
                if (stashWorkingIter != null && stashIndexIter != null)
                {
                    // Checkout index change
                    DirCacheEntry entry = cache.GetEntry(path);
                    if (entry == null)
                    {
                        entry = new DirCacheEntry(treeWalk.RawPath);
                    }
                    entry.FileMode = stashIndexIter.EntryFileMode;
                    entry.SetObjectId(stashIndexIter.EntryObjectId);
                    DirCacheCheckout.CheckoutEntry(repo, file, entry, treeWalk.ObjectReader);
                    DirCacheEntry updatedEntry = entry;
                    editor.Add(new _PathEdit_270(updatedEntry, path));
                    // Checkout working directory change
                    if (!stashWorkingIter.IdEqual(stashIndexIter))
                    {
                        entry = new DirCacheEntry(treeWalk.RawPath);
                        entry.SetObjectId(stashWorkingIter.EntryObjectId);
                        DirCacheCheckout.CheckoutEntry(repo, file, entry, treeWalk.ObjectReader);
                    }
                }
                else
                {
                    if (stashIndexIter == null || (stashHeadIter != null && !stashIndexIter.IdEqual(stashHeadIter
                                                                                                    )))
                    {
                        editor.Add(new DirCacheEditor.DeletePath(path));
                    }
                    FileUtils.Delete(file, FileUtils.RETRY | FileUtils.SKIP_MISSING);
                }
            }
        }
Exemplo n.º 16
0
        public virtual void TestAddRemovedFile()
        {
            FilePath file = new FilePath(db.WorkTree, "a.txt");

            FileUtils.CreateNewFile(file);
            PrintWriter writer = new PrintWriter(file);

            writer.Write("content");
            writer.Close();
            Git      git = new Git(db);
            DirCache dc  = git.Add().AddFilepattern("a.txt").Call();

            dc.GetEntry(0).GetObjectId();
            FileUtils.Delete(file);
            // is supposed to do nothing
            dc = git.Add().AddFilepattern("a.txt").Call();
            NUnit.Framework.Assert.AreEqual("[a.txt, mode:100644, content:content]", IndexState
                                                (CONTENT));
        }
Exemplo n.º 17
0
        public virtual void TestAddExistingSingleFileTwiceWithCommit()
        {
            FilePath file = new FilePath(db.WorkTree, "a.txt");

            FileUtils.CreateNewFile(file);
            PrintWriter writer = new PrintWriter(file);

            writer.Write("content");
            writer.Close();
            Git      git = new Git(db);
            DirCache dc  = git.Add().AddFilepattern("a.txt").Call();

            dc.GetEntry(0).GetObjectId();
            git.Commit().SetMessage("commit a.txt").Call();
            writer = new PrintWriter(file);
            writer.Write("other content");
            writer.Close();
            dc = git.Add().AddFilepattern("a.txt").Call();
            NUnit.Framework.Assert.AreEqual("[a.txt, mode:100644, content:other content]", IndexState
                                                (CONTENT));
        }
Exemplo n.º 18
0
        /// <exception cref="NGit.Errors.NoWorkTreeException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public static void ValidateIndex(Git git)
        {
            DirCache     dc = git.GetRepository().LockDirCache();
            ObjectReader r  = git.GetRepository().ObjectDatabase.NewReader();

            try
            {
                for (int i = 0; i < dc.GetEntryCount(); ++i)
                {
                    DirCacheEntry entry = dc.GetEntry(i);
                    if (entry.Length > 0)
                    {
                        NUnit.Framework.Assert.AreEqual(entry.Length, r.GetObjectSize(entry.GetObjectId()
                                                                                      , ObjectReader.OBJ_ANY));
                    }
                }
            }
            finally
            {
                dc.Unlock();
                r.Release();
            }
        }
Exemplo n.º 19
0
        /// <summary>Checkout paths into index and working directory</summary>
        /// <returns>this instance</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        /// <exception cref="NGit.Api.Errors.RefNotFoundException">NGit.Api.Errors.RefNotFoundException
        ///     </exception>
        protected internal virtual NGit.Api.CheckoutCommand CheckoutPaths()
        {
            RevWalk  revWalk = new RevWalk(repo);
            DirCache dc      = repo.LockDirCache();

            try
            {
                TreeWalk treeWalk = new TreeWalk(revWalk.GetObjectReader());
                treeWalk.Recursive = true;
                treeWalk.AddTree(new DirCacheIterator(dc));
                treeWalk.Filter = PathFilterGroup.CreateFromStrings(paths);
                IList <string> files = new List <string>();
                while (treeWalk.Next())
                {
                    files.AddItem(treeWalk.PathString);
                }
                if (startCommit != null || startPoint != null)
                {
                    DirCacheEditor editor    = dc.Editor();
                    TreeWalk       startWalk = new TreeWalk(revWalk.GetObjectReader());
                    startWalk.Recursive = true;
                    startWalk.Filter    = treeWalk.Filter;
                    startWalk.AddTree(revWalk.ParseCommit(GetStartPoint()).Tree);
                    while (startWalk.Next())
                    {
                        ObjectId blobId = startWalk.GetObjectId(0);
                        editor.Add(new _PathEdit_258(blobId, startWalk.PathString));
                    }
                    editor.Commit();
                }
                FilePath workTree = repo.WorkTree;
                foreach (string file in files)
                {
                    DirCacheCheckout.CheckoutEntry(repo, new FilePath(workTree, file), dc.GetEntry(file
                                                                                                   ));
                }
            }
            finally
            {
                dc.Unlock();
                revWalk.Release();
            }
            return(this);
        }
Exemplo n.º 20
0
 private void AssertCorrectId(DirCache treeT, TreeWalk tw)
 {
     NUnit.Framework.Assert.AreEqual(treeT.GetEntry(tw.PathString).GetObjectId(), tw.GetObjectId
                                         (0));
 }
Exemplo n.º 21
0
        //
        //
        //
        //
        //
        // Assert that every specified index entry has the same last modification
        // timestamp as the associated file
        /// <exception cref="System.IO.IOException"></exception>
        private void CheckConsistentLastModified(params string[] pathes)
        {
            DirCache dc       = db.ReadDirCache();
            FilePath workTree = db.WorkTree;

            foreach (string path in pathes)
            {
                NUnit.Framework.Assert.AreEqual(new FilePath(workTree, path).LastModified(), dc.GetEntry
                                                    (path).LastModified, "IndexEntry with path " + path + " has lastmodified with is different from the worktree file"
                                                );
            }
        }
		public virtual void TestUnsupportedOptionalExtension()
		{
			DirCache dc = new DirCache(PathOf("gitgit.index.ZZZZ"), FS.DETECTED);
			dc.Read();
			NUnit.Framework.Assert.AreEqual(1, dc.GetEntryCount());
			NUnit.Framework.Assert.AreEqual("A", dc.GetEntry(0).PathString);
		}
Exemplo n.º 23
0
		private void AssertCorrectId(DirCache treeT, TreeWalk tw)
		{
			NUnit.Framework.Assert.AreEqual(treeT.GetEntry(tw.PathString).GetObjectId(), tw.GetObjectId
				(0));
		}
Exemplo n.º 24
0
        /// <summary>Checks if a file with the given path exists in the index</summary>
        /// <param name="path"></param>
        /// <returns>true if the file exists</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        private bool InIndex(string path)
        {
            DirCache dc = DirCache.Read(db.GetIndexFile(), db.FileSystem);

            return(dc.GetEntry(path) != null);
        }
		private static void AssertV3TreeEntry(int indexPosition, string path, bool skipWorkTree
			, bool intentToAdd, DirCache dc)
		{
			DirCacheEntry entry = dc.GetEntry(indexPosition);
			NUnit.Framework.Assert.AreEqual(path, entry.PathString);
			Assert.AreEqual(skipWorkTree, entry.IsSkipWorkTree);
			Assert.AreEqual(intentToAdd, entry.IsIntentToAdd);
		}