/* * 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) 2008, 2011, Oracle and/or its affiliates. All rights reserved. */ package org.opensolaris.opengrok.history; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.logging.Level; 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 Perforce repository * * @author Emilio Monti - emilmont@gmail.com */ public class PerforceRepository extends Repository { private static final Logger logger = Logger.getLogger(PerforceRepository.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.Perforce"; /** The command to use to access the repository if none was given explicitly */ public static final String CMD_FALLBACK = "p4"; private static final Pattern annotation_pattern = Pattern.compile("^(\\d+): .*"); /** * Create a new instance of type {@code Perforce}. */ public PerforceRepository() { type = "Perforce"; } /** * {@inheritDoc} */ @Override public Annotation annotate(File file, String rev) throws IOException { Annotation a = new Annotation(file.getName()); List revisions = PerforceHistoryParser.getRevisions(file, rev).getHistoryEntries(); HashMap revAuthor = new HashMap(); for (HistoryEntry entry : revisions) { // a.addDesc(entry.getRevision(), entry.getMessage()); revAuthor.put(entry.getRevision(), entry.getAuthor()); } ArrayList cmd = new ArrayList(); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); cmd.add(this.cmd); cmd.add("annotate"); cmd.add("-q"); cmd.add(file.getPath() + ((rev == null) ? "" : "#" + rev)); Executor executor = new Executor(cmd, file.getParentFile()); executor.exec(); BufferedReader reader = new BufferedReader(executor.getOutputReader()); String line; int lineno = 0; try { while ((line = reader.readLine()) != null) { ++lineno; Matcher matcher = annotation_pattern.matcher(line); if (matcher.find()) { String revision = matcher.group(1); String author = revAuthor.get(revision); a.addLine(revision, author); } else { logger.warning("Did not find annotation in line " + lineno + " [" + line + "]"); } } } catch (IOException e) { logger.warning("Could not read annotations for '" + file + "': " + e.getMessage()); logger.log(Level.FINE, "annotate", e); } IOUtils.close(reader); return a; } /** * {@inheritDoc} */ @Override public InputStream getHistoryGet(String parent, String basename, String rev) { ArrayList cmd = new ArrayList(); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); cmd.add(this.cmd); cmd.add("print"); cmd.add("-q"); cmd.add(basename + ((rev == null) ? "" : "#" + rev)); Executor executor = new Executor(cmd, new File(parent)); executor.exec(); return new ByteArrayInputStream(executor.getOutputString().getBytes()); } /** * {@inheritDoc} */ @Override public void update() throws IOException { File directory = new File(getDirectoryName()); List cmd = new ArrayList(); ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK); cmd.add(this.cmd); cmd.add("sync"); Executor 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 fileHasAnnotation(File file) { return true; } private static final PerforceRepository testRepo = new PerforceRepository(); /** * {@inheritDoc} */ @Override public boolean isRepositoryFor(File file) { boolean status = false; if (testRepo.isWorking()) { ArrayList cmd = new ArrayList(); String name = file.getName(); File dir = file.getParentFile(); if (file.isDirectory()) { dir = file; name = "*"; cmd.add(testRepo.cmd); cmd.add("dirs"); cmd.add(name); Executor executor = new Executor(cmd, dir); executor.exec(); /* OUTPUT: stdout: //depot_path/name stderr: name - no such file(s). */ status = (executor.getOutputString().indexOf("//") != -1); } if (!status) { cmd.clear(); cmd.add(testRepo.cmd); cmd.add("files"); cmd.add(name); Executor executor = new Executor(cmd, dir); executor.exec(); /* OUTPUT: stdout: //depot_path/name stderr: name - no such file(s). */ status = (executor.getOutputString().indexOf("//") != -1); } } return status; } /** * {@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 PerforceHistoryParser.parse(file, this); } }