/** * Try public key * * @param c a ssh connection * @param keyPath a path to key * @return true if authentication is successful */ private boolean tryPublicKey(final Connection c, final String keyPath) { try { final File file = new File(keyPath); if (file.exists()) { // if encrypted ask user for passphrase String passphrase = null; char[] text = FileUtil.loadFileText(file); if (isEncryptedKey(text)) { // need to ask passphrase from user int i; for (i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) { passphrase = myXmlRpcClient.askPassphrase( myHandlerNo, getUserHostString(), keyPath, myLastError); if (passphrase == null) { // if no passphrase was entered, just return false and try something other return false; } else { try { PEMDecoder.decode(text, passphrase); myLastError = ""; } catch (IOException e) { // decoding failed myLastError = GitBundle.message("sshmain.invalidpassphrase", keyPath); continue; } break; } } if (i == 0) { myLastError = GitBundle.message( "sshmain.too.mush.passphrase.guesses", keyPath, myHost.getNumberOfPasswordPrompts()); return false; } } // try authentication if (c.authenticateWithPublicKey(myHost.getUser(), text, passphrase)) { myLastError = ""; return true; } else { if (passphrase != null) { // mark as failed authentication only if passphrase were asked myLastError = GitBundle.message("sshmain.pk.authenitication.failed", keyPath); } else { myLastError = ""; } } } return false; } catch (Exception e) { return false; } }
// todo problem: control usage public static List<TextFilePatch> loadPatches( Project project, final String patchPath, CommitContext commitContext) throws IOException, PatchSyntaxException { char[] text = FileUtil.loadFileText(new File(patchPath)); PatchReader reader = new PatchReader(new CharArrayCharSequence(text)); final List<TextFilePatch> textFilePatches = reader.readAllPatches(); final TransparentlyFailedValue<Map<String, Map<String, CharSequence>>, PatchSyntaxException> additionalInfo = reader.getAdditionalInfo(null); ApplyPatchDefaultExecutor.applyAdditionalInfoBefore(project, additionalInfo, commitContext); return textFilePatches; }
/** * Resolve remote had reference * * @param value the reference to resolve * @param rootPath the root path * @return the resolved reference or null */ @Nullable private static String resolveHead(String value, String rootPath) { if (!value.startsWith("remotes/")) { return null; } String newRef; try { final String refText = new String( FileUtil.loadFileText(new File(rootPath, "./refs/" + value), Util.UTF8_ENCODING)) .trim(); String refsPrefix = "ref: refs/"; if (refText.endsWith("/HEAD") || !refText.startsWith(refsPrefix)) { newRef = null; } else { newRef = refText.substring(refsPrefix.length()); } } catch (Exception e) { newRef = null; } return newRef; }