Search in sources :

Example 6 with FileGitEventInfo

use of com.oxygenxml.git.view.event.FileGitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method restoreLastCommitFile.

/**
 * Restores the last commit file content to the local file at the given path.
 * Both files must have the same path, otherwise it will not work.
 *
 * @param paths The paths to the files to restore.
 */
public void restoreLastCommitFile(List<String> paths) {
    try {
        fireOperationAboutToStart(new FileGitEventInfo(GitOperation.DISCARD, paths));
        CheckoutCommand checkoutCmd = git.checkout();
        checkoutCmd.addPaths(paths);
        checkoutCmd.call();
        fireOperationSuccessfullyEnded(new FileGitEventInfo(GitOperation.DISCARD, paths));
    } catch (GitAPIException e) {
        fireOperationFailed(new FileGitEventInfo(GitOperation.DISCARD, paths), e);
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo)

Example 7 with FileGitEventInfo

use of com.oxygenxml.git.view.event.FileGitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method add.

/**
 * Adds a single file to the staging area. Preparing it for commit
 *
 * @param file - the name of the file to be added
 */
public void add(FileStatus file) {
    Collection<String> filePaths = getFilePaths(Arrays.asList(file));
    try {
        fireOperationAboutToStart(new FileGitEventInfo(GitOperation.STAGE, filePaths));
        if (file.getChangeType().equals(GitChangeType.REMOVED)) {
            git.rm().addFilepattern(file.getFileLocation()).call();
        } else {
            git.add().addFilepattern(file.getFileLocation()).call();
        }
        fireOperationSuccessfullyEnded(new FileGitEventInfo(GitOperation.STAGE, filePaths));
    } catch (GitAPIException e) {
        fireOperationFailed(new FileGitEventInfo(GitOperation.STAGE, filePaths), e);
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo)

Example 8 with FileGitEventInfo

use of com.oxygenxml.git.view.event.FileGitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method commit.

/**
 * Commits a single file locally
 *
 * @param message Message for the commit
 * @param isAmendLastCommit <code>true</code> if the last commit should be amended.
 *
 * All JGit exceptions have a common ancestor, but sub classes offer different API for getting extra information
 * about the cause of the exception.
 *
 * @throws AbortedByHookException The commit failed because it a hook rejected it.
 * @throws ConcurrentRefUpdateException Exception thrown when a command wants to update a ref but failed because
 * another process is accessing (or even also updating) the ref.
 * @throws NoHeadException Exception thrown when a command expected the {@code HEAD} reference to exist
 * but couldn't find such a reference
 * @throws NoMessageException A commit was called without explicitly specifying a commit message
 * @throws UnmergedPathsException Thrown when branch deletion fails due to unmerged data
 * @throws WrongRepositoryStateException Exception thrown when the state of the repository doesn't allow the execution
 * of a certain command. E.g. when a CommitCommand should be executed on a repository with unresolved conflicts this exception will be thrown.
 * @throws GitAPIException Other unexpected exceptions.
 */
public // NOSONAR See doc above.
void commit(// NOSONAR See doc above.
String message, // NOSONAR See doc above.
boolean isAmendLastCommit) throws // NOSONAR See doc above.
GitAPIException, // NOSONAR See doc above.
NoHeadException, // NOSONAR See doc above.
NoMessageException, // NOSONAR See doc above.
UnmergedPathsException, // NOSONAR See doc above.
ConcurrentRefUpdateException, // NOSONAR See doc above.
WrongRepositoryStateException, AbortedByHookException {
    // NOSONAR See doc above.
    List<FileStatus> files = getStagedFiles();
    Collection<String> filePaths = getFilePaths(files);
    try {
        fireOperationAboutToStart(new FileGitEventInfo(GitOperation.COMMIT, filePaths));
        git.commit().setMessage(message).setAmend(isAmendLastCommit).call();
        fireOperationSuccessfullyEnded(new FileGitEventInfo(GitOperation.COMMIT, filePaths));
    } catch (GitAPIException e) {
        fireOperationFailed(new FileGitEventInfo(GitOperation.COMMIT, filePaths), e);
        LOGGER.error(e.getMessage(), e);
        // Re throw the exception so that the user sees a proper error message, depending on its type.
        throw e;
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) FileStatus(com.oxygenxml.git.service.entities.FileStatus) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo)

Aggregations

FileGitEventInfo (com.oxygenxml.git.view.event.FileGitEventInfo)8 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 FileStatus (com.oxygenxml.git.service.entities.FileStatus)4 GitAccess (com.oxygenxml.git.service.GitAccess)1 NoRepositorySelected (com.oxygenxml.git.service.NoRepositorySelected)1 File (java.io.File)1 IOException (java.io.IOException)1 AddCommand (org.eclipse.jgit.api.AddCommand)1 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)1 ResetCommand (org.eclipse.jgit.api.ResetCommand)1 RmCommand (org.eclipse.jgit.api.RmCommand)1 Repository (org.eclipse.jgit.lib.Repository)1