Esempio n. 1
0
        public virtual void RepositoryWithUnconfiguredSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_137(id, path));
            editor.Commit();
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_PATH, path);
            string url = "git://server/repo.git";

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_URL, url);
            string update = "rebase";

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_UPDATE, update);
            modulesConfig.Save();
            SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
            ICollection <string>   updated = command.Call();

            NUnit.Framework.Assert.IsNotNull(updated);
            NUnit.Framework.Assert.IsTrue(updated.IsEmpty());
        }
        public virtual void SubmoduleWithNoHead()
        {
            Git git = new Git(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit      id     = git.Commit().SetMessage("create file").Call();
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_345(id, path));
            editor.Commit();
            NUnit.Framework.Assert.IsNotNull(Git.Init().SetDirectory(new FilePath(db.WorkTree
                                                                                  , path)).Call().GetRepository());
            TreeWalk         walk         = new TreeWalk(db);
            DirCacheIterator indexIter    = new DirCacheIterator(db.ReadDirCache());
            FileTreeIterator workTreeIter = new FileTreeIterator(db);

            walk.AddTree(indexIter);
            walk.AddTree(workTreeIter);
            walk.Filter = PathFilter.Create(path);
            NUnit.Framework.Assert.IsTrue(walk.Next());
            NUnit.Framework.Assert.IsFalse(indexIter.IdEqual(workTreeIter));
            NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId, workTreeIter.EntryObjectId);
        }
        public virtual void TestAdded()
        {
            WriteTrashFile("file1", "file1");
            WriteTrashFile("dir/subfile", "dir/subfile");
            Tree tree = new Tree(db);

            tree.SetId(InsertTree(tree));
            DirCache       index  = db.LockDirCache();
            DirCacheEditor editor = index.Editor();

            editor.Add(Add(db, trash, "file1"));
            editor.Add(Add(db, trash, "dir/subfile"));
            editor.Commit();
            FileTreeIterator iterator = new FileTreeIterator(db);
            IndexDiff        diff     = new IndexDiff(db, tree.GetId(), iterator);

            diff.Diff();
            NUnit.Framework.Assert.AreEqual(2, diff.GetAdded().Count);
            NUnit.Framework.Assert.IsTrue(diff.GetAdded().Contains("file1"));
            NUnit.Framework.Assert.IsTrue(diff.GetAdded().Contains("dir/subfile"));
            NUnit.Framework.Assert.AreEqual(0, diff.GetChanged().Count);
            NUnit.Framework.Assert.AreEqual(0, diff.GetModified().Count);
            NUnit.Framework.Assert.AreEqual(0, diff.GetRemoved().Count);
            NUnit.Framework.CollectionAssert.AreEquivalent(Collections <string> .EMPTY_SET, diff.GetUntrackedFolders()
                                                           );
        }
        public virtual void SubmoduleNestedWithHeadMatchingIndex()
        {
            Git git = new Git(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            RevCommit      id     = git.Commit().SetMessage("create file").Call();
            string         path   = "sub/dir1/dir2";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_412(id, path));
            editor.Commit();
            Git.CloneRepository().SetURI(db.Directory.ToURI().ToString()).SetDirectory(new FilePath
                                                                                           (db.WorkTree, path)).Call().GetRepository().Close();
            TreeWalk         walk         = new TreeWalk(db);
            DirCacheIterator indexIter    = new DirCacheIterator(db.ReadDirCache());
            FileTreeIterator workTreeIter = new FileTreeIterator(db);

            walk.AddTree(indexIter);
            walk.AddTree(workTreeIter);
            walk.Filter = PathFilter.Create(path);
            NUnit.Framework.Assert.IsTrue(walk.Next());
            NUnit.Framework.Assert.IsTrue(indexIter.IdEqual(workTreeIter));
        }
Esempio n. 5
0
        public virtual void RepositoryWithMissingSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_93(id, path));
            editor.Commit();
            SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
            IDictionary <string, SubmoduleStatus> statuses = command.Call();

            NUnit.Framework.Assert.IsNotNull(statuses);
            NUnit.Framework.Assert.AreEqual(1, statuses.Count);
            KeyValuePair <string, SubmoduleStatus> module = statuses.EntrySet().Iterator().Next
                                                                ();

            NUnit.Framework.Assert.IsNotNull(module);
            NUnit.Framework.Assert.AreEqual(path, module.Key);
            SubmoduleStatus status = module.Value;

            NUnit.Framework.Assert.IsNotNull(status);
            NUnit.Framework.Assert.AreEqual(path, status.GetPath());
            NUnit.Framework.Assert.AreEqual(id, status.GetIndexId());
            NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.MISSING, status.GetType());
        }
