Example usage for org.eclipse.jgit.lib UserConfig KEY

List of usage examples for org.eclipse.jgit.lib UserConfig KEY

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib UserConfig KEY.

Prototype

Config.SectionParser KEY

To view the source code for org.eclipse.jgit.lib UserConfig KEY.

Click Source Link

Document

Key for Config#get(SectionParser) .

Usage

From source file:com.google.gerrit.server.GerritPersonIdentProvider.java

License:Apache License

@Inject
public GerritPersonIdentProvider(@GerritServerConfig final Config cfg) {
    String name = cfg.getString("user", null, "name");
    if (name == null) {
        name = "Gerrit Code Review";
    }/*from w  w  w. j  a  v  a  2s .  c o  m*/
    this.name = name;
    email = cfg.get(UserConfig.KEY).getCommitterEmail();
}

From source file:me.cmoz.gradle.snapshot.GitSCMCommand.java

License:Apache License

@Override
@SneakyThrows(IOException.class)
public Commit getLatestCommit(@NonNull final String dateFormat) {
    if (repoDir == null) {
        throw new IllegalStateException("'.git' folder could not be found.");
    }//w  ww . ja v  a  2 s  .co m

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();

    final Repository repo = builder.setGitDir(repoDir).readEnvironment().build();
    final StoredConfig conf = repo.getConfig();

    int abbrev = Commit.ABBREV_LENGTH;
    if (conf != null) {
        abbrev = conf.getInt("core", "abbrev", abbrev);
    }

    final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

    final Ref HEAD = repo.getRef(Constants.HEAD);
    if (HEAD.getObjectId() == null) {
        throw new RuntimeException("Could not find any commits from HEAD ref.");
    }

    final RevWalk revWalk = new RevWalk(repo);
    if (HEAD.getObjectId() == null) {
        throw new RuntimeException("Could not find any commits from HEAD ref.");
    }
    final RevCommit revCommit = revWalk.parseCommit(HEAD.getObjectId());
    revWalk.markStart(revCommit);

    try {
        // git commit time in sec and java datetime is in ms
        final Date commitTime = new Date(revCommit.getCommitTime() * 1000L);
        final PersonIdent ident = revCommit.getAuthorIdent();
        final UserConfig userConf = conf.get(UserConfig.KEY);

        return Commit.builder().buildTime(sdf.format(new Date())).buildAuthorName(userConf.getAuthorName())
                .buildAuthorEmail(userConf.getAuthorEmail()).branchName(repo.getBranch())
                .commitId(revCommit.getName()).commitTime(sdf.format(commitTime))
                .commitUserName(ident.getName()).commitUserEmail(ident.getEmailAddress())
                .commitMessage(revCommit.getFullMessage().trim()).build();
    } finally {
        revWalk.dispose();
        repo.close();
    }
}

From source file:org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand.java

License:Apache License

private UserInfo getCommitter(ScmProviderRepository repo, Git git) {
    boolean forceMvnUser = git.getRepository().getConfig().getBoolean(GIT_MAVEN_SECTION, GIT_FORCE, false);

    // git config
    UserConfig user = git.getRepository().getConfig().get(UserConfig.KEY);
    String committerName = null;//from  www .j a  v a2 s.  c o  m
    if (!forceMvnUser && !user.isCommitterNameImplicit()) {
        committerName = user.getCommitterName();
    }

    // mvn parameter
    if (StringUtils.isBlank(committerName)) {
        committerName = repo.getUser();
    }

    // git default
    if (StringUtils.isBlank(committerName)) {
        committerName = user.getCommitterName();
    }

    // git config
    String committerMail = null;
    if (!user.isCommitterEmailImplicit()) {
        committerMail = user.getCommitterEmail();
    }

    if (StringUtils.isBlank(committerMail)) {
        String defaultDomain = git.getRepository().getConfig().getString(GIT_MAVEN_SECTION, null,
                GIT_MAILDOMAIN);
        defaultDomain = StringUtils.isNotBlank(defaultDomain) ? defaultDomain : getHostname();

        // mvn parameter (constructed with username) or git default
        committerMail = StringUtils.isNotBlank(repo.getUser()) ? repo.getUser() + "@" + defaultDomain
                : user.getCommitterEmail();
    }

    return new UserInfo(committerName, committerMail);
}

