public virtual void TestFileRepository_ChangeEventsOnlyOnSave()
        {
            ConfigChangedEvent[] events = new ConfigChangedEvent[1];
            db.Listeners.AddConfigChangedListener(new _ConfigChangedListener_58(events));
            FileBasedConfig config = ((FileBasedConfig)db.GetConfig());

            NUnit.Framework.Assert.IsNull(events[0]);
            // set a value to some arbitrary key
            config.SetString("test", "section", "event", "value");
            // no changes until we save
            NUnit.Framework.Assert.IsNull(events[0]);
            config.Save();
            NUnit.Framework.Assert.IsNotNull(events[0]);
            // correct repository?
            NUnit.Framework.Assert.AreEqual(events[0].GetRepository(), db);
            // reset for the next test
            events[0] = null;
            // unset the value we have just set above
            config.Unset("test", "section", "event");
            // no changes until we save
            NUnit.Framework.Assert.IsNull(events[0]);
            config.Save();
            NUnit.Framework.Assert.IsNotNull(events[0]);
            // correct repository?
            NUnit.Framework.Assert.AreEqual(events[0].GetRepository(), db);
        }
示例#2
0
        public virtual void RepositoryWithUninitializedModule()
        {
            string        path      = AddSubmoduleToIndex();
            SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUrl());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUpdate());
            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();
            SubmoduleInitCommand command = new SubmoduleInitCommand(db);
            ICollection <string> modules = command.Call();

            NUnit.Framework.Assert.IsNotNull(modules);
            NUnit.Framework.Assert.AreEqual(1, modules.Count);
            NUnit.Framework.Assert.AreEqual(path, modules.Iterator().Next());
            generator = SubmoduleWalk.ForIndex(db);
            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.AreEqual(url, generator.GetConfigUrl());
            NUnit.Framework.Assert.AreEqual(update, generator.GetConfigUpdate());
        }
示例#3
0
        public virtual void ResolveInvalidParentUrl()
        {
            string          path   = AddSubmoduleToIndex();
            string          @base  = "no_slash";
            FileBasedConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetString(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME
                             , ConfigConstants.CONFIG_KEY_URL, @base);
            config.Save();
            SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUrl());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUpdate());
            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 = "../sub.git";

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                    .CONFIG_KEY_URL, url);
            modulesConfig.Save();
            try
            {
                new SubmoduleInitCommand(db).Call();
                NUnit.Framework.Assert.Fail("Exception not thrown");
            }
            catch (JGitInternalException e)
            {
                NUnit.Framework.Assert.IsTrue(e.InnerException is IOException);
            }
        }
        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
                                                ());
        }
        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 static FileBasedConfig  section_Set_Value_String(this FileBasedConfig config, string section, string name, string value)
 {
     if (config.notNull())
     {
         config.SetString(section, null, name, value);
         config.Save();
     }
     return(config);
 }
示例#7
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));
        }
示例#8
0
        public virtual void TestlogAllRefUpdates()
        {
            long commitTime = 1154236443000L;
            int  tz         = -4 * 60;

            // check that there are no entries in the reflog and turn off writing
            // reflogs
            NUnit.Framework.Assert.AreEqual(0, db.GetReflogReader(Constants.HEAD).GetReverseEntries
                                                ().Count);
            FileBasedConfig cfg = ((FileBasedConfig)db.GetConfig());

            cfg.SetBoolean("core", null, "logallrefupdates", false);
            cfg.Save();
            // do one commit and check that reflog size is 0: no reflogs should be
            // written
            Commit("A Commit\n", new PersonIdent(author, commitTime, tz), new PersonIdent(committer
                                                                                          , commitTime, tz));
            commitTime += 60 * 1000;
            NUnit.Framework.Assert.IsTrue(db.GetReflogReader(Constants.HEAD).GetReverseEntries
                                              ().Count == 0, "Reflog for HEAD still contain no entry");
            // set the logAllRefUpdates parameter to true and check it
            cfg.SetBoolean("core", null, "logallrefupdates", true);
            cfg.Save();
            NUnit.Framework.Assert.IsTrue(cfg.Get(CoreConfig.KEY).IsLogAllRefUpdates());
            // do one commit and check that reflog size is increased to 1
            Commit("A Commit\n", new PersonIdent(author, commitTime, tz), new PersonIdent(committer
                                                                                          , commitTime, tz));
            commitTime += 60 * 1000;
            NUnit.Framework.Assert.IsTrue(db.GetReflogReader(Constants.HEAD).GetReverseEntries
                                              ().Count == 1, "Reflog for HEAD should contain one entry");
            // set the logAllRefUpdates parameter to false and check it
            cfg.SetBoolean("core", null, "logallrefupdates", false);
            cfg.Save();
            NUnit.Framework.Assert.IsFalse(cfg.Get(CoreConfig.KEY).IsLogAllRefUpdates());
            // do one commit and check that reflog size is 2
            Commit("A Commit\n", new PersonIdent(author, commitTime, tz), new PersonIdent(committer
                                                                                          , commitTime, tz));
            NUnit.Framework.Assert.IsTrue(db.GetReflogReader(Constants.HEAD).GetReverseEntries
                                              ().Count == 2, "Reflog for HEAD should contain two entries");
        }
        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
                                                ());
        }