Esempio n. 6
0
        public virtual void RepositoryWithUninitializedSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_125(id, path));
            editor.Commit();
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_PATH, path);
            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_URL, "git://server/repo.git");
            modulesConfig.Save();
            SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
            IDictionary <string, SubmoduleStatus> statuses = command.Call();

            NUnit.Framework.Assert.IsNotNull(statuses);
            NUnit.Framework.Assert.AreEqual(1, statuses.Count);
            KeyValuePair <string, SubmoduleStatus> module = statuses.EntrySet().Iterator().Next
                                                                ();

            NUnit.Framework.Assert.IsNotNull(module);
            NUnit.Framework.Assert.AreEqual(path, module.Key);
            SubmoduleStatus status = module.Value;

            NUnit.Framework.Assert.IsNotNull(status);
            NUnit.Framework.Assert.AreEqual(path, status.GetPath());
            NUnit.Framework.Assert.AreEqual(id, status.GetIndexId());
            NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.UNINITIALIZED, status.GetType
                                                ());
        }
Esempio n. 7
0
        public virtual void ShouldReportFileModeChange()
        {
            WriteTrashFile("a.txt", "content");
            Git git = new Git(db);

            git.Add().AddFilepattern("a.txt").Call();
            RevCommit      c1     = git.Commit().SetMessage("initial commit").Call();
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();
            TreeWalk       walk   = new TreeWalk(db);

            walk.AddTree(c1.Tree);
            walk.Recursive = true;
            NUnit.Framework.Assert.IsTrue(walk.Next());
            editor.Add(new _PathEdit_318(walk, "a.txt"));
            NUnit.Framework.Assert.IsTrue(editor.Commit());
            RevCommit c2 = git.Commit().SetMessage("second commit").Call();

            walk.Reset();
            walk.AddTree(c1.Tree);
            walk.AddTree(c2.Tree);
            IList <DiffEntry> diffs = DiffEntry.Scan(walk, false);

            NUnit.Framework.Assert.AreEqual(1, diffs.Count);
            DiffEntry diff = diffs[0];

            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, diff.GetChangeType()
                                            );
            NUnit.Framework.Assert.AreEqual(diff.GetOldId(), diff.GetNewId());
            NUnit.Framework.Assert.AreEqual("a.txt", diff.GetOldPath());
            NUnit.Framework.Assert.AreEqual(diff.GetOldPath(), diff.GetNewPath());
            NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, diff.GetNewMode());
            NUnit.Framework.Assert.AreEqual(FileMode.REGULAR_FILE, diff.GetOldMode());
        }
Esempio n. 8
0
            public CommitBuilder add(String path, RevBlob id)
            {
                DirCacheEditor e = tree.editor();

                e.add(new MockPathEdit(id, path));
                e.finish();
                return(this);
            }
Esempio n. 9
0
        public virtual CommitBuilder Edit(NGit.Dircache.DirCacheEditor.PathEdit edit)
        {
            DirCacheEditor e = this.tree.Editor();

            e.Add(edit);
            e.Finish();
            return(this);
        }
Esempio n. 10
0
            /// <exception cref="System.Exception"></exception>
            public virtual TestRepository.CommitBuilder Add(string path, RevBlob id)
            {
                DirCacheEditor e = this.tree.Editor();

                e.Add(new _PathEdit_790(id, path));
                e.Finish();
                return(this);
            }
Esempio n. 11
0
        public virtual CommitBuilder Rm(string path)
        {
            DirCacheEditor e = this.tree.Editor();

            e.Add(new DirCacheEditor.DeletePath(path));
            e.Add(new DirCacheEditor.DeleteTree(path));
            e.Finish();
            return(this);
        }
Esempio n. 12
0
            public CommitBuilder rm(String path)
            {
                DirCacheEditor e = tree.editor();

                e.add(new DirCacheEditor.DeletePath(path));
                e.add(new DirCacheEditor.DeleteTree(path));
                e.finish();
                return(this);
            }