From source file:org.apache.maven.scm.provider.git.jgit.command.checkin.JGitCheckInCommand.java

License:Apache License

private UserInfo getAuthor(ScmProviderRepository repo, Git git) {
    boolean forceMvnUser = git.getRepository().getConfig().getBoolean(GIT_MAVEN_SECTION, GIT_FORCE, false);

    // git config
    UserConfig user = git.getRepository().getConfig().get(UserConfig.KEY);
    String authorName = null;//from  w w w.  ja v a  2  s.  co m
    if (!forceMvnUser && !user.isAuthorNameImplicit()) {
        authorName = user.getAuthorName();
    }

    // mvn parameter
    if (StringUtils.isBlank(authorName)) {
        authorName = repo.getUser();
    }

    // git default
    if (StringUtils.isBlank(authorName)) {
        authorName = user.getAuthorName();
    }

    // git config
    String authorMail = null;
    if (!user.isAuthorEmailImplicit()) {
        authorMail = user.getAuthorEmail();
    }

    if (StringUtils.isBlank(authorMail)) {
        String defaultDomain = git.getRepository().getConfig().getString(GIT_MAVEN_SECTION, null,
                GIT_MAILDOMAIN);
        defaultDomain = StringUtils.isNotBlank(defaultDomain) ? defaultDomain : getHostname();

        // mvn parameter (constructed with username) or git default
        authorMail = StringUtils.isNotBlank(repo.getUser()) ? repo.getUser() + "@" + defaultDomain
                : user.getAuthorEmail();
    }

    return new UserInfo(authorName, authorMail);
}

From source file:org.eclipse.egit.ui.internal.actions.CommitActionHandler.java

License:Open Source License

