Search in sources :

Example 41 with PersonIdent

use of org.eclipse.jgit.lib.PersonIdent in project egit by eclipse.

the class CreateTagHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    List<RepositoryCommit> commits = getCommits(event);
    if (commits.size() == 1) {
        RepositoryCommit commit = commits.get(0);
        Repository repository = commit.getRepository();
        CreateTagDialog dialog = new CreateTagDialog(HandlerUtil.getActiveShellChecked(event), commit.getRevCommit().getId(), repository);
        if (dialog.open() != Window.OK)
            return null;
        final TagBuilder tag = new TagBuilder();
        PersonIdent personIdent = new PersonIdent(repository);
        String tagName = dialog.getTagName();
        tag.setTag(tagName);
        tag.setTagger(personIdent);
        tag.setMessage(dialog.getTagMessage());
        tag.setObjectId(commit.getRevCommit());
        try {
            new TagOperation(repository, tag, dialog.shouldOverWriteTag()).execute(new NullProgressMonitor());
        } catch (CoreException e) {
            throw new ExecutionException(e.getMessage(), e);
        }
        if (dialog.shouldStartPushWizard())
            PushTagsWizard.openWizardDialog(repository, tagName);
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) CreateTagDialog(org.eclipse.egit.ui.internal.dialogs.CreateTagDialog) PersonIdent(org.eclipse.jgit.lib.PersonIdent) TagOperation(org.eclipse.egit.core.op.TagOperation) ExecutionException(org.eclipse.core.commands.ExecutionException) RepositoryCommit(org.eclipse.egit.ui.internal.commit.RepositoryCommit) TagBuilder(org.eclipse.jgit.lib.TagBuilder)

Example 42 with PersonIdent

use of org.eclipse.jgit.lib.PersonIdent in project fabric8 by fabric8io.

the class BuildConfigHelper method importNewGitProject.

public static CreateGitProjectResults importNewGitProject(KubernetesClient kubernetesClient, UserDetails userDetails, File basedir, String namespace, String projectName, String origin, String message, boolean apply, boolean useLocalGitAddress) throws GitAPIException, JsonProcessingException {
    GitUtils.disableSslCertificateChecks();
    InitCommand initCommand = Git.init();
    initCommand.setDirectory(basedir);
    Git git = initCommand.call();
    LOG.info("Initialised an empty git configuration repo at {}", basedir.getAbsolutePath());
    PersonIdent personIdent = userDetails.createPersonIdent();
    String user = userDetails.getUser();
    String address = userDetails.getAddress();
    String internalAddress = userDetails.getInternalAddress();
    String branch = userDetails.getBranch();
    // lets create the repository
    GitRepoClient repoClient = userDetails.createRepoClient();
    CreateRepositoryDTO createRepository = new CreateRepositoryDTO();
    createRepository.setName(projectName);
    String fullName = null;
    RepositoryDTO repository = repoClient.createRepository(createRepository);
    if (repository != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got repository: " + toJson(repository));
        }
        fullName = repository.getFullName();
    }
    if (Strings.isNullOrBlank(fullName)) {
        fullName = user + "/" + projectName;
    }
    String htmlUrl = URLUtils.pathJoin(resolveToRoot(address), user, projectName);
    String localCloneUrl = URLUtils.pathJoin(resolveToRoot(internalAddress), user, projectName + ".git");
    String cloneUrl = htmlUrl + ".git";
    String defaultCloneUrl = cloneUrl;
    // lets default to using the local git clone URL
    if (useLocalGitAddress && Strings.isNotBlank(internalAddress)) {
        defaultCloneUrl = localCloneUrl;
    }
    // now lets import the code and publish
    GitUtils.configureBranch(git, branch, origin, defaultCloneUrl);
    GitUtils.addDummyFileToEmptyFolders(basedir);
    LOG.info("About to git commit and push to: " + defaultCloneUrl + " and remote name " + origin);
    GitUtils.doAddCommitAndPushFiles(git, userDetails, personIdent, branch, origin, message, true);
    Map<String, String> annotations = new HashMap<>();
    annotations.put(Annotations.Builds.GIT_CLONE_URL, cloneUrl);
    annotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localCloneUrl);
    BuildConfig buildConfig;
    if (apply) {
        buildConfig = createAndApplyBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
    } else {
        buildConfig = createBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
    }
    return new CreateGitProjectResults(buildConfig, fullName, htmlUrl, localCloneUrl, cloneUrl);
}
Also used : Git(org.eclipse.jgit.api.Git) CreateRepositoryDTO(io.fabric8.repo.git.CreateRepositoryDTO) PersonIdent(org.eclipse.jgit.lib.PersonIdent) HashMap(java.util.HashMap) InitCommand(org.eclipse.jgit.api.InitCommand) GitRepoClient(io.fabric8.repo.git.GitRepoClient) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) RepositoryDTO(io.fabric8.repo.git.RepositoryDTO) CreateRepositoryDTO(io.fabric8.repo.git.CreateRepositoryDTO)