Esempio n. 13
0
        /// <exception cref="System.IO.IOException"></exception>
        private void RemoveFromIndex(string path)
        {
            DirCache       dirc = db.LockDirCache();
            DirCacheEditor edit = dirc.Editor();

            edit.Add(new DirCacheEditor.DeletePath(path));
            if (!edit.Commit())
            {
                throw new IOException("could not commit");
            }
        }
Esempio n. 14
0
        public virtual void RepositoryWithSubmodule()
        {
            WriteTrashFile("file.txt", "content");
            Git git = Git.Wrap(db);

            git.Add().AddFilepattern("file.txt").Call();
            git.Commit().SetMessage("create file").Call();
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_96(id, path));
            editor.Commit();
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_PATH, path);
            string url = "git://server/repo.git";

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_URL, url);
            modulesConfig.Save();
            Repository subRepo = Git.CloneRepository().SetURI(db.Directory.ToURI().ToString()
                                                              ).SetDirectory(new FilePath(db.WorkTree, path)).Call().GetRepository();

            AddRepoToClose(subRepo);
            NUnit.Framework.Assert.IsNotNull(subRepo);
            SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUrl());
            NUnit.Framework.Assert.AreEqual(url, generator.GetModulesUrl());
            SubmoduleSyncCommand         command = new SubmoduleSyncCommand(db);
            IDictionary <string, string> synced  = command.Call();

            NUnit.Framework.Assert.IsNotNull(synced);
            NUnit.Framework.Assert.AreEqual(1, synced.Count);
            KeyValuePair <string, string> module = synced.EntrySet().Iterator().Next();

            NUnit.Framework.Assert.AreEqual(path, module.Key);
            NUnit.Framework.Assert.AreEqual(url, module.Value);
            generator = SubmoduleWalk.ForIndex(db);
            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.AreEqual(url, generator.GetConfigUrl());
            Repository subModRepository = generator.GetRepository();

            AddRepoToClose(subModRepository);
            StoredConfig submoduleConfig = subModRepository.GetConfig();

            NUnit.Framework.Assert.AreEqual(url, submoduleConfig.GetString(ConfigConstants.CONFIG_REMOTE_SECTION
                                                                           , Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
        }
Esempio n. 15
0
        /// <exception cref="System.IO.IOException"></exception>
        private string AddSubmoduleToIndex()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_323(id, path));
            editor.Commit();
            return(path);
        }
Esempio n. 16
0
        public virtual void RepositoryWithDifferentRevCheckedOutSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_317(id, path));
            editor.Commit();
            string       url    = "git://server/repo.git";
            StoredConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.
                             CONFIG_KEY_URL, url);
            config.Save();
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_PATH, path);
            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_URL, url);
            modulesConfig.Save();
            Repository subRepo = Git.Init().SetBare(false).SetDirectory(new FilePath(db.WorkTree
                                                                                     , path)).Call().GetRepository();

            NUnit.Framework.Assert.IsNotNull(subRepo);
            RefUpdate update = subRepo.UpdateRef(Constants.HEAD, true);

            update.SetNewObjectId(ObjectId.FromString("aaaa0000aaaa0000aaaa0000aaaa0000aaaa0000"
                                                      ));
            update.ForceUpdate();
            SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
            IDictionary <string, SubmoduleStatus> statuses = command.Call();

            NUnit.Framework.Assert.IsNotNull(statuses);
            NUnit.Framework.Assert.AreEqual(1, statuses.Count);
            KeyValuePair <string, SubmoduleStatus> module = statuses.EntrySet().Iterator().Next
                                                                ();

            NUnit.Framework.Assert.IsNotNull(module);
            NUnit.Framework.Assert.AreEqual(path, module.Key);
            SubmoduleStatus status = module.Value;

            NUnit.Framework.Assert.IsNotNull(status);
            NUnit.Framework.Assert.AreEqual(path, status.GetPath());
            NUnit.Framework.Assert.AreEqual(id, status.GetIndexId());
            NUnit.Framework.Assert.AreEqual(update.GetNewObjectId(), status.GetHeadId());
            NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.REV_CHECKED_OUT, status.GetType
                                                ());
        }
