public static string GetUpstreamSource(NGit.Repository repo, string branch) { StoredConfig config = repo.GetConfig(); string remote = config.GetString("branch", branch, "remote"); string rbranch = config.GetString("branch", branch, "merge"); if (string.IsNullOrEmpty(rbranch)) { return(null); } if (rbranch.StartsWith(Constants.R_HEADS, System.StringComparison.Ordinal)) { rbranch = rbranch.Substring(Constants.R_HEADS.Length); } else if (rbranch.StartsWith(Constants.R_TAGS, System.StringComparison.Ordinal)) { rbranch = rbranch.Substring(Constants.R_TAGS.Length); } if (!string.IsNullOrEmpty(remote) && remote != ".") { return(remote + "/" + rbranch); } else { return(rbranch); } }
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)); }
/// <exception cref="NGit.Api.Errors.RefNotFoundException"> /// if the old branch can not be found (branch with provided old /// name does not exist or old name resolves to a tag) /// </exception> /// <exception cref="NGit.Api.Errors.InvalidRefNameException"> /// if the provided new name is <code>null</code> or otherwise /// invalid /// </exception> /// <exception cref="NGit.Api.Errors.RefAlreadyExistsException">if a branch with the new name already exists /// </exception> /// <exception cref="NGit.Api.Errors.DetachedHeadException"> /// if rename is tried without specifying the old name and HEAD /// is detached /// </exception> public override Ref Call() { CheckCallable(); if (newName == null) { throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid , "<null>")); } try { string fullOldName; string fullNewName; if (repo.GetRef(newName) != null) { throw new RefAlreadyExistsException(MessageFormat.Format(JGitText.Get().refAlreadExists , newName)); } if (oldName != null) { Ref @ref = repo.GetRef(oldName); if (@ref == null) { throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved , oldName)); } if (@ref.GetName().StartsWith(Constants.R_TAGS)) { throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().renameBranchFailedBecauseTag , oldName)); } fullOldName = @ref.GetName(); } else { fullOldName = repo.GetFullBranch(); if (ObjectId.IsId(fullOldName)) { throw new DetachedHeadException(); } } if (fullOldName.StartsWith(Constants.R_REMOTES)) { fullNewName = Constants.R_REMOTES + newName; } else { fullNewName = Constants.R_HEADS + newName; } if (!Repository.IsValidRefName(fullNewName)) { throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().branchNameInvalid , fullNewName)); } RefRename rename = repo.RenameRef(fullOldName, fullNewName); RefUpdate.Result renameResult = rename.Rename(); SetCallable(false); bool ok = RefUpdate.Result.RENAMED == renameResult; if (ok) { if (fullNewName.StartsWith(Constants.R_HEADS)) { // move the upstream configuration over to the new branch string shortOldName = Sharpen.Runtime.Substring(fullOldName, Constants.R_HEADS.Length ); StoredConfig repoConfig = repo.GetConfig(); string oldRemote = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName , ConfigConstants.CONFIG_KEY_REMOTE); if (oldRemote != null) { repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants .CONFIG_KEY_REMOTE, oldRemote); } string oldMerge = repoConfig.GetString(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName , ConfigConstants.CONFIG_KEY_MERGE); if (oldMerge != null) { repoConfig.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, newName, ConfigConstants .CONFIG_KEY_MERGE, oldMerge); } repoConfig.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName); repoConfig.Save(); } } else { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().renameBranchUnexpectedResult , renameResult.ToString())); } Ref resultRef = repo.GetRef(newName); if (resultRef == null) { throw new JGitInternalException(JGitText.Get().renameBranchFailedUnknownReason); } return(resultRef); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } }
/// <summary>Get the configured remote URL for current entry.</summary> /// <remarks> /// Get the configured remote URL for current entry. This will be the value /// from the repository's config. /// </remarks> /// <returns>configured URL</returns> /// <exception cref="NGit.Errors.ConfigInvalidException">NGit.Errors.ConfigInvalidException /// </exception> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public virtual string GetConfigUrl() { return(repoConfig.GetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants .CONFIG_KEY_URL)); }