Search in sources :

Example 6 with GitUser

use of org.eclipse.che.api.git.shared.GitUser in project che by eclipse.

the class LogPage method writeTo.

/** @see InfoPage#writeTo(java.io.OutputStream) */
@Override
public void writeTo(OutputStream out) throws IOException {
    PrintWriter writer = new PrintWriter(out);
    DateFormat df = (DateFormat) dateFormat.clone();
    for (Revision commit : commits) {
        writer.format("commit %s\n", commit.getId());
        GitUser commiter = commit.getCommitter();
        if (commiter != null) {
            writer.format("Author: %1$s <%2$s>\n", commiter.getName(), commiter.getEmail());
        }
        long commitTime = commit.getCommitTime();
        if (commitTime > 0) {
            writer.format("Date:   %s\n", df.format(new Date(commitTime)));
        }
        writer.println();
        // Message with indent.
        String[] lines = commit.getMessage().split("\n");
        for (String line : lines) {
            writer.format("    %s\n", line);
        }
        writer.println();
    }
    writer.flush();
}
Also used : Revision(org.eclipse.che.api.git.shared.Revision) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Date(java.util.Date) PrintWriter(java.io.PrintWriter) GitUser(org.eclipse.che.api.git.shared.GitUser)

Example 7 with GitUser

use of org.eclipse.che.api.git.shared.GitUser in project che by eclipse.

the class JGitConnection method tagCreate.

@Override
public Tag tagCreate(TagCreateParams params) throws GitException {
    String commit = params.getCommit();
    if (commit == null) {
        commit = Constants.HEAD;
    }
    try {
        RevWalk revWalk = new RevWalk(repository);
        RevObject revObject;
        try {
            revObject = revWalk.parseAny(repository.resolve(commit));
        } finally {
            revWalk.close();
        }
        TagCommand tagCommand = getGit().tag().setName(params.getName()).setObjectId(revObject).setMessage(params.getMessage()).setForceUpdate(params.isForce());
        GitUser tagger = getUser();
        if (tagger != null) {
            tagCommand.setTagger(new PersonIdent(tagger.getName(), tagger.getEmail()));
        }
        Ref revTagRef = tagCommand.call();
        RevTag revTag = revWalk.parseTag(revTagRef.getLeaf().getObjectId());
        return newDto(Tag.class).withName(revTag.getTagName());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TagCommand(org.eclipse.jgit.api.TagCommand) GitUser(org.eclipse.che.api.git.shared.GitUser) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RevTag(org.eclipse.jgit.revwalk.RevTag) Tag(org.eclipse.che.api.git.shared.Tag)

Example 8 with GitUser

use of org.eclipse.che.api.git.shared.GitUser in project che by eclipse.

the class JGitConnection method getCommiters.

@Override
public List<GitUser> getCommiters() throws GitException {
    List<GitUser> gitUsers = new ArrayList<>();
    try {
        LogCommand logCommand = getGit().log();
        for (RevCommit commit : logCommand.call()) {
            PersonIdent committerIdentity = commit.getCommitterIdent();
            GitUser gitUser = newDto(GitUser.class).withName(committerIdentity.getName()).withEmail(committerIdentity.getEmailAddress());
            if (!gitUsers.contains(gitUser)) {
                gitUsers.add(gitUser);
            }
        }
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
    return gitUsers;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) LogCommand(org.eclipse.jgit.api.LogCommand) GitException(org.eclipse.che.api.git.exception.GitException) ArrayList(java.util.ArrayList) GitUser(org.eclipse.che.api.git.shared.GitUser) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with GitUser

use of org.eclipse.che.api.git.shared.GitUser in project che by eclipse.

the class JGitConnection method clone.

public void clone(CloneParams params) throws GitException, UnauthorizedException {
    String remoteUri = params.getRemoteUrl();
    boolean removeIfFailed = false;
    try {
        if (params.getRemoteName() == null) {
            params.setRemoteName(Constants.DEFAULT_REMOTE_NAME);
        }
        if (params.getWorkingDir() == null) {
            params.setWorkingDir(repository.getWorkTree().getCanonicalPath());
        }
        // If clone fails and the .git folder didn't exist we want to remove it.
        // We have to do this here because the clone command doesn't revert its own changes in case of failure.
        removeIfFailed = !repository.getDirectory().exists();
        CloneCommand cloneCommand = Git.cloneRepository().setDirectory(new File(params.getWorkingDir())).setRemote(params.getRemoteName()).setCloneSubmodules(params.isRecursive()).setURI(remoteUri);
        if (params.getBranchesToFetch().isEmpty()) {
            cloneCommand.setCloneAllBranches(true);
        } else {
            cloneCommand.setBranchesToClone(params.getBranchesToFetch());
        }
        LineConsumer lineConsumer = lineConsumerFactory.newLineConsumer();
        cloneCommand.setProgressMonitor(new BatchingProgressMonitor() {

            @Override
            protected void onUpdate(String taskName, int workCurr) {
                try {
                    lineConsumer.writeLine(taskName + ": " + workCurr + " completed");
                } catch (IOException exception) {
                    LOG.error(exception.getMessage(), exception);
                }
            }

            @Override
            protected void onEndTask(String taskName, int workCurr) {
            }

            @Override
            protected void onUpdate(String taskName, int workCurr, int workTotal, int percentDone) {
                try {
                    lineConsumer.writeLine(taskName + ": " + workCurr + " of " + workTotal + " completed, " + percentDone + "% done");
                } catch (IOException exception) {
                    LOG.error(exception.getMessage(), exception);
                }
            }

            @Override
            protected void onEndTask(String taskName, int workCurr, int workTotal, int percentDone) {
            }
        });
        ((Git) executeRemoteCommand(remoteUri, cloneCommand, params.getUsername(), params.getPassword())).close();
        StoredConfig repositoryConfig = getRepository().getConfig();
        GitUser gitUser = getUser();
        if (gitUser != null) {
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, gitUser.getName());
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL, gitUser.getEmail());
        }
        repositoryConfig.save();
    } catch (IOException | GitAPIException exception) {
        // Delete .git directory in case it was created
        if (removeIfFailed) {
            deleteRepositoryFolder();
        }
        //try to clone repository by replacing http to https in the url if HTTP 301 redirect happened
        if (exception.getMessage().contains(": 301 Moved Permanently")) {
            remoteUri = "https" + remoteUri.substring(4);
            try {
                clone(params.withRemoteUrl(remoteUri));
            } catch (UnauthorizedException | GitException e) {
                throw new GitException("Failed to clone the repository", e);
            }
            return;
        }
        String message = generateExceptionMessage(exception);
        throw new GitException(message, exception);
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) GitUser(org.eclipse.che.api.git.shared.GitUser) StoredConfig(org.eclipse.jgit.lib.StoredConfig) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) Git(org.eclipse.jgit.api.Git) BatchingProgressMonitor(org.eclipse.jgit.lib.BatchingProgressMonitor) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File)

Aggregations

GitUser (org.eclipse.che.api.git.shared.GitUser)9 IOException (java.io.IOException)4 GitException (org.eclipse.che.api.git.exception.GitException)3 Revision (org.eclipse.che.api.git.shared.Revision)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 GitConnection (org.eclipse.che.api.git.GitConnection)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 RepositoryState (org.eclipse.jgit.lib.RepositoryState)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Files (com.google.common.io.Files)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 FileOutputStream (java.io.FileOutputStream)1 FilenameFilter (java.io.FilenameFilter)1 PrintWriter (java.io.PrintWriter)1