Example 43 with PersonIdent

use of org.eclipse.jgit.lib.PersonIdent in project stdlib by petergeneric.

the class RepoHelper method log.

public static List<ConfigCommit> log(final Repository repo, final int max) {
    try {
        Git git = new Git(repo);
        final Iterable<RevCommit> commits = git.log().setMaxCount(max).call();
        List<ConfigCommit> ret = new ArrayList<>();
        for (RevCommit commit : commits) {
            final PersonIdent author = commit.getAuthorIdent();
            final Date timestamp = new Date(commit.getCommitTime() * 1000L);
            ret.add(new ConfigCommit(timestamp, author.getName(), author.getEmailAddress(), commit.getFullMessage()));
        }
        return ret;
    } catch (GitAPIException e) {
        throw new RuntimeException("Error issuing git log", e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ArrayList(java.util.ArrayList) Date(java.util.Date) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 44 with PersonIdent

use of org.eclipse.jgit.lib.PersonIdent in project repairnator by Spirals-Team.

the class CheckoutRepository method businessExecute.

@Override
protected void businessExecute() {
    Metrics metric = this.getInspector().getJobStatus().getMetrics();
    Git git;
    try {
        GitHelper gitHelper = this.getInspector().getGitHelper();
        git = Git.open(new File(inspector.getRepoLocalPath()));
        Build build;
        switch(checkoutType) {
            case CHECKOUT_BUGGY_BUILD:
                build = inspector.getBuggyBuild();
                metric.setBugCommit(build.getCommit().getSha());
                metric.setBugCommitUrl(this.getCommitUrl(build.getCommit().getSha()));
                break;
            case CHECKOUT_BUGGY_BUILD_SOURCE_CODE:
                build = inspector.getBuggyBuild();
                metric.setBugCommit(build.getCommit().getSha());
                metric.setBugCommitUrl(this.getCommitUrl(build.getCommit().getSha()));
                metric.setReconstructedBugCommit(true);
                break;
            case CHECKOUT_PATCHED_BUILD:
                build = inspector.getPatchedBuild();
                metric.setPatchCommit(build.getCommit().getSha());
                metric.setPatchCommitUrl(this.getCommitUrl(build.getCommit().getSha()));
                break;
            default:
                this.getLogger().warn("A case seems not to have been considered. Buggy build will be used.");
                build = inspector.getBuggyBuild();
        }
        if (build.isPullRequest()) {
            PRInformation prInformation = build.getPRInformation();
            if (prInformation != null) {
                if (checkoutType == CheckoutType.CHECKOUT_PATCHED_BUILD) {
                    this.writeProperty("is-pr", "true");
                    this.writeProperty("pr-remote-repo", prInformation.getOtherRepo().getSlug());
                    this.writeProperty("pr-head-commit-id", prInformation.getHead().getSha());
                    this.writeProperty("pr-head-commit-id-url", prInformation.getHead().getCompareUrl());
                    this.writeProperty("pr-base-commit-id", prInformation.getBase().getSha());
                    this.writeProperty("pr-base-commit-id-url", prInformation.getBase().getCompareUrl());
                    this.writeProperty("pr-id", build.getPullRequestNumber());
                }
            } else {
                this.addStepError("Error while getting the PR information...");
                this.shouldStop = true;
                return;
            }
            gitHelper.addAndCommitRepairnatorLogAndProperties(this.getInspector().getJobStatus(), git, "After getting PR information");
            String repository = this.inspector.getRepoSlug();
            this.getLogger().debug("Reproduce the PR for " + repository + " by fetching remote branch and merging.");
            List<String> pathes;
            if (checkoutType == CheckoutType.CHECKOUT_BUGGY_BUILD_SOURCE_CODE) {
                pathes = new ArrayList<String>();
                for (File path : this.getInspector().getJobStatus().getRepairSourceDir()) {
                    URI gitRepoURI = git.getRepository().getDirectory().getParentFile().toURI();
                    URI pathURI = path.getCanonicalFile().toURI();
                    String relativePath = gitRepoURI.relativize(pathURI).getPath();
                    pathes.add(relativePath);
                }
            } else {
                pathes = null;
            }
            boolean successfulMerge = gitHelper.mergeTwoCommitsForPR(git, build, prInformation, repository, this, pathes);
            if (!successfulMerge) {
                this.getLogger().debug("Error while merging two commits to reproduce the PR.");
                this.shouldStop = true;
            }
        } else {
            String commitCheckout = build.getCommit().getSha();
            commitCheckout = gitHelper.testCommitExistence(git, commitCheckout, this, build);
            if (commitCheckout != null) {
                this.getLogger().debug("Get the commit " + commitCheckout + " for repo " + this.inspector.getRepoSlug());
                if (checkoutType != CheckoutType.CHECKOUT_BUGGY_BUILD_SOURCE_CODE) {
                    git.checkout().setName(commitCheckout).call();
                } else {
                    List<String> pathes = new ArrayList<String>();
                    for (File path : this.getInspector().getJobStatus().getRepairSourceDir()) {
                        URI gitRepoURI = git.getRepository().getDirectory().getParentFile().toURI();
                        URI pathURI = path.getCanonicalFile().toURI();
                        String relativePath = gitRepoURI.relativize(pathURI).getPath();
                        pathes.add(relativePath);
                    }
                    git.checkout().setStartPoint(commitCheckout).addPaths(pathes).call();
                    PersonIdent personIdent = new PersonIdent("Luc Esape", "luc.esape@gmail.com");
                    git.commit().setMessage("Undo changes on source code").setAuthor(personIdent).setCommitter(personIdent).call();
                }
                this.writeProperty("bugCommit", this.inspector.getBuggyBuild().getCommit().getCompareUrl());
            } else {
                this.addStepError("Error while getting the commit to checkout from the repo.");
                this.shouldStop = true;
                return;
            }
        }
    } catch (IOException | GitAPIException e) {
        this.addStepError("Error while getting the commit to checkout from the repo.");
        this.shouldStop = true;
    }
    this.writeProperty("hostname", Utils.getHostname());
    switch(this.getInspector().getBuildToBeInspected().getStatus()) {
        case ONLY_FAIL:
            this.writeProperty("bugType", "only_fail");
            break;
        case FAILING_AND_PASSING:
            this.writeProperty("bugType", "failing_passing");
            break;
        case PASSING_AND_PASSING_WITH_TEST_CHANGES:
            this.writeProperty("bugType", "passing_passing");
            break;
    }
}
Also used : GitHelper(fr.inria.spirals.repairnator.process.git.GitHelper) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Metrics(fr.inria.spirals.repairnator.process.inspectors.Metrics) Git(org.eclipse.jgit.api.Git) PRInformation(fr.inria.jtravis.entities.PRInformation) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Build(fr.inria.jtravis.entities.Build) File(java.io.File)

Example 45 with PersonIdent

use of org.eclipse.jgit.lib.PersonIdent in project repairnator by Spirals-Team.

the class GitHelper method mergeTwoCommitsForPR.

public boolean mergeTwoCommitsForPR(Git git, Build build, PRInformation prInformation, String repository, AbstractStep step, List<String> pathes) {
    try {
        String remoteBranchPath = CloneRepository.GITHUB_ROOT_REPO + prInformation.getOtherRepo().getSlug() + ".git";
        RemoteAddCommand remoteBranchCommand = git.remoteAdd();
        remoteBranchCommand.setName("PR");
        remoteBranchCommand.setUri(new URIish(remoteBranchPath));
        remoteBranchCommand.call();
        git.fetch().setRemote("PR").call();
        String commitHeadSha = this.testCommitExistence(git, prInformation.getHead().getSha(), step, build);
        String commitBaseSha = this.testCommitExistence(git, prInformation.getBase().getSha(), step, build);
        if (commitHeadSha == null) {
            step.addStepError("Commit head ref cannot be retrieved in the repository: " + prInformation.getHead().getSha() + ". Operation aborted.");
            this.getLogger().debug("Step " + step.getName() + " - " + prInformation.getHead().toString());
            return false;
        }
        if (commitBaseSha == null) {
            step.addStepError("Commit base ref cannot be retrieved in the repository: " + prInformation.getBase().getSha() + ". Operation aborted.");
            this.getLogger().debug("Step " + step.getName() + " - " + prInformation.getBase().toString());
            return false;
        }
        this.getLogger().debug("Step " + step.getName() + " - Get the commit " + commitHeadSha + " for repo " + repository);
        if (pathes != null) {
            git.checkout().setName(commitHeadSha).addPaths(pathes).call();
            PersonIdent personIdent = new PersonIdent("Luc Esape", "luc.esape@gmail.com");
            git.commit().setMessage("Undo changes on source code").setAuthor(personIdent).setCommitter(personIdent).call();
        } else {
            git.checkout().setName(commitHeadSha).call();
        }
        RevWalk revwalk = new RevWalk(git.getRepository());
        RevCommit revCommitBase = revwalk.lookupCommit(git.getRepository().resolve(commitBaseSha));
        this.getLogger().debug("Step " + step.getName() + " - Do the merge with the PR commit for repo " + repository);
        MergeResult result = git.merge().include(revCommitBase).setFastForward(MergeCommand.FastForwardMode.NO_FF).call();
        this.nbCommits++;
    } catch (Exception e) {
        step.addStepError(e.getMessage());
        this.getLogger().error("Step " + step.getName() + " - Repository " + repository + " cannot be cloned.", e);
        return false;
    }
    return true;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) MergeResult(org.eclipse.jgit.api.MergeResult) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) PatchApplyException(org.eclipse.jgit.api.errors.PatchApplyException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) PatchFormatException(org.eclipse.jgit.api.errors.PatchFormatException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

PersonIdent (org.eclipse.jgit.lib.PersonIdent)221 RevCommit (org.eclipse.jgit.revwalk.RevCommit)78 ObjectId (org.eclipse.jgit.lib.ObjectId)55 Test (org.junit.Test)55 Repository (org.eclipse.jgit.lib.Repository)48 IOException (java.io.IOException)41 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)41 RevWalk (org.eclipse.jgit.revwalk.RevWalk)38 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)31 Change (com.google.gerrit.entities.Change)28 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)26 Ref (org.eclipse.jgit.lib.Ref)26 Git (org.eclipse.jgit.api.Git)25 Account (com.google.gerrit.entities.Account)22 Instant (java.time.Instant)21 TestRepository (org.eclipse.jgit.junit.TestRepository)20 ArrayList (java.util.ArrayList)18 File (java.io.File)17 Date (java.util.Date)17 RefUpdate (org.eclipse.jgit.lib.RefUpdate)17