示例#10
0
        public virtual void TestCloneWithAutoSetupRebase()
        {
            FilePath     directory = CreateTempDirectory("testCloneRepository1");
            CloneCommand command   = Git.CloneRepository();

            command.SetDirectory(directory);
            command.SetURI("file://" + git.GetRepository().WorkTree.GetPath());
            Git git2 = command.Call();

            AddRepoToClose(git2.GetRepository());
            NUnit.Framework.Assert.IsFalse(git2.GetRepository().GetConfig().GetBoolean(ConfigConstants
                                                                                       .CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REBASE, false));
            FileBasedConfig userConfig = SystemReader.GetInstance().OpenUserConfig(null, git.
                                                                                   GetRepository().FileSystem);

            userConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants
                                 .CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_ALWAYS);
            userConfig.Save();
            directory = CreateTempDirectory("testCloneRepository2");
            command   = Git.CloneRepository();
            command.SetDirectory(directory);
            command.SetURI("file://" + git.GetRepository().WorkTree.GetPath());
            git2 = command.Call();
            AddRepoToClose(git2.GetRepository());
            NUnit.Framework.Assert.IsTrue(git2.GetRepository().GetConfig().GetBoolean(ConfigConstants
                                                                                      .CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REBASE, false));
            userConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, null, ConfigConstants
                                 .CONFIG_KEY_AUTOSETUPREBASE, ConfigConstants.CONFIG_KEY_REMOTE);
            userConfig.Save();
            directory = CreateTempDirectory("testCloneRepository2");
            command   = Git.CloneRepository();
            command.SetDirectory(directory);
            command.SetURI("file://" + git.GetRepository().WorkTree.GetPath());
            git2 = command.Call();
            AddRepoToClose(git2.GetRepository());
            NUnit.Framework.Assert.IsTrue(git2.GetRepository().GetConfig().GetBoolean(ConfigConstants
                                                                                      .CONFIG_BRANCH_SECTION, "test", ConfigConstants.CONFIG_KEY_REBASE, false));
        }
示例#11
0
        public virtual void ResolveWorkingDirectoryRelativeUrl()
        {
            string path  = AddSubmoduleToIndex();
            string @base = db.WorkTree.GetAbsolutePath();

            if (FilePath.separatorChar == '\\')
            {
                @base = @base.Replace('\\', '/');
            }
            FileBasedConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetString(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME
                             , ConfigConstants.CONFIG_KEY_URL, null);
            config.Save();
            SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUrl());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUpdate());
            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 = "./sub.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();
            SubmoduleInitCommand command = new SubmoduleInitCommand(db);
            ICollection <string> modules = command.Call();

            NUnit.Framework.Assert.IsNotNull(modules);
            NUnit.Framework.Assert.AreEqual(1, modules.Count);
            NUnit.Framework.Assert.AreEqual(path, modules.Iterator().Next());
            generator = SubmoduleWalk.ForIndex(db);
            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.AreEqual(@base + "/sub.git", generator.GetConfigUrl());
            NUnit.Framework.Assert.AreEqual(update, generator.GetConfigUpdate());
        }
示例#12
0
        public virtual void ResolveTwoLevelHigherRelativeUrl()
        {
            string          path   = AddSubmoduleToIndex();
            string          @base  = "git://server/repo.git";
            FileBasedConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetString(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME
                             , ConfigConstants.CONFIG_KEY_URL, @base);
            config.Save();
            SubmoduleWalk generator = SubmoduleWalk.ForIndex(db);

            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUrl());
            NUnit.Framework.Assert.IsNull(generator.GetConfigUpdate());
            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 = "../../server2/sub.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();
            SubmoduleInitCommand command = new SubmoduleInitCommand(db);
            ICollection <string> modules = command.Call();

            NUnit.Framework.Assert.IsNotNull(modules);
            NUnit.Framework.Assert.AreEqual(1, modules.Count);
            NUnit.Framework.Assert.AreEqual(path, modules.Iterator().Next());
            generator = SubmoduleWalk.ForIndex(db);
            NUnit.Framework.Assert.IsTrue(generator.Next());
            NUnit.Framework.Assert.AreEqual("git://server2/sub.git", generator.GetConfigUrl()
                                            );
            NUnit.Framework.Assert.AreEqual(update, generator.GetConfigUpdate());
        }
