List of usage examples for org.eclipse.jgit.lib Repository getFS
public FS getFS()
From source file:com.bacoder.scmtools.git.internal.CloneAndProcessCommand.java
License:Apache License
@Override protected void doCheckout(final Repository repository, RevCommit commit) throws MissingObjectException, IncorrectObjectTypeException, IOException, GitAPIException { DirCache dirCache;//ww w . j a va2s.com if (isInMemory()) { dirCache = new InMemoryDirCache(repository.getIndexFile(), repository.getFS()); dirCache.setRepository(repository); } else { dirCache = repository.lockDirCache(); } CloneAndProcessDirCacheCheckout checkout = new CloneAndProcessDirCacheCheckout(repository, dirCache, commit.getTree(), pathPrefix); checkout.setProcessor(processor); checkout.checkout(); submodulesConfig = checkout.getSubmodulesConfig(); if (cloneSubmodules) { cloneSubmodules(repository); } }
From source file:com.google.gerrit.server.git.ReadOnlyRepository.java
License:Apache License
private static BaseRepositoryBuilder<?, ?> builder(Repository r) { checkNotNull(r);//from w w w. j av a 2 s. com BaseRepositoryBuilder<?, ?> builder = new BaseRepositoryBuilder<>().setFS(r.getFS()) .setGitDir(r.getDirectory()); if (!r.isBare()) { builder.setWorkTree(r.getWorkTree()).setIndexFile(r.getIndexFile()); } return builder; }
From source file:com.googlesource.gerrit.plugins.gitiles.FilteredRepository.java
License:Apache License
private static RepositoryBuilder toBuilder(Repository repo) { RepositoryBuilder b = new RepositoryBuilder().setGitDir(repo.getDirectory()).setFS(repo.getFS()); if (!repo.isBare()) { b.setWorkTree(repo.getWorkTree()).setIndexFile(repo.getIndexFile()); }//w ww.j a va 2 s. c o m return b; }
From source file:com.tactfactory.harmony.utils.GitUtils.java
License:Open Source License
/** * Add a submodule to .gitmodules file.//from ww w . j a va 2 s.c om * @param repositoryPath Absolute path of the repository * @param submodulePath Absolute path of the submodule repository * @param submoduleUrl Url of the submodule * @throws IOException */ public static void addSubmodule(String repositoryPath, String submodulePath, String submoduleUrl) throws IOException { // Get main repository RepositoryBuilder repoBuilder = new RepositoryBuilder(); Repository repository = repoBuilder.setWorkTree(new File(repositoryPath)) .setGitDir(new File(repositoryPath + "/.git")).readEnvironment().findGitDir().build(); // Get submodule relative path String path = TactFileUtils.absoluteToRelativePath(submodulePath, repositoryPath); path = path.substring(0, path.length() - 1); // Update .gitmodules file try { FileBasedConfig modulesConfig = new FileBasedConfig( new File(repository.getWorkTree(), Constants.DOT_GIT_MODULES), repository.getFS()); 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, submoduleUrl); modulesConfig.save(); } catch (ConfigInvalidException e) { ConsoleUtils.displayError(e); } repository.close(); }
From source file:net.polydawn.mdm.commands.MdmReleaseInitCommand.java
License:Open Source License
/** * Writes a new submodule block to the parentRepo's .gitmodules file declaring the * release repo, and initializes the local .git/config file to match. * * @param parentRepo/*from w w w . j av a 2 s . co m*/ * @throws IOException * @throws ConfigInvalidException */ void writeParentGitmoduleConfig(Repository parentRepo) throws IOException, ConfigInvalidException { // write gitmodule config for the new submodule StoredConfig gitmodulesCfg = new FileBasedConfig( new File(parentRepo.getWorkTree(), Constants.DOT_GIT_MODULES), parentRepo.getFS()); gitmodulesCfg.load(); gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_PATH, path); gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL, remotePublicUrl); gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, MdmConfigConstants.Module.MODULE_TYPE.toString(), MdmModuleType.RELEASES.toString()); gitmodulesCfg.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_UPDATE, "none"); gitmodulesCfg.save(); // initialize local parent repo config for the submodule MdmModuleRelease module = MdmModuleRelease.load(parentRepo, path, gitmodulesCfg); Plumbing.initLocalConfig(parentRepo, module); parentRepo.getConfig().save(); }
From source file:net.polydawn.mdm.MdmModuleSet.java
License:Open Source License
public MdmModuleSet(Repository repo) throws IOException, ConfigInvalidException { gitmodulesCfg = new FileBasedConfig(new File(repo.getWorkTree(), Constants.DOT_GIT_MODULES), repo.getFS()); gitmodulesCfg.load();/*w ww .jav a 2 s. c o m*/ /* * To maximize similarity with how the `git submodule` command behaves, we treat the SubmoduleWalk as canonical and the content of the .gitmodules file as tertiary. * However, that may change. There's a lot about MdmModule that doesn't work without a .gitmodules entry anyway, so if it's faster to start from that list, we might as well. */ SubmoduleWalk mw = new SubmoduleWalk(repo); mw.setModulesConfig(gitmodulesCfg); SubmoduleWalk generator = SubmoduleWalk.forIndex(repo); while (generator.next()) { try { // get the handle. which we presume to be rather like the path, but git config always uses forward slashes. // (the MdmModule constructor will also enforce this, but here we have to walk config ourselves before we get that far.) String handle = (File.separatorChar != '/') ? generator.getPath().replace(File.separatorChar, '/') : generator.getPath(); // get submodule.[handle].mdm config value from gitmodules config file String type_configured_string = gitmodulesCfg.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, handle, MdmConfigConstants.Module.MODULE_TYPE.toString()); MdmModuleType type_configured = MdmModuleType.fromString(type_configured_string); // if the submodule.[handle].mdm config value was unrecognized or missing, ignore; it's not ours. if (type_configured == null) continue; // load whichever type of mdm module it is switch (type_configured) { case DEPENDENCY: MdmModuleDependency modDep = MdmModuleDependency.load(repo, generator, gitmodulesCfg); dependencyModules.put(modDep.getHandle(), modDep); allModules.put(modDep.getHandle(), modDep); break; case RELEASES: MdmModuleRelease modRel = MdmModuleRelease.load(repo, generator, gitmodulesCfg); releasesModules.put(modRel.getHandle(), modRel); allModules.put(modRel.getHandle(), modRel); break; } } catch (MdmModuleTypeException e) { throw new MajorBug(e); } catch (MdmRepositoryNonexistant e) { throw e; } catch (MdmRepositoryIOException e) { throw e; } } }
From source file:net.polydawn.mdm.Plumbing.java
License:Open Source License
/** * Copy in `url` git config keys from the parent repo config into the submodule config. * This allows for easily having per-project 'insteadof' url rewrites which apply even * when mdm is doing the creation of new repos (which is otherwise a tad hard to get at with git submodules). * @return true if module.getRepo().getConfig() has been modified and should be saved. * @throws ConfigInvalidException//from w ww .jav a 2 s . c o m * @throws IOException */ public static boolean initModuleConfig(Repository repo, MdmModule module) throws IOException, ConfigInvalidException { Config moduleConfig = module.getRepo().getConfig(); // have to explicitly load the parent repo config in isolate, because `repo.getConfig` includes views of the system and user gitconfig, which we won't want to proxy here. FileBasedConfig parentConfig = new FileBasedConfig(new File(repo.getDirectory(), "config"), repo.getFS()); try { parentConfig.load(); } catch (IOException e) { throw new MdmRepositoryIOException(false, "the local git configuration file", e); } // copy any url_insteadof patterns from the parent repo's git config into the module's git config. // note that we do not strip out any additional insteadof's the module may have; if you've added those, it's none of our business (though at this point, we do overwrite). // see org.eclipse.jgit.transport.RemoteConfig for how these actually get used. for (String url : parentConfig.getSubsections(ConfigConstants.CONFIG_KEY_URL)) for (String insteadOf : parentConfig.getStringList(ConfigConstants.CONFIG_KEY_URL, url, "insteadof")) moduleConfig.setString(ConfigConstants.CONFIG_KEY_URL, url, "insteadof", insteadOf); for (String url : parentConfig.getSubsections(ConfigConstants.CONFIG_KEY_URL)) for (String insteadOf : parentConfig.getStringList(ConfigConstants.CONFIG_KEY_URL, url, "pushinsteadof")) moduleConfig.setString(ConfigConstants.CONFIG_KEY_URL, url, "pushinsteadof", insteadOf); return true; }
From source file:org.eclipse.egit.core.GitMoveDeleteHookTest.java
License:Open Source License
@Test public void testDeleteProject() throws Exception { TestProject project = initRepoAboveProjectInsideWs("P/", ""); testUtils.addFileToProject(project.getProject(), "file.txt", "some text"); AddToIndexOperation addToIndexOperation = new AddToIndexOperation( new IResource[] { project.getProject().getFile("file.txt") }); addToIndexOperation.execute(null);// w w w . ja v a 2s .c o m RepositoryMapping mapping = RepositoryMapping.getMapping(project.getProject()); IPath gitDirAbsolutePath = mapping.getGitDirAbsolutePath(); Repository db = new FileRepository(gitDirAbsolutePath.toFile()); DirCache index = DirCache.read(db.getIndexFile(), db.getFS()); assertNotNull(index.getEntry("P/Project-1/file.txt")); db.close(); db = null; project.getProject().delete(true, null); assertNull(RepositoryMapping.getMapping(project.getProject())); // Check that the repo is still there. Being a bit paranoid we look for // a file assertTrue(gitDirAbsolutePath.toString(), gitDirAbsolutePath.append("HEAD").toFile().exists()); db = new FileRepository(gitDirAbsolutePath.toFile()); index = DirCache.read(db.getIndexFile(), db.getFS()); // FIXME: Shouldn't we unstage deleted projects? assertNotNull(index.getEntry("P/Project-1/file.txt")); db.close(); }
From source file:org.eclipse.egit.core.op.ConfigureGerritAfterCloneTask.java
License:Open Source License
/** * Try to use Gerrit's "Get Version" REST API endpoint [1] to detect if the * repository is hosted on a Gerrit server. * <p/>//from w w w .j a va 2 s .co m * [1] <a href= * "https://gerrit-documentation.storage.googleapis.com/Documentation/2.11 * /rest-api-config.html#get-version">Gerrit 2.11 Get Version REST * endpoint</a> * * @param repo * the repository to be configured * * @return {@code true} if the repository is hosted on a Gerrit server * @throws IOException * @throws MalformedURLException * @throws URISyntaxException */ private boolean isGerrit(Repository repo) throws MalformedURLException, IOException, URISyntaxException { URIish u = new URIish(uri); final String s = u.getScheme(); final String host = u.getHost(); final String path = u.getPath(); // shortcut for Eclipse Gerrit server if (host != null && host.equals(GIT_ECLIPSE_ORG)) { if (HTTPS.equals(s) && (u.getPort() == 443 || u.getPort() == -1) && path != null && path.startsWith(GERRIT_CONTEXT_ROOT)) { return true; } else if (SSH.equals(s) && u.getPort() == GERRIT_SSHD_DEFAULT_PORT) { return true; } } if (path != null && (HTTP.equals(s) || HTTPS.equals(s))) { String baseURL = u.setPath("/").toString(); //$NON-NLS-1$ baseURL = baseURL.substring(0, baseURL.length() - 1); String tmpPath = ""; //$NON-NLS-1$ int slash = 1; while (true) { HttpURLConnection httpConnection = null; try { httpConnection = (HttpURLConnection) new URL( baseURL + tmpPath + GERRIT_CONFIG_SERVER_VERSION_API).openConnection(); NetUtil.setSslVerification(repo, httpConnection); httpConnection.setRequestMethod("GET"); //$NON-NLS-1$ httpConnection.setReadTimeout(1000 * timeout); int responseCode = httpConnection.getResponseCode(); switch (responseCode) { case HttpURLConnection.HTTP_OK: try (InputStream in = httpConnection.getInputStream()) { String response = readFully(in, "UTF-8"); //$NON-NLS-1$ return response.startsWith(GERRIT_XSSI_MAGIC_STRING); } case HttpURLConnection.HTTP_NOT_FOUND: if (slash > path.length()) { return false; } slash = path.indexOf('/', slash); if (slash == -1) { // try the entire path slash = path.length(); } tmpPath = path.substring(0, slash); slash++; break; default: return false; } } catch (IOException e) { return false; } finally { if (httpConnection != null) { httpConnection.disconnect(); } } } } else if (SSH.equals(s)) { if (u.getPort() < 0 || u.getUser() == null || credentialsProvider == null) { return false; } URIish sshUri = u.setPath(""); //$NON-NLS-1$ try { String result = runSshCommand(sshUri, credentialsProvider, repo.getFS(), GERRIT_SSHD_VERSION_API); return result != null && GERRIT_SSHD_REPLY.matcher(result).matches(); } catch (IOException e) { // Something went wrong with the connection or with the command // execution. Maybe the server didn't recognize the command. Do // the safe thing and claim it wasn't a Gerrit. In the worst // case, the user may have to do the Gerrit config setup via // the ConfigureGerritWizard. return false; } } return false; }
From source file:org.eclipse.egit.ui.internal.repository.RepositoryPropertyPage.java
License:Open Source License
protected Control createContents(Composite parent) { Composite displayArea = new Composite(parent, SWT.NONE); GridLayoutFactory.fillDefaults().applyTo(displayArea); GridDataFactory.fillDefaults().applyTo(displayArea); Repository repo = (Repository) getElement().getAdapter(Repository.class); if (repo == null) return displayArea; StoredConfig config = repo.getConfig(); if (config instanceof FileBasedConfig) { File configFile = ((FileBasedConfig) config).getFile(); config = new FileBasedConfig(configFile, repo.getFS()); }/*from w ww . java 2 s .c om*/ editor = new ConfigurationEditorComponent(displayArea, config, true, false) { @Override protected void setErrorMessage(String message) { RepositoryPropertyPage.this.setErrorMessage(message); } }; editor.createContents(); return displayArea; }