Esempio n. 17
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
            {
                DirCacheEditor editor    = dc.Editor();
                TreeWalk       startWalk = new TreeWalk(revWalk.GetObjectReader());
                startWalk.Recursive = true;
                if (!checkoutAllPaths)
                {
                    startWalk.Filter = PathFilterGroup.CreateFromStrings(paths);
                }
                bool checkoutIndex = startCommit == null && startPoint == null;
                if (!checkoutIndex)
                {
                    startWalk.AddTree(revWalk.ParseCommit(GetStartPoint()).Tree);
                }
                else
                {
                    startWalk.AddTree(new DirCacheIterator(dc));
                }
                FilePath     workTree = repo.WorkTree;
                ObjectReader r        = repo.ObjectDatabase.NewReader();
                try
                {
                    while (startWalk.Next())
                    {
                        ObjectId blobId = startWalk.GetObjectId(0);
                        FileMode mode   = startWalk.GetFileMode(0);
                        editor.Add(new _PathEdit_349(this, checkoutIndex, blobId, mode, workTree, r, startWalk
                                                     .PathString));
                    }
                    editor.Commit();
                }
                finally
                {
                    startWalk.Release();
                    r.Release();
                }
            }
            finally
            {
                dc.Unlock();
                revWalk.Release();
            }
            return(this);
        }
Esempio n. 18
0
        public virtual void RepositoryWithRootLevelSubmoduleRelativeRef()
        {
            ObjectId id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string   path   = "sub";
            FilePath dotGit = new FilePath(db.WorkTree, path + FilePath.separatorChar + Constants
                                           .DOT_GIT);

            if (!dotGit.GetParentFile().Exists())
            {
                dotGit.GetParentFile().Mkdirs();
            }
            FilePath modulesGitDir = new FilePath(db.Directory, "modules" + FilePath.separatorChar
                                                  + path);

            new FileWriter(dotGit).Append("gitdir: " + "../" + Constants.DOT_GIT + "/modules/"
                                          + path).Close();
            FileRepositoryBuilder builder = new FileRepositoryBuilder();

            builder.SetWorkTree(new FilePath(db.WorkTree, path));
            builder.Build().Create();
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_203(id, path));
            editor.Commit();
            SubmoduleWalk gen = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(gen.Next());
            NUnit.Framework.Assert.AreEqual(path, gen.GetPath());
            NUnit.Framework.Assert.AreEqual(id, gen.GetObjectId());
            NUnit.Framework.Assert.AreEqual(new FilePath(db.WorkTree, path), gen.GetDirectory
                                                ());
            NUnit.Framework.Assert.IsNull(gen.GetConfigUpdate());
            NUnit.Framework.Assert.IsNull(gen.GetConfigUrl());
            NUnit.Framework.Assert.IsNull(gen.GetModulesPath());
            NUnit.Framework.Assert.IsNull(gen.GetModulesUpdate());
            NUnit.Framework.Assert.IsNull(gen.GetModulesUrl());
            Repository subRepo = gen.GetRepository();

            AddRepoToClose(subRepo);
            NUnit.Framework.Assert.IsNotNull(subRepo);
            NUnit.Framework.Assert.AreEqual(modulesGitDir, subRepo.Directory);
            NUnit.Framework.Assert.AreEqual(new FilePath(db.WorkTree, path), subRepo.WorkTree
                                            );
            NUnit.Framework.Assert.IsFalse(gen.Next());
        }
Esempio n. 19
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);
                }
            }
        }
Esempio n. 20
0
 /// <exception cref="System.IO.IOException"></exception>
 private void BuildIndex(Dictionary <string, string> indexEntries)
 {
     dirCache = new DirCache(db.GetIndexFile(), db.FileSystem);
     if (indexEntries != null)
     {
         NUnit.Framework.Assert.IsTrue(dirCache.Lock());
         DirCacheEditor editor = dirCache.Editor();
         foreach (KeyValuePair <string, string> e in indexEntries.EntrySet())
         {
             WriteTrashFile(e.Key, e.Value);
             ObjectInserter inserter = db.NewObjectInserter();
             ObjectId       id       = inserter.Insert(Constants.OBJ_BLOB, Constants.Encode(e.Value));
             editor.Add(new DirCacheEditor.DeletePath(e.Key));
             editor.Add(new _PathEdit_287(id, e.Key));
         }
         NUnit.Framework.Assert.IsTrue(editor.Commit());
     }
 }
