Example usage for org.eclipse.jgit.lib Repository getConfig

List of usage examples for org.eclipse.jgit.lib Repository getConfig

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getConfig.

Prototype

@NonNull
public abstract StoredConfig getConfig();

Source Link

Document

Get the configuration of this repository.

Usage

From source file:org.jboss.tools.openshift.test.util.ResourceMocks.java

License:Open Source License

public static org.eclipse.core.resources.IProject mockGitSharedProject(String name, String gitRemoteUri)
        throws CoreException {
    org.eclipse.core.resources.IProject project = createEclipseProject(name);

    when(project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY)).thenReturn(GitProvider.ID);

    when(project.getWorkingLocation(any()))
            .thenReturn(new Path(ResourcesPlugin.getWorkspace().getRoot().getFullPath().toString()));

    StoredConfig config = mock(StoredConfig.class);
    when(config.getSubsections("remote")).thenReturn(new HashSet<String>(Arrays.asList("origin")));
    when(config.getStringList(any(), any(), any())).thenReturn(new String[] { gitRemoteUri });
    when(config.getStringList("remote", "origin", "url")).thenReturn(new String[] { gitRemoteUri });

    Repository repository = mock(Repository.class);
    when(repository.getConfig()).thenReturn(config);

    RepositoryMapping mapping = mock(RepositoryMapping.class);
    when(mapping.getRepository()).thenReturn(repository);

    GitProjectData data = mock(GitProjectData.class);
    when(data.getRepositoryMapping(project)).thenReturn(mapping);

    GitProvider repositoryProvider = mock(GitProvider.class);
    when(repositoryProvider.getID()).thenReturn(GitProvider.ID);
    when(repositoryProvider.getData()).thenReturn(data);
    when(project.getSessionProperty(TeamPlugin.PROVIDER_PROP_KEY)).thenReturn(repositoryProvider);

    return project;
}

From source file:org.jboss.tools.tycho.sitegenerator.GenerateRepositoryFacadeMojo.java

License:Open Source License

private ModelNode createRevisionObject() throws IOException, FileNotFoundException {
    ModelNode res = new ModelNode();
    File repoRoot = findRepoRoot(this.project.getBasedir());
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository gitRepo = builder.setGitDir(new File(repoRoot, ".git")).readEnvironment() // scan environment GIT_* variables
            .findGitDir() // scan up the file system tree
            .build();/*from   w ww .  j  av  a2  s .co m*/
    Ref head = gitRepo.getRef(Constants.HEAD);
    res.get("HEAD").set(head.getObjectId().getName());
    if (head.getTarget() != null && head.getTarget().getName() != null) {
        res.get("currentBranch").set(head.getTarget().getName());
    }
    ModelNode knownReferences = new ModelNode();
    for (Entry<String, Ref> entry : gitRepo.getAllRefs().entrySet()) {
        if (entry.getKey().startsWith(Constants.R_REMOTES)
                && entry.getValue().getObjectId().getName().equals(head.getObjectId().getName())) {
            ModelNode reference = new ModelNode();
            String remoteName = entry.getKey().substring(Constants.R_REMOTES.length());
            remoteName = remoteName.substring(0, remoteName.indexOf('/'));
            String remoteUrl = gitRepo.getConfig().getString("remote", remoteName, "url");
            String branchName = entry.getKey()
                    .substring(Constants.R_REMOTES.length() + 1 + remoteName.length());
            reference.get("name").set(remoteName);
            reference.get("url").set(remoteUrl);
            reference.get("ref").set(branchName);
            knownReferences.add(reference);
        }
    }
    res.get("knownReferences").set(knownReferences);
    return res;
}

From source file:org.kercoin.magrit.core.utils.GitUtils.java

License:Open Source License

public void addRemote(Repository toConfigure, String name, Repository remoteRepo) throws IOException {
    final String refSpec = String.format(REF_SPEC_PATTERN, name);
    File dest = remoteRepo.getDirectory();
    if (!remoteRepo.isBare()) {
        dest = dest.getParentFile();/*from   w  w  w .ja va2  s .  c o  m*/
    }
    synchronized (toConfigure) {
        toConfigure.getConfig().setString("remote", name, "fetch", refSpec);
        toConfigure.getConfig().setString("remote", name, "url", dest.getAbsolutePath());
        // write down configuration in .git/config
        toConfigure.getConfig().save();
    }
}

