/// <summary>Parse and load the repository specific configuration.</summary> /// <remarks> /// Parse and load the repository specific configuration. /// <p> /// The default implementation reads /// <code>gitDir/config</code> /// , or returns an /// empty configuration if gitDir was not set. /// </remarks> /// <returns>the repository's configuration.</returns> /// <exception cref="System.IO.IOException">the configuration is not available.</exception> protected internal virtual Config LoadConfig() { if (GetGitDir() != null) { // We only want the repository's configuration file, and not // the user file, as these parameters must be unique to this // repository and not inherited from other files. // FilePath path = SafeFS().Resolve(GetGitDir(), Constants.CONFIG); FileBasedConfig cfg = new FileBasedConfig(path, SafeFS()); try { cfg.Load(); } catch (ConfigInvalidException err) { throw new ArgumentException(MessageFormat.Format(JGitText.Get().repositoryConfigFileInvalid , path.GetAbsolutePath(), err.Message)); } return(cfg); } else { return(new Config()); } }
/// <summary> /// Load the config for this walk from /// <code>.gitmodules</code> /// . /// <p> /// Uses the root tree if /// <see cref="SetRootTree(NGit.Treewalk.AbstractTreeIterator)">SetRootTree(NGit.Treewalk.AbstractTreeIterator) /// </see> /// was /// previously called, otherwise uses the working tree. /// <p> /// If no submodule config is found, loads an empty config. /// </summary> /// <returns>this generator</returns> /// <exception cref="System.IO.IOException">if an error occurred, or if the repository is bare /// </exception> /// <exception cref="NGit.Errors.ConfigInvalidException">NGit.Errors.ConfigInvalidException /// </exception> public virtual NGit.Submodule.SubmoduleWalk LoadModulesConfig() { if (rootTree == null) { FilePath modulesFile = new FilePath(repository.WorkTree, Constants.DOT_GIT_MODULES ); FileBasedConfig config = new FileBasedConfig(modulesFile, repository.FileSystem); config.Load(); modulesConfig = config; } else { TreeWalk configWalk = new TreeWalk(repository); try { configWalk.AddTree(rootTree); // The root tree may be part of the submodule walk, so we need to revert // it after this walk. int idx; for (idx = 0; !rootTree.First; idx++) { rootTree.Back(1); } try { configWalk.Recursive = false; PathFilter filter = PathFilter.Create(Constants.DOT_GIT_MODULES); configWalk.Filter = filter; while (configWalk.Next()) { if (filter.IsDone(configWalk)) { modulesConfig = new BlobBasedConfig(null, repository, configWalk.GetObjectId(0)); return(this); } } modulesConfig = new Config(); } finally { if (idx > 0) { rootTree.Next(idx); } } } finally { configWalk.Release(); } } return(this); }
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)); }
/// <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); }