public Object execute(final ExecutionEvent event) throws ExecutionException {
    // let's see if there is any dirty editor around and
    // ask the user if they want to save or abort
    if (!PlatformUI.getWorkbench().saveAllEditors(true)) {
        return null;
    }//from  w w w . ja  va 2s .  co m

    resetState();
    final IProject[] projects = getProjectsInRepositoryOfSelectedResources(event);
    try {
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    buildIndexHeadDiffList(projects, monitor);
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        Activator.handleError(UIText.CommitAction_errorComputingDiffs, e.getCause(), true);
        return null;
    } catch (InterruptedException e) {
        return null;
    }

    Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources(event));
    Repository repository = null;
    Repository mergeRepository = null;
    amendAllowed = repos.length == 1;
    boolean isMergedResolved = false;
    for (Repository repo : repos) {
        repository = repo;
        RepositoryState state = repo.getRepositoryState();
        if (!state.canCommit()) {
            MessageDialog.openError(getShell(event), UIText.CommitAction_cannotCommit,
                    NLS.bind(UIText.CommitAction_repositoryState, state.getDescription()));
            return null;
        } else if (state.equals(RepositoryState.MERGING_RESOLVED)) {
            isMergedResolved = true;
            mergeRepository = repo;
        }
    }

    loadPreviousCommit(event);
    if (files.isEmpty()) {
        if (amendAllowed && previousCommit != null) {
            boolean result = MessageDialog.openQuestion(getShell(event), UIText.CommitAction_noFilesToCommit,
                    UIText.CommitAction_amendCommit);
            if (!result)
                return null;
            amending = true;
        } else {
            MessageDialog.openWarning(getShell(event), UIText.CommitAction_noFilesToCommit,
                    UIText.CommitAction_amendNotPossible);
            return null;
        }
    }

    String author = null;
    String committer = null;
    if (repository != null) {
        final UserConfig config = repository.getConfig().get(UserConfig.KEY);
        author = config.getAuthorName();
        final String authorEmail = config.getAuthorEmail();
        author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$

        committer = config.getCommitterName();
        final String committerEmail = config.getCommitterEmail();
        committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    CommitDialog commitDialog = new CommitDialog(getShell(event));
    commitDialog.setAmending(amending);
    commitDialog.setAmendAllowed(amendAllowed);
    commitDialog.setFileList(files, indexDiffs);
    commitDialog.setPreselectedFiles(getSelectedFiles(event));
    commitDialog.setAuthor(author);
    commitDialog.setCommitter(committer);
    commitDialog.setAllowToChangeSelection(!isMergedResolved);

    if (previousCommit != null) {
        commitDialog.setPreviousCommitMessage(previousCommit.getFullMessage());
        PersonIdent previousAuthor = previousCommit.getAuthorIdent();
        commitDialog
                .setPreviousAuthor(previousAuthor.getName() + " <" + previousAuthor.getEmailAddress() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (isMergedResolved) {
        commitDialog.setCommitMessage(getMergeResolveMessage(mergeRepository, event));
    }

    if (commitDialog.open() != IDialogConstants.OK_ID)
        return null;

    final CommitOperation commitOperation = new CommitOperation(commitDialog.getSelectedFiles(), notIndexed,
            notTracked, commitDialog.getAuthor(), commitDialog.getCommitter(), commitDialog.getCommitMessage());
    if (commitDialog.isAmending()) {
        commitOperation.setAmending(true);
        commitOperation.setPreviousCommit(previousCommit);
        commitOperation.setRepos(repos);
    }
    commitOperation.setComputeChangeId(commitDialog.getCreateChangeId());
    commitOperation.setCommitAll(isMergedResolved);
    if (isMergedResolved)
        commitOperation.setRepos(repos);
    String jobname = UIText.CommitAction_CommittingChanges;
    Job job = new Job(jobname) {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                commitOperation.execute(monitor);

                for (IProject proj : getProjectsForSelectedResources(event)) {
                    RepositoryMapping.getMapping(proj).fireRepositoryChanged();
                }
            } catch (CoreException e) {
                return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e);
            } catch (ExecutionException e) {
                return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e);
            } finally {
                GitLightweightDecorator.refresh();
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (family.equals(JobFamilies.COMMIT))
                return true;
            return super.belongsTo(family);
        }

    };
    job.setUser(true);
    job.schedule();
    return null;
}

From source file:org.eclipse.egit.ui.internal.commit.CommitHelper.java

License:Open Source License

private void calculateCommitInfo() {
    Repository mergeRepository = null;//from   w w  w .j a  v a 2  s.  co m
    isMergedResolved = false;
    isCherryPickResolved = false;
    RepositoryState state = repository.getRepositoryState();
    canCommit = state.canCommit();
    if (!canCommit) {
        cannotCommitMessage = NLS.bind(UIText.CommitAction_repositoryState, state.getDescription());
        return;
    }
    if (state.equals(RepositoryState.MERGING_RESOLVED)) {
        isMergedResolved = true;
        mergeRepository = repository;
    } else if (state.equals(RepositoryState.CHERRY_PICKING_RESOLVED)) {
        isCherryPickResolved = true;
        mergeRepository = repository;
    }
    previousCommit = getHeadCommit(repository);
    final UserConfig config = repository.getConfig().get(UserConfig.KEY);
    author = config.getAuthorName();
    final String authorEmail = config.getAuthorEmail();
    author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$

    committer = config.getCommitterName();
    final String committerEmail = config.getCommitterEmail();
    committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$

    if (isMergedResolved || isCherryPickResolved) {
        commitMessage = getMergeResolveMessage(mergeRepository);
    }

    if (isCherryPickResolved) {
        author = getCherryPickOriginalAuthor(mergeRepository);
    }
}

From source file:org.eclipse.egit.ui.internal.dialogs.BasicConfigurationDialog.java

License:Open Source License