Esempio n. 21
0
        /// <exception cref="System.IO.IOException"></exception>
        private ObjectId BuildTree(Dictionary <string, string> headEntries)
        {
            DirCache lockDirCache = DirCache.NewInCore();
            // assertTrue(lockDirCache.lock());
            DirCacheEditor editor = lockDirCache.Editor();

            if (headEntries != null)
            {
                foreach (KeyValuePair <string, string> e in headEntries.EntrySet())
                {
                    DirCacheCheckoutTest.AddEdit addEdit = new DirCacheCheckoutTest.AddEdit(e.Key, GenSha1
                                                                                                (e.Value), e.Value.Length);
                    editor.Add(addEdit);
                }
            }
            editor.Finish();
            return(lockDirCache.WriteTree(db.NewObjectInserter()));
        }
Esempio n. 22
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);
        }
Esempio n. 23
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ResetIndex(RevCommit commit)
        {
            DirCache dc   = repo.LockDirCache();
            TreeWalk walk = null;

            try
            {
                DirCacheEditor editor = dc.Editor();
                walk = new TreeWalk(repo);
                walk.AddTree(commit.Tree);
                walk.AddTree(new DirCacheIterator(dc));
                walk.Recursive = true;
                while (walk.Next())
                {
                    AbstractTreeIterator cIter = walk.GetTree <AbstractTreeIterator>(0);
                    if (cIter == null)
                    {
                        editor.Add(new DirCacheEditor.DeletePath(walk.PathString));
                        continue;
                    }
                    DirCacheEntry entry = new DirCacheEntry(walk.RawPath);
                    entry.FileMode = cIter.EntryFileMode;
                    entry.SetObjectIdFromRaw(cIter.IdBuffer, cIter.IdOffset);
                    DirCacheIterator dcIter = walk.GetTree <DirCacheIterator>(1);
                    if (dcIter != null && dcIter.IdEqual(cIter))
                    {
                        DirCacheEntry indexEntry = dcIter.GetDirCacheEntry();
                        entry.LastModified = indexEntry.LastModified;
                        entry.SetLength(indexEntry.Length);
                    }
                    editor.Add(new _PathEdit_356(entry, entry));
                }
                editor.Commit();
            }
            finally
            {
                dc.Unlock();
                if (walk != null)
                {
                    walk.Release();
                }
            }
        }
Esempio n. 24
0
        public virtual void RepositoryWithInitializedSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_172(id, path));
            editor.Commit();
            Repository subRepo = Git.Init().SetBare(false).SetDirectory(new FilePath(db.WorkTree
                                                                                     , path)).Call().GetRepository();

            NUnit.Framework.Assert.IsNotNull(subRepo);
            SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
            ICollection <string>   updated = command.Call();

            NUnit.Framework.Assert.IsNotNull(updated);
            NUnit.Framework.Assert.IsTrue(updated.IsEmpty());
        }
Esempio n. 25
0
        public virtual void GeneratorFilteredToOneOfTwoSubmodules()
        {
            ObjectId       id1    = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path1  = "sub1";
            ObjectId       id2    = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1235");
            string         path2  = "sub2";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_271(id1, path1));
            editor.Add(new _PathEdit_278(id2, path2));
            editor.Commit();
            SubmoduleWalk gen = SubmoduleWalk.ForIndex(db);

            gen.SetFilter(PathFilter.Create(path1));
            NUnit.Framework.Assert.IsTrue(gen.Next());
            NUnit.Framework.Assert.AreEqual(path1, gen.GetPath());
            NUnit.Framework.Assert.AreEqual(id1, gen.GetObjectId());
            NUnit.Framework.Assert.IsFalse(gen.Next());
        }
Esempio n. 26
0
        public virtual void RepositoryWithSubmodule()
        {
            WriteTrashFile("file.txt", "content");
            Git git = Git.Wrap(db);

            git.Add().AddFilepattern("file.txt").Call();
            RevCommit      commit = git.Commit().SetMessage("create file").Call();
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_94(commit, path));
            editor.Commit();
            StoredConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.
                             CONFIG_KEY_URL, db.Directory.ToURI().ToString());
            config.Save();
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_PATH, path);
            modulesConfig.Save();
            SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
            ICollection <string>   updated = command.Call();

            NUnit.Framework.Assert.IsNotNull(updated);
            NUnit.Framework.Assert.AreEqual(1, updated.Count);
            NUnit.Framework.Assert.AreEqual(path, updated.Iterator().Next());
            SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(generator.Next());
            Repository subRepo = generator.GetRepository();

            AddRepoToClose(subRepo);
            NUnit.Framework.Assert.IsNotNull(subRepo);
            NUnit.Framework.Assert.AreEqual(commit, subRepo.Resolve(Constants.HEAD));
        }
