Search in sources :

Example 41 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 42 with GitAPIException

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

the class GitManager method commitModifiedFiles.

public GitManager commitModifiedFiles(final ChangeSummary changeSummary) throws GitSubsystemException {
    lockAnd((me) -> {
        Status status;
        try {
            status = git.status().call();
        } catch (NoWorkTreeException | GitAPIException e) {
            throw new GitSubsystemException("Failed to retrieve status of: %s. Reason: %s", e, rootDir, e.getMessage());
        }
        final Map<String, StageState> css = status.getConflictingStageState();
        if (!css.isEmpty()) {
            throw new GitSubsystemException("%s contains conflicts. Cannot auto-commit.\n  %s", rootDir, new JoinString("\n  ", css.entrySet()));
        }
        final Set<String> toAdd = new HashSet<>();
        final Set<String> modified = status.getModified();
        if (modified != null && !modified.isEmpty()) {
            toAdd.addAll(modified);
        }
        final Set<String> untracked = status.getUntracked();
        if (untracked != null && !untracked.isEmpty()) {
            toAdd.addAll(untracked);
        }
        final Set<String> untrackedFolders = status.getUntrackedFolders();
        if (untrackedFolders != null && !untrackedFolders.isEmpty()) {
            toAdd.addAll(untrackedFolders);
        //            for ( String folderPath : untrackedFolders )
        //            {
        //                File dir = new File( rootDir, folderPath );
        //                Files.walkFileTree( null, null )
        //            }
        }
        if (!toAdd.isEmpty()) {
            addAndCommitPaths(changeSummary, toAdd);
        }
        return me;
    });
    return this;
}
Also used : Status(org.eclipse.jgit.api.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) NoWorkTreeException(org.eclipse.jgit.errors.NoWorkTreeException) StageState(org.eclipse.jgit.lib.IndexDiff.StageState) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) HashSet(java.util.HashSet)

Example 43 with GitAPIException

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

the class ArchiveServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    GitilesView view = ViewFilter.getView(req);
    Revision rev = view.getRevision();
    Repository repo = ServletUtils.getRepository(req);
    ObjectId treeId = getTree(view, repo, rev);
    if (treeId.equals(ObjectId.zeroId())) {
        res.sendError(SC_NOT_FOUND);
        return;
    }
    Optional<ArchiveFormat> format = ArchiveFormat.byExtension(view.getExtension(), getAccess(req).getConfig());
    if (!format.isPresent()) {
        res.setStatus(SC_NOT_FOUND);
        return;
    }
    String filename = getFilename(view, rev, view.getExtension());
    setDownloadHeaders(req, res, filename, format.get().getMimeType());
    res.setStatus(SC_OK);
    try {
        new ArchiveCommand(repo).setFormat(format.get().getRegisteredName()).setTree(treeId).setOutputStream(res.getOutputStream()).call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand)

Example 44 with GitAPIException

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

the class GitManager method deleteAndCommitPaths.

public GitManager deleteAndCommitPaths(final ChangeSummary summary, final Collection<String> paths) throws GitSubsystemException {
    lockAnd(me -> {
        try {
            RmCommand rm = git.rm();
            CommitCommand commit = git.commit();
            for (final String path : paths) {
                rm = rm.addFilepattern(path);
                commit = commit.setOnly(path);
            }
            logger.info("Deleting:\n  " + join(paths, "\n  ") + "\n\nSummary: " + summary);
            rm.call();
            commit.setMessage(buildMessage(summary, paths)).setAuthor(summary.getUser(), email).call();
        } catch (final NoFilepatternException e) {
            throw new GitSubsystemException("Cannot remove from git: " + e.getMessage(), e);
        } catch (final JGitInternalException | GitAPIException e) {
            throw new GitSubsystemException("Cannot remove from git: " + e.getMessage(), e);
        }
        return me;
    });
    return this;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoFilepatternException(org.eclipse.jgit.api.errors.NoFilepatternException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) RmCommand(org.eclipse.jgit.api.RmCommand) CommitCommand(org.eclipse.jgit.api.CommitCommand) JoinString(org.commonjava.maven.atlas.ident.util.JoinString)

Example 45 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project POL-POM-5 by PlayOnLinux.

the class GitRepository method fetchInstallableApplications.

@Override
public synchronized List<CategoryDTO> fetchInstallableApplications() {
    LOGGER.info(String.format("Begin fetching process of git-repository '%s' in '%s'", this.gitRepositoryUri, gitRepositoryLocation.getAbsolutePath()));
    boolean folderExists = gitRepositoryLocation.exists();
    // check that the repository folder exists
    if (!folderExists) {
        LOGGER.info(String.format("Creating new folder '%s' for git-repository '%s'", gitRepositoryLocation.getAbsolutePath(), this.gitRepositoryUri));
        if (!gitRepositoryLocation.mkdirs()) {
            LOGGER.error(String.format("Couldn't create folder for git repository '%s' at '%s'", this.gitRepositoryUri, gitRepositoryLocation.getAbsolutePath()));
            return Collections.emptyList();
        }
    }
    List<CategoryDTO> result = Collections.emptyList();
    try {
        Git gitRepository = null;
        /*
             * if the repository folder previously didn't exist, clone the
             * repository now
             */
        if (!folderExists) {
            LOGGER.info(String.format("Cloning git-repository '%s' to '%s'", this.gitRepositoryUri, gitRepositoryLocation.getAbsolutePath()));
            gitRepository = Git.cloneRepository().setURI(this.gitRepositoryUri.toString()).setDirectory(gitRepositoryLocation).call();
        } else /*
             * otherwise open the folder and pull the newest updates from the
             * repository
             */
        {
            LOGGER.info(String.format("Opening git-repository '%s' at '%s'", this.gitRepositoryUri, gitRepositoryLocation.getAbsolutePath()));
            gitRepository = Git.open(gitRepositoryLocation);
            LOGGER.info(String.format("Pulling new commits from git-repository '%s' to '%s'", this.gitRepositoryUri, gitRepositoryLocation.getAbsolutePath()));
            gitRepository.pull().call();
        }
        // close repository to free resources
        gitRepository.close();
        result = localRepositoryFactory.createInstance(this.gitRepositoryLocation, this.gitRepositoryUri).fetchInstallableApplications();
    } catch (RepositoryNotFoundException | GitAPIException e) {
        LOGGER.error(String.format("Folder '%s' is no git-repository", gitRepositoryLocation.getAbsolutePath()), e);
    } catch (IOException e) {
        LOGGER.error(String.format("An unknown error occured", e));
    }
    return result;
}
Also used : CategoryDTO(org.phoenicis.repository.dto.CategoryDTO) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException)

Aggregations

GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)60 IOException (java.io.IOException)25 Git (org.eclipse.jgit.api.Git)22 GitException (org.eclipse.che.api.git.exception.GitException)18 File (java.io.File)16 Ref (org.eclipse.jgit.lib.Ref)13 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ArrayList (java.util.ArrayList)11 ObjectId (org.eclipse.jgit.lib.ObjectId)11 RefSpec (org.eclipse.jgit.transport.RefSpec)11 Repository (org.eclipse.jgit.lib.Repository)9 URISyntaxException (java.net.URISyntaxException)8 PushResult (org.eclipse.jgit.transport.PushResult)8 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)8 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)8 PushCommand (org.eclipse.jgit.api.PushCommand)7 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)6 FetchCommand (org.eclipse.jgit.api.FetchCommand)6 HashMap (java.util.HashMap)5 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)5