/** * Executes remote command. * * <p>Note: <i>'need for authorization'</i> check based on command execution fail message, so this * check can fail when i.e. git version updated, for more information see {@link * #isOperationNeedAuth(String)} * * @param command remote command which should be executed * @throws GitException when error occurs while {@code command} execution is going except of * unauthorized error * @throws UnauthorizedException when it is not possible to execute {@code command} with existing * credentials */ private void executeRemoteCommand(RemoteOperationCommand<?> command) throws GitException, UnauthorizedException { try { command.execute(); } catch (GitException gitEx) { if (!isOperationNeedAuth(gitEx.getMessage())) { throw gitEx; } ProviderInfo info = credentialsLoader.getProviderInfo(command.getRemoteUri()); if (info != null) { boolean isAuthenticated = credentialsLoader.getUserCredential(command.getRemoteUri()) != null; throw new UnauthorizedException( gitEx.getMessage(), ErrorCodes.UNAUTHORIZED_GIT_OPERATION, ImmutableMap.of( PROVIDER_NAME, info.getProviderName(), AUTHENTICATE_URL, info.getAuthenticateUrl(), "authenticated", Boolean.toString(isAuthenticated))); } throw new UnauthorizedException(gitEx.getMessage(), ErrorCodes.UNAUTHORIZED_GIT_OPERATION); } }
/** * Executes remote command. * * <p>Note: <i>'need for authorization'</i> check based on command execution fail message, so this * check can fail when i.e. git version updated, for more information see {@link * #isOperationNeedAuth(String)} * * @param command remote command which should be executed * @throws GitException when error occurs while {@code command} execution is going except of * unauthorized error * @throws UnauthorizedException when it is not possible to execute {@code command} with existing * credentials */ private void executeRemoteCommand(RemoteOperationCommand<?> command) throws GitException, UnauthorizedException { try { command.execute(); } catch (GitException gitEx) { if (!isOperationNeedAuth(gitEx.getMessage())) { throw gitEx; } throw new UnauthorizedException(gitEx.getMessage()); } }
@Override public PullResponse pull(PullRequest request) throws GitException, UnauthorizedException { String remoteUri = getRemoteUri(request.getRemote()); PullCommand pullCommand = nativeGit.createPullCommand(); pullCommand .setRemote(request.getRemote()) .setRefSpec(request.getRefSpec()) .setAuthor(getLocalCommitter()) .setRemoteUri(remoteUri) .setTimeout(request.getTimeout()); try { executeRemoteCommand(pullCommand); } catch (GitException exception) { if (noInitCommitWhenPullErrorPattern.matcher(exception.getMessage()).find()) { throw new GitException( exception.getMessage(), ErrorCodes.NO_COMMITTER_NAME_OR_EMAIL_DEFINED); } else if ("Unable get private ssh key".equals(exception.getMessage())) { throw new GitException(exception.getMessage(), ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY); } else if (("Auto-merging file\nCONFLICT (content): Merge conflict in file\n" + "Automatic merge failed; fix conflicts and then commit the result.\n") .equals(exception.getMessage())) { throw new GitException(exception.getMessage(), ErrorCodes.MERGE_CONFLICT); } else { throw exception; } } return pullCommand.getPullResponse(); }
/** * Ensure existence repository root directory inside working directory * * @throws GitException if git root folder is not in working directory */ boolean isInsideWorkTree() throws GitException { final EmptyGitCommand emptyGitCommand = nativeGit.createEmptyGitCommand(); // command "rev-parse --is-inside-work-tree" returns true/false try { emptyGitCommand .setNextParameter("rev-parse") .setNextParameter("--is-inside-work-tree") .execute(); final String output = emptyGitCommand.getText(); return Boolean.valueOf(output); } catch (GitException ge) { String msg = ge.getMessage(); if (msg != null && notInGitRepoErrorPattern.matcher(msg).matches()) { return false; } throw ge; } }