From source file:org.kie.eclipse.navigator.view.server.KieRepositoryHandler.java

License:Open Source License

public Object load() {
    if (repository == null) {
        final File repoRoot = new File(PreferencesUtils.getRepoRoot(this));
        final Set<File> gitDirs = new HashSet<File>();
        final IRunnableWithProgress runnable = new IRunnableWithProgress() {

            @Override//from   w w  w.  j a v a  2s.co m
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Searching for Repositories", IProgressMonitor.UNKNOWN);
                try {
                    findGitDirsRecursive(repoRoot, gitDirs, monitor, false);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                if (monitor.isCanceled()) {
                    throw new InterruptedException();
                }
            }
        };
        IProgressService ps = PlatformUI.getWorkbench().getProgressService();
        try {
            ps.busyCursorWhile(runnable);
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }

        for (File dir : gitDirs) {
            if (getName().equals(dir.getParentFile().getName())) {
                try {
                    Repository repository = repositoryCache.lookupRepository(dir);
                    StoredConfig storedConfig = repository.getConfig();
                    Set<String> remotes = storedConfig.getSubsections("remote");
                    for (String remoteName : remotes) {
                        String url = storedConfig.getString("remote", remoteName, "url");
                        System.out.println(repository.getDirectory());
                        System.out.println(url);
                        try {
                            URI u = new URI(url);
                            int port = u.getPort();
                            String host = u.getHost();
                            String scheme = u.getScheme();
                            String path[] = u.getPath().split("/");
                            String repoName = path[path.length - 1];
                            if (name.equals(repoName) && host.equals(getServer().getHost())
                                    && port == getDelegate().getGitPort() && "ssh".equals(scheme)) {
                                this.repository = repository;
                                break;
                            }
                        } catch (URISyntaxException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        if (repository != null)
            // TODO: why doesn't this work?
            repository.getListenerList().addListener(ConfigChangedListener.class, this);

    }
    return repository;
}

From source file:org.kie.eclipse.server.KieRepositoryHandler.java

License:Open Source License

@Override
public Object load() {
    if (repository == null) {
        try {// ww  w  .  j  av  a2  s  . com
            final File repoRoot = new File(PreferencesUtils.getRepoRoot(this));
            final Set<File> gitDirs = new HashSet<File>();
            GitUtils.findGitDirsRecursive(repoRoot, gitDirs, false);
            for (File dir : gitDirs) {
                if (getName().equals(dir.getParentFile().getName())) {
                    Git git = Git.open(dir);
                    Repository repository = git.getRepository();
                    StoredConfig storedConfig = repository.getConfig();
                    Set<String> remotes = storedConfig.getSubsections("remote");
                    for (String remoteName : remotes) {
                        try {
                            String url = storedConfig.getString("remote", remoteName, "url");
                            URI uri = new URI(url);
                            int port = uri.getPort();
                            String host = uri.getHost();
                            String scheme = uri.getScheme();
                            String path[] = uri.getPath().split("/");
                            String repoName = path[path.length - 1];
                            if (name.equals(repoName) && host.equals(getServer().getHost())
                                    && port == getDelegate().getGitPort() && "ssh".equals(scheme)) {
                                this.repository = repository;
                                break;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (git != null) {
                                git.close();
                                git = null;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return repository;
}

From source file:org.kuali.student.git.importer.JGitPushMain.java

License:Educational Community License

public static void push(Repository repo, String remoteName, List<RemoteRefUpdate> refsToUpdate,
        String remoteUserName, String remotePassword) throws IOException, URISyntaxException {

    ArrayList<PushResult> pushResults = new ArrayList<PushResult>(3);

    RemoteConfig config = new RemoteConfig(repo.getConfig(), remoteName);

    final List<Transport> transports;
    transports = Transport.openAll(repo, config, Transport.Operation.PUSH);

    for (final Transport transport : transports) {
        transport.setPushThin(false);//from   w  w  w.  j av a  2  s.com
        transport.setOptionReceivePack(RemoteConfig.DEFAULT_RECEIVE_PACK);
        transport.setDryRun(false);

        configure(transport, remoteUserName, remotePassword);

        try {
            PushResult result = transport.push(new TextProgressMonitor(), refsToUpdate, System.out);
            pushResults.add(result);

        } catch (TransportException e) {
            throw new TransportException(e.getMessage(), e);
        } finally {
            transport.close();
        }
    }

}

From source file:org.nbgit.GitModuleConfig.java

License:Open Source License

public Properties getProperties(File file) {
    Properties props = new Properties();
    Repository repo = Git.getInstance().getRepository(file);
    RepositoryConfig config = repo.getConfig();

    props.setProperty("user.email", config.getAuthorEmail()); // NOI18N
    props.setProperty("user.name", config.getAuthorName()); // NOI18N

    boolean signOff = config.getBoolean("nbgit", "signoff", getSignOffCommits());
    props.setProperty("nbgit.signoff", signOff ? "yes" : "no"); // NOI18N

    boolean stripSpace = config.getBoolean("nbgit", "stripspace", getStripSpace());
    props.setProperty("nbgit.stripspace", stripSpace ? "yes" : "no"); // NOI18N

    return props;
}

From source file:org.nbgit.ui.clone.CloneAction.java

License:Open Source License

public static void doInit(Repository repo, URIish uri, OutputLogger logger)
        throws IOException, URISyntaxException {
    repo.create();/*from  ww  w.jav  a  2s .  c  o  m*/

    repo.getConfig().setBoolean("core", null, "bare", false);
    repo.getConfig().save();

    logger.output("Initialized empty Git repository in " + repo.getWorkDir().getAbsolutePath());
    logger.flushLog();

    // save remote
    final RemoteConfig rc = new RemoteConfig(repo.getConfig(), "origin");
    rc.addURI(uri);
    rc.addFetchRefSpec(new RefSpec().setForceUpdate(true).setSourceDestination(Constants.R_HEADS + "*",
            Constants.R_REMOTES + "origin" + "/*"));
    rc.update(repo.getConfig());
    repo.getConfig().save();
}

From source file:org.nbgit.ui.custom.CustomMenu.java

License:Open Source License

private RepositoryConfig getRepositoryConfig(VCSContext context) {
    File root = GitUtils.getRootFile(context);
    Repository repo = Git.getInstance().getRepository(root);

    return repo == null ? null : repo.getConfig();
}

From source file:org.nbgit.ui.properties.GitProperties.java

License:Open Source License

public void setProperties() {
    RequestProcessor rp = Git.getInstance().getRequestProcessor(root.getAbsolutePath());
    try {/*from   ww w.  j a  v  a2s. co m*/
        support = new GitProgressSupport() {

            protected void perform() {
                Repository repo = Git.getInstance().getRepository(root);
                if (repo == null) {
                    return;
                }
                RepositoryConfig config = repo.getConfig();
                boolean save = false;
                GitPropertiesNode[] gitPropertiesNodes = propTable.getNodes();
                for (int i = 0; i < gitPropertiesNodes.length; i++) {
                    String name = gitPropertiesNodes[i].getName();
                    String value = gitPropertiesNodes[i].getValue().trim();
                    if (value.length() == 0) {
                        continue;
                    }

                    if (name.equals("user.name")) {
                        config.setString("user", null, "name", value);
                        save = true;
                    }

                    if (name.equals("user.email")) {
                        if (!GitModuleConfig.getDefault().isEmailValid(value)) {
                            GitUtils.warningDialog(GitProperties.class, "MSG_WARN_EMAIL_TEXT", // NOI18N
                                    "MSG_WARN_EMAIL_TITLE"); // NOI18N
                            return;
                        }
                        config.setString("user", null, "email", value);
                        save = true;
                    }

                    if (name.equals("nbgit.signoff")) {
                        config.setString("nbgit", null, "signoff", value);
                        save = true;
                    }

                    if (name.equals("nbgit.stripspace")) {
                        config.setString("nbgit", null, "stripspace", value);
                        save = true;
                    }
                }

                try {
                    if (save)
                        config.save();
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        };
        support.start(rp, root.getAbsolutePath(),
                org.openide.util.NbBundle.getMessage(GitProperties.class, "LBL_Properties_Progress")); // NOI18N
    } finally {
        support = null;
    }
}