List of usage examples for org.eclipse.jgit.api.errors PatchApplyException PatchApplyException
public PatchApplyException(String message)
From source file:MySmartApply.java
License:Eclipse Distribution License
/** * Executes the {@code ApplyCommand} command with all the options and * parameters collected by the setter methods (e.g. * {@link #setPatch(InputStream)} of this class. Each instance of this class * should only be used for one invocation of the command. Don't call this * method twice on an instance./*from ww w.j av a 2 s .c om*/ * * @return an {@link ApplyResult} object representing the command result * @throws GitAPIException * @throws PatchFormatException * @throws PatchApplyException */ public ApplyResult call() throws GitAPIException, PatchFormatException, PatchApplyException { ApplyResult r = new ApplyResult(); try { final Patch p = new Patch(); try { p.parse(in); } finally { in.close(); } if (!p.getErrors().isEmpty()) throw new PatchFormatException(p.getErrors()); for (FileHeader fh : p.getFiles()) { if (listener != null) listener.setFilename( getFile((fh.getChangeType() != ChangeType.ADD) ? fh.getOldPath() : fh.getNewPath(), false).getName()); if (fh.getChangeType() == ChangeType.ADD || fh.getChangeType() == ChangeType.MODIFY || fh.getChangeType() == ChangeType.COPY) { if (fh.getNewPath().contains(".lang.php") || fh.getNewPath().contains(".gitignore") || fh.getNewPath().equals("includes/config.php") || fh.getNewPath().contains("/images")) { if (fh.getNewPath().contains(".lang.php") || fh.getNewPath().contains("/images")) this.failed_files.add(getFile( (fh.getChangeType() != ChangeType.ADD) ? fh.getOldPath() : fh.getNewPath(), false)); if (listener != null) listener.skipped(); continue; } } if (listener != null) listener.started(); ChangeType type = fh.getChangeType(); File f = null; switch (type) { case ADD: f = getFile(fh.getNewPath(), true); apply(f, fh); break; case MODIFY: f = getFile(fh.getOldPath(), false); apply(f, fh); break; case DELETE: f = getFile(fh.getOldPath(), false); if (!f.delete()) throw new PatchApplyException(MessageFormat.format(JGitText.get().cannotDeleteFile, f)); break; case RENAME: f = getFile(fh.getOldPath(), false); File dest = getFile(fh.getNewPath(), false); if (!f.renameTo(dest)) throw new PatchApplyException( MessageFormat.format(JGitText.get().renameFileFailed, f, dest)); break; case COPY: f = getFile(fh.getOldPath(), false); byte[] bs = IO.readFully(f); FileOutputStream fos = new FileOutputStream(getFile(fh.getNewPath(), true)); try { fos.write(bs); } finally { fos.close(); } } // ... // if (!failed_files.contains(f) && type != ChangeType.DELETE) r.addUpdatedFile(f); // ... // } } catch (IOException e) { throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, e.getMessage()), e); } return r; }
From source file:de.codesourcery.gittimelapse.TextFile.java
License:Apache License
public void backwardsPatch(Patch patch) throws IOException, PatchApplyException { if (patch.getFiles().size() != 1) { throw new IllegalArgumentException("Patch needs to contain exactly one FileHeader"); }//w w w . ja v a2s. com final FileHeader fh = patch.getFiles().get(0); final List<String> oldLines = new ArrayList<String>(rawText.size()); for (int i = 0; i < rawText.size(); i++) { oldLines.add(rawText.getString(i)); } final List<String> newLines = new ArrayList<String>(oldLines); for (final HunkHeader hh : fh.getHunks()) { final StringBuilder hunk = new StringBuilder(); for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++) { hunk.append((char) hh.getBuffer()[j]); } final RawText hrt = new RawText(hunk.toString().getBytes()); final List<String> hunkLines = new ArrayList<String>(hrt.size()); for (int i = 0; i < hrt.size(); i++) { hunkLines.add(hrt.getString(i)); } int pos = 0; for (int j = 1; j < hunkLines.size(); j++) { final String hunkLine = hunkLines.get(j); switch (hunkLine.charAt(0)) { case ' ': if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals(hunkLine.substring(1))) { throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, hh)); } pos++; break; case '-': if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals(hunkLine.substring(1))) { throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, hh)); } newLines.remove(hh.getNewStartLine() - 1 + pos); break; case '+': newLines.add(hh.getNewStartLine() - 1 + pos, hunkLine.substring(1)); changesByLine.put(hh.getNewStartLine() - 1 + pos, ChangeType.ADDED); pos++; break; } } } if (!isNoNewlineAtEndOfFile(fh)) { newLines.add(""); //$NON-NLS-1$ } if (!rawText.isMissingNewlineAtEnd()) { oldLines.add(""); //$NON-NLS-1$ } final StringBuilder sb = new StringBuilder(); for (final String l : newLines) { // don't bother handling line endings - if it was windows, the \r is // still there! sb.append(l).append('\n'); } sb.deleteCharAt(sb.length() - 1); setText(sb.toString()); }