Esempio n. 27
0
        public virtual void IndexWithGitmodules()
        {
            ObjectId subId      = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string   path       = "sub";
            Config   gitmodules = new Config();

            gitmodules.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                 .CONFIG_KEY_PATH, "sub");
            // Different config in the index should be overridden by the working tree.
            gitmodules.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                 .CONFIG_KEY_URL, "git://example.com/bad");
            RevBlob gitmodulesBlob = testDb.Blob(gitmodules.ToText());

            gitmodules.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                 .CONFIG_KEY_URL, "git://example.com/sub");
            WriteTrashFile(Constants.DOT_GIT_MODULES, gitmodules.ToText());
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_315(subId, path));
            editor.Add(new _PathEdit_322(gitmodulesBlob, Constants.DOT_GIT_MODULES));
            editor.Commit();
            SubmoduleWalk gen = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(gen.Next());
            NUnit.Framework.Assert.AreEqual(path, gen.GetPath());
            NUnit.Framework.Assert.AreEqual(subId, gen.GetObjectId());
            NUnit.Framework.Assert.AreEqual(new FilePath(db.WorkTree, path), gen.GetDirectory
                                                ());
            NUnit.Framework.Assert.IsNull(gen.GetConfigUpdate());
            NUnit.Framework.Assert.IsNull(gen.GetConfigUrl());
            NUnit.Framework.Assert.AreEqual("sub", gen.GetModulesPath());
            NUnit.Framework.Assert.IsNull(gen.GetModulesUpdate());
            NUnit.Framework.Assert.AreEqual("git://example.com/sub", gen.GetModulesUrl());
            NUnit.Framework.Assert.IsNull(gen.GetRepository());
            NUnit.Framework.Assert.IsFalse(gen.Next());
        }
Esempio n. 28
0
        public virtual void RepositoryWithRootLevelSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_108(id, path));
            editor.Commit();
            SubmoduleWalk gen = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(gen.Next());
            NUnit.Framework.Assert.AreEqual(path, gen.GetPath());
            NUnit.Framework.Assert.AreEqual(id, gen.GetObjectId());
            NUnit.Framework.Assert.AreEqual(new FilePath(db.WorkTree, path), gen.GetDirectory
                                                ());
            NUnit.Framework.Assert.IsNull(gen.GetConfigUpdate());
            NUnit.Framework.Assert.IsNull(gen.GetConfigUrl());
            NUnit.Framework.Assert.IsNull(gen.GetModulesPath());
            NUnit.Framework.Assert.IsNull(gen.GetModulesUpdate());
            NUnit.Framework.Assert.IsNull(gen.GetModulesUrl());
            NUnit.Framework.Assert.IsNull(gen.GetRepository());
            NUnit.Framework.Assert.IsFalse(gen.Next());
        }
Esempio n. 29
0
        public virtual void AddExistentSubmodule()
        {
            ObjectId       id     = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
            string         path   = "sub";
            DirCache       cache  = db.LockDirCache();
            DirCacheEditor editor = cache.Editor();

            editor.Add(new _PathEdit_160(id, path));
            editor.Commit();
            SubmoduleAddCommand command = new SubmoduleAddCommand(db);

            command.SetPath(path);
            command.SetURI("git://server/repo.git");
            try
            {
                command.Call().Close();
                NUnit.Framework.Assert.Fail("Exception not thrown");
            }
            catch (JGitInternalException e)
            {
                NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().submoduleExists
                                                                     , path), e.Message);
            }
        }
