/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * See LICENSE.txt included in this distribution for the specific * language governing permissions and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at LICENSE.txt. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. */ package org.opensolaris.opengrok.history; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.opensolaris.opengrok.configuration.Configuration; import org.opensolaris.opengrok.util.Executor; import org.opensolaris.opengrok.util.IOUtils; /** * Access to a Monotone repository. * * @author Trond Norbye */ public class MonotoneRepository extends Repository { private static final Logger logger = Logger.getLogger(MonotoneRepository.class.getName()); private static final long serialVersionUID = 1L; /** The property name used to obtain the client command for this repository. */ public static final String CMD_PROPERTY_KEY = Configuration.PROPERTY_KEY_PREFIX + "history.Monotone"; /** The command to use to access the repository if none was given explicitly */ public static final String CMD_FALLBACK = "mnt"; /** * Create a new instance of type {@code Monotone}. */ public MonotoneRepository() { type = "Monotone"; datePattern = "yyyy-MM-dd'T'hh:mm:ss"; } /** * {@inheritDoc} */ @SuppressWarnings("resource") @Override public InputStream getHistoryGet(String parent, String basename, String rev) { InputStream ret = null; File directory = new File(directoryName); Process process = null; InputStream in = null; String revision = rev; try { String filename = (new File(parent, basename)).getCanonicalPath() .substring(directoryName.length() + 1); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); String argv[] = { cmd, "cat", "-r", revision, filename }; process = Runtime.getRuntime().exec(argv, null, directory); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[32 * 1024]; in = process.getInputStream(); int len; while ((len = in.read(buffer)) != -1) { if (len > 0) { out.write(buffer, 0, len); } } ret = new BufferedInputStream(new ByteArrayInputStream(out.toByteArray())); } catch (Exception exp) { logger.warning("Failed to get history: " + exp.getMessage()); } finally { IOUtils.close(in); // Clean up zombie-processes... if (process != null) { try { process.exitValue(); } catch (IllegalThreadStateException exp) { // the process is still running??? just kill it.. process.destroy(); } } } return ret; } /** * Get an executor to be used for retrieving the history log for the given * file. * * @param file file for which history is to be retrieved (canonical path * incl. source root). * @return An Executor ready to be started */ Executor getHistoryLogExecutor(File file, String changeset) throws IOException { String abs = file.getCanonicalPath(); String filename = ""; if (abs.length() > directoryName.length()) { filename = abs.substring(directoryName.length() + 1); } List cmd = new ArrayList(); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); cmd.add(this.cmd); cmd.add("log"); if (changeset != null) { cmd.add("--to"); cmd.add(changeset); } cmd.add("--no-graph"); cmd.add("--no-merges"); cmd.add("--no-format-dates"); cmd.add(filename); return new Executor(cmd, new File(directoryName)); } /** Pattern used to extract author/revision from hg annotate. */ private static final Pattern ANNOTATION_PATTERN = Pattern.compile("^(\\w+)\\p{Punct}\\p{Punct} by (\\S+)"); /** * {@inheritDoc} */ @Override public Annotation annotate(File file, String revision) throws IOException { ArrayList cmd = new ArrayList(); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); cmd.add(this.cmd); cmd.add("annotate"); cmd.add(getQuietOption()); if (revision != null) { cmd.add("-r"); cmd.add(revision); } cmd.add(file.getName()); File directory = new File(directoryName); Executor executor = new Executor(cmd, directory); if (executor.exec() != 0) { throw new IOException(executor.getErrorString()); } BufferedReader in = null; Annotation ret = null; try { in = new BufferedReader(executor.getOutputReader()); ret = new Annotation(file.getName()); String line; String author = null; String rev = null; Matcher matcher = ANNOTATION_PATTERN.matcher(""); while ((line = in.readLine()) != null) { matcher.reset(line); if (matcher.find()) { rev = matcher.group(1); author = matcher.group(2); ret.addLine(rev, author); } else { ret.addLine(rev, author); } } } finally { IOUtils.close(in); } return ret; } /** * {@inheritDoc} */ @Override public boolean fileHasAnnotation(File file) { return true; } /** * {@inheritDoc} */ @Override public void update() throws IOException { File directory = new File(directoryName); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); List cmd = new ArrayList(); cmd.add(this.cmd); cmd.add("pull"); cmd.add(getQuietOption()); Executor executor = new Executor(cmd, directory); if (executor.exec() != 0) { throw new IOException(executor.getErrorString()); } cmd.clear(); cmd.add(this.cmd); cmd.add("update"); cmd.add(getQuietOption()); executor = new Executor(cmd, directory); if (executor.exec() != 0) { throw new IOException(executor.getErrorString()); } } /** * {@inheritDoc} */ @Override public boolean fileHasHistory(File file) { return true; } /** * {@inheritDoc} */ @Override public boolean isRepositoryFor(File file) { File f = new File(file, "_MTN"); return f.exists() && f.isDirectory(); } /** * {@inheritDoc} */ @Override public boolean isWorking() { if (working == null) { ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); working = checkCmd(cmd, "--help"); } return working.booleanValue(); } /** * {@inheritDoc} */ @Override public boolean hasHistoryForDirectories() { return true; } /** * {@inheritDoc} */ @Override public History getHistory(File file) throws HistoryException { return getHistory(file, null); } /** * {@inheritDoc} */ @Override public History getHistory(File file, String sinceRevision) throws HistoryException { return new MonotoneHistoryParser(this).parse(file, sinceRevision); } private String quietOption; private String getQuietOption() { if (quietOption == null) { quietOption = System.getProperty(DEPRECATED_KEY, null); quietOption = quietOption != null && Boolean.parseBoolean(quietOption) ? "--reallyquiet" : "--quiet --quiet"; } return quietOption; } /** * The boolean system property to set to {@code true} if the deprecated * quiet option {@code --reallyquiet} should be used instead of * {@code --quiet --quiet}. */ public static final String DEPRECATED_KEY = Configuration.PROPERTY_KEY_PREFIX + "history.monotone.deprecated"; }