private static boolean isImplicitUserConfig(Repository... repositories) {
    if (repositories == null)
        return false;

    for (Repository repository : repositories) {
        UserConfig uc = loadRepoScopedConfig(repository).get(UserConfig.KEY);
        if (uc.isAuthorNameImplicit() //
                || uc.isAuthorEmailImplicit() || uc.isCommitterNameImplicit() || uc.isCommitterEmailImplicit())
            return true;
    }/*  w  ww .j  a v  a2  s  . c o  m*/
    return false;
}

From source file:org.eclipse.egit.ui.internal.dialogs.BasicConfigurationDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    userScopedConfig = loadUserScopedConfig();

    UserConfig userConfig = userScopedConfig.get(UserConfig.KEY);
    Composite main = new Composite(parent, SWT.NONE);
    main.setLayout(new GridLayout(2, false));
    GridDataFactory.fillDefaults().grab(true, true).applyTo(main);

    // user name//from   ww w  . ja v a 2s . c  om
    Label userNameLabel = new Label(main, SWT.NONE);
    userNameLabel.setText(UIText.BasicConfigurationDialog_UserNameLabel);
    userName = new Text(main, SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(userName);
    String currentName = null;
    if (userConfig != null)
        currentName = userConfig.getAuthorName();
    if (currentName != null)
        userName.setText(currentName);
    userName.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            needsUpdate = true;
        }
    });

    // user email
    Label emailLabel = new Label(main, SWT.NONE);
    emailLabel.setText(UIText.BasicConfigurationDialog_UserEmailLabel);
    email = new Text(main, SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(email);
    String currentMail = null;
    if (userConfig != null)
        currentMail = userConfig.getAuthorEmail();
    if (currentMail != null)
        email.setText(currentMail);
    email.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            needsUpdate = true;
        }
    });

    dontShowAgain = new Button(main, SWT.CHECK);
    GridDataFactory.fillDefaults().span(2, 1).applyTo(dontShowAgain);
    dontShowAgain.setText("&Don't show this dialog again"); //$NON-NLS-1$
    dontShowAgain.setSelection(true);

    Link link = new Link(main, SWT.UNDERLINE_LINK);
    GridDataFactory.fillDefaults().span(2, 1).applyTo(link);
    link.setText("Open the <a>Git Configuration</a> Preference Page"); //$NON-NLS-1$
    link.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            PreferencesUtil
                    .createPreferenceDialogOn(getShell(), GlobalConfigurationPreferencePage.ID, null, null)
                    .open();
        }
    });
    applyDialogFont(main);
    return main;
}

From source file:org.jboss.tools.feedhenry.ui.internal.util.GitUtil.java

License:Open Source License

private static UserConfig getUserConfig(Repository repository) throws CoreException {
    Assert.isNotNull(repository, "Could not get user configuration. No repository provided.");

    if (repository.getConfig() == null) {
        throw new CoreException(new Status(IStatus.ERROR, FHPlugin.PLUGIN_ID,
                NLS.bind("no user configuration (author, committer) are present in repository \"{0}\"",
                        repository.toString())));
    }// w  w w .  j a v  a2  s. c o m
    return repository.getConfig().get(UserConfig.KEY);
}

From source file:org.jboss.tools.openshift.egit.core.EGitUtils.java

License:Open Source License

/**
 * Gets the UserConfig from the given repository. The UserConfig of a repo
 * holds the default author and committer.
 * //w  w w.  j  a  va  2s.  c o m
 * @param repository
 *            the repository
 * @return the user configuration
 * @throws CoreException
 * 
 * @see PersonIdent(Repository)
 * @see CommittHelper#calculateCommitInfo
 */
private static UserConfig getUserConfig(Repository repository) throws CoreException {
    Assert.isLegal(repository != null, "Could not get user configuration. No repository provided.");

    if (repository.getConfig() == null) {
        throw new CoreException(createStatus(null,
                "no user configuration (author, committer) are present in repository \"{0}\"",
                repository.toString()));
    }
    return repository.getConfig().get(UserConfig.KEY);
}