示例#13
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));
        }
示例#14
0
        public virtual void AddSubmoduleWithExistingSubmoduleDefined()
        {
            string          path1         = "sub1";
            string          url1          = "git://server/repo1.git";
            string          path2         = "sub2";
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), db.FileSystem);

            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, ConfigConstants
                                    .CONFIG_KEY_PATH, path1);
            modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, ConfigConstants
                                    .CONFIG_KEY_URL, url1);
            modulesConfig.Save();
            Git git = new Git(db);

            WriteTrashFile("file.txt", "content");
            git.Add().AddFilepattern("file.txt").Call();
            NUnit.Framework.Assert.IsNotNull(git.Commit().SetMessage("create file").Call());
            SubmoduleAddCommand command = new SubmoduleAddCommand(db);

            command.SetPath(path2);
            string url2 = db.Directory.ToURI().ToString();

            command.SetURI(url2);
            Repository r = command.Call();

            NUnit.Framework.Assert.IsNotNull(r);
            AddRepoToClose(r);
            modulesConfig.Load();
            NUnit.Framework.Assert.AreEqual(path1, modulesConfig.GetString(ConfigConstants.CONFIG_SUBMODULE_SECTION
                                                                           , path1, ConfigConstants.CONFIG_KEY_PATH));
            NUnit.Framework.Assert.AreEqual(url1, modulesConfig.GetString(ConfigConstants.CONFIG_SUBMODULE_SECTION
                                                                          , path1, ConfigConstants.CONFIG_KEY_URL));
            NUnit.Framework.Assert.AreEqual(path2, modulesConfig.GetString(ConfigConstants.CONFIG_SUBMODULE_SECTION
                                                                           , path2, ConfigConstants.CONFIG_KEY_PATH));
            NUnit.Framework.Assert.AreEqual(url2, modulesConfig.GetString(ConfigConstants.CONFIG_SUBMODULE_SECTION
                                                                          , path2, ConfigConstants.CONFIG_KEY_URL));
        }
示例#15
0
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override Repository Call()
        {
            CheckCallable();
            if (path == null || path.Length == 0)
            {
                throw new ArgumentException(JGitText.Get().pathNotConfigured);
            }
            if (uri == null || uri.Length == 0)
            {
                throw new ArgumentException(JGitText.Get().uriNotConfigured);
            }
            try
            {
                if (SubmoduleExists())
                {
                    throw new JGitInternalException(MessageFormat.Format(JGitText.Get().submoduleExists
                                                                         , path));
                }
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            string resolvedUri;

            try
            {
                resolvedUri = SubmoduleWalk.GetSubmoduleRemoteUrl(repo, uri);
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            // Clone submodule repository
            FilePath     moduleDirectory = SubmoduleWalk.GetSubmoduleDirectory(repo, path);
            CloneCommand clone           = Git.CloneRepository();

            Configure(clone);
            clone.SetDirectory(moduleDirectory);
            clone.SetURI(resolvedUri);
            if (monitor != null)
            {
                clone.SetProgressMonitor(monitor);
            }
            Repository subRepo = clone.Call().GetRepository();
            // Save submodule URL to parent repository's config
            StoredConfig config = repo.GetConfig();

            config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.
                             CONFIG_KEY_URL, resolvedUri);
            try
            {
                config.Save();
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            // Save path and URL to parent repository's .gitmodules file
            FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(repo.WorkTree, Constants
                                                                             .DOT_GIT_MODULES), repo.FileSystem);

            try
            {
                modulesConfig.Load();
                modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                        .CONFIG_KEY_PATH, path);
                modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants
                                        .CONFIG_KEY_URL, uri);
                modulesConfig.Save();
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            catch (ConfigInvalidException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            AddCommand add = new AddCommand(repo);

            // Add .gitmodules file to parent repository's index
            add.AddFilepattern(Constants.DOT_GIT_MODULES);
            // Add submodule directory to parent repository's index
            add.AddFilepattern(path);
            try
            {
                add.Call();
            }
            catch (NoFilepatternException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            return(subRepo);
        }