Search in sources :

Example 26 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project zeppelin by apache.

the class GitNotebookRepo method checkpoint.

/* implemented as git add+commit
   * @param pattern is the noteId
   * @param commitMessage is a commit message (checkpoint message)
   * (non-Javadoc)
   * @see org.apache.zeppelin.notebook.repo.VFSNotebookRepo#checkpoint(String, String)
   */
@Override
public Revision checkpoint(String pattern, String commitMessage, AuthenticationInfo subject) {
    Revision revision = Revision.EMPTY;
    try {
        List<DiffEntry> gitDiff = git.diff().call();
        if (!gitDiff.isEmpty()) {
            LOG.debug("Changes found for pattern '{}': {}", pattern, gitDiff);
            DirCache added = git.add().addFilepattern(pattern).call();
            LOG.debug("{} changes are about to be commited", added.getEntryCount());
            RevCommit commit = git.commit().setMessage(commitMessage).call();
            revision = new Revision(commit.getName(), commit.getShortMessage(), commit.getCommitTime());
        } else {
            LOG.debug("No changes found {}", pattern);
        }
    } catch (GitAPIException e) {
        LOG.error("Failed to add+comit {} to Git", pattern, e);
    }
    return revision;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) DirCache(org.eclipse.jgit.dircache.DirCache) DiffEntry(org.eclipse.jgit.diff.DiffEntry) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 27 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project zeppelin by apache.

the class GitNotebookRepo method get.

/**
   * the idea is to:
   * 1. stash current changes
   * 2. remember head commit and checkout to the desired revision
   * 3. get note and checkout back to the head
   * 4. apply stash on top and remove it
   */
@Override
public synchronized Note get(String noteId, String revId, AuthenticationInfo subject) throws IOException {
    Note note = null;
    RevCommit stash = null;
    try {
        List<DiffEntry> gitDiff = git.diff().setPathFilter(PathFilter.create(noteId)).call();
        boolean modified = !gitDiff.isEmpty();
        if (modified) {
            // stash changes
            stash = git.stashCreate().call();
            Collection<RevCommit> stashes = git.stashList().call();
            LOG.debug("Created stash : {}, stash size : {}", stash, stashes.size());
        }
        ObjectId head = git.getRepository().resolve(Constants.HEAD);
        // checkout to target revision
        git.checkout().setStartPoint(revId).addPath(noteId).call();
        // get the note
        note = super.get(noteId, subject);
        // checkout back to head
        git.checkout().setStartPoint(head.getName()).addPath(noteId).call();
        if (modified && stash != null) {
            // unstash changes
            ObjectId applied = git.stashApply().setStashRef(stash.getName()).call();
            ObjectId dropped = git.stashDrop().setStashRef(0).call();
            Collection<RevCommit> stashes = git.stashList().call();
            LOG.debug("Stash applied as : {}, and dropped : {}, stash size: {}", applied, dropped, stashes.size());
        }
    } catch (GitAPIException e) {
        LOG.error("Failed to return note from revision \"{}\"", revId, e);
    }
    return note;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ObjectId(org.eclipse.jgit.lib.ObjectId) Note(org.apache.zeppelin.notebook.Note) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 28 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project che by eclipse.

the class JGitConnection method rm.

@Override
public void rm(RmParams params) throws GitException {
    List<String> files = params.getItems();
    RmCommand rmCommand = getGit().rm();
    rmCommand.setCached(params.isCached());
    if (files != null) {
        files.forEach(rmCommand::addFilepattern);
    }
    try {
        rmCommand.call();
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(org.eclipse.che.api.git.exception.GitException) RmCommand(org.eclipse.jgit.api.RmCommand)

Example 29 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException 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 30 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project che by eclipse.

the class JGitConnection method reset.

@Override
public void reset(ResetParams params) throws GitException {
    try {
        ResetCommand resetCommand = getGit().reset();
        resetCommand.setRef(params.getCommit());
        List<String> patterns = params.getFilePattern();
        patterns.forEach(resetCommand::addPath);
        if (params.getType() != null && patterns.isEmpty()) {
            switch(params.getType()) {
                case HARD:
                    resetCommand.setMode(ResetType.HARD);
                    break;
                case KEEP:
                    resetCommand.setMode(ResetType.KEEP);
                    break;
                case MERGE:
                    resetCommand.setMode(ResetType.MERGE);
                    break;
                case MIXED:
                    resetCommand.setMode(ResetType.MIXED);
                    break;
                case SOFT:
                    resetCommand.setMode(ResetType.SOFT);
                    break;
            }
        }
        resetCommand.call();
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(org.eclipse.che.api.git.exception.GitException) ResetCommand(org.eclipse.jgit.api.ResetCommand)

Aggregations

GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)49 IOException (java.io.IOException)23 GitException (org.eclipse.che.api.git.exception.GitException)18 Git (org.eclipse.jgit.api.Git)18 File (java.io.File)14 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ArrayList (java.util.ArrayList)11 ObjectId (org.eclipse.jgit.lib.ObjectId)11 Ref (org.eclipse.jgit.lib.Ref)10 Repository (org.eclipse.jgit.lib.Repository)9 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)8 RevWalk (org.eclipse.jgit.revwalk.RevWalk)7 RefSpec (org.eclipse.jgit.transport.RefSpec)7 PushResult (org.eclipse.jgit.transport.PushResult)6 HashMap (java.util.HashMap)5 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)5 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)5 GitUser (org.eclipse.che.api.git.shared.GitUser)4 AddCommand (org.eclipse.jgit.api.AddCommand)4 CloneCommand (org.eclipse.jgit.api.CloneCommand)4