Esempio n. 30
0
        /// <exception cref="System.IO.IOException"></exception>
        private DirCache CreateTemporaryIndex(ObjectId headId, DirCache index)
        {
            ObjectInserter inserter = null;
            // get DirCacheEditor to modify the index if required
            DirCacheEditor dcEditor = index.Editor();
            // get DirCacheBuilder for newly created in-core index to build a
            // temporary index for this commit
            DirCache        inCoreIndex = DirCache.NewInCore();
            DirCacheBuilder dcBuilder   = inCoreIndex.Builder();

            onlyProcessed = new bool[only.Count];
            bool     emptyCommit = true;
            TreeWalk treeWalk    = new TreeWalk(repo);
            int      dcIdx       = treeWalk.AddTree(new DirCacheIterator(index));
            int      fIdx        = treeWalk.AddTree(new FileTreeIterator(repo));
            int      hIdx        = -1;

            if (headId != null)
            {
                hIdx = treeWalk.AddTree(new RevWalk(repo).ParseTree(headId));
            }
            treeWalk.Recursive = true;
            while (treeWalk.Next())
            {
                string path = treeWalk.PathString;
                // check if current entry's path matches a specified path
                int pos = LookupOnly(path);
                CanonicalTreeParser hTree = null;
                if (hIdx != -1)
                {
                    hTree = treeWalk.GetTree <CanonicalTreeParser>(hIdx);
                }
                if (pos >= 0)
                {
                    // include entry in commit
                    DirCacheIterator dcTree = treeWalk.GetTree <DirCacheIterator>(dcIdx);
                    FileTreeIterator fTree  = treeWalk.GetTree <FileTreeIterator>(fIdx);
                    // check if entry refers to a tracked file
                    bool tracked = dcTree != null || hTree != null;
                    if (!tracked)
                    {
                        break;
                    }
                    if (fTree != null)
                    {
                        // create a new DirCacheEntry with data retrieved from disk
                        DirCacheEntry dcEntry     = new DirCacheEntry(path);
                        long          entryLength = fTree.GetEntryLength();
                        dcEntry.SetLength(entryLength);
                        dcEntry.LastModified = fTree.GetEntryLastModified();
                        dcEntry.FileMode     = fTree.GetIndexFileMode(dcTree);
                        bool objectExists = (dcTree != null && fTree.IdEqual(dcTree)) || (hTree != null &&
                                                                                          fTree.IdEqual(hTree));
                        if (objectExists)
                        {
                            dcEntry.SetObjectId(fTree.EntryObjectId);
                        }
                        else
                        {
                            if (FileMode.GITLINK.Equals(dcEntry.FileMode))
                            {
                                dcEntry.SetObjectId(fTree.EntryObjectId);
                            }
                            else
                            {
                                // insert object
                                if (inserter == null)
                                {
                                    inserter = repo.NewObjectInserter();
                                }
                                long        contentLength = fTree.GetEntryContentLength();
                                InputStream inputStream   = fTree.OpenEntryStream();
                                try
                                {
                                    dcEntry.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, contentLength, inputStream
                                                                        ));
                                }
                                finally
                                {
                                    inputStream.Close();
                                }
                            }
                        }
                        // update index
                        dcEditor.Add(new _PathEdit_375(dcEntry, path));
                        // add to temporary in-core index
                        dcBuilder.Add(dcEntry);
                        if (emptyCommit && (hTree == null || !hTree.IdEqual(fTree) || hTree.EntryRawMode
                                            != fTree.EntryRawMode))
                        {
                            // this is a change
                            emptyCommit = false;
                        }
                    }
                    else
                    {
                        // if no file exists on disk, remove entry from index and
                        // don't add it to temporary in-core index
                        dcEditor.Add(new DirCacheEditor.DeletePath(path));
                        if (emptyCommit && hTree != null)
                        {
                            // this is a change
                            emptyCommit = false;
                        }
                    }
                    // keep track of processed path
                    onlyProcessed[pos] = true;
                }
                else
                {
                    // add entries from HEAD for all other paths
                    if (hTree != null)
                    {
                        // create a new DirCacheEntry with data retrieved from HEAD
                        DirCacheEntry dcEntry = new DirCacheEntry(path);
                        dcEntry.SetObjectId(hTree.EntryObjectId);
                        dcEntry.FileMode = hTree.EntryFileMode;
                        // add to temporary in-core index
                        dcBuilder.Add(dcEntry);
                    }
                }
            }
            // there must be no unprocessed paths left at this point; otherwise an
            // untracked or unknown path has been specified
            for (int i = 0; i < onlyProcessed.Length; i++)
            {
                if (!onlyProcessed[i])
                {
                    throw new JGitInternalException(MessageFormat.Format(JGitText.Get().entryNotFoundByPath
                                                                         , only[i]));
                }
            }
            // there must be at least one change
            if (emptyCommit)
            {
                throw new JGitInternalException(JGitText.Get().emptyCommit);
            }
            // update index
            dcEditor.Commit();
            // finish temporary in-core index used for this commit
            dcBuilder.Finish();
            return(inCoreIndex);
        }